/// <summary>
        /// Uninstalls package by executing apt-get remove.
        /// </summary>
        /// <param name="PackageName">Package to uninstall.</param>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        public static bool UninstallAptGetPackage(string PackageName)
        {
            log.Trace("(FileName:'{0}')", PackageName);

            bool res = false;

            try
            {
                ConsoleProcess process = new ConsoleProcess("apt-get", string.Format("remove -qy {0}", PackageName));
                if (process.RunAndWaitSuccessExit(120000))
                {
                    res = true;
                }
                else
                {
                    log.Error("Executing '{0}' failed.", process.GetCommandLine());
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Executes schtasks command on Windows.
        /// </summary>
        /// <param name="Arguments">Arguments for schtasks command.</param>
        /// <returns>true if the function succeeded, false otherwise.</returns>
        public static bool Schtasks(string Arguments)
        {
            log.Trace("(Arguments:'{0}')", Arguments);
            bool res = false;

            if (!SystemInfo.CurrentRuntime.IsWindows())
            {
                res = true;
                log.Trace("(-)[NOT_WINDOWS]:{0}", res);
                return(res);
            }

            ConsoleProcess schtasks = new ConsoleProcess("schtasks.exe", Arguments);

            log.Debug("Starting '{0}'.", schtasks.GetCommandLine());
            if (schtasks.RunAndWaitSuccessExit())
            {
                log.Debug("schtasks succeeded.");
                res = true;
            }
            else
            {
                log.Error("schtasks failed.");
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Starts init.d script on Linux.
        /// </summary>
        /// <param name="ScriptName">Name of the init.d script to start.</param>
        /// <param name="Args">Arguments for the command.</param>
        /// <returns>true if the function succeeded, false otherwise.</returns>
        public static bool RunInitdScript(string ScriptName, string Args)
        {
            log.Trace("(ScriptName:'{0}',Args:'{1}')", ScriptName, Args);
            bool res = false;

            if (!SystemInfo.CurrentRuntime.IsLinux())
            {
                res = true;
                log.Trace("(-)[NOT_LINUX]:{0}", res);
                return(res);
            }

            string         scriptFile = string.Format("/etc/init.d/{0}", ScriptName);
            ConsoleProcess script     = new ConsoleProcess(scriptFile, Args);

            log.Debug("Starting '{0}'.", script.GetCommandLine());
            if (script.RunAndWaitSuccessExit())
            {
                log.Debug("'{0}' succeeded.", ScriptName);
                res = true;
            }
            else
            {
                log.Error("'{0}' failed.", ScriptName);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <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);
        }
        /// <summary>
        /// Executes update-rc.d on Linux.
        /// </summary>
        /// <param name="Args">Arguments for the command.</param>
        /// <returns>true if the function succeeded, false otherwise.</returns>
        public static bool UpdateRcd(string Args)
        {
            log.Trace("(Args:'{0}')", Args);
            bool res = false;

            if (!SystemInfo.CurrentRuntime.IsLinux())
            {
                res = true;
                log.Trace("(-)[NOT_LINUX]:{0}", res);
                return(res);
            }

            ConsoleProcess updateRcd = new ConsoleProcess("update-rc.d", Args);

            log.Debug("Starting '{0}'.", updateRcd.GetCommandLine());
            if (updateRcd.RunAndWaitSuccessExit())
            {
                log.Debug("update-rc.d succeeded.");
                res = true;
            }
            else
            {
                log.Error("update-rc.d failed.");
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Change access rights of file on Linux using chmod.
        /// </summary>
        /// <param name="Path">Path to file or folder.</param>
        /// <param name="AccessRights">chmod access rights.</param>
        /// <returns>true if the function succeeded, false otherwise.</returns>
        public static bool Chmod(string Path, string AccessRights)
        {
            log.Trace("(Path:'{0}',AccessRights:'{1}')", Path, AccessRights);
            bool res = false;

            if (!SystemInfo.CurrentRuntime.IsLinux())
            {
                res = true;
                log.Trace("(-)[NOT_LINUX]:{0}", res);
                return(res);
            }

            ConsoleProcess chmod = new ConsoleProcess("chmod", string.Format("{0} \"{1}\"", AccessRights, Path));

            log.Debug("Starting '{0}'.", chmod.GetCommandLine());
            if (chmod.RunAndWaitSuccessExit())
            {
                log.Debug("Access rights of '{0}' changed.", Path);
                res = true;
            }
            else
            {
                log.Error("chmod on '{0}' failed.", Path);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Change owner of file or folder on Linux using chown.
        /// </summary>
        /// <param name="Path">Path to file or folder.</param>
        /// <returns>true if the function succeeded, false otherwise.</returns>
        public static bool Chown(string Path)
        {
            log.Trace("(Path:'{0}')", Path);
            bool res = false;

            if (!SystemInfo.CurrentRuntime.IsLinux())
            {
                res = true;
                log.Trace("(-)[NOT_LINUX]:{0}", res);
                return(res);
            }

            if (string.IsNullOrEmpty(Program.SudoUserGroup))
            {
                res = true;
                log.Trace("(-)[NOT_SUDO]:{0}", res);
                return(res);
            }

            ConsoleProcess chown = new ConsoleProcess("chown", string.Format("-R {0} \"{1}\"", Program.SudoUserGroup, Path));

            log.Debug("Starting '{0}'.", chown.GetCommandLine());
            if (chown.RunAndWaitSuccessExit())
            {
                log.Debug("Owner of '{0}' changed.", Path);
                res = true;
            }
            else
            {
                log.Error("chown on '{0}' failed.", Path);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
Example #8
0
        /// <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);
        }