Esempio n. 1
0
        private void DeleteStandardFile(string extension = "")
        {
            if (SettingServices.ServerIsNotClustered)
            {
                try
                {
                    File.Delete(SettingServices.GetSettingValue(SettingStrings.TftpPath) + ConfigFolder +
                                Path.DirectorySeparatorChar +
                                _bootFile + extension);
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message);
                }
            }
            else
            {
                var clusterGroup = _computerServices.GetClusterGroup(_computer.Id);
                var tftpServers  = _clusterGroupServices.GetClusterTftpServers(clusterGroup.Id);  // patch by Diederick Niehorster! See comment below. Ask Diederick
                if (tftpServers.Count == 0)
                {
                    // no tftp server found for this cluster. I have seen that happen when using a
                    // fake cluster where both members are different NICs on the same computer. That
                    // means no secondary server can be defined for the cluster, which messes up
                    // defining tftp role for for the cluster group (foreign key constraint failure
                    // because there is no secondary server). Then we get count=0 here. Fall back to
                    // seeing if the server we're on has tftp role defined in its cluster server role
                    // and if so, use it
                    if (SettingServices.GetSettingValue(SettingStrings.TftpServerRole).Equals("1"))
                    {
                        var fakeServer = new ClusterGroupServerEntity();
                        fakeServer.ServerId = -1;    // only thing that needs to be set
                        tftpServers.Add(fakeServer);
                    }
                }
                foreach (var tftpServer in tftpServers)
                {
                    if (tftpServer.ServerId == -1)
                    {
                        try
                        {
                            File.Delete(SettingServices.GetSettingValue(SettingStrings.TftpPath) + ConfigFolder +
                                        Path.DirectorySeparatorChar +
                                        _bootFile + extension);
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex.Message);
                        }
                    }
                    else
                    {
                        var secondaryServer = _secondaryServerServices.GetSecondaryServer(tftpServer.ServerId);
                        var tftpPath        =
                            new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                            .SettingApi.GetSetting("Tftp Path").Value;

                        var path = tftpPath + ConfigFolder + Path.DirectorySeparatorChar +
                                   _bootFile + extension;
                        ;

                        new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                        .ServiceAccountApi.DeleteTftpFile(path);
                    }
                }
            }
        }
Esempio n. 2
0
        public bool CreatePxeBootFiles()
        {
            var pxeComputerMac     = StringManipulationServices.MacToPxeMac(_computer.Mac);
            var webPath            = SettingServices.GetSettingValue(SettingStrings.WebPath) + "api/ClientImaging/";
            var globalComputerArgs = SettingServices.GetSettingValue(SettingStrings.GlobalComputerArgs);
            var userToken          = SettingServices.GetSettingValue(SettingStrings.WebTaskRequiresLogin) == "No"
                ? SettingServices.GetSettingValue(SettingStrings.UniversalToken)
                : "";
            const string newLineChar = "\n";

            if (_computer.AlternateServerIpId != -1)
            {
                var altServer = new AlternateServerIpServices().GetAlternateServerIp(_computer.AlternateServerIpId);
                if (altServer != null)
                {
                    webPath = altServer.ApiUrl + "api/ClientImaging/";
                }
            }

            var replacedPath = webPath;

            if (SettingServices.GetSettingValue(SettingStrings.IpxeSSL).Equals("1"))
            {
                replacedPath = replacedPath.ToLower().Replace("http", "https");
            }
            else
            {
                replacedPath = replacedPath.ToLower().Replace("https", "http");
            }

            var ipxe = new StringBuilder();

            ipxe.Append("#!ipxe" + newLineChar);
            ipxe.Append("kernel " + replacedPath + "IpxeBoot?filename=" + _imageProfile.Kernel +
                        "&type=kernel" + " initrd=" + _imageProfile.BootImage +
                        " root=/dev/ram0 rw ramdisk_size=156000" +
                        " consoleblank=0" + " web=" + webPath + " USER_TOKEN=" + userToken + " " + globalComputerArgs +
                        " " + _imageProfile.KernelArguments + newLineChar);
            ipxe.Append("imgfetch --name " + _imageProfile.BootImage + " " + replacedPath +
                        "IpxeBoot?filename=" + _imageProfile.BootImage + "&type=bootimage" + newLineChar);
            ipxe.Append("boot" + newLineChar);

            var sysLinux = new StringBuilder();

            sysLinux.Append("DEFAULT clonedeploy" + newLineChar);
            sysLinux.Append("LABEL clonedeploy" + newLineChar);
            sysLinux.Append("KERNEL kernels" + Path.DirectorySeparatorChar + _imageProfile.Kernel + newLineChar);
            sysLinux.Append("APPEND initrd=images" + Path.DirectorySeparatorChar + _imageProfile.BootImage +
                            " root=/dev/ram0 rw ramdisk_size=156000" +
                            " consoleblank=0" + " web=" + webPath + " USER_TOKEN=" + userToken + " " +
                            globalComputerArgs +
                            " " + _imageProfile.KernelArguments + newLineChar);

            var grub = new StringBuilder();

            grub.Append("set default=0" + newLineChar);
            grub.Append("set timeout=0" + newLineChar);
            grub.Append("menuentry CloneDeploy --unrestricted {" + newLineChar);
            grub.Append("echo Please Wait While The Boot Image Is Transferred.  This May Take A Few Minutes." +
                        newLineChar);
            grub.Append("linux /kernels/" + _imageProfile.Kernel +
                        " root=/dev/ram0 rw ramdisk_size=156000" + " consoleblank=0" + " web=" + webPath +
                        " USER_TOKEN=" +
                        userToken + " " +
                        globalComputerArgs + " " + _imageProfile.KernelArguments + newLineChar);
            grub.Append("initrd /images/" + _imageProfile.BootImage + newLineChar);
            grub.Append("}" + newLineChar);

            var list = new List <Tuple <string, string, string> >
            {
                Tuple.Create("bios", "", sysLinux.ToString()),
                Tuple.Create("bios", ".ipxe", ipxe.ToString()),
                Tuple.Create("efi32", "", sysLinux.ToString()),
                Tuple.Create("efi32", ".ipxe", ipxe.ToString()),
                Tuple.Create("efi64", "", sysLinux.ToString()),
                Tuple.Create("efi64", ".ipxe", ipxe.ToString()),
                Tuple.Create("efi64", ".cfg", grub.ToString())
            };

            //In proxy mode all boot files are created regardless of the pxe mode, this way computers can be customized
            //to use a specific boot file without affecting all others, using the proxydhcp reservations file.
            if (SettingServices.GetSettingValue(SettingStrings.ProxyDhcp) == "Yes")
            {
                if (SettingServices.ServerIsNotClustered)
                {
                    foreach (var bootMenu in list)
                    {
                        var path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                   Path.DirectorySeparatorChar + bootMenu.Item1 +
                                   Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                   pxeComputerMac +
                                   bootMenu.Item2;

                        if (!new FileOpsServices().WritePath(path, bootMenu.Item3))
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    var clusterGroup = new ComputerServices().GetClusterGroup(_computer.Id);
                    var tftpServers  = _clusterGroupServices.GetClusterTftpServers(clusterGroup.Id);  // patch by Diederick Niehorster! See comment below. Ask Diederick
                    if (tftpServers.Count == 0)
                    {
                        // no tftp server found for this cluster. I have seen that happen when using a
                        // fake cluster where both members are different NICs on the same computer. That
                        // means no secondary server can be defined for the cluster, which messes up
                        // defining tftp role for for the cluster group (foreign key constraint failure
                        // because there is no secondary server). Then we get count=0 here. Fall back to
                        // seeing if the server we're on has tftp role defined in its cluster server role
                        // and if so, use it
                        if (SettingServices.GetSettingValue(SettingStrings.TftpServerRole).Equals("1"))
                        {
                            var fakeServer = new ClusterGroupServerEntity();
                            fakeServer.ServerId = -1;    // only thing that needs to be set
                            tftpServers.Add(fakeServer);
                        }
                    }
                    foreach (var tftpServer in tftpServers)
                    {
                        foreach (var bootMenu in list)
                        {
                            if (tftpServer.ServerId == -1)
                            {
                                var path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                           Path.DirectorySeparatorChar + bootMenu.Item1 +
                                           Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                           pxeComputerMac +
                                           bootMenu.Item2;

                                if (!new FileOpsServices().WritePath(path, bootMenu.Item3))
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                var secondaryServer = _secondaryServerServices.GetSecondaryServer(tftpServer.ServerId);
                                var tftpPath        =
                                    new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                                    .SettingApi.GetSetting("Tftp Path").Value;

                                var path = tftpPath + "proxy" + Path.DirectorySeparatorChar + bootMenu.Item1 +
                                           Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                           pxeComputerMac +
                                           bootMenu.Item2;

                                if (
                                    !new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                                    .ServiceAccountApi.WriteTftpFile(new TftpFileDTO
                                {
                                    Path = path,
                                    Contents = bootMenu.Item3
                                }))
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }
            //When not using proxy dhcp, only one boot file is created
            else
            {
                var mode = SettingServices.GetSettingValue(SettingStrings.PxeMode);
                var path = "";
                if (SettingServices.ServerIsNotClustered)
                {
                    path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "pxelinux.cfg" +
                           Path.DirectorySeparatorChar + pxeComputerMac;

                    string fileContents = null;
                    if (mode == "pxelinux" || mode == "syslinux_32_efi" || mode == "syslinux_64_efi")
                    {
                        fileContents = sysLinux.ToString();
                    }

                    else if (mode.Contains("ipxe"))
                    {
                        path        += ".ipxe";
                        fileContents = ipxe.ToString();
                    }
                    else if (mode.Contains("grub"))
                    {
                        path        += ".cfg";
                        fileContents = grub.ToString();
                    }

                    if (!new FileOpsServices().WritePath(path, fileContents))
                    {
                        return(false);
                    }
                }
                else
                {
                    var clusterGroup    = new ComputerServices().GetClusterGroup(_computer.Id);
                    var secondaryServer = new SecondaryServerEntity();
                    var tftpServers     = _clusterGroupServices.GetClusterTftpServers(clusterGroup.Id); // patch by Diederick Niehorster! See comment below. Ask Diederick
                    if (tftpServers.Count == 0)
                    {
                        // no tftp server found for this cluster. I have seen that happen when using a
                        // fake cluster where both members are different NICs on the same computer. That
                        // means no secondary server can be defined for the cluster, which messes up
                        // defining tftp role for for the cluster group (foreign key constraint failure
                        // because there is no secondary server). Then we get count=0 here. Fall back to
                        // seeing if the server we're on has tftp role defined in its cluster server role
                        // and if so, use it
                        if (SettingServices.GetSettingValue(SettingStrings.TftpServerRole).Equals("1"))
                        {
                            var fakeServer = new ClusterGroupServerEntity();
                            fakeServer.ServerId = -1;    // only thing that needs to be set
                            tftpServers.Add(fakeServer);
                        }
                    }
                    foreach (var tftpServer in tftpServers)
                    {
                        if (tftpServer.ServerId == -1)
                        {
                            path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "pxelinux.cfg" +
                                   Path.DirectorySeparatorChar + pxeComputerMac;
                        }
                        else
                        {
                            secondaryServer = _secondaryServerServices.GetSecondaryServer(tftpServer.ServerId);
                            var tftpPath =
                                new APICall(_secondaryServerServices.GetToken(secondaryServer.Name)).SettingApi
                                .GetSetting("Tftp Path").Value;

                            path = tftpPath + "pxelinux.cfg" + Path.DirectorySeparatorChar + pxeComputerMac;
                        }

                        string fileContents = null;
                        if (mode == "pxelinux" || mode == "syslinux_32_efi" || mode == "syslinux_64_efi")
                        {
                            fileContents = sysLinux.ToString();
                        }

                        else if (mode.Contains("ipxe"))
                        {
                            path        += ".ipxe";
                            fileContents = ipxe.ToString();
                        }
                        else if (mode.Contains("grub"))
                        {
                            path        += ".cfg";
                            fileContents = grub.ToString();
                        }

                        if (tftpServer.ServerId == -1)
                        {
                            if (!new FileOpsServices().WritePath(path, fileContents))
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (
                                !new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                                .ServiceAccountApi.WriteTftpFile(new TftpFileDTO
                            {
                                Path = path,
                                Contents = fileContents
                            }))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 3
0
        protected void btnAddCluster_OnClick(object sender, EventArgs e)
        {
            RequiresAuthorization(AuthorizationStrings.UpdateAdmin);
            var clusterGroup = new ClusterGroupEntity
            {
                Name    = txtClusterName.Text,
                Default = chkDefault.Checked ? 1 : 0
            };

            var result = Call.ClusterGroupApi.Post(clusterGroup);

            if (result.Success)
            {
                var listOfServers = new List <ClusterGroupServerEntity>();
                foreach (GridViewRow row in gvServers.Rows)
                {
                    var cb = (CheckBox)row.FindControl("chkSelector");
                    if (!cb.Checked)
                    {
                        continue;
                    }
                    var cbTftp      = (CheckBox)row.FindControl("chkTftp");
                    var cbMulticast = (CheckBox)row.FindControl("chkMulticast");
                    var dataKey     = gvServers.DataKeys[row.RowIndex];
                    if (dataKey == null)
                    {
                        continue;
                    }

                    var clusterGroupServer = new ClusterGroupServerEntity();
                    clusterGroupServer.ClusterGroupId = result.Id;
                    clusterGroupServer.ServerId       = Convert.ToInt32(dataKey.Value);

                    if (cbTftp.Checked)
                    {
                        clusterGroupServer.TftpRole = 1;
                    }
                    if (cbMulticast.Checked)
                    {
                        clusterGroupServer.MulticastRole = 1;
                    }

                    listOfServers.Add(clusterGroupServer);
                }

                Call.ClusterGroupServerApi.Post(listOfServers);

                var listOfDps = new List <ClusterGroupDistributionPointEntity>();
                foreach (GridViewRow row in gvDps.Rows)
                {
                    var cb = (CheckBox)row.FindControl("chkSelector");
                    if (!cb.Checked)
                    {
                        continue;
                    }

                    var dataKey = gvDps.DataKeys[row.RowIndex];
                    if (dataKey == null)
                    {
                        continue;
                    }

                    var clusterGroupDistributionPoint = new ClusterGroupDistributionPointEntity();
                    clusterGroupDistributionPoint.ClusterGroupId      = result.Id;
                    clusterGroupDistributionPoint.DistributionPointId = Convert.ToInt32(dataKey.Value);

                    listOfDps.Add(clusterGroupDistributionPoint);
                }

                Call.ClusterGroupDistributionPointApi.Post(listOfDps);

                EndUserMessage = "Successfully Created Cluster Group";
                Response.Redirect("~/views/admin/cluster/editcluster.aspx?cat=sub1&clusterid=" + result.Id);
            }
            else
            {
                EndUserMessage = result.ErrorMessage;
            }
        }
Esempio n. 4
0
        protected void btnUpdateCluster_OnClick(object sender, EventArgs e)
        {
            RequiresAuthorization(AuthorizationStrings.UpdateAdmin);
            var clusterGroup = new ClusterGroupEntity
            {
                Id      = ClusterGroup.Id,
                Name    = txtClusterName.Text,
                Default = chkDefault.Checked ? 1 : 0
            };

            var result = Call.ClusterGroupApi.Put(clusterGroup.Id, clusterGroup);

            if (result.Success)
            {
                var listOfServers = new List <ClusterGroupServerEntity>();
                foreach (GridViewRow row in gvServers.Rows)
                {
                    var cb = (CheckBox)row.FindControl("chkSelector");
                    if (!cb.Checked)
                    {
                        continue;
                    }

                    var cbImage     = (CheckBox)row.FindControl("chkImage");
                    var cbTftp      = (CheckBox)row.FindControl("chkTftp");
                    var cbMulticast = (CheckBox)row.FindControl("chkMulticast");
                    var dataKey     = gvServers.DataKeys[row.RowIndex];
                    if (dataKey == null)
                    {
                        continue;
                    }

                    var clusterGroupServer = new ClusterGroupServerEntity();
                    clusterGroupServer.ClusterGroupId = result.Id;
                    clusterGroupServer.ServerId       = Convert.ToInt32(dataKey.Value);

                    if (cbTftp.Checked)
                    {
                        clusterGroupServer.TftpRole = 1;
                    }
                    if (!cbTftp.Visible)
                    {
                        clusterGroupServer.TftpRole = 0;
                    }
                    if (cbMulticast.Checked)
                    {
                        clusterGroupServer.MulticastRole = 1;
                    }
                    if (!cbMulticast.Visible)
                    {
                        clusterGroupServer.MulticastRole = 0;
                    }

                    listOfServers.Add(clusterGroupServer);
                }

                if (listOfServers.Count == 0)
                {
                    listOfServers.Add(new ClusterGroupServerEntity
                    {
                        ClusterGroupId = ClusterGroup.Id,
                        ServerId       = -2
                    });
                }
                Call.ClusterGroupServerApi.Post(listOfServers);

                var listOfDps = new List <ClusterGroupDistributionPointEntity>();
                foreach (GridViewRow row in gvDps.Rows)
                {
                    var cb = (CheckBox)row.FindControl("chkSelector");
                    if (!cb.Checked)
                    {
                        continue;
                    }

                    var dataKey = gvDps.DataKeys[row.RowIndex];
                    if (dataKey == null)
                    {
                        continue;
                    }

                    var clusterGroupDistributionPoint = new ClusterGroupDistributionPointEntity();
                    clusterGroupDistributionPoint.ClusterGroupId      = result.Id;
                    clusterGroupDistributionPoint.DistributionPointId = Convert.ToInt32(dataKey.Value);

                    listOfDps.Add(clusterGroupDistributionPoint);
                }

                if (listOfDps.Count == 0)
                {
                    listOfDps.Add(new ClusterGroupDistributionPointEntity
                    {
                        ClusterGroupId      = ClusterGroup.Id,
                        DistributionPointId = -2
                    });
                }
                Call.ClusterGroupDistributionPointApi.Post(listOfDps);

                EndUserMessage = "Successfully Updated Cluster Group";
            }
            else
            {
                EndUserMessage = result.ErrorMessage;
            }
        }