示例#1
0
        public async Task <IHost[]> LaunchAsync(Cluster cluster, ISecurityContext context)
        {
            Validate.NotNull(cluster, nameof(cluster));
            Validate.NotNull(context, nameof(context));

            var zoneId = LocationId.Create(cluster.LocationId).WithZoneNumber(1);

            var zone = Locations.Get(zoneId);

            // TODO:
            // if the location is a region, balance hosts within available zones

            var template = await hostTemplateService.GetAsync(cluster.HostTemplateId);

            var request = new LaunchHostRequest(cluster, zone, template);

            return(await LaunchAsync(request, context));;
        }
示例#2
0
        public async Task <IHost[]> LaunchAsync(LaunchHostRequest launchRequest, ISecurityContext context)
        {
            Validate.NotNull(launchRequest, nameof(launchRequest));
            Validate.NotNull(context, nameof(context));

            var zoneId = LocationId.Create(launchRequest.Location.Id);

            if (zoneId.ZoneNumber == 0)
            {
                throw new Exception("Must launch within in availability zone. Was a region:" + launchRequest.Location.Name);
            }

            var cluster  = launchRequest.Cluster;
            var zone     = launchRequest.Location;
            var template = launchRequest.Template;

            var region = Locations.Get(zoneId.WithZoneNumber(0));

            var image = await imageService.GetAsync(template.ImageId);;

            var machineType = AwsInstanceType.Get(template.MachineTypeId);

            var request = new RunInstancesRequest {
                ClientToken       = Guid.NewGuid().ToString(),
                InstanceType      = machineType.Name,
                ImageId           = image.ResourceId,
                MinCount          = launchRequest.LaunchCount,
                MaxCount          = launchRequest.LaunchCount,
                Placement         = new Placement(availabilityZone: zone.Name),
                TagSpecifications = new[] {
                    new TagSpecification(
                        resourceType: "instance",
                        tags: new[] { new Amazon.Ec2.Tag("envId", cluster.EnvironmentId.ToString()) }
                        )
                }
            };

            var startupScript = launchRequest.StartupScript ?? template.StartupScript;

            if (startupScript != null)
            {
                request.UserData = Convert.ToBase64String(Encoding.UTF8.GetBytes(startupScript));
            }

            #region AWS Specific Properties

            foreach (var property in template.Properties)
            {
                switch (property.Key)
                {
                case HostTemplateProperties.IamRole:
                    // NOTE: This requires the PassRole permission
                    // https://aws.amazon.com/blogs/security/granting-permission-to-launch-ec2-instances-with-iam-roles-passrole-permission/

                    request.IamInstanceProfile = new IamInstanceProfileSpecification(property.Value);

                    break;

                case HostTemplateProperties.KernelId:
                    request.KernelId = property.Value;

                    break;

                case HostTemplateProperties.SecurityGroupIds:
                    request.SecurityGroupIds = property.Value.ToArrayOf <string>();

                    break;

                case HostTemplateProperties.EbsOptimized:
                    request.EbsOptimized = (bool)property.Value;

                    break;

                case HostTemplateProperties.Monitoring:
                    request.Monitoring = new RunInstancesMonitoringEnabled((bool)property.Value);

                    break;

                case HostTemplateProperties.Volume:
                    var volSpec = property.Value.As <AwsVolumeSpecification>();

                    // TODO: Device Name
                    request.BlockDeviceMappings = new[] {
                        new BlockDeviceMapping(BlockDeviceNames.Root, new EbsBlockDevice(
                                                   volumeType: volSpec.Type,
                                                   volumeSize: (int)volSpec.Size
                                                   ))
                    };

                    break;

                case HostTemplateProperties.KeyName:
                    request.KeyName = property.Value;

                    break;
                }
            }

            #endregion

            var runInstancesResponse = await ec2.RunInstancesAsync(request);;

            var hosts = new IHost[runInstancesResponse.Instances.Length];

            for (var i = 0; i < hosts.Length; i++)
            {
                var registerRequest = await GetRegistrationAsync(
                    instance : runInstancesResponse.Instances[i],
                    cluster : cluster,
                    image : image,
                    machineType : machineType,
                    location : zone
                    );

                hosts[i] = await hostService.RegisterAsync(registerRequest);;
            }

            #region Logging

            await eventLog.CreateAsync(new Event(
                                           action   : "launch",
                                           resource : "hosts",
                                           userId   : context.UserId)
                                       );;

            #endregion

            return(hosts);
        }