Пример #1
0
        public void Test_RubyHash()
        {
            bool test = true;

            try
            {
                Dictionary <string, object> ctorTestO = new Dictionary <string, object>();
                ctorTestO.Add("testkey", new List <int>()
                {
                    1, 2, 3, 4
                });
                Dictionary <string, string> ctorTest = new Dictionary <string, string>();
                ctorTest.Add("test", "testValue");
                RubyHash hash       = new RubyHash(ctorTestO);
                RubyHash hash2      = new RubyHash(ctorTest);
                RubyHash mergedHash = hash.Merge(hash2);
                mergedHash = hash.Merge(ctorTest);
                mergedHash = hash2.Merge(ctorTestO);
            }
            catch
            {
                test = false;
            }
            Assert.AreEqual(true, test);
        }
Пример #2
0
        public static RubyHash GetSSHDUsers(string cygwinPath)
        {
            string passwdFile = Path.Combine(cygwinPath, "etc", "passwd");

            RubyHash users = new RubyHash();

            foreach (string line in File.ReadAllLines(passwdFile))
            {
                string[] userInfo = line.Split(':');
                if (userInfo.Length == 7)
                {                    
                    string user = userInfo[0];
                    string windowsUserSID = userInfo[4].Split(',')[1];
                    users[user] = new RubyHash() {
                        {"user", userInfo[0]},
                        {"uid", userInfo[2]},
                        {"gid", userInfo[3]},
                        {"sid", windowsUserSID},
                        {"home", userInfo[5]},
                        {"shell", userInfo[6]}
                    };
                }
            }
            return users;
        }
Пример #3
0
        public void PostReceive(RubyHash options)
        {
            Logger.Debug("Running post receive for gear {0}", this.Uuid);

            Dictionary <string, string> gearEnv = Environ.ForGear(this.ContainerDir);

            string repoDir = Path.Combine(this.ContainerDir, "app-root", "runtime", "repo");

            Directory.CreateDirectory(repoDir);

            ApplicationRepository applicationRepository = new ApplicationRepository(this);

            applicationRepository.Archive(repoDir, options["ref"]);

            options["deployment_datetime"] = this.LatestDeploymentDateTime();

            Build(options);

            Logger.Debug("Running post receive - prepare for gear {0}", this.Uuid);
            Prepare(options);

            Logger.Debug("Running post receive - distribute for gear {0}", this.Uuid);
            Distribute(options);

            Logger.Debug("Running post receive - activate for gear {0}", this.Uuid);
            Activate(options);
        }
Пример #4
0
        public static RubyHash GetOpenshiftFacts()
        {
            RubyHash nodeUtilization = Node.NodeUtilization();

            return(new RubyHash()
            {
                { "district_uuid", DistrictConfig.Exists() ? DistrictConfig.Values["uuid"] : "NONE" },
                { "district_active", DistrictConfig.Exists() ? (DistrictConfig.Values["active"] == "true") : false },
                { "district_first_uid", DistrictConfig.Exists() ? DistrictConfig.Values["first_uid"] : "1000" },
                { "district_max_uid", DistrictConfig.Exists() ? DistrictConfig.Values["max_uid"] : "6999" },

                { "public_ip", NodeConfig.Values["PUBLIC_IP"] },
                { "public_hostname", NodeConfig.Values["PUBLIC_HOSTNAME"] },

                { "node_profile", nodeUtilization["node_profile"] },
                { "max_active_gears", nodeUtilization["max_active_gears"] },
                { "no_overcommit_active", nodeUtilization["no_overcommit_active"] },
                { "quota_blocks", nodeUtilization["quota_blocks"] },
                { "quota_files", nodeUtilization["quota_files"] },
                { "gears_active_count", nodeUtilization["gears_active_count"] },
                { "gears_total_count", nodeUtilization["gears_total_count"] },
                { "gears_idle_count", nodeUtilization["gears_idled_count"] },
                { "gears_stopped_count", nodeUtilization["gears_stopped_count"] },
                { "gears_started_count", nodeUtilization["gears_started_count"] },
                { "gears_deploying_count", nodeUtilization["gears_deploying_count"] },
                { "gears_unknown_count", nodeUtilization["gears_unknown_count"] },
                { "gears_usage_pct", nodeUtilization["gears_usage_pct"] },
                { "gears_active_usage_pct", nodeUtilization["gears_active_usage_pct"] },

                { "git_repos", nodeUtilization["git_repos_count"] },
                { "capacity", nodeUtilization["capacity"] },
                { "active_capacity", nodeUtilization["active_capacity"] },
                { "sshfp", "" },
            });
        }
Пример #5
0
        public static RubyHash GetSSHDUsers(string cygwinPath)
        {
            string passwdFile = Path.Combine(cygwinPath, "etc", "passwd");

            RubyHash users = new RubyHash();

            foreach (string line in File.ReadAllLines(passwdFile))
            {
                string[] userInfo = line.Split(':');
                if (userInfo.Length == 7)
                {
                    string user           = userInfo[0];
                    string windowsUserSID = userInfo[4].Split(',')[1];
                    users[user] = new RubyHash()
                    {
                        { "user", userInfo[0] },
                        { "uid", userInfo[2] },
                        { "gid", userInfo[3] },
                        { "sid", windowsUserSID },
                        { "home", userInfo[5] },
                        { "shell", userInfo[6] }
                    };
                }
            }
            return(users);
        }
Пример #6
0
        public ReturnStatus Execute()
        {
            ReturnStatus status = new ReturnStatus();

            try
            {
                ApplicationContainer container = new ApplicationContainer(WithAppUuid, WithContainerUuid, null, WithAppName,
                                                                          WithContainerName, WithNamespace, null, null, null, WithUid);
                RubyHash options = new RubyHash();
                options["all"] = All;
                if (ParallelConcurrencyRatio != 0.0)
                {
                    options["parallelConcurrencyRatio"] = ParallelConcurrencyRatio;
                }

                status.Output   = container.Restart(CartName, options);
                status.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running oo-restart command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output   = ex.ToString();
                status.ExitCode = 1;
            }
            return(status);
        }
Пример #7
0
        public ReturnStatus Execute()
        {
            ReturnStatus status = new ReturnStatus();
            try
            {
                ApplicationContainer container = new ApplicationContainer(WithAppUuid, WithContainerUuid, null, WithAppName,
                WithContainerName, WithNamespace, null, null, null, WithUid);
                RubyHash options = new RubyHash();
                options["all"] = All;
                if (ParallelConcurrencyRatio != 0.0)
                {
                    options["parallelConcurrencyRatio"] = ParallelConcurrencyRatio;
                }

                status.Output = container.Restart(CartName, options);
                status.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running oo-restart command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output = ex.ToString();
                status.ExitCode = 1;

            }
            return status;
        }        
Пример #8
0
            private void _read()
            {
                _code = ((RubyMarshal.Codes)m_io.ReadU1());
                switch (Code)
                {
                case RubyMarshal.Codes.PackedInt: {
                    _body = new PackedInt(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.Bignum: {
                    _body = new Bignum(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubyArray: {
                    _body = new RubyArray(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubySymbolLink: {
                    _body = new PackedInt(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubyStruct: {
                    _body = new RubyStruct(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubyString: {
                    _body = new RubyString(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.InstanceVar: {
                    _body = new InstanceVar(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubyHash: {
                    _body = new RubyHash(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubySymbol: {
                    _body = new RubySymbol(m_io, this, m_root);
                    break;
                }

                case RubyMarshal.Codes.RubyObjectLink: {
                    _body = new PackedInt(m_io, this, m_root);
                    break;
                }
                }
            }
Пример #9
0
        public List <RubyHash> WithGearRotation(RubyHash options, GearRotationCallback action)
        {
            dynamic       localGearEnv = Environ.ForGear(this.ContainerDir);
            Manifest      proxyCart    = this.Cartridge.WebProxy();
            List <object> gears        = new List <object>();

            // TODO: vladi: verify if this is needed for scalable apps
            //if (options.ContainsKey("all") && proxyCart != null)
            //{
            //    if ((bool)options["all"])
            //    {
            //        gears = this.GearRegist.Entries["web"].Keys.ToList<object>();
            //    }
            //    else if (options.ContainsKey("gears"))
            //    {
            //        List<string> g = (List<string>)options["gears"];
            //        gears = this.GearRegist.Entries["web"].Keys.Where(e => g.Contains(e)).ToList<object>();
            //    }
            //    else
            //    {
            //        try
            //        {
            //            gears.Add(this.GearRegist.Entries["web"][this.Uuid]);
            //        }
            //        catch
            //        {
            //            gears.Add(this.Uuid);
            //        }
            //    }
            //}
            //else
            {
                gears.Add(this.Uuid);
            }

            double parallelConcurrentRatio = PARALLEL_CONCURRENCY_RATIO;

            if (options.ContainsKey("parallel_concurrency_ratio"))
            {
                parallelConcurrentRatio = (double)options["parallel_concurrency_ratio"];
            }

            int batchSize = CalculateBatchSize(gears.Count, parallelConcurrentRatio);

            int threads = Math.Max(batchSize, MAX_THREADS);

            List <RubyHash> result = new List <RubyHash>();

            // need to parallelize
            foreach (var targetGear in gears)
            {
                result.Add(RotateAndYield(targetGear, localGearEnv, options, action));
            }

            return(result);
        }
 public void Test_PostReceive_Init()
 {
     ApplicationContainer container = TestHelper.CreateAppContainer();
     RubyHash options = new RubyHash();
     options["init"] = true;
     options["all"] = true;
     options["reportDeployment"] = true;
     options["ref"] = "master";
     container.PostReceive(options);
 }
Пример #11
0
        public List <RubyHash> Restart(string cartName, RubyHash options)
        {
            List <RubyHash> result = WithGearRotation(options,
                                                      (GearRotationCallback) delegate(object targetGear, Dictionary <string, string> localGearEnv, RubyHash opts)
            {
                return(RestartGear(targetGear, localGearEnv, cartName, opts));
            });


            return(result);
        }
Пример #12
0
        public void Test_PostReceive_Init()
        {
            ApplicationContainer container = TestHelper.CreateAppContainer();
            RubyHash             options   = new RubyHash();

            options["init"]             = true;
            options["all"]              = true;
            options["reportDeployment"] = true;
            options["ref"]              = "master";
            container.PostReceive(options);
        }
Пример #13
0
        public void TestFacterOK()
        {
            // Arrange

            // Act
            RubyHash facts = Uhuru.Openshift.Runtime.Utils.Facter.GetFacterFacts();

            // Assert
            Assert.IsTrue(facts.ContainsKey("operatingsystem"));
            Assert.AreEqual(facts["operatingsystem"], "windows");
        }
Пример #14
0
        private RubyHash ActivateRemoteGear(GearRegistry.Entry gear, Dictionary <string, string> gearEnv, RubyHash options)
        {
            string gearUuid = gear.Uuid;

            RubyHash result = new RubyHash();

            result["status"]        = RESULT_FAILURE;
            result["gear_uuid"]     = this.Uuid;
            result["deployment_id"] = options["deployment_id"];
            result["messages"]      = new List <string>();
            result["errors"]        = new List <string>();

            string postInstallOptions = options["post_install"] == true ? "--post-install" : "";

            result["messages"].Add(string.Format("Activating gear {0}, deployment id: {1}, {2}", gearUuid, options["deployment_id"], postInstallOptions));
            try
            {
                string ooSSH      = @"/cygpath/c/openshift/oo-bin/oo-ssh";
                string bashBinary = Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], "bin\bash.exe");

                string sshCommand = string.Format("{0} {1} gear activate {2} --as-json {3} --no-rotation",
                                                  ooSSH, gear.ToSshUrl(), options["deployment_id"], postInstallOptions);

                string bashArgs = string.Format("--norc --login -c '{0}'", sshCommand);

                string command = string.Format("{0} {1}", bashBinary, bashArgs);

                string output = RunProcessInContainerContext(this.ContainerDir, command).StdOut;

                if (string.IsNullOrEmpty(output))
                {
                    throw new Exception("No result JSON was received from the remote activate call");
                }
                Dictionary <string, object> activateResult = JsonConvert.DeserializeObject <Dictionary <string, object> >(output);
                if (!activateResult.ContainsKey("status"))
                {
                    throw new Exception("Invalid result JSON received from remote activate call");
                }

                result["messages"].Add(activateResult["messages"]);
                result["errors"].Add(activateResult["errors"]);
                result["status"] = activateResult["status"];
            }
            catch (Exception e)
            {
                result["errors"].Add("Gear activation failed: " + e.ToString());
            }

            return(result);
        }
Пример #15
0
        public string Deploy(RubyHash options)
        {
            StringBuilder output = new StringBuilder();

            if (!options.ContainsKey("artifact_url"))
            {
                output.AppendLine(PreReceive(options));
                PostReceive(options);
            }
            else
            {
                output.AppendLine(DeployBinaryArtifact(options));
            }
            return(output.ToString());
        }
Пример #16
0
 public DeploymentMetadata(ApplicationContainer container, string deploymentDatetime)
 {
     this.file = Path.Combine(container.ContainerDir, "app-deployments", deploymentDatetime, "metadata.json");
     if (File.Exists(file))
     {
         Load();
     }
     else
     {
         using (File.Create(this.file)) { };
         container.SetRWPermissions(this.file);
         this.metadata = this.defaults;
         Save();
     }
 }
Пример #17
0
        public static void RemoveKey(string targetDirectory, string user, string key)
        {
            RubyHash userInfo           = CygwinPasswd.GetSSHDUser(targetDirectory, user);
            string   homeDir            = LinuxFiles.Cygpath(userInfo["home"], true);
            string   authorizedKeysFile = Path.Combine(homeDir, ".ssh", "authorized_keys");

            if (!File.Exists(authorizedKeysFile))
            {
                return;
            }
            List <string> content = File.ReadAllLines(authorizedKeysFile).ToList();

            content.Remove(key);
            File.WriteAllLines(authorizedKeysFile, content, Encoding.ASCII);
        }
Пример #18
0
        public static void AddKey(string targetDirectory, string user, string key)
        {
            RubyHash userInfo = CygwinPasswd.GetSSHDUser(targetDirectory, user);
            string   homeDir  = LinuxFiles.Cygpath(userInfo["home"], true);

            Directory.CreateDirectory(Path.Combine(homeDir, ".ssh"));
            string authorizedKeysFile = Path.Combine(homeDir, ".ssh", "authorized_keys");

            if (!File.Exists(authorizedKeysFile))
            {
                File.Create(authorizedKeysFile).Dispose();
            }
            Logger.Debug("Adding key to {0}", authorizedKeysFile);
            File.AppendAllLines(authorizedKeysFile, new string[] { key }, Encoding.ASCII);
        }
Пример #19
0
 public void Test_ConnectionModel()
 {
     bool result = false;
     RubyHash specs = new RubyHash();
     specs["Components"] = new List<object>(){"TestComponent"};
     Connection m = Connection.FromDescriptor("testConnection", specs);
     if (m != null)
     {
         var desc = m.ToDescriptor();
         if (desc != null)
         {
             result = true;
         }
     }
     Assert.AreEqual(true, result);
 }
Пример #20
0
        public string Prepare(RubyHash options = null)
        {
            if (options == null)
            {
                options = new RubyHash();
            }
            StringBuilder output = new StringBuilder();

            output.AppendLine("Preparing build for deployment");
            if (!options.ContainsKey("deployment_datetime"))
            {
                throw new ArgumentException("deployment_datetime is required");
            }
            string deploymentDatetime       = options["deployment_datetime"].ToString();
            Dictionary <string, string> env = Environ.ForGear(this.ContainerDir);

            // TODO clean runtime dirs, extract archive

            this.Cartridge.DoActionHook("prepare", env, options);
            string deploymentId = CalculateDeploymentId();

            LinkDeploymentId(deploymentDatetime, deploymentId);

            try
            {
                SyncRuntimeRepoDirToDeployment(deploymentDatetime);
                SyncRuntimeDependenciesDirToDeployment(deploymentDatetime);
                SyncRuntimeBuildDependenciesDirToDeployment(deploymentDatetime);

                DeploymentMetadata deploymentMetadata = DeploymentMetadataFor(deploymentDatetime);
                deploymentMetadata.Id       = deploymentId;
                deploymentMetadata.Checksum = CalculateDeploymentChecksum(deploymentId);
                deploymentMetadata.Save();

                options["deployment_id"] = deploymentId;
                output.AppendLine("Deployment id is " + deploymentId);
            }
            catch (Exception e)
            {
                Logger.Error("Error preparing deployment. Options: {0} - {1} - {2}", JsonConvert.SerializeObject(options), e.Message, e.StackTrace);
                output.AppendLine("Error preparing deployment " + deploymentId);
                UnlinkDeploymentId(deploymentId);
            }

            return(output.ToString());
        }
Пример #21
0
        public RubyHash UpdateLocalProxyStatus(RubyHash args)
        {
            RubyHash result = new RubyHash();

            object cartridge  = args["cartridge"];
            object action     = args["action"];
            object targetGear = args["target_gear"];
            object persist    = args["persist"];

            try
            {
                string output = this.UpdateProxyStatusForGear(new RubyHash()
                {
                    { "cartridge", cartridge },
                    { "action", action },
                    { "gear_uuid", targetGear },
                    { "persist", persist }
                });

                result = new RubyHash()
                {
                    { "status", RESULT_SUCCESS },
                    { "proxy_gear_uuid", this.Uuid },
                    { "target_gear_uuid", targetGear },
                    { "messages", new List <string>() },
                    { "errors", new List <string>() }
                };
            }
            catch (Exception ex)
            {
                result = new RubyHash()
                {
                    { "status", RESULT_FAILURE },
                    { "proxy_gear_uuid", this.Uuid },
                    { "target_gear_uuid", targetGear },
                    { "messages", new List <string>() },
                    { "errors", new List <string>()
                      {
                          string.Format("An exception occured updating the proxy status: {0}\n{1}", ex.Message, ex.StackTrace)
                      } }
                };
            }

            return(result);
        }
Пример #22
0
        public int Stop(bool force = false)
        {
            return(PRunner(gear =>
            {
                RubyHash options = new RubyHash()
                {
                    { "user_initiated", false },
                };

                if (force)
                {
                    options["force"] = true;
                    options["term_delay"] = 10;
                }

                gear.StopGear(options);
            }));
        }
        public int Stop(bool force = false)
        {
            return PRunner(gear =>
            {
                RubyHash options = new RubyHash()
                {
                    { "user_initiated", false },
                };

                if (force)
                {
                    options["force"] = true;
                    options["term_delay"] = 10;
                }

                gear.StopGear(options);
            });
        }
Пример #24
0
        public string UpdateProxyStatusForGear(RubyHash options)
        {
            string action = options["action"];

            if (action != "enable" && action != "disable")
            {
                new ArgumentException("action must either be :enable or :disable");
            }

            string gearUuid = options["gear_uuid"];

            if (gearUuid == null)
            {
                new ArgumentException("gear_uuid is required");
            }

            Manifest cartridge = options["cartridge"] ?? this.Cartridge.WebProxy();

            if (cartridge == null)
            {
                throw new ArgumentNullException("Unable to update proxy status - no proxy cartridge found");
            }

            bool   persist = options["persist"] != null && options["persist"] == true;
            string control = string.Format("{0}-server", action);

            List <string> args = new List <string>();

            if (persist)
            {
                args.Add("persist");
            }

            args.Add(gearUuid);

            return(this.Cartridge.DoControl(
                       control, cartridge, new Dictionary <string, object>()
            {
                { "args", string.Join(" ", args) },
                { "pre_action_hooks_enabled", false },
                { "post_action_hooks_enabled", false }
            }));
        }
Пример #25
0
        public RubyHash RestartGear(object targetGear, Dictionary <string, string> localGearEnv, string cartName, dynamic options)
        {
            string   targetGearUuid = targetGear is string?(string)targetGear : ((GearRegistry.Entry)targetGear).Uuid;
            RubyHash result         = new RubyHash()
            {
                { "status", RESULT_SUCCESS },
                { "messages", new List <string>() },
                { "errors", new List <string>() },
                { "target_gear_uuid", targetGearUuid }
            };

            try
            {
                if (targetGearUuid == this.Uuid)
                {
                    result["messages"].Add(this.Cartridge.StartCartridge("restart", cartName, options));
                }
                else
                {
                    string ooSSH      = @"/cygpath/c/openshift/oo-bin/oo-ssh";
                    string bashBinary = Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], "bin\bash.exe");

                    if (targetGear is string)
                    {
                        targetGear = new GearRegistry.Entry(options);
                    }
                    string sshCommand = string.Format("{0} {1} gear restart --cart {2} --as-json",
                                                      ooSSH, ((GearRegistry.Entry)targetGear).ToSshUrl(), cartName);
                    string bashArgs = string.Format("--norc --login -c '{0}'", sshCommand);
                    string command  = string.Format("{0} {1}", bashBinary, bashArgs);

                    RunProcessInContainerContext(this.ContainerDir, command);
                }
            }
            catch (Exception ex)
            {
                result["errors"].Add(ex.ToString());
                result["status"] = RESULT_FAILURE;
                Logger.Error(ex.ToString());
            }

            return(result);
        }
Пример #26
0
        public static IEnumerable <ApplicationContainer> All(Hourglass hourglass = null, bool loadenv = true)
        {
            EtcUser[] users = new Etc(NodeConfig.Values).GetAllUsers();

            foreach (EtcUser user in users)
            {
                if (user.Gecos.StartsWith("openshift_service"))
                {
                    RubyHash env           = new RubyHash();
                    string   gearNamespace = null;

                    if (loadenv)
                    {
                        env = new RubyHash(Environ.Load(new string[] { Path.Combine(user.Dir, ".env") }));
                    }

                    if (env.ContainsKey("OPENSHIFT_GEAR_DNS"))
                    {
                        gearNamespace = env["OPENSHIFT_GEAR_DNS"];
                    }

                    ApplicationContainer app = null;

                    try
                    {
                        app = new ApplicationContainer(env["OPENSHIFT_APP_UUID"],
                                                       user.Name, user, env["OPENSHIFT_APP_NAME"], env["OPENSHIFT_GEAR_NAME"], gearNamespace, null, null, hourglass);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Failed to instantiate ApplicationContainer for uid {0}/uuid {1}: {2}",
                                     user.Uid, env["OPENSHIFT_APP_UUID"], ex.Message);
                        Logger.Error("Stacktrace: {0}", ex.StackTrace);

                        continue;
                    }

                    yield return(app);
                }
            }
        }
Пример #27
0
        public void Test_ConnectionModel()
        {
            bool     result = false;
            RubyHash specs  = new RubyHash();

            specs["Components"] = new List <object>()
            {
                "TestComponent"
            };
            Connection m = Connection.FromDescriptor("testConnection", specs);

            if (m != null)
            {
                var desc = m.ToDescriptor();
                if (desc != null)
                {
                    result = true;
                }
            }
            Assert.AreEqual(true, result);
        }
Пример #28
0
        protected override void ProcessRecord()
        {
            ReturnStatus         status    = new ReturnStatus();
            ApplicationContainer container = new ApplicationContainer(WithAppUuid, WithContainerUuid, null, WithAppName,
                                                                      WithContainerName, WithNamespace, null, null, null);

            try
            {
                RubyHash options = new RubyHash();
                options["deployment_id"] = DeploymentId;
                status.Output            = container.Activate(options);
                status.ExitCode          = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running oo-activate command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output   = ex.ToString();
                status.ExitCode = 1;
            }
            this.WriteObject(status);
        }
Пример #29
0
 public void Test_RubyHash()
 {
     bool test = true;
     try
     {
         Dictionary<string, object> ctorTestO = new Dictionary<string, object>();
         ctorTestO.Add("testkey", new List<int>() { 1, 2, 3, 4 });
         Dictionary<string, string> ctorTest = new Dictionary<string, string>();
         ctorTest.Add("test", "testValue");
         RubyHash hash = new RubyHash(ctorTestO);
         RubyHash hash2 = new RubyHash(ctorTest);
         RubyHash mergedHash = hash.Merge(hash2);
         mergedHash = hash.Merge(ctorTest);
         mergedHash = hash2.Merge(ctorTestO);
     }
     catch
     {
         test = false;
     }
     Assert.AreEqual(true, test);
 }
Пример #30
0
        public ReturnStatus Execute()
        {
            ReturnStatus status = new ReturnStatus();
            try
            {
            ApplicationContainer container = new ApplicationContainer(WithAppUuid, WithContainerUuid, null, WithAppName,
                                WithContainerName, WithNamespace, null, null, null, WithUid);

                RubyHash options = new RubyHash();
                options["deployment_id"] = WithDeploymentId;
                status.Output = container.Activate(options);
                status.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running oo-activate command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output = ex.ToString();
                status.ExitCode = 1;
            }
            return status;
        }
Пример #31
0
        public ReturnStatus Execute()
        {
            ReturnStatus status = new ReturnStatus();

            try
            {
                ApplicationContainer container = new ApplicationContainer(WithAppUuid, WithContainerUuid, null, WithAppName,
                                                                          WithContainerName, WithNamespace, null, null, null);

                RubyHash options = new RubyHash();
                options["deployment_id"] = WithDeploymentId;
                status.Output            = container.Activate(options);
                status.ExitCode          = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running oo-activate command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output   = ex.ToString();
                status.ExitCode = 1;
            }
            return(status);
        }
Пример #32
0
        public static void WriteFactsFile(string file)
        {
            try
            {
                RubyHash common       = Facter.GetFacterFacts();
                RubyHash nodeSpecific = Facter.GetOpenshiftFacts();

                dynamic desc = common.Merge(nodeSpecific);
                using (StringWriter sw = new StringWriter())
                {
                    Serializer serializer = new Serializer();
                    serializer.Serialize(new Emitter(sw, 2, int.MaxValue, true), desc);

                    File.WriteAllText(file, sw.ToString());
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error while generating facts file: {0} - {1}", ex.Message, ex.StackTrace);
                throw;
            }
        }
Пример #33
0
        protected override void ProcessRecord()
        {
            ReturnStatus         status    = new ReturnStatus();
            ApplicationContainer container = new ApplicationContainer(WithAppUuid, WithContainerUuid, null, WithAppName,
                                                                      WithContainerName, WithNamespace, null, null, null);
            RubyHash options = new RubyHash();

            options["all"] = All;
            options["parallelConcurrencyRatio"] = ParallelConcurrencyRatio;
            try
            {
                status.Output   = container.Restart(CartName, options);
                status.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running oo-restart command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output   = ex.ToString();
                status.ExitCode = 1;
            }
            this.WriteObject(status);
        }
Пример #34
0
        public void CleanRuntimeDirs(RubyHash options)
        {
            List <string> dirs = new List <string>();

            foreach (string dir in new string[] { "dependencies", "build_dependencies", "repo" })
            {
                if (options[dir] == true)
                {
                    dirs.Add(dir);
                }
            }

            if (dirs.Count == 0)
            {
                return;
            }

            foreach (string dir in dirs)
            {
                string directory = Path.Combine(this.ContainerDir, "app-root", "runtime", dir.Replace('_', '-'));
                DirectoryUtil.EmptyDirectory(directory);
            }
        }
Пример #35
0
        public ReturnStatus Execute()
        {
            Logger.Debug("Running gear command: '{0}'", Environment.CommandLine);

            ReturnStatus status = new ReturnStatus();
            try
            {
                string appUuid = Environment.GetEnvironmentVariable("OPENSHIFT_APP_UUID");
                string gearUuid = Environment.GetEnvironmentVariable("OPENSHIFT_GEAR_UUID");
                string appName = Environment.GetEnvironmentVariable("OPENSHIFT_APP_NAME");
                string gearName = Environment.GetEnvironmentVariable("OPENSHIFT_GEAR_NAME");
                string nmSpace = Environment.GetEnvironmentVariable("OPENSHIFT_NAMESPACE");

                NodeConfig config = new NodeConfig();
                EtcUser etcUser = new Etc(config).GetPwanam(gearUuid);

                container = new ApplicationContainer(appUuid, gearUuid, etcUser, appName, gearName, nmSpace, null, null, null);
                repo = new ApplicationRepository(container);

                if (Prereceive)
                {
                    Dictionary<string, object> options = new Dictionary<string, object>();
                    options["init"] = Init;
                    options["hotDeploy"] = true;
                    options["forceCleanBuild"] = true;
                    options["ref"] = container.DetermineDeploymentRef();
                    container.PreReceive(options);
                }
                else if (Postreceive)
                {
                    RubyHash options = new RubyHash();
                    options["init"] = Init;
                    options["all"] = true;
                    options["reportDeployment"] = true;
                    options["ref"] = container.DetermineDeploymentRef();
                    container.PostReceive(options);
                }
                else if (Build)
                {
                    string ciRepoPath = Path.Combine(Environment.GetEnvironmentVariable("OPENSHIFT_REPO_DIR"), ".git");
                    string repoDir = null;
                    if(Directory.Exists(ciRepoPath))
                    {
                        repoDir = ciRepoPath;
                    }
                    repo = new ApplicationRepository(this.container, repoDir);
                    string gitRef = null;
                    bool archive =false;
                    if(repoDir == null)
                    {
                        gitRef = this.container.DetermineDeploymentRef(this.RefId);
                        archive = true;
                    }
                    else
                    {
                        gitRef = Environment.GetEnvironmentVariable("GIT_BRANCH");
                    }
                    if(!ValidGitRef(gitRef))
                    {
                        throw new Exception(string.Format("Git ref {0} is invalid", gitRef));
                    }
                    if(archive)
                    {
                        repo.Archive(Environment.GetEnvironmentVariable("OPENSHIFT_REPO_DIR"), gitRef);
                    }

                    RubyHash options = new RubyHash();
                    options["ref"] = gitRef;
                    options["hot_deploy"] = repo.FileExists(HOT_DEPLOY_MARKER, gitRef);
                    options["force_clean_build"] = repo.FileExists(FORCE_CLEAN_BUILD_MARKER, gitRef);
                    options["git_repo"] = repo;

                    Console.WriteLine(container.Build(options));
                }
                else if (Prepare)
                {
                    throw new NotImplementedException();
                }
                else if (Deploy)
                {
                    if (Environment.GetEnvironmentVariable("OPENSHIFT_DEPLOYMENT_TYPE") == "binary")
                    {
                        throw new Exception("OPENSHIFT_DEPLOYMENT_TYPE is 'binary' - git-based deployments are disabled.");
                    }
                    string refToDeploy = container.DetermineDeploymentRef(this.DeployRefId);
                    if (!ValidGitRef(refToDeploy))
                    {
                        throw new Exception("Git ref " + refToDeploy + " is not valid");
                    }
                    RubyHash options = new RubyHash();
                    options["hot_deploy"] = this.HotDeploy;
                    options["force_clean_build"] = this.ForceCleanBuild;
                    options["ref"] = this.DeployRefId;
                    options["report_deployments"] = true;
                    options["all"] = true;
                    container.Deploy(options);
                }
                else if (Activate)
                {
                    status.Output = container.Activate(new RubyHash
                    {
                        {"deployment_id", this.DeploymentId},
                        {"post_install", this.PostInstall.ToBool()},
                        {"all", this.All.ToBool()},
                        {"rotate", this.Rotation && !this.NoRotation},
                        {"report_deployments", true},
                        {"out", !this.AsJson.ToBool()}
                    });
                }
                status.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running gear command: {0} - {1}", ex.Message, ex.StackTrace);
                status.Output = string.Format("{0}", ex.Message, ex.StackTrace);
                status.ExitCode = 255;
            }

            return status;
        }
 public static object TransformToNativeModel(this RubyHash self)
 {
     return(self.value.ToDictionary(pair => TransformToNativeModel(pair.Key), pair => TransformToNativeModel(pair.Value)));
 }
        public RubyHash RotateAndYield(object targetGear, Dictionary<string, string> localGearEnv, RubyHash options, GearRotationCallback action)
        {
            RubyHash result = new RubyHash()
            {
                { "status", RESULT_FAILURE },
                { "messages", new List<string>() },
                { "errors", new List<string>() }
            };

            string proxyCart = options["proxy_cart"];

            string targetGearUuid = targetGear is string ? (string)targetGear : ((GearRegistry.Entry)targetGear).Uuid;

            // TODO: vladi: check if this condition also needs boolean verification on the value in the hash
            if (options["init"] == null && options["rotate"] != null && options["rotate"] && options["hot_deploy"] != null && options["hot_deploy"])
            {
                result["messages"].Add("Rotating out gear in proxies");

                RubyHash rotateOutResults = this.UpdateProxyStatus(new RubyHash()
                    {
                        { "action", "disable" },
                        { "gear_uuid", targetGearUuid },
                        { "cartridge", proxyCart }
                    });

                result["rotate_out_results"] = rotateOutResults;

                if (rotateOutResults["status"] != RESULT_SUCCESS)
                {
                    result["errors"].Add("Rotating out gear in proxies failed.");
                    return result;
                }
            }

            RubyHash yieldResult = action(targetGear, localGearEnv, options);

            dynamic yieldStatus = yieldResult.Delete("status");
            dynamic yieldMessages = yieldResult.Delete("messages");
            dynamic yieldErrors = yieldResult.Delete("errors");

            result["messages"].AddRange(yieldMessages);
            result["errors"].AddRange(yieldErrors);

            result = result.Merge(yieldResult);

            if (yieldStatus != RESULT_SUCCESS)
            {
                return result;
            }

            if (options["init"] == null && options["rotate"] != null && options["rotate"] && options["hot_deploy"] != null && options["hot_deploy"])
            {
                result["messages"].Add("Rotating in gear in proxies");

                RubyHash rotateInResults = this.UpdateProxyStatus(new RubyHash()
                {
                    { "action", "enable" },
                    { "gear_uuid", targetGearUuid },
                    { "cartridge", proxyCart }
                });

                result["rotate_in_results"] = rotateInResults;

                if (rotateInResults["status"] != RESULT_SUCCESS)
                {
                    result["errors"].Add("Rotating in gear in proxies failed");
                    return result;
                }
            }

            result["status"] = RESULT_SUCCESS;

            return result;
        }
 public void Load()
 {
     this.metadata = JsonConvert.DeserializeObject<RubyHash>(File.ReadAllText(file));
 }
Пример #39
0
        public string Prepare(RubyHash options = null)
        {
            if (options == null)
            {
                options = new RubyHash();
            }
            StringBuilder output = new StringBuilder();
            output.AppendLine("Preparing build for deployment");
            if (!options.ContainsKey("deployment_datetime"))
            {
                throw new ArgumentException("deployment_datetime is required");
            }
            string deploymentDatetime = options["deployment_datetime"].ToString();
            Dictionary<string, string> env = Environ.ForGear(this.ContainerDir);

            // TODO clean runtime dirs, extract archive

            this.Cartridge.DoActionHook("prepare", env, options);
            string deploymentId = CalculateDeploymentId();
            LinkDeploymentId(deploymentDatetime, deploymentId);

            try
            {
                SyncRuntimeRepoDirToDeployment(deploymentDatetime);
                SyncRuntimeDependenciesDirToDeployment(deploymentDatetime);
                SyncRuntimeBuildDependenciesDirToDeployment(deploymentDatetime);

                DeploymentMetadata deploymentMetadata = DeploymentMetadataFor(deploymentDatetime);
                deploymentMetadata.Id = deploymentId;
                deploymentMetadata.Checksum = CalculateDeploymentChecksum(deploymentId);
                deploymentMetadata.Save();

                options["deployment_id"] = deploymentId;
                output.AppendLine("Deployment id is " + deploymentId);
            }
            catch (Exception e)
            {
                Logger.Error("Error preparing deployment. Options: {0} - {1} - {2}", JsonConvert.SerializeObject(options), e.Message, e.StackTrace);
                output.AppendLine("Error preparing deployment " + deploymentId);
                UnlinkDeploymentId(deploymentId);
            }

            return output.ToString();
        }
Пример #40
0
        public MapInfoFile(Dictionary <string, object> node)
            : base(
                System.IO.Path.Combine(
                    ProjectManager.ProjectDir,
                    node["FileName"].ToString()
                    )
                )
        {
            this.maps = new Dictionary <string, MapFile>();
            this.IsSubfileProvider = true;

            this.TilesetFile = ProjectManager.Components[node["TilesetProvider"].ToString()] as TilesetFile;

            RubyHash mapHash = NekoKun.Serialization.RubyMarshal.RubyMarshal.Load(
                new System.IO.FileStream(this.filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)
                ) as RubyHash;

            foreach (var item in mapHash)
            {
                string  key = null;
                string  filename;
                MapFile map = null;
                if (item.Key is int)
                {
                    key      = item.Key.ToString();
                    filename = String.Format(node["FileNameFormat"].ToString(), (int)item.Key);
                    map      = new MapFile(System.IO.Path.Combine(ProjectManager.ProjectDir, filename), this.TilesetFile);
                }
                else
                {
                    this.MakeDirty();
                    continue;
                }
                this.maps.Add(key, map);

                RubyObject info = item.Value as RubyObject;
                map.Title    = (info.InstanceVariable["@name"] as RubyString).Text;
                map.ParentID = info.InstanceVariable["@parent_id"].ToString();
                map.Order    = (int)info.InstanceVariable["@order"];

                /*
                 *  parent_id
                 *  The parent map ID.
                 *
                 *  order
                 *  The map tree display order, which is used internally.
                 *
                 *  expanded
                 *  The map tree expansion flag, which is used internally.
                 *
                 *  scroll_x
                 *  The x-axis scroll position, which is used internally.
                 *
                 *  scroll_y
                 *  The y-axis scroll position, which is used internally.
                 */
            }
            this.maps.ToString();



            nodes = new Dictionary <string, System.Windows.Forms.TreeNode>();
            List <System.Windows.Forms.TreeNode> order = new List <System.Windows.Forms.TreeNode>();

            foreach (var item in maps)
            {
                System.Windows.Forms.TreeNode node2 = new System.Windows.Forms.TreeNode(item.Value.Title);
                node2.Tag = item.Value;
                nodes.Add(item.Key, node2);
                order.Add(node2);
            }

            order.Sort(
                delegate(System.Windows.Forms.TreeNode me, System.Windows.Forms.TreeNode other)
            {
                return((me.Tag as MapFile).Order.CompareTo((other.Tag as MapFile).Order));
            }
                );

            foreach (System.Windows.Forms.TreeNode item in order)
            {
                MapFile map = item.Tag as MapFile;
                if (map.ParentID != null && nodes.ContainsKey(map.ParentID))
                {
                    nodes[map.ParentID].Nodes.Add(item);
                }
            }
        }
Пример #41
0
        public string Build(RubyHash options)
        {
            this.State.Value(Runtime.State.BUILDING);
            string deploymentDateTime = options["deployment_datetime"] != null ? options["deployment_datetime"] : LatestDeploymentDateTime();
            DeploymentMetadata deploymentMetadata = DeploymentMetadataFor(deploymentDateTime);

            if (!options.ContainsKey("deployment_datetime"))
            {
                // this will execute if coming from a CI builder, since it doesn't
                // specify :deployment_datetime in the options hash
                ApplicationRepository applicationRepository = options["git_repo"];
                string gitRef = options["ref"];
                string gitSha1 = applicationRepository.GetSha1(gitRef);
                deploymentMetadata.GitSha = gitSha1;
                deploymentMetadata.GitRef = gitRef;
                deploymentMetadata.HotDeploy = options["hot_deploy"];
                deploymentMetadata.ForceCleanBuild = options["force_clean_build"];
                deploymentMetadata.Save();
            }

            StringBuilder buffer = new StringBuilder();

            if(deploymentMetadata.ForceCleanBuild)
            {
                buffer.AppendLine("Force clean build enabled - cleaning dependencies");

                CleanRuntimeDirs(new RubyHash() { { "dependencies", true }, { "build_dependencies", true } });

                this.Cartridge.EachCartridge(delegate(Manifest cartridge) {
                    this.Cartridge.CreateDependencyDirectories(cartridge);
                });
            }

            buffer.AppendLine(string.Format("Building git ref {0}, commit {1}", deploymentMetadata.GitRef, deploymentMetadata.GitSha));

            Dictionary<string, string> env = Environ.ForGear(this.ContainerDir);
            int deploymentsToKeep = DeploymentsToKeep(env);

            try
            {
                Manifest primaryCartridge = this.Cartridge.GetPrimaryCartridge();
                buffer.AppendLine(this.Cartridge.DoControl("update-configuration", primaryCartridge, new RubyHash() { { "pre_action_hooks_enabled", false }, { "post_action_hooks_enabled", false } }));
                buffer.AppendLine(this.Cartridge.DoControl("pre-build", primaryCartridge, new RubyHash() { { "pre_action_hooks_enabled", false }, { "post_action_hooks_enabled", false } }));
                buffer.AppendLine(this.Cartridge.DoControl("build", primaryCartridge, new RubyHash() { { "pre_action_hooks_enabled", false }, { "post_action_hooks_enabled", false } }));
            }
            catch(Exception ex)
            {
                buffer.AppendLine("Encountered a failure during build: " + ex.ToString());
                if(deploymentsToKeep > 1)
                {
                    buffer.AppendLine("Restarting application");
                    buffer.AppendLine(StartGear(new RubyHash() { { "user_initiated", true }, { "hot_deploy", deploymentMetadata.HotDeploy } }));
                }
                throw ex;
            }

            return buffer.ToString();
        }
        public List<RubyHash> WithGearRotation(RubyHash options, GearRotationCallback action)
        {
            dynamic localGearEnv = Environ.ForGear(this.ContainerDir);
            Manifest proxyCart = this.Cartridge.WebProxy();
            List<object> gears = new List<object>();

            // TODO: vladi: verify if this is needed for scalable apps
            //if (options.ContainsKey("all") && proxyCart != null)
            //{
            //    if ((bool)options["all"])
            //    {
            //        gears = this.GearRegist.Entries["web"].Keys.ToList<object>();
            //    }
            //    else if (options.ContainsKey("gears"))
            //    {
            //        List<string> g = (List<string>)options["gears"];
            //        gears = this.GearRegist.Entries["web"].Keys.Where(e => g.Contains(e)).ToList<object>();
            //    }
            //    else
            //    {
            //        try
            //        {
            //            gears.Add(this.GearRegist.Entries["web"][this.Uuid]);
            //        }
            //        catch
            //        {
            //            gears.Add(this.Uuid);
            //        }
            //    }
            //}
            //else
            {
                gears.Add(this.Uuid);
            }

            double parallelConcurrentRatio = PARALLEL_CONCURRENCY_RATIO;
            if (options.ContainsKey("parallel_concurrency_ratio"))
            {
                parallelConcurrentRatio = (double)options["parallel_concurrency_ratio"];
            }

            int batchSize = CalculateBatchSize(gears.Count, parallelConcurrentRatio);

            int threads = Math.Max(batchSize, MAX_THREADS);

            List<RubyHash> result = new List<RubyHash>();

            // need to parallelize
            foreach (var targetGear in gears)
            {
                result.Add(RotateAndYield(targetGear, localGearEnv, options, action));
            }

            return result;
        }
        public RubyHash UpdateRemoteProxyStatus(RubyHash args)
        {
            RubyHash result = new RubyHash();
            string currentGear = args["current_gear"];
            GearRegistry.Entry proxyGear = args["proxy_gear"];
            object targetGear = args["target_gear"];
            Manifest cartridge = args["cartridge"];
            string action = args["action"];
            bool persist = args["persist"] != null && args["persist"] == true;
            object gearEnv = args["gear_env"];

            if (currentGear == proxyGear.Uuid)
            {
                // self, no need to ssh
                return this.UpdateLocalProxyStatus(new RubyHash()
                {
                    { "cartridge", cartridge }, 
                    { "action", action }, 
                    { "target_gear", targetGear }, 
                    { "persist", persist }
                });
            }

            string direction = action == "enable" ? "in" : "out";
            string persistOption = persist ? "--persist" : "";

            string url = string.Format("{0}@{1}", proxyGear.Uuid, proxyGear.ProxyHostname);

            string ooSSH = @"/cygpath/c/openshift/oo-bin/oo-ssh";
            string bashBinary = Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], "bin\bash.exe");

            string sshCommand = string.Format("{0} {1} gear rotate-{2} --gear {3} {4} --cart {5}-{6} --as-json", 
                ooSSH, url, direction, targetGear, persistOption, cartridge.Name, cartridge.Version);

            string bashArgs = string.Format("--norc --login -c '{0}'", sshCommand);

            string command = string.Format("{0} {1}", bashBinary, bashArgs);

            try
            {
                ProcessResult processResult = this.RunProcessInContainerContext(this.ContainerDir, command, 0);

                if (string.IsNullOrEmpty(processResult.StdOut))
                {
                    throw new Exception("No result JSON was received from the remote proxy update call");
                }

                result = JsonConvert.DeserializeObject<RubyHash>(processResult.StdOut);

                if (!result.ContainsKey("status"))
                {
                    throw new Exception(string.Format("Invalid result JSON received from remote proxy update call: {0}", processResult.StdOut));
                }
            }
            catch (Exception ex)
            {
                result = new RubyHash()
                {
                    { "status", RESULT_FAILURE },
                    { "proxy_gear_uuid", proxyGear.Uuid },
                    { "messages", new List<string>() },
                    { "errors", new List<string> { string.Format("An exception occured updating the proxy status: {0}\n{1}", ex.Message, ex.StackTrace) } }
                };
            }

            return result;
        }
        public RubyHash RestartGear(object targetGear, Dictionary<string, string> localGearEnv, string cartName, dynamic options)
        {
            string targetGearUuid = targetGear is string ? (string)targetGear : ((GearRegistry.Entry)targetGear).Uuid;
            RubyHash result = new RubyHash()
            {
                { "status", RESULT_SUCCESS },
                { "messages", new List<string>() },
                { "errors", new List<string>() },
                { "target_gear_uuid", targetGearUuid }
            };

            try
            {
                if (targetGearUuid == this.Uuid)
                {
                    result["messages"].Add(this.Cartridge.StartCartridge("restart", cartName, options));
                }
                else
                {

                    string ooSSH = @"/cygpath/c/openshift/oo-bin/oo-ssh";
                    string bashBinary = Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], "bin\bash.exe");

                    if (targetGear is string)
                    {
                        targetGear = new GearRegistry.Entry(options);
                    }
                    string sshCommand = string.Format("{0} {1} gear restart --cart {2} --as-json",
                        ooSSH, ((GearRegistry.Entry)targetGear).ToSshUrl(), cartName);
                    string bashArgs = string.Format("--norc --login -c '{0}'", sshCommand);
                    string command = string.Format("{0} {1}", bashBinary, bashArgs);

                    RunProcessInContainerContext(this.ContainerDir, command);

                }
            }
            catch(Exception ex)
            {
                result["errors"].Add(ex.ToString());
                result["status"] = RESULT_FAILURE;
                Logger.Error(ex.ToString());
            }

            return result;
        }
Пример #45
0
        /// <summary>
        /// static VALUE r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
        /// </summary>
        /// <param name="hasivp"></param>
        /// <param name="ivp"></param>
        /// <param name="extmod"></param>
        /// <returns></returns>
        public object ReadObject0(bool hasivp, ref bool ivp, List<RubyModule> extmod)
        {
            object v = null;
            int type = ReadByte();
            int id;
            object link;
            switch (type)
            {
                case RubyMarshal.Types.Link:
                    id = ReadLong();
                    if (!this.m_objects.TryGetValue(id, out link))
                    {
                        throw new InvalidDataException("dump format error (unlinked)");
                    }
                    v = link;
                    if (this.m_proc != null)
                        v = this.m_proc(v);
                    break;
                case RubyMarshal.Types.InstanceVariable:
                    {
                        bool ivar = true;
                        v = ReadObject0(ref ivar, extmod);
                        bool hasenc = false;
                        if (ivar) ReadInstanceVariable(v, ref hasenc);
                    }
                    break;
                case RubyMarshal.Types.Extended:
                    {
                        RubyModule m = RubyModule.GetModule(ReadUnique());
                        if (extmod == null)
                            extmod = new List<RubyModule>();
                        extmod.Add(m);
                        v = ReadObject0(extmod);
                        RubyObject fobj = v as RubyObject;
                        if (fobj != null)
                        {
                            fobj.ExtendModules.AddRange(extmod);
                        }
                    }
                    break;
                case RubyMarshal.Types.UserClass:
                    {
                        RubyClass c = RubyClass.GetClass(ReadUnique());

                        v = ReadObject0(extmod);
                        if (v is RubyObject)
                            (v as RubyObject).ClassName = c.Symbol;
                    }
                    break;
                case RubyMarshal.Types.Nil:
                    v = RubyNil.Instance;
                    v = Leave(v);
                    break;
                case RubyMarshal.Types.True:
                    v = true;
                    v = Leave(v);
                    break;
                case RubyMarshal.Types.False:
                    v = false;
                    v = Leave(v);
                    break;
                case RubyMarshal.Types.Fixnum:
                    v = ReadLong();
                    v = Leave(v);
                    break;
                case RubyMarshal.Types.Float:
                    {
                        double d;
                        RubyString fstr = ReadString();
                        string str = fstr.Text;

                        if (str == "inf")
                            d = double.PositiveInfinity;
                        else if (str == "-inf")
                            d = double.NegativeInfinity;
                        else if (str == "nan")
                            d = double.NaN;
                        else
                        {
                            if (str.Contains("\0"))
                            {
                                str = str.Remove(str.IndexOf("\0"));
                            }
                            d = Convert.ToDouble(str);
                        }
                        v = new RubyFloat(d);
                        v = Entry(v);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Bignum:
                    {
                        int sign = 0;
                        switch (ReadByte())
                        {
                            case 0x2b:
                                sign = 1;
                                break;

                            case 0x2d:
                                sign = -1;
                                break;

                            default:
                                sign = 0;
                                break;
                        }
                        int num3 = ReadLong();
                        int index = num3 / 2;
                        int num5 = (num3 + 1) / 2;
                        uint[] data = new uint[num5];
                        for (int i = 0; i < index; i++)
                        {
                            data[i] = m_reader.ReadUInt32();
                        }
                        if (index != num5)
                        {
                            data[index] = m_reader.ReadUInt16();
                        }
                        v = new RubyBignum(sign, data);
                        v = Entry(v);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.String:
                    v = Entry(ReadString());
                    v = Leave(v);
                    break;
                case RubyMarshal.Types.Regexp:
                    {
                        RubyString str = ReadString();
                        int options = ReadByte();
                        bool has_encoding = false;
                        int idx = Prepare();
                        if (hasivp)
                        {
                            ReadInstanceVariable(str, ref has_encoding);
                            ivp = false;
                        }
                        if (!has_encoding)
                        {
                            // TODO: 1.8 compatibility; remove escapes undefined in 1.8
                            /*
                            char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
                            long len = RSTRING_LEN(str);
                            long bs = 0;
                            for (; len-- > 0; *dst++ = *src++) {
                                switch (*src) {
                                    case '\\': bs++; break;
                                    case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
                                    case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
                                    case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
                                    case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
                                    case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
                                    if (bs & 1) --dst;
                                    default: bs = 0; break;
                                }
                            }
                            rb_str_set_len(str, dst - ptr);
                            */
                        }
                        v = Entry0(new RubyRegexp(str, (RubyRegexpOptions)options), idx);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Array:
                    {
                        int len = ReadLong();
                        RubyArray ary = new RubyArray();
                        v = ary;
                        v = Entry(v);
                        while (len-- > 0)
                        {
                            ary.Push(ReadObject());
                        }
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Hash:
                case RubyMarshal.Types.HashWithDefault:
                    {
                        int len = ReadLong();
                        RubyHash hash = new RubyHash();
                        v = hash;
                        v = Entry(v);
                        while (len-- > 0)
                        {
                            object key = ReadObject();
                            object value = ReadObject();
                            hash.Add(key, value);
                        }
                        if (type == RubyMarshal.Types.HashWithDefault)
                        {
                            hash.DefaultValue = ReadObject();
                        }
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Struct:
                    {
                        int idx = Prepare();
                        RubyStruct obj = new RubyStruct();
                        RubySymbol klass = ReadUnique();
                        obj.ClassName = klass;
                        int len = ReadLong();
                        v = obj;
                        v = Entry0(v, idx);
                        while (len-- > 0)
                        {
                            RubySymbol key = ReadSymbol();
                            object value = ReadObject();
                            obj.InstanceVariable[key] = value;
                        }
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.UserDefined:
                    {
                        RubySymbol klass = ReadUnique();
                        RubyString data = ReadString();
                        if (hasivp)
                        {
                            ReadInstanceVariable(data);
                            ivp = false;
                        }
                        RubyUserdefinedDumpObject obj = new RubyUserdefinedDumpObject();
                        obj.ClassName = klass;
                        obj.DumpedObject = data.Raw;
                        v = obj;
                        v = Entry(v);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.UserMarshal:
                    {
                        RubySymbol klass = ReadUnique();
                        FuzzyUserdefinedMarshalDumpObject obj = new FuzzyUserdefinedMarshalDumpObject();
                        v = obj;
                        if (extmod != null)
                            AppendExtendedModule(obj, extmod);
                        v = Entry(v);
                        object data = ReadObject();
                        obj.ClassName = klass;
                        obj.DumpedObject = data;
                        v = Leave(v);
                        if (extmod != null)
                        {
                            extmod.Clear();
                        }
                    }
                    break;
                case RubyMarshal.Types.Object:
                    {
                        int idx = Prepare();
                        RubyObject obj = new RubyObject();
                        RubySymbol klass = ReadUnique();
                        obj.ClassName = klass;
                        v = obj;
                        v = Entry0(v, idx);
                        ReadInstanceVariable(v);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Class:
                    {
                        RubyString str = ReadString();
                        v = RubyClass.GetClass(RubySymbol.GetSymbol(str));
                        v = Entry(v);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Module:
                    {
                        RubyString str = ReadString();
                        v = RubyModule.GetModule(RubySymbol.GetSymbol(str));
                        v = Entry(v);
                        v = Leave(v);
                    }
                    break;
                case RubyMarshal.Types.Symbol:
                    if (hasivp)
                    {
                        v = ReadSymbolReal(ivp);
                        ivp = false;
                    }
                    else
                    {
                        v = ReadSymbolReal(false);
                    }
                    v = Leave(v);
                    break;
                case RubyMarshal.Types.SymbolLink:
                    v = ReadSymbolLink();
                    break;
                case RubyMarshal.Types.Data:
                /*  TODO: Data Support
                    {
                        VALUE klass = path2class(r_unique(arg));
                        VALUE oldclass = 0;

                        v = obj_alloc_by_klass(klass, arg, &oldclass);
                        if (!RB_TYPE_P(v, T_DATA)) {
                            rb_raise(rb_eArgError, "dump format error");
                        }
                        v = r_entry(v, arg);
                        if (!rb_respond_to(v, s_load_data)) {
                            rb_raise(rb_eTypeError, "class %s needs to have instance method `_load_data'", rb_class2name(klass));
                        }
                        rb_funcall(v, s_load_data, 1, r_object0(arg, 0, extmod));
                        check_load_arg(arg, s_load_data);
                        v = r_leave(v, arg);
                    }
                 */
                case RubyMarshal.Types.ModuleOld:
                /*
                    TODO: ModuleOld Support
                    {
                        volatile VALUE str = r_bytes(arg);
                        v = rb_path_to_class(str);
                        v = r_entry(v, arg);
                        v = r_leave(v, arg);
                    }
                 */
                default:
                    throw new InvalidDataException(string.Format("dump format error(0x{0:X2})", type));
            }
            return v;
        }
Пример #46
0
        private RubyHash ActivateLocalGear(dynamic options)
        {
            string deploymentId = options["deployment_id"];

            Logger.Debug("Activating local gear with deployment id {0}", deploymentId);

            RubyHash result = new RubyHash();
            result["status"] = RESULT_FAILURE;
            result["gear_uuid"] = this.Uuid;
            result["deployment_id"] = deploymentId;
            result["messages"] = new List<string>();
            result["errors"] = new List<string>();

            if (!DeploymentExists(deploymentId))
            {
                Logger.Warning("No deployment with id {0} found on gear", deploymentId);
                result["errors"].Add(string.Format("No deployment with id {0} found on gear", deploymentId));
                return result;
            }

            try
            {
                string deploymentDateTime = GetDeploymentDateTimeForDeploymentId(deploymentId);
                string deploymentDir = Path.Combine(this.ContainerDir, "app-deployments", deploymentDateTime);

                Dictionary<string, string> gearEnv = Environ.ForGear(this.ContainerDir);

                string output = string.Empty;

                Logger.Debug("Current deployment state for deployment {0} is {1}", deploymentId, this.State.Value());

                if (Runtime.State.STARTED.EqualsString(State.Value()))
                {
                    options["exclude_web_proxy"] = true;
                    output = StopGear(options);
                    result["messages"].Add(output);
                }

                SyncDeploymentRepoDirToRuntime(deploymentDateTime);
                SyncDeploymentDependenciesDirToRuntime(deploymentDateTime);
                SyncDeploymentBuildDependenciesDirToRuntime(deploymentDateTime);

                UpdateCurrentDeploymentDateTimeSymlink(deploymentDateTime);

                FixHomeDir();

                Manifest primaryCartridge = this.Cartridge.GetPrimaryCartridge();
                
                this.Cartridge.DoControl("update-configuration", primaryCartridge);

                result["messages"].Add("Starting application " + ApplicationName);

                Dictionary<string, object> opts = new Dictionary<string,object>();
                opts["secondary_only"] = true;
                opts["user_initiated"] = true;
                opts["hot_deploy"] = options["hot_deploy"];

                output = StartGear(opts);
                result["messages"].Add(output);

                this.State.Value(Runtime.State.DEPLOYING);

                opts = new Dictionary<string, object>();
                opts["pre_action_hooks_enabled"] = false;
                opts["prefix_action_hooks"] = false;
                output = this.Cartridge.DoControl("deploy", primaryCartridge, opts);
                result["messages"].Add(output);

                opts = new Dictionary<string, object>();
                opts["primary_only"] = true;
                opts["user_initiated"] = true;
                opts["hot_deploy"] = options["hot_deploy"];
                output = StartGear(opts);
                result["messages"].Add(output);

                opts = new Dictionary<string, object>();
                opts["pre_action_hooks_enabled"] = false;
                opts["prefix_action_hooks"] = false;
                output = this.Cartridge.DoControl("post-deploy", primaryCartridge, opts);
                result["messages"].Add(output);

                if (options.ContainsKey("post_install"))
                {
                    string primaryCartEnvDir = Path.Combine(this.ContainerDir, primaryCartridge.Dir, "env");
                    Dictionary<string, string> primaryCartEnv = Environ.Load(primaryCartEnvDir);
                    string ident = (from kvp in primaryCartEnv
                                    where Regex.Match(kvp.Key, "^OPENSHIFT_.*_IDENT").Success
                                    select kvp.Value).FirstOrDefault();
                    string version = Manifest.ParseIdent(ident)[2];
                    this.Cartridge.PostInstall(primaryCartridge, version);
                }

                DeploymentMetadata deploymentMetadata = DeploymentMetadataFor(deploymentDateTime);
                deploymentMetadata.RecordActivation();
                deploymentMetadata.Save();

                if (options.ContainsKey("report_deployments") && gearEnv["OPENSHIFT_APP_DNS"] == gearEnv["OPENSHIFT_GEAR_DNS"])
                {
                    ReportDeployments(gearEnv);
                }

                result["status"] = RESULT_SUCCESS;
            }
            catch(Exception e)
            {
                result["status"] = RESULT_FAILURE;
                result["errors"].Add(string.Format("Error activating gear: {0}", e.ToString()));
            }

            return result;
        }
        public List<RubyHash> Restart(string cartName, RubyHash options)
        {
            List<RubyHash> result = WithGearRotation(options,
                (GearRotationCallback)delegate(object targetGear, Dictionary<string, string> localGearEnv, RubyHash opts)
                {
                    return RestartGear(targetGear, localGearEnv, cartName, opts);
                });


            return result;
        }
Пример #48
0
        public string Activate(RubyHash options = null)
        {
            Logger.Debug("Activating gear {0}", this.Uuid);

            if(options == null)
            {
                options = new RubyHash();
            }

            bool useOutput = options.ContainsKey("out") && options["out"];

            StringBuilder output = new StringBuilder();
            dynamic result = new Dictionary<string, object>();

            if (useOutput)
            {
                output.Append("Activating deployment");
            }

            if (!options.ContainsKey("deployment_id"))
            {
                throw new Exception("deployment_id must be supplied");
            }
            string deploymentId = options["deployment_id"];
            string deploymentDateTime = GetDeploymentDateTimeForDeploymentId(deploymentId);
            DeploymentMetadata deploymentMetadata = DeploymentMetadataFor(deploymentDateTime);
       
            options["hot_deploy"] = deploymentMetadata.HotDeploy;
            if (options.ContainsKey("post_install") || options.ContainsKey("restore"))
            {
                options["hot_deploy"] = false;
            }

            List<RubyHash> parallelResults = WithGearRotation(options, (GearRotationCallback)delegate(object targetGear, Dictionary<string, string> localGearEnv, RubyHash opts)
                {
                    string targetGearUuid;
                    if (targetGear is string)
                    {
                        targetGearUuid = targetGear.ToString();
                    }
                    else
                    {
                        targetGearUuid = ((Model.GearRegistry.Entry)targetGear).Uuid;
                    }
                    if (targetGearUuid == this.Uuid)
                    {
                        return ActivateLocalGear(options);
                    }
                    else
                    {                        
                        return ActivateRemoteGear((GearRegistry.Entry)targetGear, localGearEnv, options);
                    }
                });

            List<string> activatedGearUuids = new List<string>();

            if ((options.ContainsKey("all") && options["all"]) || (options.ContainsKey("gears") && options["gears"]))
            {
                result["status"] = RESULT_SUCCESS;
                result["gear_results"] = new Dictionary<string, object>();

                foreach (RubyHash gearResult in parallelResults)
                {
                    string gearUuid = gearResult["gear_uuid"];
                    activatedGearUuids.Add(gearUuid);

                    result["gear_results"][gearUuid] = gearResult;

                    if (gearResult["status"] != RESULT_SUCCESS)
                    {
                        result["status"] = RESULT_FAILURE;
                    }
                }
            }
            else
            {
                activatedGearUuids.Add(this.Uuid);
                result = parallelResults[0];
            }

            output.Append(JsonConvert.SerializeObject(result));

            return output.ToString();
        }
Пример #49
0
        private RubyHash ActivateRemoteGear(GearRegistry.Entry gear, Dictionary<string, string> gearEnv, RubyHash options)
        {
            string gearUuid = gear.Uuid;

            RubyHash result = new RubyHash();
            result["status"] = RESULT_FAILURE;
            result["gear_uuid"] = this.Uuid;
            result["deployment_id"] = options["deployment_id"];
            result["messages"] = new List<string>();
            result["errors"] = new List<string>();

            string postInstallOptions = options["post_install"] == true ? "--post-install" : "";

            result["messages"].Add(string.Format("Activating gear {0}, deployment id: {1}, {2}", gearUuid, options["deployment_id"], postInstallOptions));
            try
            {
                string ooSSH = @"/cygpath/c/openshift/oo-bin/oo-ssh";
                string bashBinary = Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], "bin\bash.exe");

                string sshCommand = string.Format("{0} {1} gear activate {2} --as-json {3} --no-rotation",
                    ooSSH, gear.ToSshUrl(), options["deployment_id"], postInstallOptions);

                string bashArgs = string.Format("--norc --login -c '{0}'", sshCommand);

                string command = string.Format("{0} {1}", bashBinary, bashArgs);

                string output = RunProcessInContainerContext(this.ContainerDir, command).StdOut;

                if (string.IsNullOrEmpty(output))
                {
                    throw new Exception("No result JSON was received from the remote activate call");
                }
                Dictionary<string, object> activateResult = JsonConvert.DeserializeObject<Dictionary<string, object>>(output);
                if (!activateResult.ContainsKey("status"))
                {
                    throw new Exception("Invalid result JSON received from remote activate call");
                }

                result["messages"].Add(activateResult["messages"]);
                result["errors"].Add(activateResult["errors"]);
                result["status"] = activateResult["status"];
            }
            catch (Exception e)
            {
                result["errors"].Add("Gear activation failed: " + e.ToString());                
            }

            return result;
        }
Пример #50
0
        public void Test_FacterOpenshift()
        {
            RubyHash facts = Uhuru.Openshift.Runtime.Utils.Facter.GetOpenshiftFacts();

            Assert.IsTrue(facts.ContainsKey("node_profile"));
        }
        public string UpdateProxyStatusForGear(RubyHash options)
        {
            string action = options["action"];

            if (action != "enable" && action != "disable")
            {
                new ArgumentException("action must either be :enable or :disable");
            }

            string gearUuid = options["gear_uuid"];

            if (gearUuid == null)
            {
                new ArgumentException("gear_uuid is required");
            }

            Manifest cartridge = options["cartridge"] ?? this.Cartridge.WebProxy();

            if (cartridge == null)
            {
                throw new ArgumentNullException("Unable to update proxy status - no proxy cartridge found");
            }

            bool persist = options["persist"] != null && options["persist"] == true;
            string control = string.Format("{0}-server", action);

            List<string> args = new List<string>();

            if (persist)
            {
                args.Add("persist");
            }

            args.Add(gearUuid);

            return this.Cartridge.DoControl(
                control, cartridge, new Dictionary<string, object>()
                {
                    { "args", string.Join(" ", args) },
                    { "pre_action_hooks_enabled", false },
                    { "post_action_hooks_enabled", false }
                });
        }
        public RubyHash UpdateLocalProxyStatus(RubyHash args)
        {
            RubyHash result = new RubyHash();

            object cartridge = args["cartridge"];
            object action = args["action"];
            object targetGear =args["target_gear"];
            object persist = args["persist"];

            try
            {
                string output = this.UpdateProxyStatusForGear(new RubyHash(){
                    { "cartridge", cartridge },
                    { "action", action },
                    { "gear_uuid", targetGear },
                    { "persist", persist }
                });

                result = new RubyHash()
                {
                    { "status", RESULT_SUCCESS },
                    { "proxy_gear_uuid", this.Uuid },
                    { "target_gear_uuid", targetGear },
                    { "messages", new List<string>() },
                    { "errors", new List<string>() }
                };
            }
            catch (Exception ex)
            {
                result = new RubyHash()
                {
                    { "status", RESULT_FAILURE },
                    { "proxy_gear_uuid", this.Uuid },
                    { "target_gear_uuid", targetGear },
                    { "messages", new List<string>() },
                    { "errors", new List<string>() { string.Format("An exception occured updating the proxy status: {0}\n{1}", ex.Message, ex.StackTrace) } }
                };
            }

            return result;
        }
        public RubyHash UpdateProxyStatus(RubyHash options)
        {
            string action = options["action"];

            if (action != "enable" && action != "disable")
            {
                throw new ArgumentException("action must either be :enable or :disable");
            }

            if (options["gear_uuid"] == null)
            {
                throw new ArgumentException("gear_uuid is required");
            }

            string gearUuid = options["gear_uuid"];

            Manifest cartridge;

            if (options["cartridge"] == null)
            {
                cartridge = this.Cartridge.WebProxy();
            }
            else
            {
                cartridge = options["cartridge"];
            }

            if (cartridge == null)
            {
                throw new ArgumentException("Unable to update proxy status - no proxy cartridge found");
            }

            dynamic persist = options["persist"];

            Dictionary<string, string> gearEnv = Environ.ForGear(this.ContainerDir);

            RubyHash result = new RubyHash() {
                { "status", RESULT_SUCCESS },
                { "target_gear_uuid", gearUuid },
                { "proxy_results", new RubyHash() }
            };

            RubyHash gearResult = new RubyHash();

            if (gearEnv["OPENSHIFT_APP_DNS"] != gearEnv["OPENSHIFT_GEAR_DNS"])
            {
                gearResult = this.UpdateLocalProxyStatus(new RubyHash(){
                    { "cartridge", cartridge },
                    { "action", action },
                    { "proxy_gear", this.Uuid },
                    { "target_gear", gearUuid },
                    { "persist", persist }
                });

                result["proxy_results"][this.Uuid] = gearResult;
            }
            else
            {
                // only update the other proxies if we're the currently elected proxy
                // TODO the way we determine this needs to change so gears other than
                // the initial proxy gear can be elected
                GearRegistry.Entry[] proxyEntries = this.gearRegistry.Entries["proxy"].Values.ToArray();

                // TODO: vladi: Make this parallel
                RubyHash[] parallelResults = proxyEntries.Select(entry =>
                    this.UpdateRemoteProxyStatus(new RubyHash()
                    {
                        { "current_gear", this.Uuid },
                        { "proxy_gear", entry },
                        { "target_gear", gearUuid },
                        { "cartridge", cartridge },
                        { "action", action },
                        { "persist", persist },
                        { "gear_env", gearEnv }
                    })).ToArray();
                
                foreach (RubyHash parallelResult in parallelResults)
                {
                    if (parallelResult.ContainsKey("proxy_results"))
                    {
                        result["proxy_results"] = result["proxy_results"].Merge(parallelResult["proxy_results"]);
                    }
                    else
                    {
                        result["proxy_results"][parallelResult["proxy_gear_uuid"]] = parallelResult;
                    }
                }
            }

            // if any results failed, consider the overall operation a failure
            foreach (RubyHash proxyResult in result["proxy_results"].Values)
            {
                if (proxyResult["status"] != RESULT_SUCCESS)
                {
                    result["status"] = RESULT_FAILURE;
                }
            }

            return result;
        }
Пример #54
0
        public void PostReceive(RubyHash options)
        {
            Logger.Debug("Running post receive for gear {0}", this.Uuid);

            Dictionary<string, string> gearEnv = Environ.ForGear(this.ContainerDir);
            
            string repoDir = Path.Combine(this.ContainerDir, "app-root", "runtime", "repo");

            Directory.CreateDirectory(repoDir);

            ApplicationRepository applicationRepository = new ApplicationRepository(this);
            applicationRepository.Archive(repoDir, options["ref"]);

            options["deployment_datetime"] = this.LatestDeploymentDateTime();

            Build(options);

            Logger.Debug("Running post receive - prepare for gear {0}", this.Uuid);
            Prepare(options);

            Logger.Debug("Running post receive - distribute for gear {0}", this.Uuid);
            Distribute(options);

            Logger.Debug("Running post receive - activate for gear {0}", this.Uuid);
            Activate(options);
        }
 public DeploymentMetadata(ApplicationContainer container, string deploymentDatetime)
 {
     this.file = Path.Combine(container.ContainerDir, "app-deployments", deploymentDatetime, "metadata.json");
     if(File.Exists(file))
     {
         Load();
     }
     else
     {
         using (File.Create(this.file)) { };
         container.SetRWPermissions(this.file);
         this.metadata = this.defaults;
         Save();
     }
 }
Пример #56
0
 public string Deploy(RubyHash options)
 {
     StringBuilder output = new StringBuilder();
     if (!options.ContainsKey("artifact_url"))
     {
         output.AppendLine(PreReceive(options));
         PostReceive(options);
     }
     else
     {
         output.AppendLine(DeployBinaryArtifact(options));
     }
     return output.ToString();
 }
        public static IEnumerable<ApplicationContainer> All(Hourglass hourglass = null, bool loadenv = true)
        {
            EtcUser[] users = new Etc(NodeConfig.Values).GetAllUsers();

            foreach (EtcUser user in users)
            {
                if (user.Gecos.StartsWith("openshift_service"))
                {
                    RubyHash env = new RubyHash();
                    string gearNamespace = null;

                    if (loadenv)
                    {
                        env = new RubyHash(Environ.Load(new string[] { Path.Combine(user.Dir, ".env") }));
                    }

                    if (env.ContainsKey("OPENSHIFT_GEAR_DNS"))
                    {
                        gearNamespace = env["OPENSHIFT_GEAR_DNS"];
                    }

                    ApplicationContainer app = null;
                    
                    try
                    {
                        app = new ApplicationContainer(env["OPENSHIFT_APP_UUID"],
                            user.Name, user, env["OPENSHIFT_APP_NAME"], env["OPENSHIFT_GEAR_NAME"], gearNamespace, null, null, hourglass);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Failed to instantiate ApplicationContainer for uid {0}/uuid {1}: {2}",
                            user.Uid, env["OPENSHIFT_APP_UUID"], ex.Message);
                        Logger.Error("Stacktrace: {0}", ex.StackTrace);

                        continue;
                    }

                    yield return app;
                }
            }
        }
Пример #58
0
        public static RubyHash NodeUtilization()
        {
            RubyHash result = new RubyHash();

            Common.Config resource = Node.ResourceLimits;

            if (resource == null)
            {
                return result;
            }

            result["node_profile"] = resource.Get("node_profile", Node.DEFAULT_NODE_PROFILE);
            result["quota_blocks"] = resource.Get("quota_blocks", Node.DEFAULT_QUOTA_BLOCKS);
            result["quota_files"] = resource.Get("quota_files", Node.DEFAULT_QUOTA_FILES);
            result["no_overcommit_active"] = Boolean.Parse(resource.Get("no_overcommit_active", Node.DEFAULT_NO_OVERCOMMIT_ACTIVE));


            result["max_active_gears"] = Convert.ToInt32(resource.Get("max_active_gears", Node.DEFAULT_MAX_ACTIVE_GEARS));


            //
            // Count number of git repos and gear status counts
            //
            result["git_repos_count"] = 0;
            result["gears_total_count"] = 0;
            result["gears_idled_count"] = 0;
            result["gears_stopped_count"] = 0;
            result["gears_started_count"] = 0;
            result["gears_deploying_count"] = 0;
            result["gears_unknown_count"] = 0;

            
            foreach (ApplicationContainer app in ApplicationContainer.All(null, false))
            {
                result["gears_total_count"] += 1;

                switch (app.State.Value())
                {
                    case "building":
                    case "deploying":
                    case "new":
                        result["gears_deploying_count"] += 1;
                        break;
                    case "started":
                        result["gears_started_count"] += 1;
                        break;
                    case "idle":
                        result["gears_idled_count"] += 1;
                        break;
                    case "stopped":
                        result["gears_stopped_count"] += 1;
                        break;
                    case "unknown":
                        result["gears_unknown_count"] += 1;
                        break;
                }
            }


            // consider a gear active unless explicitly not
            result["gears_active_count"] = result["gears_total_count"] - result["gears_idled_count"] - result["gears_stopped_count"];
            result["gears_usage_pct"] = result["max_active_gears"] == 0 ? 0.0f : (result["gears_total_count"] * 100.0f) / result["max_active_gears"];
            result["gears_active_usage_pct"] = result["max_active_gears"] == 0 ? 0.0f : (result["gears_active_count"] * 100.0f) / result["max_active_gears"];
            result["capacity"] = result["gears_usage_pct"].ToString();
            result["active_capacity"] = result["gears_active_usage_pct"].ToString();
            return result;
        }
        public ReturnStatus Execute()
        {
            ReturnStatus returnStatus = new ReturnStatus();
            try
            {
                NodeConfig config = new NodeConfig();
                string gearPath = config.Get("GEAR_BASE_DIR");
                string[] folders = Directory.GetDirectories(gearPath);
                List<RubyHash> endpoints = new List<RubyHash>();
                RubyHash outputHash = new RubyHash();


                foreach (string folder in folders)
                {
                    string folderName = Path.GetFileName(folder);
                    if (!folderName.StartsWith("."))
                    {
                        ApplicationContainer container = ApplicationContainer.GetFromUuid(folderName);
                        Dictionary<string, string> env = Environ.ForGear(container.ContainerDir);

                        container.Cartridge.EachCartridge(cart =>
                        {
                            cart.Endpoints.ForEach(endpoint =>
                            {
                                RubyHash endpointHash = new RubyHash();

                                endpointHash.Add("cartridge_name", string.Format("{0}-{1}", cart.Name, cart.Version));
                                if (env.ContainsKey(endpoint.PublicPortName))
                                {
                                    endpointHash.Add("external_port", env[endpoint.PublicPortName]);
                                }
                                else
                                {
                                    endpointHash.Add("external_port", null);
                                }
                                endpointHash.Add("internal_address", env[endpoint.PrivateIpName]);
                                endpointHash.Add("internal_port", endpoint.PrivatePort);
                                endpointHash.Add("protocols", endpoint.Protocols);
                                endpointHash.Add("type", new List<string>());

                                if (cart.WebProxy)
                                {
                                    endpointHash["protocols"] = container.Cartridge.GetPrimaryCartridge().Endpoints.First().Protocols;
                                    endpointHash["type"] = new List<string>() { "load_balancer" };
                                }
                                else if (cart.WebFramework)
                                {
                                    endpointHash["type"] = new List<string>() { "web_framework" };
                                }
                                else if (cart.Categories.Contains("database"))
                                {
                                    endpointHash["type"] = new List<string>() { "web_framework" };
                                }
                                else if (cart.Categories.Contains("plugin"))
                                {
                                    endpointHash["type"] = new List<string>() { "plugin" };
                                }
                                else
                                {
                                    endpointHash["type"] = new List<string>() { "other" };
                                }

                                if (endpoint.Mappings != null && endpoint.Mappings.Count > 0)
                                {
                                    List<RubyHash> mappingsList = new List<RubyHash>();
                                    foreach (Uhuru.Openshift.Common.Models.Endpoint.Mapping mapping in endpoint.Mappings)
                                    {
                                        RubyHash mappings = new RubyHash();
                                        mappings.Add("frontend", mapping.Frontend);
                                        mappings.Add("backend", mapping.Backend);
                                        mappingsList.Add(mappings);
                                    }
                                    endpointHash.Add("mappings", mappingsList);
                                }
                                endpoints.Add(endpointHash);
                            });
                        });
                        if (endpoints.Count > 0)
                        {
                            outputHash.Add(folderName, endpoints);
                        }
                    }
                }
                string output = Newtonsoft.Json.JsonConvert.SerializeObject(outputHash);
                returnStatus.Output = output;
                returnStatus.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running get-all-gears-endpoints-action command: {0} - {1}", ex.Message, ex.StackTrace);
                returnStatus.Output = ex.ToString();
                returnStatus.ExitCode = 1;

            }

            return returnStatus;
        }