예제 #1
0
        protected override int Execute()
        {
            var cwd = Directory.GetCurrentDirectory();

            configuration = string.IsNullOrEmpty(configuration) ? "full-build" : configuration;

            Log.Info("Updating packages");
            PackageUpdater.UpdatePackages();
            var modules = Helper.GetModules();

            var moduleName = Path.GetFileName(cwd);

            var curRepo = new GitRepository(moduleName, Helper.CurrentWorkspace, Log);

            if (curRepo.IsGitRepo)
            {
                curRepo.TryUpdateUrl(modules.FirstOrDefault(m => m.Name.Equals(moduleName)));
            }
            HooksHelper.InstallHooks(moduleName);

            var getter = new ModuleGetter(
                Helper.GetModules(),
                new Dep(moduleName, null, configuration),
                policy,
                mergedBranch,
                verbose,
                localBranchForce);

            getter.GetDeps();


            Log.Info("SUCCESS UPDATE DEPS");
            return(0);
        }
예제 #2
0
        private void InitializeHelpers(HttpClient client = null)
        {
            var triggerHelper = new TriggerHooksHelper(client ?? A.Fake <HttpClient>(), A.Fake <IMetrics>());

            fakeGit     = A.Fake <Func <string, Task <string> > >();
            hooksHelper = new HooksHelper(fakeGit, triggerHelper, A.Fake <IMetrics>());
        }
예제 #3
0
        public static Func <HttpRequest, HttpResponse, RouteData, Task> Create(SyncActor syncActor, IMetrics metrics, HooksHelper hooksHelper)
        {
            return(async(req, res, routeData) =>
            {
                var commitId = req.Query["commit"].ToString();
                if (!IsCommitIdString(commitId))
                {
                    res.StatusCode = 400;
                    await res.WriteAsync("Invalid commit id");

                    return;
                }

                try
                {
                    await syncActor.PushToUpstream(commitId);

                    metrics.Measure.Counter.Increment(Push, Success);

                    #pragma warning disable CS4014
                    hooksHelper.TriggerPostCommitHooks(commitId);
                    #pragma warning restore CS4014
                }
                catch (Exception ex)
                {
                    metrics.Measure.Counter.Increment(Push, Failure);
                    res.StatusCode = 500;
                    await res.WriteAsync(ex.Message);
                }
                #pragma warning disable CS4014
                syncActor.SyncToLatest();
                #pragma warning restore CS4014
            });
        }
예제 #4
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory,
                              IHostApplicationLifetime lifetime)
        {
            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(_configuration)
                         .WriteTo.Console(new JsonFormatter())
                         .Enrich.FromLogContext()
                         .CreateLogger();

            _logger.LogInformation("Starting service");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            RunSSHDeamon(lifetime, loggerFactory.CreateLogger("sshd"));

            var executor = ShellHelper.Executor.WithWorkingDirectory(_configuration.GetValue <string>("REPO_LOCATION"))
                           .ForwardEnvVariable("GIT_SSH");



            var gitValidationFlow = new GitValidationFlow
            {
                Validators =
                {
                    (Patterns.Manifests,              new CircularDependencyValidator()),
                    (Patterns.Manifests,              new ManifestStructureValidator()),
                    (Patterns.Manifests,              new NestedKeysValidator()),
                    (Patterns.JPad,                   new CompileJPadValidator()),
                    (Patterns.SubjectExtractionRules, new SubjectExtractionValidator()),
                    (Patterns.Policy,                 new PolicyValidator()),
                }
            };

            var minioConfig    = _configuration.GetSection("Minio");
            var metricsService = app.ApplicationServices.GetService <IMetrics>();

            var storageClient = Polly.Policy.Handle <Exception>()
                                .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
                                .ExecuteAsync(() =>
                                              MinioBucketStorage.GetOrCreateBucket(CreateMinioClient(minioConfig),
                                                                                   minioConfig.GetValue("Bucket", DEFAULT_MINIO_BUCKET_NAME)))
                                .Result;

            var natsClient       = new NatsPublisher(_configuration.GetSection("Nats").GetValue <string>("Endpoint"));
            var versionPublisher = natsClient.GetSubjectPublisher("version");

            var repoSynchronizer    = new RepoSynchronizer(executor.WithUser("git").CreateCommandExecutor("git"));
            var storageSynchronizer = CreateStorageSynchronizer(storageClient, executor.WithUser("git"), metricsService);

            storageSynchronizer.Sync(repoSynchronizer.CurrentHead().Result, checkForStaleRevision: false).Wait();
            RunIntervalPublisher(lifetime, versionPublisher, repoSynchronizer, storageSynchronizer);

            var syncActor = SyncActor.Create(storageSynchronizer, repoSynchronizer, natsClient, lifetime.ApplicationStopping, loggerFactory.CreateLogger("SyncActor"));

            var triggerHelper = new TriggerHooksHelper(new HttpClient(), metricsService, loggerFactory.CreateLogger("TriggerHooksHelper"));
            var hooksHelper   = new HooksHelper(
                executor.WithUser("git").CreateCommandExecutor("git"),
                triggerHelper,
                metricsService,
                loggerFactory.CreateLogger("HooksHelper")
                );

            app.UseRouter(router =>
            {
                router.MapGet("validate",
                              ValidationHandler.Create(executor, gitValidationFlow,
                                                       loggerFactory.CreateLogger <ValidationHandler>(),
                                                       metricsService));
                router.MapGet("sync",
                              SyncHandler.Create(syncActor, _syncPolicy, metricsService));
                router.MapGet("push", PushHandler.Create(syncActor, metricsService, hooksHelper));
                router.MapGet("log", async(req, res, routedata) => _logger.LogInformation(req.Query["message"]));
                router.MapGet("health", async(req, res, routedata) => await res.WriteAsync(JsonConvert.SerializeObject(new { })));
                router.MapGet("version", async(req, res, routedata) => await res.WriteAsync(Assembly.GetEntryAssembly()
                                                                                            .GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion));
            });
        }