Esempio n. 1
0
        /// <summary>
        /// Asks user to open a specific port, then checks it.
        /// </summary>
        /// <param name="Port">TCP port to check.</param>
        /// <returns>true if the port is open, false if user want to otherwise.</returns>
        public bool CheckPortOpenUI(int Port)
        {
            log.Trace("(Port:{0})", Port);

            bool res = false;

            bool done = false;

            while (!done)
            {
                CUI.WriteRich("Checking whether the TCP port <white>{0}</white> can be accessed from the Internet by connecting to <white>{1}:{0}</white>... ", Port, GeneralConfiguration.ExternalIpAddress);

                if (CheckPortOpen(Port))
                {
                    res = true;
                    break;
                }

                CUI.WriteRich("TCP port <white>{0}</white> is not open or an error occurred. How would you like to proceed? [<white>C</white>HECK AGAIN / <white>s</white>elect different port] ", Port, GeneralConfiguration.ExternalIpAddress);

                done = CUI.ReadKeyAnswer(new char[] { 'c', 's' }) == 's';
            }

            CUI.WriteLine();

            log.Trace("(-):{0}", res);
            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// Asks user to input a TCP port number and verifies that it is an open port.
        /// </summary>
        /// <param name="RichMessage">Message to print on the console.</param>
        /// <param name="DefaultValue">Default value for the port.</param>
        /// <param name="ServiceName">Name of the service this port will be used by.</param>
        /// <param name="AllowCancel">Allow user to cancel port selection.</param>
        /// <returns>Selected port value that is open from outside.</returns>
        public int AskForOpenPort(string RichMessage, int DefaultValue, string ServiceName, bool AllowCancel = false)
        {
            log.Trace("(DefaultValue:{0},ServiceName:'{1}',AllowCancel:{2})", DefaultValue, ServiceName, AllowCancel);

            int res = 0;

            bool done = false;

            while (!done)
            {
                CUI.WriteRich(RichMessage);
                string answer = CUI.ReadStringAnswer(DefaultValue.ToString());

                int port;
                if (int.TryParse(answer, out port) && (0 < port) && (port <= 65535))
                {
                    if (!GeneralConfiguration.UsedPorts.ContainsKey(port))
                    {
                        if (CheckPortOpenUI(port))
                        {
                            GeneralConfiguration.UsedPorts[port] = ServiceName;
                            res  = port;
                            done = true;
                        }
                        else
                        {
                            log.Warn("Port {0} is not open.", port);
                        }
                    }
                    else
                    {
                        log.Error("Port {0} is already used for '{1}'.", port, GeneralConfiguration.UsedPorts[port]);
                        CUI.WriteRich("<red>ERROR:</red> Port <white>{0}</white> is already used by <white>{1}</white>. Please try different port.\n", port, GeneralConfiguration.UsedPorts[port]);
                    }
                }
                else if (AllowCancel && (answer == "0"))
                {
                    log.Debug("Port selection cancelled.");
                    done = true;
                }
                else
                {
                    log.Error("Invalid port number entered '{0}'.", answer);
                    CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid port number. It has to be an integer between 1 and 65535. Please try again.\n", answer);
                }
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
Esempio n. 3
0
        /// <summary>
        /// Installs all installation files of the component.
        /// </summary>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        public virtual bool Install()
        {
            log.Trace("()");
            bool res = false;

            Status = InstallableComponentStatus.InstallationInitiated;

            bool error = false;

            installationFiles = GetInstallationFiles();
            foreach (InstallationFile instFile in installationFiles)
            {
                log.Info("Installing '{0}'.", instFile.Name);
                CUI.WriteRich("Installing module <white>{0}</white>.\n", instFile.Name);

                if (!instFile.Install())
                {
                    log.Error("Installation of '{0}' failed.", instFile.Name);
                    error = true;
                    break;
                }
            }

            if (!error)
            {
                Status |= InstallableComponentStatus.InstallationCompleted;
                res     = true;
            }
            else
            {
                // Cleanup.
                foreach (InstallationFile instFile in installationFiles)
                {
                    if (instFile.Status != InstallationFileStatus.None)
                    {
                        log.Info("Uninstalling '{0}'.", instFile.Name);
                        instFile.Uninstall();
                    }
                }
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Installs init.d script on Linux from script template file.
        /// </summary>
        /// <param name="TemplateFile">Name of the template file, which is also a name of the init.d script.</param>
        /// <param name="Replacements">List of template replacements mapped by the patterns to replace.</param>
        /// <param name="UpdateRcdInstallArgs">Arguments for update-rc.d command for script installation.</param>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        public static bool InstallInitdScript(string TemplateFile, Dictionary <string, string> Replacements, string UpdateRcdInstallArgs)
        {
            log.Trace("(TemplateFile:'{0}',UpdateRcdInstallArgs:'{1}')", TemplateFile, UpdateRcdInstallArgs);

            bool res = false;

            CUI.WriteRich("Creating init.d script <white>{0}</white>... ", TemplateFile);

            bool   error       = true;
            string content     = null;
            string initdScript = Path.Combine("init.d", TemplateFile);
            string destFile    = string.Format("/etc/init.d/{0}", TemplateFile);

            try
            {
                content = File.ReadAllText(initdScript);
                foreach (KeyValuePair <string, string> kvp in Replacements)
                {
                    content = content.Replace(kvp.Key, kvp.Value);
                }

                File.WriteAllText(destFile, content);

                if (Chmod(destFile, "a+x"))
                {
                    CUI.WriteOk();
                    error = false;
                }
                else
                {
                    log.Error("chmod on '{0}' failed.", destFile);
                    CUI.WriteFailed();
                    CUI.Write("<red>ERROR:</red> Unable to set access rights of file <white>{0}</white>.\n", initdScript);
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
                CUI.WriteFailed();
                if (content == null)
                {
                    CUI.Write("<red>ERROR:</red> Unable to read file <white>{0}</white>: {1}\n", initdScript, e.Message);
                }
                else
                {
                    CUI.Write("<red>ERROR:</red> Unable to write to file <white>{0}</white>: {1}\n", destFile, e.Message);
                }
            }

            if (!error)
            {
                CUI.WriteRich("Installing init script links for <white>{0}</white>... ", TemplateFile);
                if (UpdateRcdInstall(TemplateFile, UpdateRcdInstallArgs))
                {
                    CUI.WriteOk();
                    res = true;
                }
                else
                {
                    log.Error("update-rc.d failed for '{0}'.", TemplateFile);
                    CUI.WriteFailed();
                }
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Asks user to input a name of an empty or non-existing directory.
        /// </summary>
        /// <param name="RichMessage">Message to print on the console.</param>
        /// <param name="DefaultValue">Default value for the directory name.</param>
        /// <returns>Selected directory name that is empty.</returns>
        public static string AskForEmptyDirectory(string RichMessage, string DefaultValue)
        {
            log.Trace("(DefaultValue:'{0}')", DefaultValue);

            string res = "";

            bool done = false;

            while (!done)
            {
                CUI.WriteRich(RichMessage);
                string answer = CUI.ReadStringAnswer(DefaultValue);

                string existingDir;
                if (FindDirectory(answer, out existingDir))
                {
                    if (IsEmptyDirectory(existingDir))
                    {
                        res  = existingDir;
                        done = true;
                    }
                    else
                    {
                        CUI.WriteRich("\n<yellow>Directory '{0}' is not empty.</yellow> Do you want to erase its contents or select another directory? [<white>S</white>ELECT ANOTHER / <white>e</white>rase contents] ", existingDir);
                        bool eraseDirectory = CUI.ReadKeyAnswer(new char[] { 's', 'e' }) == 'e';
                        if (eraseDirectory)
                        {
                            CUI.WriteRich("Erasing directory <white>'{0}'</white>... ", existingDir);
                            if (CleanDirectory(existingDir))
                            {
                                CUI.WriteOk();
                                res  = existingDir;
                                done = true;
                            }
                            else
                            {
                                CUI.WriteFailed();
                                CUI.WriteRich("\n<red>ERROR:</red> Unable to erase contents of directory <white>'{0}'</white>. Please select different directory.\n", existingDir);
                            }
                        }
                    }
                }
                else
                {
                    existingDir = CreateDirectory(answer);
                    if (existingDir != null)
                    {
                        res  = existingDir;
                        done = true;
                    }
                    else
                    {
                        CUI.WriteRich("\n<red>ERROR:</red> Unable to create directory <white>'{0}'</white>. Please try again.\n", answer);
                    }
                }
            }

            CUI.WriteLine();

            log.Trace("(-):'{0}'", res);
            return(res);
        }
Esempio n. 6
0
        /// <summary>
        /// Tries to listen on a specific port and asks seed node to connect to it.
        /// </summary>
        /// <param name="Port">TCP port to check.</param>
        /// <returns>true if the port is open, false otherwise.</returns>
        public bool CheckPortOpen(int Port)
        {
            log.Trace("(Port:{0})", Port);

            bool res = false;

            if (Program.TestMode)
            {
                if (Port > 50000)
                {
                    CUI.WriteOk();
                    res = true;
                    log.Trace("(-)[TEST_MODE_ENABLED]:{0}", res);
                    return(res);
                }
            }

            try
            {
                portListenerThreadShutdownEvent.Reset();

                Thread portListenerThread = new Thread(new ParameterizedThreadStart(PortListenerThread));
                portListenerThread.Start(Port);

                if (portListenerReadyEvent.WaitOne(5000))
                {
                    res = GeneralConfiguration.CheckPort(GeneralConfiguration.ExternalIpAddress, Port);
                    if (res)
                    {
                        CUI.WriteOk();
                    }
                    else
                    {
                        CUI.WriteFailed();
                    }
                }
                else
                {
                    log.Error("Port listener thread did not get ready on time.");
                    CUI.WriteFailed();
                    CUI.WriteRich("<red>ERROR:</red> Unexpected error occurred, please try again.\n");
                }

                portListenerThreadShutdownEvent.Set();

                if (!portListenerThread.Join(10000))
                {
                    log.Error("Port listener thread failed to finish on time.");
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
                CUI.WriteFailed();
                CUI.WriteRich("<red>ERROR:</red> Exception occurred: {0}\n", e.Message);
            }


            log.Trace("(-):{0}", res);
            return(res);
        }