Exemplo n.º 1
0
        public void Execute(Guid profileId, StackConfiguration configuration)
        {
            IAwsClient awsClient;
            if (!TryInitializeClient(profileId, out awsClient))
            {
                return;
            }

            // Go ahead and reserve some IP addresses for this stack
            // If building the stack fails, the refresh IP range job will make those IP addresses available again anyway
            IPRange ipRange = _ipRangeRepository.FindAll().Single(x => x.AwsProfileId == profileId);
            var reservedIps = ipRange.Addresses.Values.Where(x => !x.IsInUse).Take(configuration.Instances.Count());

            var index = 0;
            foreach(var ip in reservedIps)
            {
                ip.IsInUse = true;
                configuration.Instances[index].PrivateAddresses.Add(ip.Address.ToString());
                index++;
            }

            var stack = new Stack();
            foreach (var instance in configuration.Instances)
            {
                var baseImage = _baseImageRepository.Find(instance.Role.BaseImageId);
                configuration.BaseImages.Add(baseImage);
                instance.Id = Guid.NewGuid();
                instance.Tags.Add(new Tag {Name = "StackItId", Value = instance.Id.ToString()});

                _instanceRepository.Add(instance);

                stack.InstanceIds.Add(instance.Id);
            }
            // Do not change instance after this line and expect the value to be in the database!

            _ipRangeRepository.Update(ipRange);

            object templateObj = GetTemplate(configuration);
            var templateString = JsonConvert.SerializeObject(templateObj);

            awsClient.StackService.CreateStack(configuration.StackName, templateString);

            // Create a bare-bones stack in the database with the values that are not stored in AWS
            stack.NeedsRefreshing = true;
            stack.OwnerProfileId = configuration.OwnerProfileId;
            stack.OwnerUserName = configuration.OwnerUserName;
            stack.CreateTime = DateTime.UtcNow;
            stack.Name = configuration.StackName;
            stack.Notes = configuration.Notes;
            stack.CreatedByApplication = true;
            stack.ScheduleEnabled = configuration.ScheduleEnabled;

            var selectedSchedule = _scheduleRepository.Find(configuration.ScheduleId);
            stack.ScheduleId = selectedSchedule.Id;

            _stackRepository.Add(stack);

            _stackPowerKickstarter.KickstartSchedule(stack);
        }
Exemplo n.º 2
0
        private static object GetTemplate(StackConfiguration configuration)
        {
            var script = Resources.GenerateCloudFormationTemplate;

            var func = Edge.Func(script);

            return(func(configuration).Result);
        }
Exemplo n.º 3
0
        public void Execute(Guid profileId, StackConfiguration configuration)
        {
            IAwsClient awsClient;

            if (!TryInitializeClient(profileId, out awsClient))
            {
                return;
            }

            // Go ahead and reserve some IP addresses for this stack
            // If building the stack fails, the refresh IP range job will make those IP addresses available again anyway
            IPRange ipRange     = _ipRangeRepository.FindAll().Single(x => x.AwsProfileId == profileId);
            var     reservedIps = ipRange.Addresses.Values.Where(x => !x.IsInUse).Take(configuration.Instances.Count());

            var index = 0;

            foreach (var ip in reservedIps)
            {
                ip.IsInUse = true;
                configuration.Instances[index].PrivateAddresses.Add(ip.Address.ToString());
                index++;
            }

            var stack = new Stack();

            foreach (var instance in configuration.Instances)
            {
                var baseImage = _baseImageRepository.Find(instance.Role.BaseImageId);
                configuration.BaseImages.Add(baseImage);
                instance.Id = Guid.NewGuid();
                instance.Tags.Add(new Tag {
                    Name = "StackItId", Value = instance.Id.ToString()
                });

                _instanceRepository.Add(instance);

                stack.InstanceIds.Add(instance.Id);
            }
            // Do not change instance after this line and expect the value to be in the database!

            _ipRangeRepository.Update(ipRange);

            object templateObj    = GetTemplate(configuration);
            var    templateString = JsonConvert.SerializeObject(templateObj);

            awsClient.StackService.CreateStack(configuration.StackName, templateString);

            // Create a bare-bones stack in the database with the values that are not stored in AWS
            stack.NeedsRefreshing      = true;
            stack.OwnerProfileId       = configuration.OwnerProfileId;
            stack.OwnerUserName        = configuration.OwnerUserName;
            stack.CreateTime           = DateTime.UtcNow;
            stack.Name                 = configuration.StackName;
            stack.Notes                = configuration.Notes;
            stack.CreatedByApplication = true;
            stack.ScheduleEnabled      = configuration.ScheduleEnabled;

            var selectedSchedule = _scheduleRepository.Find(configuration.ScheduleId);

            stack.ScheduleId = selectedSchedule.Id;

            _stackRepository.Add(stack);

            _stackPowerKickstarter.KickstartSchedule(stack);
        }
Exemplo n.º 4
0
        private static object GetTemplate(StackConfiguration configuration)
        {
            var script = Resources.GenerateCloudFormationTemplate;

            var func = Edge.Func(script);

            return func(configuration).Result;
        }
Exemplo n.º 5
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"));
        }
            public static StackConfiguration FromHashTable(Hashtable t)
            {
                StackConfiguration RetVal = new StackConfiguration();

                if (t.Contains("outputpath")) RetVal.outputpath = (string)t["outputpath"];
                if (t.Contains("platform")) RetVal.TargetPlatform = (PLATFORMS)(int)t["platform"];
                if (t.Contains("projectname")) RetVal.projectname = (string)t["projectname"];
                if (t.Contains("newline")) RetVal.newline = (NEWLINETYPE)(int)t["newline"];
                if (t.Contains("callconvention")) RetVal.callconvention = (CALLINGCONVENTION)(int)t["callconvention"];
                if (t.Contains("prefixlib")) RetVal.prefixlib = (string)t["prefixlib"];
                if (t.Contains("indent")) RetVal.indent = (INDENTATION)(int)t["indent"];
                if (t.Contains("classname")) RetVal.classname = (string)t["classname"];
                if (t.Contains("ExplicitErrorEncoding")) { RetVal.ExplicitErrorEncoding = (bool)t["ExplicitErrorEncoding"]; }
                if (t.Contains("ExternCallbacks")) { RetVal.EXTERN_Callbacks = (bool)t["ExternCallbacks"]; }
                if (t.Contains("DefaultIPAddressMonitor")) { RetVal.DefaultIPAddressMonitor = (bool)t["DefaultIPAddressMonitor"]; }
                if (t.Contains("HTTP11Support")) { RetVal.HTTP_1dot1 = (bool)t["HTTP11Support"]; }
                if (t.Contains("SupressSample")) { RetVal.SupressSampleProject = (bool)t["SupressSample"]; }
                if (t.Contains("UPnP1dot1Enabled")) { RetVal.UPNP_1dot1 = (bool)t["UPnP1dot1Enabled"]; }
                if (t.Contains("XSDSchemaGeneration")) { RetVal.SchemaGeneration = (XSDSchemaGeneration)(int)t["XSDSchemaGeneration"]; }
                if (t.Contains("GenerateThreadPoolLibrary")) { RetVal.GenerateThreadPoolLibrary = (bool)t["GenerateThreadPoolLibrary"]; }
                if (t.Contains("InitThreadPoolInSampleApp")) { RetVal.InitThreadPoolInSampleApp = (bool)t["InitThreadPoolInSampleApp"]; }
                if (t.Contains("ThreadPoolThreads_InSampleApp")) { RetVal.ThreadPoolThreads_InSampleApp = (int)t["ThreadPoolThreads_InSampleApp"]; }
                if (t.Contains("DynamicObjectModel")) { RetVal.DynamicObjectModel = (bool)t["DynamicObjectModel"]; }
                if (t.Contains("CPlusPlusWrapper")) { RetVal.CPlusPlusWrapper = (bool)t["CPlusPlusWrapper"]; }
                if (t.Contains("BareBonesSample")) { RetVal.BareBonesSample = (bool)t["BareBonesSample"]; }
                if (t.Contains("MaxHTTPHeaderSize")) { RetVal.MaxHTTPHeaderSize = (int)t["MaxHTTPHeaderSize"]; }
                if (t.Contains("MaxHTTPPacketSize")) { RetVal.MaxHTTPPacketSize = (int)t["MaxHTTPPacketSize"]; }
                if (t.Contains("InitialHTTPBufferSize")) { RetVal.InitialHTTPBufferSize = (int)t["InitialHTTPBufferSize"]; }

                return (RetVal);
            }