Пример #1
0
        public string AddSshKey(string sshKey, string keyType, string comment)
        {
            string output = "";

            string key = string.Format("{0} {1} {2}", keyType, sshKey, comment);

            string binLocation  = Path.GetDirectoryName(this.GetType().Assembly.Location);
            string addKeyScript = Path.GetFullPath(Path.Combine(binLocation, @"powershell\Tools\sshd\add-key.ps1"));

            ProcessStartInfo pi = new ProcessStartInfo();

            pi.UseShellExecute        = false;
            pi.RedirectStandardError  = true;
            pi.RedirectStandardOutput = true;
            pi.FileName = ProcessExtensions.Get64BitPowershell();

            pi.Arguments = string.Format(@"-ExecutionPolicy Bypass -InputFormat None -noninteractive -file {0} -targetDirectory {2} -windowsUser {3} -key ""{1}""",
                                         addKeyScript,
                                         key,
                                         NodeConfig.Values["SSHD_BASE_DIR"],
                                         Environment.UserName);
            Process p = Process.Start(pi);

            p.WaitForExit(60000);
            output += p.StandardError.ReadToEnd();
            output += p.StandardOutput.ReadToEnd();

            return(output);
        }
Пример #2
0
        public void Test_ProcessExtensions()
        {
            bool testresult = true;

            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = "cmd";
                process.Start();
                try
                {
                    ProcessExtensions.KillProcessAndChildren(process);
                }
                catch
                {
                    if (!process.HasExited)
                    {
                        throw new Exception("Test Failure");
                    }
                }
                process.Start();
                try
                {
                    ProcessExtensions.KillProcessAndChildren(process.Id);
                }
                catch
                {
                    if (!process.HasExited)
                    {
                        throw new Exception("Test Failure");
                    }
                }
                string powershell = ProcessExtensions.Get64BitPowershell();
                if (powershell == string.Empty)
                {
                    testresult = false;
                }
            }
            catch
            {
                testresult = false;
            }
            Assert.AreEqual(true, testresult);
        }
Пример #3
0
        public string RemoveSshdUser()
        {
            string binLocation = Path.GetDirectoryName(this.GetType().Assembly.Location);
            string script      = Path.GetFullPath(Path.Combine(binLocation, @"powershell\Tools\sshd\remove-sshd-user.ps1"));

            ProcessResult result = ProcessExtensions.RunCommandAndGetOutput(ProcessExtensions.Get64BitPowershell(), string.Format(
                                                                                @"-ExecutionPolicy Bypass -InputFormat None -noninteractive -file {0} -targetDirectory {2} -user {1} -windowsUser {5} -userHomeDir {3} -userShell {4}",
                                                                                script,
                                                                                this.container.Uuid,
                                                                                NodeConfig.Values["SSHD_BASE_DIR"],
                                                                                this.container.ContainerDir,
                                                                                NodeConfig.Values["GEAR_SHELL"],
                                                                                Environment.UserName));

            if (result.ExitCode != 0)
            {
                throw new Exception(string.Format("Error deleting sshd user for gear {0} - rc={1}; out={2}; err={3}", this.container.Uuid, result.ExitCode, result.StdOut, result.StdErr));
            }

            return(result.StdOut);
        }
Пример #4
0
        public string DoActionHook(string action, Dictionary <string, string> env, dynamic options)
        {
            StringBuilder output = new StringBuilder();

            action = action.Replace('-', '_');
            string actionHook = Path.Combine(env["OPENSHIFT_REPO_DIR"], ".openshift", "action_hooks", action + ".ps1");

            if (File.Exists(actionHook))
            {
                string cmd = string.Format("{0} -ExecutionPolicy Bypass -InputFormat None -noninteractive -file {1}", ProcessExtensions.Get64BitPowershell(), actionHook);

                ProcessResult processResult = container.RunProcessInContainerContext(container.ContainerDir, cmd);

                output.AppendLine(processResult.StdOut);
                output.AppendLine(processResult.StdErr);
            }
            return(output.ToString());
        }
Пример #5
0
        public string CartridgeAction(Manifest cartridge, string action, string softwareVersion, bool renderErbs = false)
        {
            string cartridgeHome = Path.Combine(this.container.ContainerDir, cartridge.Dir);
            bool   ps            = false;

            if (File.Exists(Path.Combine(cartridgeHome, "bin", action + ".exe")))
            {
                action = Path.Combine(cartridgeHome, "bin", action + ".exe");
            }
            else
            {
                action = Path.Combine(cartridgeHome, "bin", action + ".ps1");
                ps     = true;
            }
            if (!File.Exists(action))
            {
                return(string.Empty);
            }

            Dictionary <string, string> gearEnv      = Environ.ForGear(this.container.ContainerDir);
            string cartridgeEnvHome                  = Path.Combine(cartridgeHome, "env");
            Dictionary <string, string> cartridgeEnv = Environ.Load(cartridgeEnvHome);

            cartridgeEnv.Remove("PATH");
            foreach (var kvp in gearEnv)
            {
                cartridgeEnv[kvp.Key] = kvp.Value;
            }
            if (renderErbs)
            {
                // TODO: render erb
            }

            // TODO: vladi: implement hourglass
            string cmd = null;

            if (ps)
            {
                cmd = string.Format("{0} -ExecutionPolicy Bypass -InputFormat None -noninteractive -file {1} --version {2}", ProcessExtensions.Get64BitPowershell(), action, softwareVersion);
            }
            else
            {
                cmd = string.Format("{0} --version {1}", action, softwareVersion);
            }
            string output = this.container.RunProcessInContainerContext(cartridgeHome, cmd, 0).StdOut;

            // TODO: vladi: add logging
            return(output);
        }
Пример #6
0
        public string DoControlWithDirectory(string action, dynamic options)
        {
            // TODO: vladi: complete implementation of this method

            StringBuilder output             = new StringBuilder();
            string        cartridgeDirectory = options["cartridgeDir"];

            ProcessCartridges(cartridgeDirectory, delegate(string cartridgeDir)
            {
                bool isExe     = File.Exists(Path.Combine(cartridgeDir, "bin", "control.exe"));
                string control = string.Empty;
                string cmd     = string.Empty;

                if (isExe)
                {
                    control = Path.Combine(cartridgeDir, "bin", "control.exe");
                    cmd     = string.Format("{0} {1}", control, action);
                }
                else
                {
                    control = Path.Combine(cartridgeDir, "bin", "control.ps1");
                    cmd     = string.Format("{0} -ExecutionPolicy Bypass -InputFormat None -noninteractive -file {1} -command {2}", ProcessExtensions.Get64BitPowershell(), control, action);
                }
                ProcessResult processResult = container.RunProcessInContainerContext(container.ContainerDir, cmd);

                output.AppendLine(processResult.StdOut);
                output.AppendLine(processResult.StdErr);

                if (processResult.ExitCode != 0)
                {
                    throw new Exception(string.Format("CLIENT_ERROR: Failed to execute: 'control {0}' for {1}", action, container.ContainerDir));
                }
            });

            return(output.ToString());
        }
Пример #7
0
        public string DoControlWithDirectory(string action, dynamic options)
        {
            // TODO: vladi: complete implementation of this method

            StringBuilder output             = new StringBuilder();
            string        cartridgeDirectory = options["cartridgeDir"];

            ProcessCartridges(cartridgeDirectory, delegate(string cartridgeDir)
            {
                string control = Path.Combine(cartridgeDir, "bin", "control.ps1");
                string cmd     = string.Format("{0} -ExecutionPolicy Bypass -InputFormat None -noninteractive -file {1} -command {2}", ProcessExtensions.Get64BitPowershell(), control, action);

                ProcessResult processResult = container.RunProcessInContainerContext(container.ContainerDir, cmd);

                output.AppendLine(processResult.StdOut);
                output.AppendLine(processResult.StdErr);
            });

            return(output.ToString());
        }
Пример #8
0
        public void Create()
        {
            Guid prisonGuid = Guid.Parse(container.Uuid.PadLeft(32, '0'));

            Logger.Debug("Creating prison with guid: {0}", prisonGuid);

            Uhuru.Prison.Prison prison = new Uhuru.Prison.Prison(prisonGuid);
            prison.Tag = "oo";

            Uhuru.Prison.PrisonRules prisonRules = new Uhuru.Prison.PrisonRules();

            prisonRules.CellType  = Prison.RuleType.None;
            prisonRules.CellType |= Prison.RuleType.Memory;
            prisonRules.CellType |= Prison.RuleType.CPU;
            prisonRules.CellType |= Prison.RuleType.WindowStation;
            prisonRules.CellType |= Prison.RuleType.Httpsys;
            prisonRules.CellType |= Prison.RuleType.IISGroup;
            // prisonRules.CellType |= Prison.RuleType.Filesystem;
            prisonRules.CellType |= Prison.RuleType.MsSqlInstance;

            prisonRules.CPUPercentageLimit   = Convert.ToInt64(Node.ResourceLimits["cpu_quota"]);
            prisonRules.ActiveProcessesLimit = Convert.ToInt32(Node.ResourceLimits["max_processes"]);
            prisonRules.PriorityClass        = ProcessPriorityClass.BelowNormal;

            // TODO: vladi: make sure these limits are ok being the same
            prisonRules.NetworkOutboundRateLimitBitsPerSecond = Convert.ToInt64(Node.ResourceLimits["max_upload_bandwidth"]);
            prisonRules.AppPortOutboundRateLimitBitsPerSecond = Convert.ToInt64(Node.ResourceLimits["max_upload_bandwidth"]);

            prisonRules.TotalPrivateMemoryLimitBytes = Convert.ToInt64(Node.ResourceLimits["max_memory"]) * 1024 * 1024;
            prisonRules.DiskQuotaBytes = Convert.ToInt64(Node.ResourceLimits["quota_blocks"]) * 1024;

            prisonRules.PrisonHomePath = container.ContainerDir;
            prisonRules.UrlPortAccess  = Network.GetUniquePredictablePort(@"c:\openshift\ports");

            Logger.Debug("Assigning port {0} to gear {1}", prisonRules.UrlPortAccess, container.Uuid);

            prison.Lockdown(prisonRules);

            // Configure SSHD for the new prison user
            string binLocation     = Path.GetDirectoryName(this.GetType().Assembly.Location);
            string configureScript = Path.GetFullPath(Path.Combine(binLocation, @"powershell\Tools\sshd\configure-sshd.ps1"));

            ProcessResult result = ProcessExtensions.RunCommandAndGetOutput(ProcessExtensions.Get64BitPowershell(), string.Format(
                                                                                @"-ExecutionPolicy Bypass -InputFormat None -noninteractive -file {0} -targetDirectory {2} -user {1} -windowsUser {5} -userHomeDir {3} -userShell {4}",
                                                                                configureScript,
                                                                                container.Uuid,
                                                                                NodeConfig.Values["SSHD_BASE_DIR"],
                                                                                container.ContainerDir,
                                                                                NodeConfig.Values["GEAR_SHELL"],
                                                                                Environment.UserName));

            if (result.ExitCode != 0)
            {
                throw new Exception(string.Format("Error setting up sshd for gear {0} - rc={1}; out={2}; err={3}", container.Uuid, result.ExitCode, result.StdOut, result.StdErr));
            }

            this.container.InitializeHomedir(this.container.BaseDir, this.container.ContainerDir);

            container.AddEnvVar("PRISON_PORT", prisonRules.UrlPortAccess.ToString());

            LinuxFiles.TakeOwnershipOfGearHome(this.container.ContainerDir, prison.User.Username);
        }