public void Run()
        {
            List<string> usersNotInPrison = new List<string>();
            List<string> usersWithNoGears = new List<string>();

            Output.WriteDebug("Checking cygwing passwd file consistency");
            NodeConfig nodeConfig = new NodeConfig();
            Etc etc = new Etc(nodeConfig);
            EtcUser[] etcUsers = etc.GetAllUsers();
            Output.WriteDebug(string.Format("Found {0} Etc Users", etcUsers));

            Prison.Prison[] prisons = Prison.Prison.Load();
            Output.WriteDebug(string.Format("Found {0} Prison Users", prisons.Count()));

            List<ApplicationContainer> gears =  ApplicationContainer.All(null, false).ToList<ApplicationContainer>();
            Output.WriteDebug(string.Format("Found {0} gears", gears.Count()));

            foreach (EtcUser etcUser in etcUsers)
            {
                Output.WriteDebug(string.Format("Checking user {0}", etcUser.Name));

                if (etcUser.Name == "administrator")
                {
                    //skipping administrator user
                    continue;
                }

               if (prisons.Where(p => p.ID.ToString().TrimStart('0').Replace("-", "") == etcUser.Name).Count() == 0)
               {
                   usersNotInPrison.Add(etcUser.Name);
               }
               
               if (gears.Where(g => g.Uuid == etcUser.Name).Count() == 0)
               {
                   usersWithNoGears.Add(etcUser.Name);
               }
            }

            if (usersNotInPrison.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("The following users exist in /etc/passwd");
                sb.AppendLine(String.Join(", ", usersNotInPrison.ToArray()));
                sb.AppendLine("but have no prison user associated to them");
                Output.WriteWarn(sb.ToString());
                exitCode = ExitCode.WARNING;
            }
            
            if (usersWithNoGears.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("The following users exist in /etc/passwd");
                sb.AppendLine(String.Join(", ", usersWithNoGears.ToArray()));
                sb.AppendLine("but have no gears associated to them");
                Output.WriteWarn(sb.ToString());
                exitCode = ExitCode.WARNING;
            }

        }
Example #2
0
 public void Test_Etc()
 {
     bool testresults = true;
     try
     {
         NodeConfig config = new NodeConfig();
         Etc etcobj = new Etc(config);
         if (etcobj.GetAllUsers().Count() > 0)
         {
             EtcUser user = etcobj.GetAllUsers().FirstOrDefault();
             if (etcobj.GetPwanam(user.Name) == null)
             {
                 testresults = false;
             }
         }
     }
     catch
     {
         testresults = false;
     }
     Assert.AreEqual(true, testresults);
 }
Example #3
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;
        }
        private static EtcUser GetPasswdFor(string containerUuid)
        {
            NodeConfig config = new Config.NodeConfig();

            string gecos = config.Get("GEAR_GECOS");

            if (string.IsNullOrEmpty(gecos))
            {
                gecos = "OO application container";
            }
            EtcUser etcUser = new Etc(config).GetPwanam(containerUuid);
            etcUser.Gecos = gecos;
            
            return etcUser;

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