Пример #1
0
        private void DeleteProxyFile(string architecture, string extension = "")
        {
            if (SettingServices.ServerIsNotClustered)
            {
                try
                {
                    File.Delete(SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                Path.DirectorySeparatorChar + architecture +
                                Path.DirectorySeparatorChar + ConfigFolder + Path.DirectorySeparatorChar + _bootFile +
                                extension);
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message);
                }
            }
            else
            {
                var clusterGroup = _computerServices.GetClusterGroup(_computer.Id);
                foreach (var tftpServer in _clusterGroupServices.GetClusterTftpServers(clusterGroup.Id))
                {
                    if (tftpServer.ServerId == -1)
                    {
                        try
                        {
                            File.Delete(SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                        Path.DirectorySeparatorChar + architecture +
                                        Path.DirectorySeparatorChar + 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 + "proxy" + Path.DirectorySeparatorChar + architecture +
                                   Path.DirectorySeparatorChar + ConfigFolder + Path.DirectorySeparatorChar +
                                   _bootFile + extension;

                        new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                        .ServiceAccountApi.DeleteTftpFile(path);
                    }
                }
            }
        }
Пример #2
0
        public void Execute()
        {
            if (SettingServices.GetSettingValue(SettingStrings.MonitorSecondaryServer) == "0")
            {
                return;
            }

            var secServers = _secondaryServerServices.GetAll();

            foreach (var server in secServers)
            {
                bool serverIsUp = false;
                var  counter    = 1;
                while (counter <= 3)
                {
                    var result = _secondaryServerServices.GetToken(server.Name);
                    if (result != null)
                    {
                        if (!string.IsNullOrEmpty(result.Token))
                        {
                            //connection test passed
                            serverIsUp = true;
                            break;
                        }
                    }
                    counter++;
                }
                if (serverIsUp && server.IsActive != 1)
                {
                    //mark server active
                    server.IsActive = 1;
                    UpdateServer(server);
                }
                else if (!serverIsUp && server.IsActive != 0)
                {
                    //mark sever inactive
                    server.IsActive = 0;
                    UpdateServer(server);
                }
            }
        }
Пример #3
0
        public bool Execute()
        {
            var copyResult = false;

            if (SettingServices.ServerIsNotClustered ||
                (SettingServices.ServerIsClusterSecondary && SettingServices.TftpServerRole) ||
                (SettingServices.ServerIsClusterPrimary && SettingServices.TftpServerRole))
            {
                copyResult = SettingServices.GetSettingValue(SettingStrings.ProxyDhcp) == "Yes"
                    ? CopyFilesForProxy()
                    : CopyFilesForNonProxy();
            }

            if (SettingServices.ServerIsClusterPrimary)
            {
                foreach (var tftpServer in _secondaryServerServices.GetAllWithTftpRole())
                {
                    copyResult = new APICall(_secondaryServerServices.GetToken(tftpServer.Name))
                                 .ServiceAccountApi.CopyPxeBinaries();
                }
            }
            return(copyResult);
        }
Пример #4
0
        private void CreateGrubMenu()
        {
            var customMenuEntries =
                _bootEntryServices.SearchBootEntrys()
                .Where(x => x.Type == "grub" && x.Active == 1)
                .OrderBy(x => x.Order)
                .ThenBy(x => x.Name);
            var defaultCustomEntry = customMenuEntries.FirstOrDefault(x => x.Default == 1);

            var grubMenu = new StringBuilder();

            grubMenu.Append("insmod password_pbkdf2" + NewLineChar);
            grubMenu.Append("insmod regexp" + NewLineChar);
            grubMenu.Append("set default=0" + NewLineChar);
            grubMenu.Append("set timeout=10" + NewLineChar);
            grubMenu.Append("set pager=1" + NewLineChar);
            if (!string.IsNullOrEmpty(_defaultBoot.GrubUserName) && !string.IsNullOrEmpty(_defaultBoot.GrubPassword))
            {
                grubMenu.Append("set superusers=\"" + _defaultBoot.GrubUserName + "\"" + NewLineChar);
                string sha = null;
                try
                {
                    sha =
                        new WebClient().DownloadString(
                            "http://docs.clonedeploy.org/grub-pass-gen/encrypt.php?password="******"\n \n\n\n", "");
                }
                catch
                {
                    log.Error("Could not generate sha for grub password.  Could not contact http://clonedeploy.org");
                }
                grubMenu.Append("password_pbkdf2 " + _defaultBoot.GrubUserName + " " + sha + "" + NewLineChar);
                grubMenu.Append("export superusers" + NewLineChar);
                grubMenu.Append("" + NewLineChar);
            }
            grubMenu.Append(@"regexp -s 1:b1 '(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3})' $net_default_mac" +
                            NewLineChar);
            grubMenu.Append(@"regexp -s 2:b2 '(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3})' $net_default_mac" +
                            NewLineChar);
            grubMenu.Append(@"regexp -s 3:b3 '(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3})' $net_default_mac" +
                            NewLineChar);
            grubMenu.Append(@"regexp -s 4:b4 '(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3})' $net_default_mac" +
                            NewLineChar);
            grubMenu.Append(@"regexp -s 5:b5 '(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3})' $net_default_mac" +
                            NewLineChar);
            grubMenu.Append(@"regexp -s 6:b6 '(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3}):(.{1,3})' $net_default_mac" +
                            NewLineChar);
            grubMenu.Append(@"mac=01-$b1-$b2-$b3-$b4-$b5-$b6" + NewLineChar);
            grubMenu.Append("" + NewLineChar);

            if (_defaultBoot.Type == "standard")
            {
                grubMenu.Append("if [ -s /pxelinux.cfg/$mac.cfg ]; then" + NewLineChar);
                grubMenu.Append("configfile /pxelinux.cfg/$mac.cfg" + NewLineChar);
                grubMenu.Append("fi" + NewLineChar);
            }
            else
            {
                grubMenu.Append("if [ -s /proxy/efi64/pxelinux.cfg/$mac.cfg ]; then" + NewLineChar);
                grubMenu.Append("configfile /proxy/efi64/pxelinux.cfg/$mac.cfg" + NewLineChar);
                grubMenu.Append("fi" + NewLineChar);
            }

            if (defaultCustomEntry != null)
            {
                grubMenu.Append("" + NewLineChar);
                grubMenu.Append("menuentry \"" + _alphaNumericSpace.Replace(defaultCustomEntry.Name, "") +
                                "\" --unrestricted {" + NewLineChar);
                grubMenu.Append(defaultCustomEntry.Content + NewLineChar);
                grubMenu.Append("}" + NewLineChar);
            }

            grubMenu.Append("" + NewLineChar);
            grubMenu.Append("menuentry \"Boot To Local Machine\" --unrestricted {" + NewLineChar);
            grubMenu.Append("exit" + NewLineChar);
            grubMenu.Append("}" + NewLineChar);
            grubMenu.Append("" + NewLineChar);

            grubMenu.Append("menuentry \"CloneDeploy\" --user {" + NewLineChar);
            grubMenu.Append("echo Please Wait While The Boot Image Is Transferred.  This May Take A Few Minutes." +
                            NewLineChar);
            grubMenu.Append("linux /kernels/" + _defaultBoot.Kernel + " root=/dev/ram0 rw ramdisk_size=156000 " +
                            " web=" +
                            _webPath +
                            " USER_TOKEN=" + _userToken + " consoleblank=0 " + _registration + _globalComputerArgs + "" +
                            NewLineChar);
            grubMenu.Append("initrd /images/" + _defaultBoot.BootImage + "" + NewLineChar);
            grubMenu.Append("}" + NewLineChar);
            grubMenu.Append("" + NewLineChar);

            grubMenu.Append("menuentry \"Client Console\" --user {" + NewLineChar);
            grubMenu.Append("echo Please Wait While The Boot Image Is Transferred.  This May Take A Few Minutes." +
                            NewLineChar);
            grubMenu.Append("linux /kernels/" + _defaultBoot.Kernel + " root=/dev/ram0 rw ramdisk_size=156000 " +
                            " web=" +
                            _webPath +
                            " USER_TOKEN=" + _userToken + " task=debug consoleblank=0 " + _globalComputerArgs + "" +
                            NewLineChar);
            grubMenu.Append("initrd /images/" + _defaultBoot.BootImage + "" + NewLineChar);
            grubMenu.Append("}" + NewLineChar);
            grubMenu.Append("" + NewLineChar);

            foreach (var customEntry in customMenuEntries)
            {
                if (defaultCustomEntry != null && customEntry.Id == defaultCustomEntry.Id)
                {
                    continue;
                }

                grubMenu.Append("" + NewLineChar);
                grubMenu.Append("menuentry \"" + _alphaNumericSpace.Replace(customEntry.Name, "") +
                                "\" --user {" + NewLineChar);
                grubMenu.Append(customEntry.Content + NewLineChar);
                grubMenu.Append("}" + NewLineChar);

                grubMenu.Append("" + NewLineChar);
            }

            var path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "grub" + Path.DirectorySeparatorChar +
                       "grub.cfg";

            if (SettingServices.ServerIsNotClustered)
            {
                new FileOpsServices().WritePath(path, grubMenu.ToString());
            }
            else
            {
                if (SettingServices.TftpServerRole)
                {
                    new FileOpsServices().WritePath(path, grubMenu.ToString());
                }
                foreach (var tftpServer in _secondaryServerServices.GetAllWithTftpRole())
                {
                    var tftpPath =
                        new APICall(_secondaryServerServices.GetToken(tftpServer.Name))
                        .SettingApi.GetSetting("Tftp Path").Value;

                    var tftpFile = new TftpFileDTO();
                    tftpFile.Contents = grubMenu.ToString();
                    tftpFile.Path     = tftpPath + "grub" + Path.DirectorySeparatorChar + "grub.cfg";

                    new APICall(_secondaryServerServices.GetToken(tftpServer.Name))
                    .ServiceAccountApi.WriteTftpFile(tftpFile);
                }
            }
        }
Пример #5
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 ipxe = new StringBuilder();

            ipxe.Append("#!ipxe" + newLineChar);
            ipxe.Append("kernel " + webPath + "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 + " " + webPath +
                        "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);
                    foreach (var tftpServer in _clusterGroupServices.GetClusterTftpServers(clusterGroup.Id))
                    {
                        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();
                    foreach (var tftpServer in _clusterGroupServices.GetClusterTftpServers(clusterGroup.Id))
                    {
                        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);
        }
Пример #6
0
        private void DeleteProxyFile(string architecture, string extension = "")
        {
            if (SettingServices.ServerIsNotClustered)
            {
                try
                {
                    File.Delete(SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                Path.DirectorySeparatorChar + architecture +
                                Path.DirectorySeparatorChar + 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) + "proxy" +
                                        Path.DirectorySeparatorChar + architecture +
                                        Path.DirectorySeparatorChar + 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 + "proxy" + Path.DirectorySeparatorChar + architecture +
                                   Path.DirectorySeparatorChar + ConfigFolder + Path.DirectorySeparatorChar +
                                   _bootFile + extension;

                        new APICall(_secondaryServerServices.GetToken(secondaryServer.Name))
                        .ServiceAccountApi.DeleteTftpFile(path);
                    }
                }
            }
        }
Пример #7
0
        public bool Execute()
        {
            //If a cluster primary - cancel all tasks on secondaries first, then move on
            if (SettingServices.ServerIsClusterPrimary)
            {
                foreach (var server in _secondaryServerServices.GetAllWithActiveRoles())
                {
                    new APICall(_secondaryServerServices.GetToken(server.Name))
                    .ServiceAccountApi.CancelAllImagingTasks();
                }
            }

            var tftpPath = SettingServices.GetSettingValue(SettingStrings.TftpPath);
            var pxePaths = new List <string>
            {
                tftpPath + "pxelinux.cfg" + Path.DirectorySeparatorChar,
                tftpPath + "proxy" + Path.DirectorySeparatorChar + "bios" +
                Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar,
                tftpPath + "proxy" + Path.DirectorySeparatorChar + "efi32" +
                Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar,
                tftpPath + "proxy" + Path.DirectorySeparatorChar + "efi64" +
                Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar
            };

            foreach (var pxePath in pxePaths)
            {
                var pxeFiles = Directory.GetFiles(pxePath, "01*");
                try
                {
                    foreach (var pxeFile in pxeFiles)
                    {
                        File.Delete(pxeFile);
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString());
                    return(false);
                }
            }

            if (Environment.OSVersion.ToString().Contains("Unix"))
            {
                for (var x = 1; x <= 10; x++)
                {
                    try
                    {
                        var killProcInfo = new ProcessStartInfo
                        {
                            FileName  = "killall",
                            Arguments = " -s SIGKILL udp-sender"
                        };
                        Process.Start(killProcInfo);
                    }
                    catch
                    {
                        // ignored
                    }

                    Thread.Sleep(200);
                }
            }

            else
            {
                for (var x = 1; x <= 10; x++)
                {
                    foreach (var p in Process.GetProcessesByName("udp-sender"))
                    {
                        try
                        {
                            p.Kill();
                            p.WaitForExit();
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex.ToString());
                        }
                    }
                    Thread.Sleep(200);
                }
            }

            if (!SettingServices.ServerIsClusterSecondary)
            {
                //The database tasks are only removed on a single server or cluster primary,
                //There are no database tasks in a clustered secondary.

                new ActiveImagingTaskServices().DeleteAll();
                new ActiveMulticastSessionServices().DeleteAll();

                //Recreate any custom boot menu's that were just deleted
                foreach (var computer in _computerServices.ComputersWithCustomBootMenu())
                {
                    //The create boot files method handles creating the file for the secondary servers
                    _computerServices.CreateBootFiles(computer.Id);
                }
            }
            return(true);
        }
Пример #8
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);
        }
Пример #9
0
        public bool Execute()
        {
            var webPath            = SettingServices.GetSettingValue(SettingStrings.WebPath) + "api/ClientImaging/";
            var globalComputerArgs = SettingServices.GetSettingValue(SettingStrings.GlobalComputerArgs);
            var namePromptArg      = "";

            if (_promptComputerName)
            {
                namePromptArg = " name_prompt=true";
            }

            var userToken = SettingServices.GetSettingValue(SettingStrings.ClobberRequiresLogin) == "No"
                ? SettingServices.GetSettingValue(SettingStrings.UniversalToken)
                : "";
            const string newLineChar = "\n";

            var ipxe = new StringBuilder();

            ipxe.Append("#!ipxe" + newLineChar);
            ipxe.Append("kernel " + webPath + "IpxeBoot?filename=" + _imageProfile.Kernel +
                        "&type=kernel" + " initrd=" + _imageProfile.BootImage +
                        " root=/dev/ram0 rw ramdisk_size=156000 task=clobber " + "image_profile_id=" + _imageProfile.Id +
                        namePromptArg +
                        " consoleblank=0" + " web=" + webPath + " USER_TOKEN=" + userToken + " " + globalComputerArgs +
                        " " + _imageProfile.KernelArguments + newLineChar);
            ipxe.Append("imgfetch --name " + _imageProfile.BootImage + " " + webPath +
                        "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 task=clobber " + "image_profile_id=" +
                            _imageProfile.Id + namePromptArg +
                            " 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 task=clobber " + "image_profile_id=" + _imageProfile.Id +
                        namePromptArg + " 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")
            {
                string path = null;

                if (SettingServices.ServerIsNotClustered ||
                    (SettingServices.ServerIsClusterPrimary && SettingServices.TftpServerRole))
                {
                    foreach (var bootMenu in list)
                    {
                        switch (bootMenu.Item2)
                        {
                        case ".cfg":
                            path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "grub" +
                                   Path.DirectorySeparatorChar + "grub.cfg";
                            break;

                        case ".ipxe":
                            path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                   Path.DirectorySeparatorChar + bootMenu.Item1 +
                                   Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                   "default.ipxe";
                            break;

                        case "":
                            path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "proxy" +
                                   Path.DirectorySeparatorChar + bootMenu.Item1 +
                                   Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                   "default";
                            break;
                        }

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

                        foreach (var bootMenu in list)
                        {
                            switch (bootMenu.Item2)
                            {
                            case ".cfg":
                                path = tftpPath + "grub" + Path.DirectorySeparatorChar + "grub.cfg";
                                break;

                            case ".ipxe":
                                path = tftpPath + "proxy" + Path.DirectorySeparatorChar + bootMenu.Item1 +
                                       Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                       "default.ipxe";
                                break;

                            case "":
                                path = tftpPath + "proxy" + Path.DirectorySeparatorChar + bootMenu.Item1 +
                                       Path.DirectorySeparatorChar + "pxelinux.cfg" + Path.DirectorySeparatorChar +
                                       "default";
                                break;
                            }

                            var tftpFile = new TftpFileDTO();
                            tftpFile.Contents = bootMenu.Item3;
                            tftpFile.Path     = path;

                            new APICall(_secondaryServerServices.GetToken(tftpServer.Name))
                            .ServiceAccountApi.WriteTftpFile(tftpFile);
                        }
                    }
                }
            }
            //When not using proxy dhcp, only one boot file is created
            else
            {
                var    mode         = SettingServices.GetSettingValue(SettingStrings.PxeMode);
                string path         = null;
                string fileContents = null;

                if (SettingServices.ServerIsNotClustered ||
                    (SettingServices.ServerIsClusterPrimary && SettingServices.TftpServerRole))
                {
                    if (mode == "pxelinux" || mode == "syslinux_32_efi" || mode == "syslinux_64_efi")
                    {
                        path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "pxelinux.cfg" +
                               Path.DirectorySeparatorChar + "default";
                        fileContents = sysLinux.ToString();
                    }

                    else if (mode.Contains("ipxe"))
                    {
                        path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "pxelinux.cfg" +
                               Path.DirectorySeparatorChar + "default.ipxe";
                        fileContents = ipxe.ToString();
                    }
                    else if (mode.Contains("grub"))
                    {
                        path = SettingServices.GetSettingValue(SettingStrings.TftpPath) + "grub" +
                               Path.DirectorySeparatorChar + "grub.cfg";
                        fileContents = grub.ToString();
                    }

                    if (!new FileOpsServices().WritePath(path, fileContents))
                    {
                        return(false);
                    }
                }
                else
                {
                    foreach (var tftpServer in _secondaryServerServices.GetAllWithTftpRole())
                    {
                        var tftpPath =
                            new APICall(_secondaryServerServices.GetToken(tftpServer.Name))
                            .SettingApi.GetSetting("Tftp Path").Value;

                        if (mode == "pxelinux" || mode == "syslinux_32_efi" || mode == "syslinux_64_efi")
                        {
                            path         = tftpPath + "pxelinux.cfg" + Path.DirectorySeparatorChar + "default";
                            fileContents = sysLinux.ToString();
                        }

                        else if (mode.Contains("ipxe"))
                        {
                            path         = tftpPath + "pxelinux.cfg" + Path.DirectorySeparatorChar + "default.ipxe";
                            fileContents = ipxe.ToString();
                        }
                        else if (mode.Contains("grub"))
                        {
                            path         = tftpPath + "grub" + Path.DirectorySeparatorChar + "grub.cfg";
                            fileContents = grub.ToString();
                        }

                        var tftpFile = new TftpFileDTO();
                        tftpFile.Contents = fileContents;
                        tftpFile.Path     = path;

                        new APICall(_secondaryServerServices.GetToken(tftpServer.Name))
                        .ServiceAccountApi.WriteTftpFile(tftpFile);
                    }
                }
            }
            return(true);
        }