Example #1
0
        public async Task <Dictionary <string, string> > GetArtifacts(string environmentName, string projectName, params string[] artifactNames)
        {
            var endpoint = new OctopusServerEndpoint(octopusEndpoint, octopusApiKey);
            var client   = await OctopusAsyncClient.Create(endpoint);

            OctopusAsyncRepository repository = new OctopusAsyncRepository(client);

            var artifacts = await GetArtifactsList(repository, environmentName, projectName);

            var artifactsByName = new Dictionary <string, string>();

            foreach (var artifactName in artifactNames)
            {
                var matchingArtifact = artifacts.Items.SingleOrDefault(a => a.Filename == artifactName);
                if (matchingArtifact == null)
                {
                    throw new ArtifactsProviderException(
                              $"У деплоя проекта {projectName} на среду {environmentName} отсутствует артефакт {artifactName}. " +
                              "Проверьте свежесть и полноту деплоя.");
                }
                using (var stream = await repository.Artifacts.GetContent(matchingArtifact))
                    using (var streamReader = new StreamReader(stream))
                    {
                        artifactsByName[artifactName] = await streamReader.ReadToEndAsync();
                    }
            }

            return(artifactsByName);
        }
Example #2
0
        public void IfTheServerReturnsAnUnauthorisedResultASecurityExceptionShouldBeThrown()
        {
            var         repo    = new OctopusAsyncRepository(AsyncClient);
            Func <Task> getUser = () => repo.Users.Get("users-1");

            getUser.ShouldThrow <OctopusSecurityException>().WithMessage(ErrorMessage);
        }
Example #3
0
        private static async Task <ResourceCollection <ArtifactResource> > GetArtifactsList(
            OctopusAsyncRepository repository,
            string environmentName,
            string projectName)
        {
            var environment = await repository.Environments.FindOne(x => x.Name.Equals(environmentName, System.StringComparison.OrdinalIgnoreCase));

            if (environment == null)
            {
                throw new ArtifactsProviderException($"Среда {environmentName} не найдена. Проверьте написание среды (case sensetive)");
            }

            var project = await repository.Projects.FindOne(x => x.Name.Equals(projectName, System.StringComparison.OrdinalIgnoreCase));

            if (project == null)
            {
                throw new ArtifactsProviderException($"Проект {projectName} не найден в октопусе. Проверьте конфигурацию");
            }

            var deployments = await repository.Deployments.FindBy(new[] { project.Id }, new[] { environment.Id }, take : 1);

            var deployment = deployments.Items.SingleOrDefault();

            if (deployment == null)
            {
                throw new ArtifactsProviderException($"Не удалось найти деплой проекта {projectName} на среду {environmentName}");
            }

            return(await repository.Artifacts.FindRegarding(new DeploymentResource { Id = deployment.Id }));
        }
        public void Setup()
        {
            var asyncClient = Substitute.For <IOctopusAsyncClient>();

            repository = new OctopusAsyncRepository(asyncClient);
            asyncClient.Post <ConvertProjectVariablesToGitCommand, ConvertProjectVariablesToGitResponse>(Arg.Do <string>(x => urlUsed = x), Arg.Do <ConvertProjectVariablesToGitCommand>(x => commandUsed = x)).Returns(new ConvertProjectVariablesToGitResponse());
        }
Example #5
0
        public void Setup()
        {
            var asyncClient = Substitute.For <IOctopusAsyncClient>();

            repository = new OctopusAsyncRepository(asyncClient);

            asyncClient.Get <VariableSetResource>(Arg.Do <string>(x => getUrlUsed = x), Arg.Do <object>(x => getParamsUsed = x));
        }
        /// <summary>
        /// Executes the operation against the specified Octopus Deploy server.
        /// </summary>
        /// <param name="serverEndpoint">The Octopus Deploy server endpoint.</param>
        /// <exception cref="System.ArgumentException">
        /// </exception>
        public async Task ExecuteAsync(OctopusServerEndpoint serverEndpoint)
        {
            using (var client = await clientFactory.CreateAsyncClient(serverEndpoint).ConfigureAwait(false))
            {
                var repository = new OctopusAsyncRepository(client);

                await ExecuteAsync(repository).ConfigureAwait(false);
            }
        }
Example #7
0
        public async Task <string[]> GetEnvironments()
        {
            var endpoint = new OctopusServerEndpoint(octopusEndpoint, octopusApiKey);
            var client   = await OctopusAsyncClient.Create(endpoint);

            OctopusAsyncRepository repository = new OctopusAsyncRepository(client);

            var environments = await repository.Environments.FindAll();

            return(environments.Select(x => x.Name).ToArray());
        }
        public void AllPropertiesAreNotNull()
        {
            var repository = new OctopusAsyncRepository(Substitute.For<IOctopusAsyncClient>());
            var nullPropertiesQ = from p in typeof(OctopusAsyncRepository).GetTypeInfo().GetProperties()
                where p.GetMethod.Invoke(repository, new object[0]) == null
                select p.Name;

            var nullProperties = nullPropertiesQ.ToArray();
            if (nullProperties.Any())
                Assert.Fail("The following properties are null after OctopusAsyncRepository instantiation: " + nullProperties.CommaSeperate());
        }
        public void Setup()
        {
            var asyncClient = Substitute.For <IOctopusAsyncClient>();

            asyncRepository = new OctopusAsyncRepository(asyncClient);
            asyncClient.Post <ConvertProjectToGitCommand, ConvertProjectToGitResponse>(Arg.Do <string>(x => asyncUrlUsed = x), Arg.Any <ConvertProjectToGitCommand>()).Returns(new ConvertProjectToGitResponse());

            var syncClient = Substitute.For <IOctopusClient>();

            syncRepository = new OctopusRepository(syncClient);
            syncClient.Post <ConvertProjectToGitCommand, ConvertProjectToGitResponse>(Arg.Do <string>(x => syncUrlUsed = x), Arg.Any <ConvertProjectToGitCommand>()).Returns(new ConvertProjectToGitResponse());
        }
Example #10
0
        async Task <List <EnvironmentResource> > GetEnvironments(OctopusAsyncRepository repository)
        {
            var selectedEnvironments = await repository.Environments.FindByNames(EnvironmentNames).ConfigureAwait(false);

            var missing = EnvironmentNames.Except(selectedEnvironments.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToList();

            if (missing.Any())
            {
                throw new ArgumentException($"Could not find the {"environment" + (missing.Count == 1 ? "" : "s")} {string.Join(", ", missing)} on the Octopus server.");
            }

            return(selectedEnvironments);
        }
Example #11
0
        async Task <List <EnvironmentResource> > GetEnvironments(OctopusAsyncRepository repository)
        {
            var selectedEnvironments = await repository.Environments.FindByNames(EnvironmentNames).ConfigureAwait(false);

            var missing = EnvironmentNames.Except(selectedEnvironments.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToList();

            if (missing.Any())
            {
                throw new ArgumentException(CouldNotFindMessage("environment", missing.ToArray()));
            }

            return(selectedEnvironments);
        }
        public void AllPropertiesAreNotNull()
        {
            var repository      = new OctopusAsyncRepository(Substitute.For <IOctopusAsyncClient>());
            var nullPropertiesQ = from p in typeof(OctopusAsyncRepository).GetTypeInfo().GetProperties()
                                  where p.GetMethod.Invoke(repository, new object[0]) == null
                                  select p.Name;

            var nullProperties = nullPropertiesQ.ToArray();

            if (nullProperties.Any())
            {
                Assert.Fail("The following properties are null after OctopusAsyncRepository instantiation: " + nullProperties.CommaSeperate());
            }
        }
Example #13
0
        async Task <MachinePolicyResource> GetMachinePolicy(OctopusAsyncRepository repository)
        {
            var machinePolicy = default(MachinePolicyResource);

            if (!string.IsNullOrEmpty(MachinePolicy))
            {
                machinePolicy = await repository.MachinePolicies.FindByName(MachinePolicy).ConfigureAwait(false);

                if (machinePolicy == null)
                {
                    throw new ArgumentException(CouldNotFindMessage("machine policy", MachinePolicy));
                }
            }
            return(machinePolicy);
        }
Example #14
0
        async Task ValidateTenantTags(OctopusAsyncRepository repository)
        {
            if (TenantTags == null || !TenantTags.Any())
            {
                return;
            }

            var tagSets = await repository.TagSets.FindAll().ConfigureAwait(false);

            var missingTags = TenantTags.Where(tt => !tagSets.Any(ts => ts.Tags.Any(t => t.CanonicalTagName.Equals(tt, StringComparison.OrdinalIgnoreCase)))).ToList();

            if (missingTags.Any())
            {
                throw new ArgumentException(CouldNotFindMessage("tag", missingTags.ToArray()));
            }
        }
Example #15
0
        async Task ValidateTenantTags(OctopusAsyncRepository repository)
        {
            if (TenantTags == null || !TenantTags.Any())
            {
                return;
            }

            var tagSets = await repository.TagSets.FindAll().ConfigureAwait(false);

            var missingTags = TenantTags.Where(tt => !tagSets.Any(ts => ts.Tags.Any(t => t.CanonicalTagName.Equals(tt, StringComparison.OrdinalIgnoreCase)))).ToList();

            if (missingTags.Any())
            {
                throw new ArgumentException($"Could not find the {"tag" + (missingTags.Count == 1 ? "" : "s")} {string.Join(", ", missingTags)} on the Octopus server.");
            }
        }
Example #16
0
        public void AllPropertiesAreNotNullExceptDelayInitialised()
        {
            var client = Substitute.For <IOctopusAsyncClient>();

            client.Repository.LoadRootDocument().Returns(new RootResource());
            var repository      = new OctopusAsyncRepository(client);
            var nullPropertiesQ = from p in typeof(OctopusAsyncRepository).GetTypeInfo().GetProperties()
                                  where !delayInitialisedProperties.Contains(p.Name)
                                  where p.GetMethod.Invoke(repository, new object[0]) == null
                                  select p.Name;

            var nullProperties = nullPropertiesQ.ToArray();

            if (nullProperties.Any())
            {
                Assert.Fail("The following properties are null after OctopusAsyncRepository instantiation: " + nullProperties.CommaSeperate());
            }
        }
Example #17
0
        async Task <MachineResource> GetMachine(OctopusAsyncRepository repository)
        {
            var existing = default(MachineResource);

            try
            {
                existing = await repository.Machines.FindByName(MachineName).ConfigureAwait(false);

                if (!AllowOverwrite && existing?.Id != null)
                {
                    throw new ArgumentException($"A machine named '{MachineName}' already exists in the environment. Use the 'force' parameter if you intended to update the existing machine.");
                }
            }
            catch (OctopusDeserializationException) // eat it, probably caused by resource incompatability between versions
            {
            }
            return(existing ?? new MachineResource());
        }
Example #18
0
        async Task <List <TenantResource> > GetTenants(OctopusAsyncRepository repository)
        {
            if (Tenants == null || !Tenants.Any())
            {
                return(new List <TenantResource>());
            }
            var tenantsByName = await repository.Tenants.FindByNames(Tenants).ConfigureAwait(false);

            var missing = Tenants.Except(tenantsByName.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToArray();


            var tenantsById = await repository.Tenants.Get(missing).ConfigureAwait(false);

            missing = missing.Except(tenantsById.Select(e => e.Id), StringComparer.OrdinalIgnoreCase).ToArray();

            if (missing.Any())
            {
                throw new ArgumentException(CouldNotFindMessage("tenant", missing));
            }

            return(tenantsById.Concat(tenantsByName).ToList());
        }
Example #19
0
        /// <summary>
        /// Executes the operation against the specified Octopus Deploy server.
        /// </summary>
        /// <param name="repository">The Octopus Deploy server repository.</param>
        /// <exception cref="System.ArgumentException">
        /// </exception>
        public async Task ExecuteAsync(OctopusAsyncRepository repository)
        {
            var selectedEnvironments = GetEnvironments(repository).ConfigureAwait(false);
            var machinePolicy        = GetMachinePolicy(repository).ConfigureAwait(false);
            var machineTask          = GetMachine(repository).ConfigureAwait(false);
            var tenants = GetTenants(repository).ConfigureAwait(false);

            await ValidateTenantTags(repository).ConfigureAwait(false);

            var machine = await machineTask;

            ApplyChanges(machine, await selectedEnvironments, await machinePolicy, await tenants);

            if (machine.Id != null)
            {
                await repository.Machines.Modify(machine).ConfigureAwait(false);
            }
            else
            {
                await repository.Machines.Create(machine).ConfigureAwait(false);
            }
        }
Example #20
0
        async Task <List <TenantResource> > GetTenants(OctopusAsyncRepository repository)
        {
            if (Tenants == null || !Tenants.Any())
            {
                return(new List <TenantResource>());
            }
            var tenantsByName = await repository.Tenants.FindByNames(Tenants).ConfigureAwait(false);

            var missing = Tenants.Except(tenantsByName.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToArray();


            var tenantsById = await repository.Tenants.Get(missing).ConfigureAwait(false);

            missing = missing.Except(tenantsById.Select(e => e.Id), StringComparer.OrdinalIgnoreCase).ToArray();

            if (missing.Any())
            {
                throw new ArgumentException($"Could not find the {"tenant" + (missing.Length == 1 ? "" : "s")} {string.Join(", ", missing)} on the Octopus server.");
            }

            return(tenantsById.Concat(tenantsByName).ToList());
        }
 /// <summary>
 /// Executes the operation against the specified Octopus Deploy server.
 /// </summary>
 /// <param name="repository">The Octopus Deploy server repository.</param>
 /// <exception cref="System.ArgumentException">
 /// </exception>
 public async Task ExecuteAsync(OctopusAsyncRepository repository)
 {
     await ExecuteAsync((IOctopusAsyncRepository)repository);
 }
Example #22
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);
            }
        }
 public void IfTheServerReturnsAnUnauthorisedResultASecurityExceptionShouldBeThrown()
 {
     var repo = new OctopusAsyncRepository(Client);
     Func<Task> getUser = () => repo.Users.GetCurrent();
     getUser.ShouldThrow<OctopusSecurityException>().WithMessage(ErrorMessage);
 }
Example #24
0
 /// <summary>
 /// Executes the operation against the specified Octopus Deploy server.
 /// </summary>
 /// <param name="repository">The Octopus Deploy server repository.</param>
 /// <exception cref="System.ArgumentException">
 /// </exception>
 public async Task ExecuteAsync(OctopusAsyncRepository repository)
 {
     await ExecuteAsync((IOctopusSpaceAsyncRepository)repository).ConfigureAwait(false);
 }
Example #25
0
        public async Task <IDictionary <string, string> > GetVariables(string environmentName, string projectName)
        {
            var endpoint = new OctopusServerEndpoint(octopusEndpoint, octopusApiKey);
            var client   = await OctopusAsyncClient.Create(endpoint);

            var repository = new OctopusAsyncRepository(client);
            var project    = await repository.Projects.FindByName(projectName);

            var sets = new ConcurrentDictionary <string, string>();

            await Task.WhenAll(
                project.IncludedLibraryVariableSetIds
                .Select(async x =>
            {
                try
                {
                    var set = await repository.LibraryVariableSets.Get(x);
                    var vars = await repository.VariableSets.Get(set.VariableSetId);

                    var environment = vars.ScopeValues.Environments.FirstOrDefault(e => e.Name.Equals(environmentName, System.StringComparison.OrdinalIgnoreCase));
                    if (!string.IsNullOrWhiteSpace(environment?.Id))
                    {
                        var envVars = vars.Variables
                                      .Where(vs =>
                                             (vs.Scope.TryGetValue(ScopeField.Environment, out var vs1) &&
                                              vs1.Any(e => e == environment.Id)));

                        foreach (var s in envVars)
                        {
                            sets.TryAdd(s.Name, s.Value);
                        }

                        envVars = vars.Variables
                                  .Where(vs => !vs.Scope.ContainsKey(ScopeField.Environment));

                        foreach (var s in envVars)
                        {
                            sets.TryAdd(s.Name, s.Value);
                        }
                    }

                    sets.TryAdd("Octopus.Environment.Name", environment.Name);
                }
                catch { }
            })
                .ToArray());

            //меняем вложенные переменные
            for (var i = 0; i < 10; i++)
            {
                bool hasChanges = false;
                foreach (var set in sets)
                {
                    foreach (var sub in sets)
                    {
                        if (sub.Key == set.Key)
                        {
                            continue;
                        }

                        var replace = $"#{{{set.Key}}}";
                        if (sets[sub.Key]?.Contains(replace) == true)
                        {
                            hasChanges    = true;
                            sets[sub.Key] = sets[sub.Key]?.Replace(replace, set.Value);
                        }
                    }
                }

                if (!hasChanges)
                {
                    break;
                }
            }

            return(sets);
        }
        /// <summary>
        /// Executes the operation against the specified Octopus Deploy server.
        /// </summary>
        /// <param name="repository">The Octopus Deploy server repository.</param>
        /// <exception cref="System.ArgumentException">
        /// </exception>
        public async Task ExecuteAsync(OctopusAsyncRepository repository)
        {
            var selectedEnvironments = GetEnvironments(repository).ConfigureAwait(false);
            var machinePolicy = GetMachinePolicy(repository).ConfigureAwait(false);
            var machineTask = GetMachine(repository).ConfigureAwait(false);
            var tenants = GetTenants(repository).ConfigureAwait(false);
            await ValidateTenantTags(repository).ConfigureAwait(false);

            var machine = await machineTask;
            ApplyChanges(machine, await selectedEnvironments, await machinePolicy, await tenants);

            if (machine.Id != null)
                await repository.Machines.Modify(machine).ConfigureAwait(false);
            else
                await repository.Machines.Create(machine).ConfigureAwait(false);
        }
        /// <summary>
        /// Executes the operation against the specified Octopus Deploy server.
        /// </summary>
        /// <param name="serverEndpoint">The Octopus Deploy server endpoint.</param>
        /// <exception cref="System.ArgumentException">
        /// </exception>
        public async Task ExecuteAsync(OctopusServerEndpoint serverEndpoint)
        {
            using (var client = await clientFactory.CreateAsyncClient(serverEndpoint).ConfigureAwait(false))
            {
                var repository = new OctopusAsyncRepository(client);

                await ExecuteAsync(repository).ConfigureAwait(false);
            }
        }
        async Task<List<TenantResource>> GetTenants(OctopusAsyncRepository repository)
        {
            if (Tenants == null || !Tenants.Any())
            {
                return new List<TenantResource>();
            }
            var tenantsByName = await repository.Tenants.FindByNames(Tenants).ConfigureAwait(false);
            var missing = Tenants.Except(tenantsByName.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToArray();


            var tenantsById = await repository.Tenants.Get(missing).ConfigureAwait(false);
            missing = missing.Except(tenantsById.Select(e => e.Id), StringComparer.OrdinalIgnoreCase).ToArray();

            if (missing.Any())
                throw new ArgumentException($"Could not find the {"tenant" + (missing.Length == 1 ? "" : "s")} {string.Join(", ", missing)} on the Octopus server.");

            return tenantsById.Concat(tenantsByName).ToList();
        }
 async Task<MachineResource> GetMachine(OctopusAsyncRepository repository)
 {
     var existing = default(MachineResource);
     try
     {
         existing = await repository.Machines.FindByName(MachineName).ConfigureAwait(false);
         if (!AllowOverwrite && existing?.Id != null)
             throw new ArgumentException(string.Format("A machine named '{0}' already exists in the environment. Use the 'force' parameter if you intended to update the existing machine.", MachineName));
     }
     catch (OctopusDeserializationException) // eat it, probably caused by resource incompatability between versions
     {
     }
     return existing ?? new MachineResource();
 }
        async Task<MachinePolicyResource> GetMachinePolicy(OctopusAsyncRepository repository)
        {

            var machinePolicy = default(MachinePolicyResource);
            if (!string.IsNullOrEmpty(MachinePolicy))
            {
                machinePolicy = await repository.MachinePolicies.FindByName(MachinePolicy).ConfigureAwait(false);
                if (machinePolicy == null)
                    throw new ArgumentException($"Could not find a machine policy named {MachinePolicy}.");
            }
            return machinePolicy;
        }
        async Task<List<EnvironmentResource>> GetEnvironments(OctopusAsyncRepository repository)
        {
            var selectedEnvironments = await repository.Environments.FindByNames(EnvironmentNames).ConfigureAwait(false);

            var missing = EnvironmentNames.Except(selectedEnvironments.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToList();

            if (missing.Any())
                throw new ArgumentException($"Could not find the {"environment" + (missing.Count == 1 ? "" : "s")} {string.Join(", ", missing)} on the Octopus server.");

            return selectedEnvironments;
        }
        async Task ValidateTenantTags(OctopusAsyncRepository repository)
        {
            if (TenantTags == null || !TenantTags.Any())
                return;

            var tagSets = await repository.TagSets.FindAll().ConfigureAwait(false);
            var missingTags = TenantTags.Where(tt => !tagSets.Any(ts => ts.Tags.Any(t => t.CanonicalTagName.Equals(tt, StringComparison.OrdinalIgnoreCase)))).ToList();

            if (missingTags.Any())
                throw new ArgumentException($"Could not find the {"tag" + (missingTags.Count == 1 ? "" : "s")} {string.Join(", ", missingTags)} on the Octopus server.");
        }