Example #1
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;
        }