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; } }
private static void DisableDHCP(ExecutionContext context, String adapter, ExecutionResult ret) { List <string> oldIpList = GetAdapterIp(adapter); string[] ipGateways = ParseArray(context.Parameters["DefaultIPGateway"]); string[] ipAddresses = ParseArray(context.Parameters["IPAddress"]); string[] subnetMasks = ParseArray(context.Parameters["SubnetMask"]); if (subnetMasks.Length != ipAddresses.Length) { throw new ArgumentException("Number of Subnet Masks should be equal to IP Addresses"); } string[] subnetMasksPrefix = new string[subnetMasks.Length]; for (int i = 0; i < subnetMasks.Length; i++) { subnetMasksPrefix[i] = GetSubnetMaskPrefix(subnetMasks[i]); } switch (OsVersion.GetOsVersion()) { case OsVersionEnum.Ubuntu: DisableDHCP_Ubuntu(context, adapter, ret, ipAddresses, subnetMasksPrefix, subnetMasks, ipGateways[0]); break; case OsVersionEnum.CentOS: DisableDHCP_CentOS(context, adapter, ret, ipAddresses, subnetMasksPrefix, ipGateways[0]); break; case OsVersionEnum.FreeBSD: DisableDHCP_FreeBSD(context, adapter, ret, ipAddresses, subnetMasks, ipGateways[0]); break; } if (oldIpList != null)//update ip in hosts { foreach (string ip in oldIpList) { if (ip.Length > 0) { if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD) { ShellHelper.RunCmd("cp -p " + hosts + " " + compatLinux + hosts); TxtHelper.ReplaceStr(compatLinux + hosts, ip, ipAddresses[0]); ShellHelper.RunCmd("cp -p " + compatLinux + hosts + " " + hosts); } else { TxtHelper.ReplaceStr(hosts, ip, ipAddresses[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; } }
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; } }
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); }
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; } }
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."); }