/// <summary> /// Locates file on Linux using which. /// </summary> /// <param name="FileName">Name of file to look for.</param> /// <returns>Result of which or null if the function failed.</returns> public static string Which(string FileName) { log.Trace("(FileName:'{0}')", FileName); string res = null; if (!SystemInfo.CurrentRuntime.IsLinux()) { log.Trace("(-)[NOT_LINUX]:'{0}'", res); return(res); } ConsoleProcess which = new ConsoleProcess("which", FileName); log.Debug("Starting '{0}'.", which.GetCommandLine()); if (which.RunAndWaitSuccessExit()) { List <string> outputLines = which.GetOutput(); if (outputLines.Count > 0) { res = outputLines[0].Trim(); } if (string.IsNullOrEmpty(res)) { res = null; } } else { log.Error("Which on '{0}' failed.", FileName); } log.Trace("(-):'{0}'", res); return(res); }
public override bool Configure() { log.Trace("()"); bool res = false; Dictionary <string, string> conf; if (!ConfigurationByRid.TryGetValue(SystemInfo.CurrentRuntime.Rid, out conf)) { log.Trace("(-)[UNSUPPORTED]:{0}", res); return(res); } bool rpcPublic = false; int rpcPort = int.Parse(conf["RPC port"]); string rpcUser = conf["RPC user"]; string rpcPassword = "******"; string miningLicense = ""; string miningToAddress = ""; string miningLicensePrivKey = ""; bool miningEnabled = false; bool rpcEnabled = false; string dataDir = ""; string confFile = ""; while (!res) { string appDataDir = Environment.ExpandEnvironmentVariables(conf["Application data directory"]); dataDir = InstallationFile.AskForEmptyDirectory(string.Format("Where do you want <white>IoP Blockchain Core wallet application data</white> to be stored? [{0}] ", appDataDir), appDataDir); conf["Application data directory"] = dataDir; GeneralConfiguration.SharedValues.Add("CoreWallet-DataDir", dataDir); confFile = Path.Combine(dataDir, conf["Configuration file"]); int mainPort = int.Parse(conf["Main port"]); mainPort = AskForOpenPort(string.Format("Please enter a port number for <white>Core wallet P2P interface</white>. This port will have to be open and publicly accessible from the Internet. [{0}] ", mainPort), mainPort, "Core wallet P2P interface"); conf["Main port"] = mainPort.ToString(); CUI.WriteRich("Would you like to run <white>Core wallet RPC server</white> (this is required if you want to mine)? [<white>N</white>O / <white>y</white>es] "); rpcEnabled = CUI.ReadKeyAnswer(new char[] { 'n', 'y' }) == 'y'; if (rpcEnabled) { CUI.WriteRich("Would you like the <white>RPC server</white> to be accessible from the Internet? [<white>N</white>O / <white>y</white>es] "); rpcPublic = CUI.ReadKeyAnswer(new char[] { 'n', 'y' }) == 'y'; if (rpcPublic) { rpcPort = AskForOpenPort(string.Format("Please enter a port number for <white>Core wallet RPC interface</white>. This port will have to be open and publicly accessible from the Internet. If you changed your mind and you do not want the RPC server to be accessible from the Internet, enter 0. [{0}] ", rpcPort), rpcPort, "Core wallet RPC interface", true); if (rpcPort != 0) { conf["RPC port"] = rpcPort.ToString(); } else { rpcPublic = false; } } CUI.WriteRich("Enter RPC user name: [{0}] ", rpcUser); rpcUser = CUI.ReadStringAnswer(rpcUser); conf["RPC user"] = rpcUser; CUI.WriteRich("Enter RPC password: [{0}] ", rpcPassword); rpcPassword = CUI.ReadStringAnswer(rpcPassword); CUI.WriteRich("Do you have a mining license and would you like to run a miner? [<white>N</white>O / <white>y</white>es] "); miningEnabled = CUI.ReadKeyAnswer(new char[] { 'n', 'y' }) == 'y'; if (miningEnabled) { CUI.WriteRich("Enter your mining license address (or press ENTER if you changed your mind): "); miningLicense = CUI.ReadStringAnswer(miningLicense); if (!string.IsNullOrEmpty(miningLicense)) { CUI.WriteRich("Enter the private key for your mining license. <yellow>Note that If you entered a wrong mining license or if you enter wrong private key, this installer will not recognize it, but your Core wallet may fail to start.</yellow> Private key (or press ENTER if you changed your mind): "); miningLicensePrivKey = CUI.ReadStringAnswer(miningLicensePrivKey); if (!string.IsNullOrEmpty(miningLicensePrivKey)) { CUI.WriteRich("Enter the address to which the newly minted coins should be sent (or press ENTER to generate the address automatically): "); miningToAddress = CUI.ReadStringAnswer(miningToAddress); } else { miningEnabled = false; miningLicense = ""; } } else { miningEnabled = false; } } } string confFileContents = ConfigurationFileTemplate .Replace("$MAIN_PORT", conf["Main port"]) .Replace("$ENABLE_RPC", rpcEnabled ? "1" : "0") .Replace("$RPC_USER", conf["RPC user"]) .Replace("$RPC_PASSWORD", rpcPassword) .Replace("$RPC_PORT", conf["RPC port"]) .Replace("$ENABLE_MINING", "0") .Replace("$MINING_LICENSE", miningLicense) .Replace("$MINE_TO_ADDR", miningToAddress); CUI.WriteRich("Writing <white>Core wallet's configuration</white> with mining DISABLED to <white>{0}</white>... ", confFile); if (InstallationFile.CreateFileWriteTextChown(confFile, confFileContents)) { CUI.WriteOk(); res = true; } else { CUI.WriteFailed(); CUI.WriteRich("<red>ERROR:</red> unable to write to <white>{0}</white>. Please try again.\n", confFile); } } string iopd = null; if (res) { iopd = SystemInfo.CurrentRuntime.IsLinux() ? InstallationFile.Which("IoPd") : Path.Combine(GeneralConfiguration.SharedValues["CoreWalletDir-InternalPath"], "IoPd.exe"); GeneralConfiguration.SharedValues[Name + "-executable"] = iopd; GeneralConfiguration.SharedValues[Name + "-executable-args"] = string.Format("\"{0}\" -datadir=\"{1}\"", iopd, GeneralConfiguration.SharedValues["CoreWallet-DataDir"]); } if (res && miningEnabled) { log.Trace("Configuring mining."); CUI.WriteRich("The basic configuration of <white>Core Wallet</white> is complete, but as you wanted to enable mining, some more configuration is needed. " + "Please be patient, it won't take long. If this step fails, the installation will continue and your <white>Core Wallet</white> will be fully setup except for the mining.\n"); bool miningSetupOk = false; bool saveNewConfiguration = false; string iopCli = SystemInfo.CurrentRuntime.IsLinux() ? InstallationFile.Which("IoP-cli") : Path.Combine(GeneralConfiguration.SharedValues["CoreWalletDir-InternalPath"], "IoP-cli.exe"); CUI.WriteRich("Looking for <white>IoPd</white>... "); log.Debug("IoPd location is '{0}'.", iopd); if (File.Exists(iopd)) { CUI.WriteOk(); CUI.WriteRich("Looking for <white>IoP-cli</white>... "); log.Debug("IoP-cli location is '{0}'.", iopCli); if (File.Exists(iopCli)) { CUI.WriteOk(); CUI.WriteRich("Starting <white>IoPd</white>... "); ConsoleProcess iopdProcess = new ConsoleProcess(iopd, string.Format("-datadir=\"{0}\"", dataDir)); if (iopdProcess.Run()) { Thread.Sleep(5000); CUI.WriteOk(); CUI.WriteRich("Starting <white>IoP-cli</white> to import the private key... "); ConsoleProcess iopCliProcess = new ConsoleProcess(iopCli, string.Format("-rpcconnect=127.0.0.1 -rpcport={0} -rpcuser={1} -rpcpassword={2} importprivkey {3}", rpcPort, rpcUser, rpcPassword, miningLicensePrivKey)); if (iopCliProcess.RunAndWaitSuccessExit()) { CUI.WriteOk(); if (string.IsNullOrEmpty(miningToAddress)) { CUI.WriteRich("Starting <white>IoP-cli</white> to generate mining-to address... "); iopCliProcess = new ConsoleProcess(iopCli, string.Format("-rpcconnect=127.0.0.1 -rpcport={0} -rpcuser={1} -rpcpassword={2} getnewaddress", rpcPort, rpcUser, rpcPassword)); if (iopCliProcess.RunAndWaitSuccessExit()) { List <string> outputLines = iopCliProcess.GetOutput(); if (outputLines.Count != 0) { miningToAddress = outputLines[0].Trim(); CUI.WriteOk(); CUI.WriteRich("Your newly generated mining-to address is: <white>{0}</white>\n", miningToAddress); saveNewConfiguration = true; } else { log.Error("Invalid output received from IoP-cli."); CUI.WriteFailed(); } } else { log.Error("Unable to start IoP-cli or it failed."); CUI.WriteFailed(); } } } else { log.Error("Unable to start IoP-cli or it failed."); CUI.WriteFailed(); } CUI.WriteRich("Starting <white>IoP-cli</white> to stop IoPd... "); iopCliProcess = new ConsoleProcess(iopCli, string.Format("-rpcconnect=127.0.0.1 -rpcport={0} -rpcuser={1} -rpcpassword={2} stop", rpcPort, rpcUser, rpcPassword)); if (iopCliProcess.RunAndWaitSuccessExit()) { CUI.WriteOk(); } else { CUI.WriteFailed(); } CUI.WriteRich("Waiting for <white>IoP-cli</white> to stop... "); if (iopdProcess.WaitSuccessExit(15000)) { CUI.WriteOk(); } else { CUI.WriteFailed(); CUI.WriteLine("IoPd will now be killed."); iopdProcess.KillProcess(); } } else { log.Error("Unable to start IoPd."); CUI.WriteFailed(); } } else { log.Error("Unable to find IoP-cli."); CUI.WriteFailed(); } } else { log.Error("Unable to find IoPd."); CUI.WriteFailed(); } if (saveNewConfiguration) { string confFileContents = ConfigurationFileTemplate .Replace("$MAIN_PORT", conf["Main port"]) .Replace("$ENABLE_RPC", rpcEnabled ? "1" : "0") .Replace("$RPC_USER", conf["RPC user"]) .Replace("$RPC_PASSWORD", rpcPassword) .Replace("$RPC_PORT", conf["RPC port"]) .Replace("$ENABLE_MINING", "1") .Replace("$MINING_LICENSE", miningLicense) .Replace("$MINE_TO_ADDR", miningToAddress); CUI.WriteRich("Writing <white>Core wallet's configuration</white> with mining ENABLED to <white>{0}</white>... ", confFile); if (InstallationFile.CreateFileWriteTextChown(confFile, confFileContents)) { CUI.WriteOk(); miningSetupOk = true; } else { CUI.WriteFailed(); CUI.WriteRich("<red>ERROR:</red> unable to write to <white>{0}</white>.\n", confFile); } } if (miningSetupOk) { CUI.WriteRich("Configuration of <white>Core wallet</white> for mining <green>SUCCEEDED</green>.\n"); } else { CUI.WriteRich("<red>Configuration of Core wallet for mining failed.</red>\n"); } } log.Trace("(-):{0}", res); return(res); }
/// <summary> /// Obtains .NET Core Runtime identifier of the current system. /// </summary> /// <returns>Runtime identifier of the current system.</returns> private static RuntimeInfo GetSystemRuntimeInfo() { log.Trace("()"); Rid rid = Rid.Unknown; try { // We only support x64 architecture. if (RuntimeInformation.OSArchitecture == Architecture.X64) { string desc = RuntimeInformation.OSDescription.ToLowerInvariant().Trim(); log.Debug("OS description '{0}'.", desc); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { log.Debug("Windows platform detected."); string winstr = "microsoft windows "; if (desc.StartsWith(winstr)) { string ver = desc.Substring(winstr.Length); log.Debug("Windows version '{0}' detected.", ver); if (ver.StartsWith("6.1.")) { rid = Rid.win7_x64; } else if (ver.StartsWith("6.2.")) { rid = Rid.win8_x64; } else if (ver.StartsWith("6.3.")) { rid = Rid.win81_x64; } else if (ver.StartsWith("10.0.")) { rid = Rid.win10_x64; } else { log.Error("Windows version '{0}' is not supported.", ver); } } else { log.Error("Invalid or unsupported Windows OS description '{0}'.", desc); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { log.Debug("Linux platform detected."); bool isUbuntu = desc.Contains("ubuntu"); bool isDebian = desc.Contains("debian"); if (isUbuntu || isDebian) { ConsoleProcess lsb = new ConsoleProcess("lsb_release", "-r"); if (lsb.RunAndWaitSuccessExit()) { List <string> lines = lsb.GetOutput(); if ((lines != null) && (lines.Count > 0)) { string ver = lines[0].Trim(); int li = ver.LastIndexOf(':'); ver = ver.Substring(li + 1).Trim(); if (isUbuntu) { if (ver.StartsWith("16.10")) { rid = Rid.ubuntu_16_10_x64; } else if (ver.StartsWith("16.04")) { rid = Rid.ubuntu_16_04_x64; } else if (ver.StartsWith("14.10")) { rid = Rid.ubuntu_14_10_x64; } else if (ver.StartsWith("14.04")) { rid = Rid.ubuntu_14_04_x64; } else { log.Error("Ubuntu version '{0}' is not supported.", ver); } } else { if (ver.StartsWith("8.")) { rid = Rid.debian_8_x64; } else { log.Error("Debian version '{0}' is not supported.", ver); } } } else { log.Error("Empty output of 'lsb_release -r' received."); } } else { log.Error("Executing '{0}' failed.", lsb.GetCommandLine()); } } else { log.Error("This Linux distro is not supported."); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { log.Debug("OSX platform detected."); string osxstr = "darwin "; if (desc.StartsWith(osxstr)) { string ver = desc.Substring(osxstr.Length); log.Debug("OS X version '{0}' detected.", ver); if (ver.StartsWith("14.")) { rid = Rid.osx_10_10_x64; } else if (ver.StartsWith("15.")) { rid = Rid.osx_10_11_x64; } else if (ver.StartsWith("16.")) { rid = Rid.osx_10_12_x64; } else { log.Error("OS X version '{0}' is not supported.", ver); } } else { log.Error("Invalid or unsupported OS X description '{0}'.", desc); } } else { log.Error("Unknown OS platform, OS description is '{0}'.", RuntimeInformation.OSDescription); } } else { log.Error("OS architecture {0} is not supported.", RuntimeInformation.OSArchitecture); } } catch (Exception e) { log.Error("Exception occurred: {0}", e.ToString()); } RuntimeInfo res = new RuntimeInfo(rid); log.Trace("(-):{0}", res); return(res); }