public void SetUp()
        {
            deployReleaseCommand = new DeployReleaseCommand(RepositoryFactory, Log, FileSystem);

            var project = new ProjectResource();
            var release = new ReleaseResource {
                Version = "1.0.0"
            };
            var releases = new ResourceCollection <ReleaseResource>(new[] { release }, new LinkCollection());
            var deploymentPromotionTarget = new DeploymentPromotionTarget {
                Name = "TestEnvironment"
            };
            var promotionTargets = new List <DeploymentPromotionTarget> {
                deploymentPromotionTarget
            };
            var deploymentTemplate = new DeploymentTemplateResource {
                PromoteTo = promotionTargets
            };
            var deploymentPreviewResource = new DeploymentPreviewResource {
                StepsToExecute = new List <DeploymentTemplateStep>()
            };
            var deployment = new DeploymentResource {
                TaskId = "1"
            };

            taskResource = new TaskResource();

            Repository.Projects.FindByName(ProjectName).Returns(project);
            Repository.Projects.GetReleases(project).Returns(releases);
            Repository.Releases.GetPreview(deploymentPromotionTarget).Returns(deploymentPreviewResource);
            Repository.Releases.GetTemplate(release).Returns(deploymentTemplate);
            Repository.Deployments.Create(Arg.Any <DeploymentResource>()).Returns(deployment);
            Repository.Tasks.Get(deployment.TaskId).Returns(taskResource);
        }
Esempio n. 2
0
        public void SetUp()
        {
            deployReleaseCommand = new DeployReleaseCommand(RepositoryFactory, FileSystem, ClientFactory, CommandOutputProvider);

            var project = new ProjectResource();
            var release = new ReleaseResource {
                Version = "1.0.0"
            };
            var releases = new ResourceCollection <ReleaseResource>(new[] { release }, new LinkCollection());
            var deploymentPromotionTarget = new DeploymentPromotionTarget {
                Name = ValidEnvironment, Id = "Env-1"
            };
            var promotionTargets = new List <DeploymentPromotionTarget> {
                deploymentPromotionTarget
            };
            var tenantPromotionTarget1 = new DeploymentPromomotionTenant()
            {
                Id = "Tenant-1", PromoteTo = promotionTargets
            };
            var tenantPromotionTarget2 = new DeploymentPromomotionTenant()
            {
                Id = "Tenant-2", PromoteTo = new List <DeploymentPromotionTarget>()
            };
            var deploymentTemplate = new DeploymentTemplateResource {
                PromoteTo = promotionTargets, TenantPromotions = { tenantPromotionTarget1, tenantPromotionTarget2 }
            };
            var deploymentPreviewResource = new DeploymentPreviewResource {
                StepsToExecute = new List <DeploymentTemplateStep>()
            };
            var deployment = new DeploymentResource {
                TaskId = "Task-1"
            };

            taskResource = new TaskResource()
            {
                Id = "Task-1"
            };

            Repository.Projects.FindByName(ProjectName).Returns(project);
            Repository.Projects.GetReleases(project).Returns(releases);
            Repository.Releases.GetPreview(deploymentPromotionTarget).Returns(deploymentPreviewResource);
            Repository.Releases.GetTemplate(release).Returns(deploymentTemplate);
            Repository.Deployments.Create(Arg.Any <DeploymentResource>()).Returns(deployment);
            Repository.Tasks.Get(deployment.TaskId).Returns(taskResource);
            Repository.Tenants.Get(Arg.Is <string[]>(arg => arg.All(arg2 => arg2 == "Tenant-1" || arg2 == "Tenant-2")))
            .Returns(new List <TenantResource>()
            {
                new TenantResource()
                {
                    Id = "Tenant-1"
                },
                new TenantResource()
                {
                    Id = "Tenant-2"
                },
            });
        }
Esempio n. 3
0
        private async Task <List <TenantResource> > GetTenants(ProjectResource project, string environmentName, ReleaseResource release,
                                                               DeploymentTemplateResource releaseTemplate)
        {
            if (!Tenants.Any() && !TenantTags.Any())
            {
                return(new List <TenantResource>());
            }

            var deployableTenants = new List <TenantResource>();

            if (Tenants.Contains("*"))
            {
                var tenantPromotions = releaseTemplate.TenantPromotions.Where(
                    tp => tp.PromoteTo.Any(
                        promo => promo.Name.Equals(environmentName, StringComparison.CurrentCultureIgnoreCase))).Select(tp => tp.Id).ToArray();

                var tentats = await Repository.Tenants.Get(tenantPromotions).ConfigureAwait(false);

                deployableTenants.AddRange(tentats);

                Log.Information("Found {NumberOfTenants} Tenants who can deploy {Project:l} {Version:l} to {Environment:l}", deployableTenants.Count, project.Name, release.Version, environmentName);
            }
            else
            {
                if (Tenants.Any())
                {
                    var tenantsByName = await Repository.Tenants.FindByNames(Tenants).ConfigureAwait(false);

                    var missing = tenantsByName == null || !tenantsByName.Any()
                        ? Tenants.ToArray()
                        : Tenants.Except(tenantsByName.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToArray();

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

                    missing = tenantsById == null || !tenantsById.Any()
                        ? 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.");
                    }

                    deployableTenants.AddRange(tenantsByName);
                    deployableTenants.AddRange(tenantsById);

                    var unDeployableTenants =
                        deployableTenants.Where(dt => !dt.ProjectEnvironments.ContainsKey(project.Id))
                        .Select(dt => $"'{dt.Name}'")
                        .ToList();
                    if (unDeployableTenants.Any())
                    {
                        throw new CommandException(
                                  string.Format(
                                      "Release '{0}' of project '{1}' cannot be deployed for tenant{2} {3}. This may be because either a) {4} not connected to this project, or b) you do not have permission to deploy {5} to this project.",
                                      release.Version,
                                      project.Name,
                                      unDeployableTenants.Count == 1 ? "" : "s",
                                      string.Join(" or ", unDeployableTenants),
                                      unDeployableTenants.Count == 1 ? "it is" : "they are",
                                      unDeployableTenants.Count == 1 ? "it" : "them"));
                    }

                    unDeployableTenants = deployableTenants.Where(dt =>
                    {
                        var tenantPromo = releaseTemplate.TenantPromotions.FirstOrDefault(tp => tp.Id == dt.Id);
                        return(tenantPromo == null ||
                               !tenantPromo.PromoteTo.Any(
                                   tdt => tdt.Name.Equals(environmentName, StringComparison.CurrentCultureIgnoreCase)));
                    }).Select(dt => $"'{dt.Name}'").ToList();
                    if (unDeployableTenants.Any())
                    {
                        throw new CommandException(
                                  string.Format(
                                      "Release '{0}' of project '{1}' cannot be deployed for tenant{2} {3} to environment '{4}'. This may be because a) the tenant{2} {5} not connected to this environment, a) the environment does not exist or is misspelled, b) The lifecycle has not reached this phase, possibly due to previous deployment failure,  c) you don't have permission to deploy to this environment, d) the environment is not in the list of environments defined by the lifecycle, or e) {6} unable to deploy to this channel.",
                                      release.Version,
                                      project.Name,
                                      unDeployableTenants.Count == 1 ? "" : "s",
                                      string.Join(" or ", unDeployableTenants),
                                      environmentName,
                                      unDeployableTenants.Count == 1 ? "is" : "are",
                                      unDeployableTenants.Count == 1 ? "it is" : "they are"));
                    }
                }

                if (TenantTags.Any())
                {
                    var tenantsByTag = await Repository.Tenants.FindAll(null, TenantTags.ToArray()).ConfigureAwait(false);

                    var deployableByTag = tenantsByTag.Where(dt =>
                    {
                        var tenantPromo = releaseTemplate.TenantPromotions.FirstOrDefault(tp => tp.Id == dt.Id);
                        return(tenantPromo != null && tenantPromo.PromoteTo.Any(tdt => tdt.Name.Equals(environmentName, StringComparison.CurrentCultureIgnoreCase)));
                    }).Where(tenant => !deployableTenants.Any(deployable => deployable.Id == tenant.Id));
                    deployableTenants.AddRange(deployableByTag);
                }
            }

            if (!deployableTenants.Any())
            {
                throw new CommandException(
                          string.Format(
                              "No tenants are available to be deployed for release '{0}' of project '{1}' to environment '{2}'.  This may be because a) No tenants matched the tags provided b) The tenants that do match are not connected to this project or environment, c) The tenants that do match are not yet able to release to this lifecycle phase, or d) you do not have the appropriate deployment permissions.",
                              release.Version, project.Name, environmentName));
            }


            return(deployableTenants);
        }