Пример #1
0
        public ActionResult Create()
        {
            Guid profileId = this.GetActiveProfileId();

            var profile = _profileRepository.Find(profileId);

            List <Product> products  = _productRepository.FindAll().ToList();
            var            schedules = _scheduleRepository.FindAll();

            var model = new CreateStackViewModel
            {
                SelectedProfileId = profileId,
                Products          = products,
                SelectedVpcId     = profile.DefaultVpcId,
                SelectedSubnetId  = profile.DefaultSubnetId,
                InstanceTypes     = _stackItConfiguration.InstanceTypes.Select(x => x.Name).ToList(),
                Schedules         = schedules
                                    .OrderByDescending(x => x.GlobalDefault)
                                    .ThenBy(x => x.Name)
                                    .Select(x => new ScheduleViewModel {
                    Id = x.Id, Name = x.Name
                })
                                    .ToList(),
                ScheduleEnabled = true
            };

            return(View(model));
        }
Пример #2
0
        public ActionResult CreateStack(CreateStackViewModel viewModel)
        {
            // If the user doesn't have permission to the selected profile, fail early
            string currentUserName             = _owinContext.Authentication.User.Identity.Name;
            var    userProfiles                = _userProfileAccessManager.GetProfilesForUser(currentUserName).ToList();
            var    selectedProfile             = userProfiles.FirstOrDefault(x => x.Id == viewModel.SelectedProfileId);
            bool   userHasPermissionForProfile = selectedProfile != null;

            if (!userHasPermissionForProfile)
            {
                throw new InvalidOperationException("User does not have permission to use this profile.");
            }

            var stackComponentDefinition = viewModel.SelectedProductIds
                                           .Zip(viewModel.SelectedVersionNames, (productId, versionName) => new { productId, versionName })
                                           .Zip(viewModel.SelectedRoleNames, (prodVer, roleName) => new { prodVer.productId, prodVer.versionName, roleName })
                                           .Zip(viewModel.Options, (prodVerRole, options) => new { prodVerRole.productId, prodVerRole.versionName, prodVerRole.roleName, options })
                                           .Select(x => new { x.productId, x.versionName, x.roleName, x.options }).ToList();

            var configuration = new StackConfiguration
            {
                StackName              = viewModel.StackName,
                OwnerProfileId         = viewModel.SelectedProfileId,
                OwnerUserName          = currentUserName,
                VpcId                  = viewModel.SelectedVpcId,
                SubnetId               = viewModel.SelectedSubnetId,
                HostedZone             = userProfiles.Single(x => x.Id == viewModel.SelectedProfileId).HostedZone,
                BootstrapperUrl        = _stackItConfiguration.PuppetConfiguration.BootstrapperUrl,
                PuppetInstallerUrl     = _stackItConfiguration.PuppetConfiguration.PuppetInstallerUrl,
                PuppetHost             = _stackItConfiguration.PuppetConfiguration.PuppetHost,
                DefaultSecurityGroupId = selectedProfile.DefaultSecurityGroupId,
                Notes                  = viewModel.Notes,
                ScheduleId             = viewModel.SelectedScheduleId,
                ScheduleEnabled        = viewModel.ScheduleEnabled
            };

            foreach (var entry in stackComponentDefinition)
            {
                var     scopedEntry = entry;
                Product product     = _productRepository.Find(scopedEntry.productId);
                Version version     = product.Versions.Single(x => x.Name == scopedEntry.versionName);
                Role    role        = version.Roles.Single(x => x.Name == scopedEntry.roleName);

                OverwriteRoleOptions(role, scopedEntry.options);

                // If the instance type is not whitelisted, fail.
                // Do so after the role mapping so that website defaults (i.e. when a user doesn't alter the options)
                // values can be handled first.
                if (!_stackItConfiguration.InstanceTypes.Select(x => x.Name).Contains(role.Options.InstanceType))
                {
                    throw new InvalidOperationException("Instance type not supported.");
                }

                bool   useDefaultName = scopedEntry.options == null || string.IsNullOrEmpty(scopedEntry.options.InstanceName);
                string instanceName   = useDefaultName
                                        ? string.Format("{0}{1}{2}", viewModel.StackName, product.Name, role.Name)
                                        .RemoveAllWhitespace()
                                        .RemoveNonAlphaNumericCharacters()
                                        : scopedEntry.options.InstanceName;

                instanceName = _numberedStringGenerator.GetNextString(instanceName);

                var instance = new Instance
                {
                    Name         = instanceName,
                    InstanceType = role.Options.InstanceType,
                    Role         = role,
                    Tags         = new List <Tag> {
                        new Tag {
                            Name = "Name", Value = instanceName
                        }
                    },
                    VpcId           = configuration.VpcId,
                    SubnetId        = configuration.SubnetId,
                    ProductName     = product.Name,
                    VersionName     = version.Name,
                    NeedsRefreshing = true,
                    OwnerProfileId  = configuration.OwnerProfileId,
                    IamRole         = entry.options.IamRole ?? version.IamRole
                };

                configuration.Instances.Add(instance);
            }

            _backgroundJobClient.Enqueue <CreateStack>(x => x.Execute(viewModel.SelectedProfileId, configuration));

            return(RedirectToAction("Index"));
        }