public ReturnStatus Execute()
        {
            ReturnStatus returnStatus = new ReturnStatus();

            try
            {
                NodeConfig config = new NodeConfig();

                string distrinctHome = Path.Combine(config.Get("GEAR_BASE_DIR"), ".settings");

                if (!Directory.Exists(distrinctHome))
                {
                    Directory.CreateDirectory(distrinctHome);
                }

                File.WriteAllText(Path.Combine(distrinctHome, "district.info"),
                    string.Format("#Do not  modify manually!\nuuid='{0}'\nactive='{1}'\nfirst_uid={2}\nmax_uid={3}", Uuid, Active, FirstUid, MaxUid));

                //TODO handle profiling

                returnStatus.Output = string.Format("created/updated district {0} with active = {1}, first_uid = {2}, max_uid = {3}", Uuid, Active, FirstUid, MaxUid);
                returnStatus.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                returnStatus.Output = ex.ToString();
                returnStatus.ExitCode = 255;
            }
            return returnStatus;
        }
        public ReturnStatus Execute()
        {
            ReturnStatus returnStatus = new ReturnStatus();
            try
            {
                NodeConfig config = new NodeConfig();

                string distrinctInfoPath = Path.Combine(config.Get("GEAR_BASE_DIR"), ".settings", "district.info");

                string districtInfo = File.ReadAllText(distrinctInfoPath);

                districtInfo = Regex.Replace(districtInfo, "first_uid=\\d+",
                    string.Format("first_uid={0}", FirstUid), RegexOptions.Multiline);

                districtInfo = Regex.Replace(districtInfo, "max_uid=\\d+",
                    string.Format("max_uid={0}", MaxUid), RegexOptions.Multiline);

                File.WriteAllText(distrinctInfoPath, districtInfo);

                //TODO handle profiling

                returnStatus.Output = string.Format("updated district uid limits with first_uid = {0}, max_uid = {1}", FirstUid, MaxUid);
                returnStatus.ExitCode = 0;


            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                returnStatus.Output = ex.ToString();
                returnStatus.ExitCode = 255;
            }

            return returnStatus;
        }
Exemplo n.º 3
0
        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;
            }

        }
Exemplo n.º 4
0
        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 ApplicationContainer CreateAppContainer()
 {
     string applicationUuid = Guid.NewGuid().ToString("N");
     string containerUuid = applicationUuid;
     NodeConfig config = new NodeConfig();
     // EtcUser etcUser = new Etc(config).GetPwanam(containerUuid);
     string userId = WindowsIdentity.GetCurrent().Name;
     string applicationName = "testApp";
     string containerName = applicationName;
     string namespaceName = "uhuru";
     object quotaBlocks = null;
     object quotaFiles = null;
     Hourglass hourglass = null;
     Node.LimitsFile = resourceLimitsFile;
     ApplicationContainer container = new ApplicationContainer(
         applicationUuid, containerUuid, null,
         applicationName, containerName, namespaceName,
         quotaBlocks, quotaFiles, hourglass);
     
     return container;
 }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
0
 public ContainerPlugin(ApplicationContainer applicationContainer)
 {
     this.container = applicationContainer;
     this.config = NodeConfig.Values;
 }
Exemplo n.º 8
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 ApplicationContainer(string applicationUuid, string containerUuid, EtcUser userId, string applicationName,
     string containerName, string namespaceName, object quotaBlocks, object quotaFiles, Hourglass hourglass,int applicationUid=0)
 {
     this.config = NodeConfig.Values;
     this.Uuid = containerUuid;
     this.ApplicationUuid = applicationUuid;
     this.ApplicationName = applicationName;
     this.ContainerName = containerName;
     this.Namespace = namespaceName;
     this.QuotaBlocks = quotaBlocks;
     this.QuotaFiles = quotaFiles;
     this.State = new ApplicationState(this);            
     this.hourglass = hourglass ?? new Hourglass(3600);
     this.BaseDir = this.config["GEAR_BASE_DIR"];
     this.containerPlugin = new ContainerPlugin(this);
     this.Cartridge = new CartridgeModel(this, this.State, this.hourglass);            
     if (userId != null)
     {
         this.uid = userId.Uid;
         this.gid = userId.Gid;
         this.gecos = userId.Gecos;
     }
     if (applicationUid > 0)
     {
         this.uid = applicationUid;
     }
 }
        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;

        }
Exemplo n.º 11
0
 public Etc(NodeConfig config)
 {
     passwdPath = Path.Combine(config.Get("SSHD_BASE_DIR"), "etc", "passwd");
     this.config = config;
 }
        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;
        }