Exemplo n.º 1
0
        static IContainer BuildContainer()
        {
            var builder      = new ContainerBuilder();
            var thisAssembly = typeof(CliProgram).GetTypeInfo().Assembly;

            builder.RegisterModule(new LoggingModule());

            builder.RegisterAssemblyTypes(thisAssembly).As <ICommand>().AsSelf();
            builder.RegisterType <CommandLocator>().As <ICommandLocator>();

            builder.RegisterType <CommandOutputProvider>().As <ICommandOutputProvider>().SingleInstance();

            builder.RegisterAssemblyTypes(thisAssembly).As <IExporter>().AsSelf();
            builder.RegisterAssemblyTypes(thisAssembly).As <IImporter>().AsSelf();
            builder.RegisterType <ExporterLocator>().As <IExporterLocator>();
            builder.RegisterType <ImporterLocator>().As <IImporterLocator>();

            builder.RegisterType <ReleasePlanBuilder>().As <IReleasePlanBuilder>().SingleInstance();
            builder.RegisterType <PackageVersionResolver>().As <IPackageVersionResolver>().SingleInstance();
            builder.RegisterType <ChannelVersionRuleTester>().As <IChannelVersionRuleTester>().SingleInstance();

            var requestingTool = "octo";

            var octoExtensionVersion = Environment.GetEnvironmentVariable("OCTOEXTENSION");

            if (!string.IsNullOrWhiteSpace(octoExtensionVersion))
            {
                requestingTool += $" plugin/{octoExtensionVersion}";
            }

            OctopusClientFactory.SetRequestingTool(requestingTool);
            builder.RegisterType <OctopusClientFactory>().As <IOctopusClientFactory>();
            builder.RegisterType <OctopusRepositoryFactory>().As <IOctopusAsyncRepositoryFactory>();

            builder.RegisterType <OctopusPhysicalFileSystem>().As <IOctopusFileSystem>();

            return(builder.Build());
        }
Exemplo n.º 2
0
        public static void OctoImportVariables(this ICakeContext context,
                                               string octopusServerEndpoint,
                                               string octopusProjectName,
                                               string octopusApiKey,
                                               IEnumerable <OctoVariable> variables,
                                               bool clearAllNonSensitiveExistingVariables = false)
        {
            try
            {
                IOctopusAsyncClient client = new OctopusClientFactory().CreateAsyncClient(new OctopusServerEndpoint(octopusServerEndpoint, octopusApiKey)).Result;
                var octopus = new OctopusAsyncRepository(client);

                ProjectResource project = octopus.Projects.FindByName(octopusProjectName).Result;

                VariableSetResource variableSet = octopus.VariableSets.Get(project.Link("Variables")).Result;

                if (clearAllNonSensitiveExistingVariables)
                {
                    context.Log.Information($"Deleting all nonsensitive variables...");

                    List <VariableResource> sensitiveVariables = variableSet.Variables.Where(variable => variable.IsSensitive).ToList();

                    variableSet.Variables.Clear();

                    sensitiveVariables.ForEach(sensitiveVariable => { variableSet.Variables.Add(sensitiveVariable); });

                    context.Log.Information($"Deleting operation finished.");
                }

                foreach (OctoVariable variable in variables)
                {
                    var newVariable = new VariableResource
                    {
                        Name        = variable.Name,
                        Value       = variable.Value,
                        IsSensitive = variable.IsSensitive,
                        Type        = variable.IsSensitive ? VariableType.Sensitive : VariableType.String,
                        IsEditable  = variable.IsEditable,
                        Scope       = CreateScopeSpesification(variable, variableSet)
                    };

                    string scopeNames = CreateScopeInformationsForLogging(variable);

                    VariableResource existingVariable = variableSet.Variables.FirstOrDefault(x => x.Name == variable.Name && x.Scope.Equals(newVariable.Scope));
                    if (existingVariable != null)
                    {
                        context.Log.Information($"Variable: ({variable.Name}), Scopes:({scopeNames}) already exists in octopus, trying to update...");

                        variableSet.AddOrUpdateVariableValue(existingVariable.Name, newVariable.Value, newVariable.Scope, newVariable.IsSensitive);

                        context.Log.Information($"Variable: ({variable.Name}), Scopes:({scopeNames}) updated successfully...");
                    }
                    else
                    {
                        context.Log.Information($"New Variable: ({variable.Name}), Scopes:({scopeNames}) detected, trying to add...");

                        variableSet.Variables.Add(newVariable);

                        context.Log.Information($"New Variable: ({variable.Name}), Scopes:({scopeNames}) added successfully...");
                    }
                }

                octopus.VariableSets.Modify(variableSet).Wait();
                octopus.VariableSets.Refresh(variableSet).Wait();

                context.Log.Information($"Variables are all successfully set.");
            }
            catch (Exception exception)
            {
                throw new CakeException(exception.Message, exception.InnerException);
            }
        }
Exemplo n.º 3
0
        public static async Task Setup(ArgsBase args, ICakeContext context = null, IServiceCollection _coll = null, bool connectToOctopus = true)
        {
            var coll = _coll ?? new ServiceCollection();

            if (String.IsNullOrEmpty(args.OctoUri))
            {
                throw new ArgumentException("Null or empty Octopus Deploy API URI", nameof(args.OctoUri));
            }
            if (String.IsNullOrEmpty(args.ApiKey))
            {
                throw new ArgumentException("Null or empty Octopus Deploy API key", nameof(args.ApiKey));
            }

            addArgs(args, coll);

            if (context != null)
            {
                coll.AddSingleton <ILogger>(new CakeLoggerAbstraction(context.Log, null));
            }
            else
            {
                var serilogLogger = new LoggerConfiguration()
                                    .WriteTo.Console();
                if (args.Verbose)
                {
                    serilogLogger.MinimumLevel.Verbose();
                }
                else
                {
                    serilogLogger.MinimumLevel.Information();
                }
                coll.AddSingleton <ILogger>(new CakeLoggerAbstraction(null, serilogLogger.CreateLogger()));
            }

            if (connectToOctopus)
            {
                var server  = new OctopusServerEndpoint(args.OctoUri, args.ApiKey);
                var factory = new OctopusClientFactory();
                coll.AddSingleton <IOctopusAsyncRepository>(new OctopusAsyncRepository(await factory.CreateAsyncClient(server).ConfigureAwait(false)));
            }

            coll.AddSingleton <IFileSystem, FileSystem>();

            coll.AddSingleton <IVaultClientFactory, VaultClientFactory>();
            coll.AddSingleton <ISecretProviderFactory, SecretProviderFactory>();
            coll.AddSingleton <ISecretsMananger, SecretsMananger>();
            coll.AddSingleton <VaultKVV2Provider>();
            coll.AddSingleton <VaultProvider>();

            coll.AddSingleton <ILibraryManager, LibraryManager>();
            coll.AddSingleton <IProjectManager, ProjectManager>();
            coll.AddSingleton <IProjectClearer, ProjectClearer>();
            coll.AddSingleton <ITenantClearer, TenantClearer>();
            coll.AddSingleton <ITenantManager, TenantManager>();
            coll.AddSingleton <VariableConverter>();

            coll.AddSingleton <ValidateLibraryCommand>();
            coll.AddSingleton <UploadLibraryCommand>();
            coll.AddSingleton <UploadTenantCommand>();
            coll.AddSingleton <ClearVariableSetCommand>();
            coll.AddSingleton <ClearTenantCommand>();
            coll.AddSingleton <ClearProjectCommand>();
            coll.AddSingleton <ValidateTenantCommand>();
            coll.AddSingleton <UploadProjectCommand>();
            Container = coll.BuildServiceProvider();
        }