Esempio n. 1
0
        private static void EnableDHCP_FreeBSD(string adapter, ExecutionResult ret)
        {
            const string rcConfPath = compatLinux + rcConf;

            ShellHelper.RunCmd("cp -p " + rcConf + " " + rcConfPath);

            int pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "=", 0, -1);

            if (pos == -1)
            {
                pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + " =", 0, -1);
            }
            TxtHelper.ReplaceStr(rcConfPath, "ifconfig_" + adapter + "=\"DHCP\"", pos);

            ShellHelper.RunCmd("cp -p " + rcConfPath + " " + rcConf);

            ExecutionResult res = ShellHelper.RunCmd("service netif restart");

            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
            res = ShellHelper.RunCmd("service routing restart");
            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
        }
Esempio n. 2
0
        private static void EnableDHCP_Ubuntu(String adapter, ExecutionResult ret)
        {
            //netplan configuration (for Ubuntu 18.04+)
            string netplanPath = GetNetplanPath();
            int    startPos    = TxtHelper.GetStrPos(netplanPath, adapter + ":", 0, -1);

            if (startPos != -1)
            {
                string firstStr    = TxtHelper.GetStr(netplanPath, adapter + ":", startPos, startPos);
                int    spacesCount = GetNetplanSpacesCount(firstStr);

                startPos++;
                int           endPos = GetNetplanEndPos(netplanPath, startPos, spacesCount);
                List <string> config = new List <string>();
                config.Add(new String(' ', spacesCount + 2) + "dhcp4: yes");
                TxtHelper.ReplaceAllStr(netplanPath, config, startPos, endPos);
                ShellHelper.RunCmd("netplan apply");
            }

            //iterfaces configuration (for Ubuntu 16.04-)
            startPos = TxtHelper.GetStrPos(interfacesPath, "iface " + adapter, 0, -1);
            if (startPos != -1)
            {
                int endPos = TxtHelper.GetStrPos(interfacesPath, "auto ", startPos, -1);
                if (endPos != -1)
                {
                    endPos--;
                }
                List <string> config = new List <string>();
                config.Add("iface " + adapter + " inet dhcp");
                TxtHelper.ReplaceAllStr(interfacesPath, config, startPos, endPos);
                ret.RebootRequired = true;
            }
        }
Esempio n. 3
0
        private static void DisableDHCP_CentOS(ExecutionContext context, String adapter, ExecutionResult ret, string[] ipAddresses, string[] subnetMasksPrefix, string ipGateway)
        {
            string adapterPath = ifcfgBasePath + adapter;

            TxtHelper.ReplaceStr(adapterPath, "BOOTPROTO=none", TxtHelper.GetStrPos(adapterPath, "BOOTPROTO", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "ONBOOT=yes", TxtHelper.GetStrPos(adapterPath, "ONBOOT", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "IPADDR=" + ipAddresses[0], TxtHelper.GetStrPos(adapterPath, "IPADDR", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "PREFIX=" + subnetMasksPrefix[0], TxtHelper.GetStrPos(adapterPath, "PREFIX", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "GATEWAY=" + ipGateway, TxtHelper.GetStrPos(adapterPath, "GATEWAY", 0, -1));
            if (CheckParameter(context, "PreferredDNSServer"))
            {
                string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                for (int i = 0; i < dnsServers.Length; i++)
                {
                    TxtHelper.ReplaceStr(adapterPath, "DNS" + (i + 1).ToString() + "=" + dnsServers[i], TxtHelper.GetStrPos(adapterPath, "DNS" + (i + 1).ToString(), 0, -1));
                }
            }
            if (ipAddresses.Length > 1)
            {
                for (int i = 1; i < ipAddresses.Length; i++)
                {
                    TxtHelper.ReplaceStr(adapterPath, "IPADDR" + i.ToString() + "=" + ipAddresses[i], TxtHelper.GetStrPos(adapterPath, "IPADDR" + i.ToString(), 0, -1));
                    TxtHelper.ReplaceStr(adapterPath, "PREFIX" + i.ToString() + "=" + subnetMasksPrefix[i], TxtHelper.GetStrPos(adapterPath, "PREFIX" + i.ToString(), 0, -1));
                }
            }
            int pos;
            int interfaceNum = ipAddresses.Length;

            do
            {
                pos = TxtHelper.GetStrPos(adapterPath, "IPADDR" + interfaceNum.ToString(), 0, -1);
                if (pos != -1)
                {
                    TxtHelper.DelStr(adapterPath, pos);
                }
                pos = TxtHelper.GetStrPos(adapterPath, "PREFIX" + interfaceNum.ToString(), 0, -1);
                if (pos != -1)
                {
                    TxtHelper.DelStr(adapterPath, pos);
                }
                interfaceNum++;
            } while (pos != -1);
            ExecutionResult res = ShellHelper.RunCmd("systemctl restart network");

            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
        }
Esempio n. 4
0
        private static void EnableDHCP_CentOS(String adapter, ExecutionResult ret)
        {
            string adapterPath = ifcfgBasePath + adapter;

            TxtHelper.ReplaceStr(adapterPath, "BOOTPROTO=dhcp", TxtHelper.GetStrPos(adapterPath, "BOOTPROTO", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "ONBOOT=yes", TxtHelper.GetStrPos(adapterPath, "ONBOOT", 0, -1));
            ExecutionResult res = ShellHelper.RunCmd("systemctl restart network");

            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
        }
Esempio n. 5
0
        private static int GetNetplanEndPos(string netplanPath, int startPos, int spacesCount)
        {
            int tmpPos = startPos;
            int endPos = tmpPos;

            do
            {
                tmpPos = TxtHelper.GetStrPos(netplanPath, new String(' ', spacesCount + 2), tmpPos, tmpPos);
                if (tmpPos != -1)
                {
                    endPos = tmpPos;
                    tmpPos++;
                }
            } while (tmpPos != -1);
            return(endPos);
        }
Esempio n. 6
0
        public static ExecutionResult Run(ref ExecutionContext context)
        {
            ExecutionResult ret = new ExecutionResult();

            ret.ResultCode     = 0;
            ret.ErrorMessage   = null;
            ret.RebootRequired = true;
            try
            {
                context.ActivityDescription = "Changing computer name...";
                context.Progress            = 0;
                if (!context.Parameters.ContainsKey("FullComputerName"))
                {
                    ret.ResultCode   = 2;
                    ret.ErrorMessage = "Parameter 'FullComputerName' not found";
                    Log.WriteError(ret.ErrorMessage);
                    context.Progress = 100;
                    return(ret);
                }
                string computerName = context.Parameters["FullComputerName"].ToLower();
                string netBiosName  = computerName;
                int    idx          = netBiosName.IndexOf(".");
                if (idx != -1)
                {
                    netBiosName = computerName.Substring(0, idx);
                }
                ExecutionResult res;
                res = ShellHelper.RunCmd("hostname -s");
                string hostNameOld = null;
                if (res.Value != null)
                {
                    hostNameOld = res.Value.Trim();
                }

                res = ShellHelper.RunCmd("hostname");
                string fullNameOld = null;
                if (res.Value != null)
                {
                    fullNameOld = res.Value.Trim();
                }

                string domain    = computerName.Replace(netBiosName + ".", "");
                string domainOld = null;
                if (hostNameOld != null && hostNameOld.Length > 0 && fullNameOld != null && fullNameOld.Length > 0)
                {
                    domainOld = fullNameOld.Replace(hostNameOld + ".", "");
                }

                switch (OsVersion.GetOsVersion())
                {
                case OsVersionEnum.Ubuntu:
                    TxtHelper.ReplaceStr(hostname, computerName, 0);
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, hostNameOld, netBiosName);
                    }
                    TxtHelper.ReplaceStr(cloudCfg, "preserve_hostname: false", "preserve_hostname: true");
                    TxtHelper.ReplaceStr(cloudCfg, "# preserve_hostname: true", "preserve_hostname: true");
                    TxtHelper.ReplaceStr(cloudCfg, "#preserve_hostname: true", "preserve_hostname: true");
                    break;

                case OsVersionEnum.CentOS:
                    TxtHelper.ReplaceStr(hostname, computerName, 0);
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, hostNameOld, netBiosName);
                    }
                    break;

                case OsVersionEnum.FreeBSD:
                    ShellHelper.RunCmd("cp -p " + rcConf + " " + compatLinux + rcConf);
                    ShellHelper.RunCmd("cp -p " + hosts + " " + compatLinux + hosts);
                    TxtHelper.ReplaceStr(compatLinux + rcConf, "hostname=\"" + computerName + "\"", TxtHelper.GetStrPos(compatLinux + rcConf, "hostname", 0, -1));
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(compatLinux + hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(compatLinux + hosts, hostNameOld, netBiosName);
                    }
                    ShellHelper.RunCmd("cp -p " + compatLinux + rcConf + " " + rcConf);
                    ShellHelper.RunCmd("cp -p " + compatLinux + hosts + " " + hosts);
                    break;

                default:
                    TxtHelper.ReplaceStr(hostname, computerName, 0);
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, hostNameOld, netBiosName);
                    }
                    break;
                }
            }catch (Exception ex)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = "ChangeComputerName error: " + ex.ToString();
                Log.WriteError(ret.ErrorMessage);
            }
            if (ret.ResultCode == 0)
            {
                Log.WriteInfo("Computer name has been changed successfully");
            }
            context.Progress = 100;
            return(ret);
        }
Esempio n. 7
0
        private static void DisableDHCP_FreeBSD(ExecutionContext context, String adapter, ExecutionResult ret, string[] ipAddresses, string[] subnetMasks, string ipGateway)
        {
            const string rcConfPath     = compatLinux + rcConf;
            const string resolvConfPath = compatLinux + resolvConf;

            ShellHelper.RunCmd("cp -p " + rcConf + " " + rcConfPath);
            int pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "=", 0, -1);

            if (pos == -1)
            {
                pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + " =", 0, -1);
            }
            TxtHelper.ReplaceStr(rcConfPath, "ifconfig_" + adapter + "=\"inet " + ipAddresses[0] + " netmask " + subnetMasks[0] + "\"", pos);
            TxtHelper.ReplaceStr(rcConfPath, "defaultrouter=\"" + ipGateway + "\"", TxtHelper.GetStrPos(rcConfPath, "defaultrouter", 0, -1));

            if (CheckParameter(context, "PreferredDNSServer"))
            {
                ShellHelper.RunCmd("cp -p " + resolvConf + " " + resolvConfPath);
                string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                do
                {
                    pos = TxtHelper.GetStrPos(resolvConfPath, "nameserver", 0, -1);
                    if (pos != -1)
                    {
                        TxtHelper.DelStr(resolvConfPath, pos);
                    }
                } while (pos != -1);
                foreach (var dnsServer in dnsServers)
                {
                    TxtHelper.ReplaceStr(resolvConfPath, "nameserver " + dnsServer, -1);
                }
                ShellHelper.RunCmd("cp -p " + resolvConfPath + " " + resolvConf);
            }

            if (ipAddresses.Length > 1)
            {
                for (int i = 1; i < ipAddresses.Length; i++)
                {
                    pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "_alias" + (i - 1).ToString(), 0, -1);
                    TxtHelper.ReplaceStr(rcConfPath, "ifconfig_" + adapter + "_alias" + (i - 1).ToString() + "=\"inet " + ipAddresses[i] + " netmask " + subnetMasks[i] + "\"", pos);
                }
            }

            int interfaceNum = ipAddresses.Length;

            do
            {
                pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "_alias" + (interfaceNum - 1).ToString(), 0, -1);
                if (pos != -1)
                {
                    TxtHelper.DelStr(rcConfPath, pos);
                }
                interfaceNum++;
            } while (pos != -1);

            ShellHelper.RunCmd("cp -p " + rcConfPath + " " + rcConf);

            ExecutionResult res = ShellHelper.RunCmd("service netif restart");

            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
            res = ShellHelper.RunCmd("service routing restart");
            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
        }
Esempio n. 8
0
        private static void DisableDHCP_Ubuntu(ExecutionContext context, String adapter, ExecutionResult ret, string[] ipAddresses, string[] subnetMasksPrefix, string[] subnetMasks, string ipGateway)
        {
            //netplan configuration (for Ubuntu 18.04+)
            string netplanPath = GetNetplanPath();
            int    startPos    = TxtHelper.GetStrPos(netplanPath, adapter + ":", 0, -1);

            if (startPos != -1)
            {
                string firstStr    = TxtHelper.GetStr(netplanPath, adapter + ":", startPos, startPos);
                int    spacesCount = GetNetplanSpacesCount(firstStr);

                startPos++;
                int           endPos = GetNetplanEndPos(netplanPath, startPos, spacesCount);
                List <string> config = new List <string>();
                string        str    = new String(' ', spacesCount + 2) + "addresses: [";
                for (int i = 0; i < ipAddresses.Length; i++)
                {
                    str += ipAddresses[i] + "/" + subnetMasksPrefix[i];
                    if (i + 1 < ipAddresses.Length)
                    {
                        str += ",";
                    }
                }
                str += "]";
                config.Add(str);
                config.Add(new String(' ', spacesCount + 2) + "gateway4: " + ipGateway);
                config.Add(new String(' ', spacesCount + 2) + "nameservers:");
                if (CheckParameter(context, "PreferredDNSServer"))
                {
                    string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                    str = new String(' ', spacesCount + 4) + "addresses: [";
                    for (int i = 0; i < dnsServers.Length; i++)
                    {
                        str += dnsServers[i];
                        if (i + 1 < dnsServers.Length)
                        {
                            str += ",";
                        }
                    }
                    str += "]";
                    config.Add(str);
                }
                config.Add(new String(' ', spacesCount + 2) + "dhcp4: no");
                TxtHelper.ReplaceAllStr(netplanPath, config, startPos, endPos);
                ShellHelper.RunCmd("netplan apply");
            }
            //iterfaces configuration (for Ubuntu 16.04-)
            startPos = TxtHelper.GetStrPos(interfacesPath, "iface " + adapter, 0, -1);
            if (startPos != -1)
            {
                int endPos = TxtHelper.GetStrPos(interfacesPath, "auto ", startPos, -1);
                if (endPos != -1)
                {
                    endPos--;
                }
                List <string> config = new List <string>();
                string        str;
                for (int i = 0; i < ipAddresses.Length; i++)
                {
                    config.Add("iface " + adapter + " inet static");
                    config.Add("address " + ipAddresses[i]);
                    config.Add("netmask " + subnetMasks[i]);
                    config.Add("");
                }
                config.Add("gateway " + ipGateway);
                if (CheckParameter(context, "PreferredDNSServer"))
                {
                    string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                    str = "dns-nameservers ";
                    for (int t = 0; t < dnsServers.Length; t++)
                    {
                        str += dnsServers[t];
                        if (t + 1 < dnsServers.Length)
                        {
                            str += " ";
                        }
                    }
                    config.Add(str);
                    config.Add("");
                }
                TxtHelper.ReplaceAllStr(interfacesPath, config, startPos, endPos);
                ret.RebootRequired = true;
            }
        }
Esempio n. 9
0
        private static void InstallService_FreeBSD()
        {
            const string rcConf       = "/etc/rc.conf";
            const string compatLinux  = "/compat/linux";
            const string serviceName  = "solidcp";
            const string servicesPath = "/etc/rc.d/";
            string       userName     = Environment.UserName;
            string       appPath      = AppDomain.CurrentDomain.BaseDirectory;

            List <string> config = new List <string>();

            config.Add("#!/bin/sh");
            config.Add("");
            config.Add("# SolidCP LinuxVmConfig Service");
            config.Add("# PROVIDE: " + serviceName);
            config.Add("# REQUIRE: DAEMON networking");
            config.Add("# BEFORE:  LOGIN");
            config.Add("");
            config.Add(". /etc/rc.subr");
            config.Add("");
            config.Add("name=" + serviceName);
            config.Add("rcvar=" + serviceName + "_enable");
            config.Add(serviceName + "_user=\"" + userName + "\"");
            config.Add("command=\"" + appPath + "SolidCP.LinuxVmConfig" + "\"");
            config.Add("pidfile=\"/var/run/" + serviceName + ".pid\"");
            config.Add("");
            config.Add("start_cmd=\"" + serviceName + "_start\"");
            config.Add("stop_cmd=\"" + serviceName + "_stop\"");
            config.Add("status_cmd=\"" + serviceName + "_status\"");
            config.Add("");
            config.Add(serviceName + "_start() {");
            config.Add("   /usr/sbin/daemon -P ${pidfile} -r -f -u $" + serviceName + "_user $command");
            config.Add("}");
            config.Add("");
            config.Add(serviceName + "_stop() {");
            config.Add("   if [ -e \"${pidfile}\" ]; then");
            config.Add("      kill -s TERM `cat ${pidfile}`");
            config.Add("   else");
            config.Add("      echo \"SolidCP.VmConfig is not running\"");
            config.Add("   fi");
            config.Add("}");
            config.Add("");
            config.Add(serviceName + "_status() {");
            config.Add("   if [ -e \"${pidfile}\" ]; then");
            config.Add("      echo \"SolidCP.VmConfig is running as pid `cat ${pidfile}`\"");
            config.Add("   else");
            config.Add("      echo \"SolidCP.VmConfig is not running\"");
            config.Add("   fi");
            config.Add("}");
            config.Add("");
            config.Add("load_rc_config $name");
            config.Add("run_rc_command \"$1\"");

            File.WriteAllLines(servicesPath + serviceName, config);

            ShellHelper.RunCmd("chmod +x " + servicesPath + serviceName);
            ShellHelper.RunCmd("cp -p " + rcConf + " " + compatLinux + rcConf);
            int pos = TxtHelper.GetStrPos(compatLinux + rcConf, serviceName + "_enable", 0, -1);

            TxtHelper.ReplaceStr(compatLinux + rcConf, serviceName + "_enable=\"YES\"", pos);
            ShellHelper.RunCmd("cp -p " + compatLinux + rcConf + " " + rcConf);

            ExecutionResult res = ShellHelper.RunCmd("service " + serviceName + " start");

            if (res.ResultCode == 1)
            {
                ServiceLog.WriteError("Service install error: " + res.ErrorMessage);
                return;
            }
            ServiceLog.WriteInfo(serviceName + " service successfully installed.");
        }