コード例 #1
0
        public void AddBlockDeviceMapping(string deviceName, uint volumeSize, Ebs.VolumeTypes volumeType)
        {
            BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping(this, deviceName);

            blockDeviceMapping.Ebs.VolumeSize = volumeSize;
            blockDeviceMapping.Ebs.VolumeType = volumeType;
            this.BlockDeviceMappings.Add(blockDeviceMapping);
        }
コード例 #2
0
 /// <summary>
 /// Configures the server to boot from an existing volume.
 /// </summary>
 /// <param name="volumeId">The volume identifier.</param>
 /// <param name="deleteVolumeWithServer">if set to <c>true</c> [delete volume with server].</param>
 public void ConfigureBootFromVolume(Identifier volumeId, bool deleteVolumeWithServer = false)
 {
     BlockDeviceMapping.Add(new ServerBlockDeviceMapping
     {
         SourceType = ServerBlockDeviceType.Volume,
         SourceId = volumeId,
         BootIndex = 0,
         DeleteWithServer = deleteVolumeWithServer
     });
 }
コード例 #3
0
 /// <summary>
 /// Configures the server to boot from a new volume, copied from the base server image.
 /// </summary>
 /// <param name="volumeSize">Size of the volume.</param>
 /// <param name="deleteVolumeWithServer">if set to <c>true</c> [delete volume with server].</param>
 public void ConfigureBootFromNewVolume(int volumeSize, bool deleteVolumeWithServer = false)
 {
     BlockDeviceMapping.Add(new ServerBlockDeviceMapping
     {
         SourceType = ServerBlockDeviceType.Image,
         SourceId = ImageId,
         BootIndex = 0,
         DestinationType = ServerBlockDeviceType.Volume,
         DestinationVolumeSize = volumeSize,
         DeleteWithServer = deleteVolumeWithServer
     });
 }
コード例 #4
0
        public BlockDeviceMapping AddDisk(Ebs.VolumeTypes ec2DiskType,
                                          uint sizeInGigabytes,
                                          string deviceId,
                                          bool deleteOnTermination)
        {
            BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping(this, deviceId);

            blockDeviceMapping.Ebs.VolumeSize          = sizeInGigabytes;
            blockDeviceMapping.Ebs.VolumeType          = ec2DiskType;
            blockDeviceMapping.Ebs.DeleteOnTermination = deleteOnTermination;
            this.BlockDeviceMappings.Add(blockDeviceMapping);
            return(blockDeviceMapping);
        }
        public IOfferAwsImageCreateOptions Add(string deviceName, Action <IOfferAwsBootstrapEbsOptions> ebs, string deviceToSuppressFromImage = null)
        {
            var blockDevice = new BlockDeviceMapping
            {
                DeviceName = deviceName,
                NoDevice   = deviceToSuppressFromImage,
                Ebs        = new EbsBlockDevice()
            };

            var ebsValues = new AwsBootstrapEbsOptionsBuilder(blockDevice.Ebs);

            ebs(ebsValues);

            _values.Add(blockDevice);
            return(_options);
        }
コード例 #6
0
        public virtual void AddToLaunchConfiguration(LaunchConfiguration configuration)
        {
            this.Instance = configuration;

            if (!string.IsNullOrEmpty(this.SnapshotId))
            {
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping(this.Instance,
                                                                               this.Instance.GetAvailableDevice());
                blockDeviceMapping.Ebs.SnapshotId = this.SnapshotId;
                this.Instance.BlockDeviceMappings.Add(blockDeviceMapping);
            }
            if (this.Msi != null)
            {
                var fileName  = System.IO.Path.GetFileNameWithoutExtension(Msi.AbsolutePath).Replace(".", string.Empty).Replace("-", String.Empty);
                var configSet = this.Config;
                if (!configSet.Packages.ContainsKey("msi"))
                {
                    var msi = new CloudFormationDictionary();
                    msi.Add(fileName, Msi.AbsoluteUri);
                    configSet.Packages.Add("msi", msi);
                }
            }
            if (!string.IsNullOrEmpty(this.BucketName))
            {
                var    appSettingsReader = new AppSettingsReader();
                string accessKeyString   = (string)appSettingsReader.GetValue("S3AccessKey", typeof(string));
                string secretKeyString   = (string)appSettingsReader.GetValue("S3SecretKey", typeof(string));

                if (!configuration.Metadata.Authentication.ContainsKey("S3AccessCreds"))
                {
                    var auth = configuration.Metadata.Authentication.Add("S3AccessCreds",
                                                                         new S3Authentication(accessKeyString, secretKeyString, new string[] { BucketName }));
                    auth.Type = "S3";
                }
            }
        }
コード例 #7
0
        internal void LaunchServer(ref bool actionSucceeded, ref string actionMessage)
        {
            try
            {
                string keyPairPath = string.Empty;
                LaunchRequest.KeyPairName = $"KeyPair-{Guid.NewGuid().ToString()}";
                while (!CreateKeyPair(LaunchRequest.KeyPairName, ref keyPairPath))
                {
                    LaunchRequest.KeyPairName = Guid.NewGuid().ToString();
                }

                DescribeVpcsRequest  vpcRequest  = new DescribeVpcsRequest();
                DescribeVpcsResponse vpcResponse = Ec2Client.DescribeVpcsAsync(vpcRequest).GetAwaiter().GetResult();

                Vpc defaultVPC = vpcResponse.Vpcs.Find(x => x.IsDefault); //get the default vpc

                List <Filter> subnetFilter = new List <Filter>()
                {
                    new Filter()
                    {
                        Name = "availability-zone", Values = new List <string>()
                        {
                            LaunchRequest.AvailabilityZone
                        }
                    },
                    new Filter()
                    {
                        Name = "vpc-id", Values = new List <string>()
                        {
                            defaultVPC.VpcId
                        }
                    }
                };

                DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest();
                subnetRequest.Filters = subnetFilter;
                DescribeSubnetsResponse subnetResponse = Ec2Client.DescribeSubnetsAsync(subnetRequest).GetAwaiter().GetResult();
                Subnet defaultSubnet = subnetResponse.Subnets.FirstOrDefault();

                Filter SGFilter = new Filter
                {
                    Name   = "vpc-id",
                    Values = new List <string>()
                    {
                        defaultVPC.VpcId
                    }
                };

                DescribeSecurityGroupsRequest SGrequest = new DescribeSecurityGroupsRequest();
                SGrequest.Filters.Add(SGFilter);
                DescribeSecurityGroupsResponse SGresponse = Ec2Client.DescribeSecurityGroupsAsync(SGrequest).GetAwaiter().GetResult();
                SecurityGroup defaultSG = SGresponse.SecurityGroups.FirstOrDefault();

                InstanceNetworkInterfaceSpecification defaultENI = new InstanceNetworkInterfaceSpecification()
                {
                    DeviceIndex = 0,
                    SubnetId    = defaultSubnet.SubnetId,
                    Groups      = new List <string>()
                    {
                        defaultSG.GroupId
                    },
                    AssociatePublicIpAddress = true
                };

                List <InstanceNetworkInterfaceSpecification> enis = new List <InstanceNetworkInterfaceSpecification>()
                {
                    defaultENI
                };

                EbsBlockDevice ebsBlockDevice = new EbsBlockDevice
                {
                    VolumeSize = 10,
                    VolumeType = GetActualStorageType(LaunchRequest.StorageType)
                };
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping
                {
                    DeviceName = "/dev/xvda"
                };
                blockDeviceMapping.Ebs = ebsBlockDevice;

                var launchRequest = new RunInstancesRequest()
                {
                    ImageId             = GetImageID(LaunchRequest.AMIType),
                    InstanceType        = GetActualInstanceType(LaunchRequest.InstanceType),
                    MinCount            = LaunchRequest.NumOfInstances,
                    MaxCount            = LaunchRequest.NumOfInstances,
                    KeyName             = LaunchRequest.KeyPairName,
                    Placement           = new Placement(LaunchRequest.AvailabilityZone),
                    NetworkInterfaces   = enis,
                    BlockDeviceMappings = new List <BlockDeviceMapping>()
                    {
                        blockDeviceMapping
                    }
                };

                RunInstancesResponse launchResponse = Ec2Client.RunInstancesAsync(launchRequest).GetAwaiter().GetResult();

                List <String> instanceIds = new List <string>();
                foreach (Instance instance in launchResponse.Reservation.Instances)
                {
                    Console.WriteLine(instance.InstanceId);
                    instanceIds.Add(instance.InstanceId);
                }

                actionSucceeded = true;
                actionMessage   = $"The instance(s) are being launched. Please check the AWS Console to verify. {keyPairPath}";
            }
            catch (Exception ex)
            {
                context.Logger.LogLine($"ServerOperationsHelper::LaunchServer {ex.Message}");
                context.Logger.LogLine($"ServerOperationsHelper::LaunchServer {ex.StackTrace}");
                actionSucceeded = false;
                actionMessage   = $"Could not launch the server . Please contact your administrator.";
            }
        }
コード例 #8
0
        //###############################################################

        public string AddVolume(IAmazonEC2 ec2, string instidstr, string name, int size)
        {
https:      //docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TCreateVolumeRequest.html


            try
            {
                ModifyInstanceAttributeRequest  ModifyRequest  = new ModifyInstanceAttributeRequest();
                ModifyInstanceAttributeResponse ModifyResponse = new ModifyInstanceAttributeResponse();


                ModifyRequest.InstanceId = instidstr;


                BlockDeviceMapping mapping = new BlockDeviceMapping
                {
                    DeviceName = name,

                    Ebs = new EbsBlockDevice
                    {
                        VolumeType = VolumeType.Gp2,
                        VolumeSize = size
                    }
                };

                List <BlockDeviceMapping> mappinglist;
                mappinglist = new List <BlockDeviceMapping>();
                mappinglist.Add(mapping);


                List <InstanceBlockDeviceMappingSpecification> BDMSpecList;
                BDMSpecList = new List <InstanceBlockDeviceMappingSpecification>();

                InstanceBlockDeviceMappingSpecification BDMSpec = new InstanceBlockDeviceMappingSpecification
                {
                    DeviceName = name,


                    Ebs = new EbsInstanceBlockDeviceSpecification
                    {
                        VolumeId = "sdsd"

                                   //VolumeType = VolumeType.Gp2,
                                   // VolumeSize = size
                    }
                };



                //= mapping;
                BDMSpecList.Add(BDMSpec);
                //ModifyRequest.Attribute.Value.
                //--- ModifyRequest.BlockDeviceMappings.Add(BDMSpecList);

                return("Done");
            }
            catch (Exception ex)
            {
                return(ex.ToString());
            }


            //launchRequest.BlockDeviceMappings = mappinglist;
        }
コード例 #9
0
        //############################################################################################

        public string lunchInstance(IAmazonEC2 ec2, string ami, string subnetid, string Key, string insttype, List <string> secgroupid, bool publicip, string vpc, string NewSG, string privateIP, string Nametag, int rootDiskSize)
        //create new instance
        {
            RunInstancesResponse launchResponse;
            string subnetID    = subnetid; // "subnet-2e107b76";
            string amiID       = ami;      // "ami-c51e3eb6";
            string keyPairName = Key;      // "sirin-staging";
            string itype       = insttype; // "t2.small";

            // List<string> groups = new List<string>() { "sg-9f90e2f9" };
            List <string> groups = secgroupid; // new List<string>() { secgroupid };

            if (NewSG.Length > 0)              // create and add a new security group
            {
                try
                {
                    //SG.GroupName = "AccessGP";
                    SecurityGroup SG = new SecurityGroup();
                    SG = CreateSecurityGroup(ec2, NewSG, vpc);



                    groups.Add(SG.GroupId);
                }
                catch (Exception ex)
                {
                    return(ex.Message + "\n" + ex.StackTrace);
                }
            }
            try
            {
                var eni = new InstanceNetworkInterfaceSpecification()
                {
                    DeviceIndex = 0,
                    SubnetId    = subnetID,
                    Groups      = groups,
                    AssociatePublicIpAddress = publicip
                };
                if (privateIP.Length > 0)
                {
                    eni.PrivateIpAddress = privateIP;
                    // eni.PrivateIpAddress = "10.1.200.200";
                }



                List <InstanceNetworkInterfaceSpecification> enis = new List <InstanceNetworkInterfaceSpecification>()
                {
                    eni
                };

                var launchRequest = new RunInstancesRequest()
                {
                    ImageId           = amiID,
                    InstanceType      = itype,
                    MinCount          = 1,
                    MaxCount          = 1,
                    KeyName           = keyPairName,
                    NetworkInterfaces = enis

                                        // TagSpecifications = "protectorx"
                };

                if (rootDiskSize != 0)
                {
                    BlockDeviceMapping mapping = new BlockDeviceMapping
                    {
                        DeviceName = "/dev/sda1",
                        Ebs        = new EbsBlockDevice
                        {
                            VolumeType = VolumeType.Gp2,
                            VolumeSize = rootDiskSize
                        }
                    };

                    List <BlockDeviceMapping> mappinglist;
                    mappinglist = new List <BlockDeviceMapping>();
                    mappinglist.Add(mapping);
                    launchRequest.BlockDeviceMappings = mappinglist;
                }


                // ------------------------- add tag ----------------------------------------------------

                List <Amazon.EC2.Model.Tag> TagList;
                TagSpecification            Tagspec;

                Amazon.EC2.Model.Tag EC2tag;

                //Amazon.EC2.Model.resorc
                EC2tag       = new Amazon.EC2.Model.Tag();
                EC2tag.Key   = "Name";
                EC2tag.Value = Nametag;
                //EC2tag.GetType

                TagList = new List <Amazon.EC2.Model.Tag>();
                TagList.Add(EC2tag);

                // EC2tag();



                Tagspec = new Amazon.EC2.Model.TagSpecification();
                Tagspec.ResourceType = ResourceType.Instance;
                //TagSpecification(){ResourceType.Instance};
                //Tagspec.Tags.OfType<Instance>;
                Tagspec.Tags = TagList;


                launchRequest.TagSpecifications.Add(Tagspec);
                //ec2.RunInstances(launchRequest);
                launchResponse = ec2.RunInstances(launchRequest);
                return("Done");
            }
            catch (Exception ex)
            {
                return(ex.Message + "\n" + ex.StackTrace);
            }
        }