Esempio n. 1
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 static ApplicationContainer GetFromUuid(string containerUuid, Hourglass hourglass = null)
        {
            EtcUser etcUser   = GetPasswdFor(containerUuid);
            string  nameSpace = null;
            Dictionary <string, string> env = Environ.Load(Path.Combine(etcUser.Dir, ".env"));

            if (!string.IsNullOrEmpty(env["OPENSHIFT_GEAR_DNS"]))
            {
                nameSpace = Regex.Replace(Regex.Replace("testing-uhu.openshift.local", @"\..*$", ""), @"^.*\-", "");
            }

            if (string.IsNullOrEmpty(env["OPENSHIFT_APP_UUID"]))
            {
                //Maybe we should improve the exceptions we throw.
                throw new Exception("OPENSHIFT_APP_UUID is missing!");
            }
            if (string.IsNullOrEmpty(env["OPENSHIFT_APP_NAME"]))
            {
                throw new Exception("OPENSHIFT_APP_NAME is missing!");
            }
            if (string.IsNullOrEmpty(env["OPENSHIFT_GEAR_NAME"]))
            {
                throw new Exception("OPENSHIFT_GEAR_NAME is missing!");
            }

            ApplicationContainer applicationContainer = new ApplicationContainer(env["OPENSHIFT_APP_UUID"], containerUuid, etcUser,
                                                                                 env["OPENSHIFT_APP_NAME"], env["OPENSHIFT_GEAR_NAME"], nameSpace, null, null, hourglass);

            return(applicationContainer);
        }
Esempio n. 3
0
        public string CartridgeAction(Manifest cartridge, string action, string softwareVersion, bool renderErbs = false)
        {
            string cartridgeHome = Path.Combine(this.container.ContainerDir, cartridge.Dir);

            action = Path.Combine(cartridgeHome, "bin", action + ".ps1");
            if (!File.Exists(action))
            {
                return(string.Empty);
            }

            Dictionary <string, string> gearEnv      = Environ.ForGear(this.container.ContainerDir);
            string cartridgeEnvHome                  = Path.Combine(cartridgeHome, "env");
            Dictionary <string, string> cartridgeEnv = Environ.Load(cartridgeEnvHome);

            cartridgeEnv.Remove("PATH");
            foreach (var kvp in gearEnv)
            {
                cartridgeEnv[kvp.Key] = kvp.Value;
            }
            if (renderErbs)
            {
                // TODO: render erb
            }

            // TODO: vladi: implement hourglass
            string cmd    = string.Format("{0} -ExecutionPolicy Bypass -InputFormat None -noninteractive -file {1} --version {2}", ProcessExtensions.Get64BitPowershell(), action, softwareVersion);
            string output = this.container.RunProcessInContainerContext(cartridgeHome, cmd, 0).StdOut;

            // TODO: vladi: add logging
            return(output);
        }
Esempio n. 4
0
        public async Task <IActionResult> Edit(int id, [Bind("EnvID,EnvName,EnvLocation,EnvTurfType,EnvRegistrationDate")] Environ environ)
        {
            if (id != environ.EnvID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(environ);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EnvironExists(environ.EnvID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(environ));
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="job"></param>
        /// <param name="stage"></param>
        /// <param name="regex"></param>
        /// <param name="priority"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public static int Watch(IJob job, PipeLineStages stage, Regex regex, int priority, Func <int, HttpApplication, WatchResponse> request)
        {
            Environ environ = null;

            if (EnvironLock.Write(() =>
            {
                if (!Environs.TryGetValue(job.Environment.SortOrder, out environ))
                {
                    Environs.Add(job.Environment.SortOrder, environ = new Environ(job.Environment));
                }
            }))
            {
                return(EnvironLock.Read(() =>
                {
                    return environ.WatchLocks[(int)stage].Write(() =>
                    {
                        var watchList = environ.Watchers[(int)stage];
                        var count = Interlocked.Increment(ref _requestCount);
                        EnvironHasWatches[(int)stage] = true;
                        watchList.Add(new Watcher
                        {
                            Priority = priority,
                            AppId = job.App.Id,
                            Regex = regex,
                            Request = request
                        });
                        watchList.Sort(new WatchComparer());
                        return count;
                    });
                }));
            }
            return(-1);
        }
Esempio n. 6
0
        public string DeletePublicEndpoints(string cartName)
        {
            Manifest      cart              = Cartridge.GetCartridge(cartName);
            StringBuilder output            = new StringBuilder();
            Dictionary <string, string> env = Environ.ForGear(this.ContainerDir);

            try
            {
                foreach (Endpoint endpoint in cart.Endpoints)
                {
                    string port = env[endpoint.PrivatePortName];

                    // TODO: will have to change this once prison is integrated
                    Network.CloseFirewallPort(port);

                    output.AppendFormat("NOTIFY_ENDPOINT_DELETE: {0} {1}", NodeConfig.Values["PUBLIC_IP"], port);
                }

                Logger.Warning(@"Deleted all public endpoints for cart {0} in gear {1}", cartName, this.Uuid);
            }
            catch (Exception ex)
            {
                Logger.Warning(@"Couldn't delete all public endpoints for cart {0} in gear {1}: {2} - {3}", cartName, this.Uuid, ex.Message, ex.StackTrace);
            }

            return(output.ToString());
        }
        public string Tidy()
        {
            StringBuilder output = new StringBuilder();

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

            string gearDir = env["OPENSHIFT_HOMEDIR"];
            string appName = env["OPENSHIFT_APP_NAME"];

            string gearRepoDir = Path.Combine(gearDir, "git", string.Format("{0}.git", appName));
            string gearTmpDir  = Path.Combine(gearDir, ".tmp");

            output.Append(StopGear(new Dictionary <string, object>()
            {
                { "user_initiated", false }
            }));
            try
            {
                GearLevelTidyTmp(gearTmpDir);
                output.AppendLine(this.Cartridge.Tidy());
                output.AppendLine(GearLevelTidyGit(gearRepoDir));
            }
            catch (Exception ex)
            {
                output.AppendLine(ex.ToString());
            }
            finally
            {
                StartGear(new Dictionary <string, object>()
                {
                    { "user_initiated", false }
                });
            }
            return(output.ToString());
        }
Esempio n. 8
0
        public void Test_Environ()
        {
            string gearDir = "TestGearDir";
            Dictionary <string, string> result = Environ.ForGear(gearDir);

            DirectoryUtil.EmptyDirectory(gearDir);
            Directory.Delete(gearDir);
            Assert.IsNotNull(result);
        }
        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);
        }
Esempio n. 10
0
        public async Task <IActionResult> Create([Bind("EnvID,EnvName,EnvLocation,EnvTurfType,EnvRegistrationDate")] Environ environ)
        {
            if (ModelState.IsValid)
            {
                _context.Add(environ);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(environ));
        }
Esempio n. 11
0
 public void StopWatch()
 {
     lock (SyncRoot)
     {
         if (Environ != null)
         {
             Environ.UnSubscribeUpDateEvent(this);
             Environ = null;
         }
     }
 }
Esempio n. 12
0
 public void StartWatch()
 {
     lock (SyncRoot)
     {
         if (Environ == null)
         {
             Environ = new Environ();
             Environ.SubscribeUpDateEvent(this);
         }
     }
 }
Esempio n. 13
0
        public bool IsAllowed(Environ environ)
        {
            var result = (bool)_filterMethodInfo.Invoke(_filterObject, environ.ResolveParameters(_filterMethodInfo.GetParameters()));

            if (_direction == FilterDirection.Allow)
            {
                return(result);
            }
            else
            {
                return(!result);
            }
        }
Esempio n. 14
0
        private void TestContextWriteLineEnviron(Environ environ)
        {
            if (environ.Lead != null)
            {
                TestContext.WriteLine("Lead process : ");
                TestContext.WriteLine("     {0}", environ.Lead.ToString());
            }

            TestContext.WriteLine("All process :");

            foreach (var process in environ.ProcessDockers)
            {
                TestContext.WriteLine("     {0}", process.ToString());
            }
        }
Esempio n. 15
0
        private string CartridgeTeardown(string cartridgeName, bool removeCartridgeDir)
        {
            string cartridgeHome            = Path.Combine(this.container.ContainerDir, cartridgeName);
            Dictionary <string, string> env = Environ.ForGear(this.container.ContainerDir, cartridgeHome);
            string teardown = Path.Combine(cartridgeHome, "bin", "teardown.ps1");

            if (!File.Exists(teardown))
            {
                return(string.Empty);
            }

            // run teardown script

            return(string.Empty);
        }
Esempio n. 16
0
        public Manifest GetPrimaryCartridge()
        {
            Dictionary <string, string> env = Environ.ForGear(container.ContainerDir);
            string primaryCartDir           = null;

            if (env.ContainsKey("OPENSHIFT_PRIMARY_CARTRIDGE_DIR"))
            {
                primaryCartDir = env["OPENSHIFT_PRIMARY_CARTRIDGE_DIR"];
            }
            else
            {
                return(null);
            }

            return(GetCartridgeFromDirectory(primaryCartDir));
        }
Esempio n. 17
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());
        }
Esempio n. 18
0
        public ReturnStatus Execute()
        {
            ReturnStatus returnStatus = new ReturnStatus();

            try
            {
                Logger.Debug(string.Format("Running Get-Gear-Envs-Action for {0}", Uuid));
                string containerDir = ApplicationContainer.GetFromUuid(Uuid).ContainerDir;
                Dictionary <string, string> envVars = Environ.ForGear(containerDir);
                returnStatus.Output = JsonConvert.SerializeObject(envVars);
                Logger.Debug(string.Format("Output for Get-Gear-Envs-Action{0} ", returnStatus.Output));
                returnStatus.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Logger.Error("Error running get-gear-envs-actions command: {0} - {1}", ex.Message, ex.StackTrace);
                returnStatus.ExitCode = 1;
            }
            return(returnStatus);
        }
Esempio n. 19
0
        public void EnvironTest()
        {
            using (var token = new ManualResetEvent(false))
            {
                var environ = new Environ();

                environ.Event += (obj, e) =>
                {
                    var @this = obj as Environ;

                    Assert.AreNotEqual(@this.ProcessDockers.Count, 0, "vs - is not desktop process ? >_<");

                    TestContextWriteLineEnviron(@this);

                    token.Set();
                };

                token.WaitOne();
            }
        }
Esempio n. 20
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var logger = LogFactory.CreateLogger <Startup>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });

            string OWMKey       = "";
            string MongoURI     = "";
            var    keysForFront = new Dictionary <string, string>();

            if (Environ.IsDevelopment())
            {
                logger.LogDebug("OWMKey=" + Configuration["OWM_KEY"]);
                logger.LogDebug("MongoURI=" + Configuration["MONGO_URI"]);
                logger.LogDebug("GMAP_JS=" + Configuration["GMAP_JS"]);
                OWMKey   = Configuration["OWM_KEY"];
                MongoURI = Configuration["MONGO_URI"];
                keysForFront.Add(KeyField.GoogleMapJs, Configuration["GMAP_JS"]);
            }
            else if (Environ.IsProduction())
            {
                OWMKey   = System.Environment.GetEnvironmentVariable("OWM_KEY");
                MongoURI = System.Environment.GetEnvironmentVariable("MONGO_URI");
                keysForFront.Add(KeyField.GoogleMapJs,
                                 System.Environment.GetEnvironmentVariable("GMAP_JS"));
            }
            else
            {
                logger.LogError("fail to get credentials!");
            }

            services.AddSingleton <OWMHandler>(new OWMHandler(OWMKey));
            services.AddSingleton <CityModel>(new CityModel(MongoURI));
            services.AddSingleton <IDictionary <string, string> >(new Dictionary <string, string>(keysForFront));
        }
Esempio n. 21
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);
                }
            }
        }
Esempio n. 22
0
        public string CreatePublicEndpoints(string cartName)
        {
            // currently on Windows private service ports are the same as public ports

            Manifest      cart              = Cartridge.GetCartridge(cartName);
            StringBuilder output            = new StringBuilder();
            Dictionary <string, string> env = Environ.ForGear(this.ContainerDir);

            foreach (Endpoint endpoint in cart.Endpoints)
            {
                string port = env[endpoint.PrivatePortName];

                this.AddEnvVar(endpoint.PublicPortName, port);

                // TODO: will have to change this once prison is integrated
                Network.OpenFirewallPort(port, this.Uuid);

                output.Append(this.GenerateEndpointCreationNotificationMsg(cart, endpoint, "127.0.0.1", port));
            }

            return(output.ToString());
        }
Esempio n. 23
0
        public Context(Environ environ, string accountId, string accessToken)
        {
            if (!environ.IsEnumValue())
            {
                throw new ArgumentOutOfRangeException(nameof(environ));
            }

            if (string.IsNullOrWhiteSpace(accountId))
            {
                throw new ArgumentOutOfRangeException(nameof(accountId));
            }

            if (!accountIdRegex.IsMatch(accountId))
            {
                throw new ArgumentOutOfRangeException(nameof(accountId));
            }

            if (string.IsNullOrWhiteSpace(accessToken))
            {
                throw new ArgumentOutOfRangeException(nameof(accessToken));
            }

            if (accessToken.Length != 65)
            {
                throw new ArgumentOutOfRangeException(nameof(accessToken));
            }

            var guids = accessToken.Split('-');

            if (guids.Length != 2 || !guids[0].IsGuid() || !guids[1].IsGuid())
            {
                throw new ArgumentOutOfRangeException(nameof(accessToken));
            }

            Environ     = environ;
            AccountId   = accountId;
            AccessToken = accessToken;
        }
Esempio n. 24
0
        public static void Initialize(BallGameDBContext context)
        {
            context.Database.EnsureCreated();

            //Looking for any locations
            if (context.Environs.Any())
            {
                return; //DB has been seeded
            }

            var environs = new Environ[]
            {
                new Environ {
                    EnvName = "Test Field", EnvLocation = "Kingston", EnvTurfType = "Grass", EnvRegistrationDate = DateTime.Parse("2020-01-01")
                }
            };

            foreach (Environ e in environs)
            {
                context.Environs.Add(e);
            }
            context.SaveChanges();
        }
Esempio n. 25
0
        private void CreateCartridgeDirectory(Manifest cartridge, string softwareVersion)
        {
            string target = Path.Combine(this.container.ContainerDir, cartridge.Dir);

            CartridgeRepository.InstantiateCartridge(cartridge, target);

            string ident = Manifest.BuildIdent(cartridge.CartridgeVendor, cartridge.Name, softwareVersion, cartridge.CartridgeVersion);
            Dictionary <string, string> envs = new Dictionary <string, string>();

            envs[string.Format("{0}_DIR", cartridge.ShortName)]   = target + Path.DirectorySeparatorChar;
            envs[string.Format("{0}_IDENT", cartridge.ShortName)] = ident;
            WriteEnvironmentVariables(Path.Combine(target, "env"), envs);
            envs = new Dictionary <string, string>();
            if (!string.IsNullOrEmpty(this.container.Namespace))
            {
                envs["namespace"] = this.container.Namespace;
            }

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

            if (!currentGearEnv.ContainsKey("OPENSHIFT_PRIMARY_CARTRIDGE_DIR"))
            {
                envs["PRIMARY_CARTRIDGE_DIR"] = target + Path.DirectorySeparatorChar;
            }
            if (envs.Count > 0)
            {
                WriteEnvironmentVariables(Path.Combine(this.container.ContainerDir, ".env"), envs);
            }

            var prison = Prison.Prison.LoadPrisonNoAttach(PrisonIdConverter.Generate(this.container.Uuid));

            Logger.Debug("Setting permisions to dir {0}, prison user {1}", target, prison.User.Username);

            LinuxFiles.TakeOwnership(target, prison.User.Username);

            Logger.Info("Created cartridge directory {0}/{1}", container.Uuid, cartridge.Dir);
        }
Esempio n. 26
0
 public bool IsException(HashSet <object> exceptionsSet, Environ environ)
 {
     return((bool)_isExceptionMethodInfo.Invoke(_projectorObject, environ.Extend(exceptionsSet).ResolveParameters(_isExceptionMethodInfo.GetParameters())));
 }
Esempio n. 27
0
 public void Project(Environ environ)
 {
     _projectMethodInfo.Invoke(_projectorObject, environ.ResolveParameters(_projectMethodInfo.GetParameters()));
 }
Esempio n. 28
0
 /// <summary>
 /// Build an instance of this for a day and <see cref="Environ"/>.
 /// </summary>
 /// <param name="dateToRun">The day this validation run is for</param>
 /// <param name="environ">A source of data to validate</param>
 public ValidationRunner(DateTime dateToRun, Environ environ)
 {
     _dateToRun = dateToRun;
     _environ   = environ;
 }
Esempio n. 29
0
 public TemplateRunner(Environ environ, TypeInfo validatorFixtureInfo, MethodInfo validatorTemplateInfo)
 {
     _environ = environ;
     _validatorFixtureInfo  = validatorFixtureInfo;
     _validatorTemplateInfo = validatorTemplateInfo;
 }
Esempio n. 30
0
 public FixtureRunner(Environ environ, TypeInfo validatorFixtureInfo)
 {
     _environ = environ;
     _validatorFixtureInfo = validatorFixtureInfo;
 }