Beispiel #1
0
        public string PostConfigure(string cartName, string templateGitUrl = null)
        {
            Logger.Debug("Running PostConfigure for '{0}' with cart '{1}' and git url '{2}'", this.Uuid, cartName, templateGitUrl);

            StringBuilder output    = new StringBuilder();
            Manifest      cartridge = this.Cartridge.GetCartridge(cartName);

            bool performInitialBuild = !Git.EmptyCloneSpec(templateGitUrl) && (cartridge.InstallBuildRequired || !string.IsNullOrEmpty(templateGitUrl)) && cartridge.Buildable;

            if (performInitialBuild)
            {
                Logger.Info("Performing initial build");
                try
                {
                    RunProcessInContainerContext(this.ContainerDir, "gear prereceive --init");
                    RunProcessInContainerContext(this.ContainerDir, "gear postreceive --init");
                }
                catch (Exception ex)
                {
                    // TODO: vladi: implement exception handling for initial build
                }
            }
            else if (cartridge.Deployable)
            {
                string             deploymentDatetime = LatestDeploymentDateTime();
                DeploymentMetadata deploymentMetadata = DeploymentMetadataFor(deploymentDatetime);
                if (deploymentMetadata.Activations.Count == 0)
                {
                    Prepare(new RubyHash()
                    {
                        { "deployment_datetime", deploymentDatetime }
                    });
                    deploymentMetadata.Load();
                    ApplicationRepository applicationRepository = new ApplicationRepository(this);
                    string gitRef         = "master";
                    string gitSha1        = applicationRepository.GetSha1(gitRef);
                    string deploymentsDir = Path.Combine(this.ContainerDir, "app-deployments");
                    SetRWPermissions(deploymentsDir);
                    // TODO: reset_permission_R(deployments_dir)

                    deploymentMetadata.RecordActivation();
                    deploymentMetadata.Save();

                    UpdateCurrentDeploymentDateTimeSymlink(deploymentDatetime);

                    FixHomeDir();
                }
            }

            output.AppendLine(this.Cartridge.PostConfigure(cartName));

            if (performInitialBuild)
            {
                // TODO: grep build log
            }

            return(output.ToString());
        }
Beispiel #2
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);

                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);
        }