RunInstances() private method

private RunInstances ( RunInstancesRequest request ) : RunInstancesResponse
request RunInstancesRequest
return RunInstancesResponse
コード例 #1
0
        public static Ec2Response StartServer(DeveloperOptions developerOptions)
        {
            try
            {
                var ec2Config = new AmazonEC2Config { AuthenticationRegion = developerOptions.RegionEndpont };

                var ec2Client = new AmazonEC2Client(
                    new BasicAWSCredentials(developerOptions.AccessKey, developerOptions.SecretAccessKey), ec2Config);

                var launchRequest = new RunInstancesRequest
                {
                    ImageId = developerOptions.AmiId,
                    InstanceType = developerOptions.InstanceType,
                    MinCount = 1,
                    MaxCount = 1,
                    KeyName = developerOptions.Ec2KeyPair,
                    SecurityGroupIds = new List<string> { developerOptions.SecurityGroupId }
                };
                var launchResponse = ec2Client.RunInstances(launchRequest);
                if (launchResponse.HttpStatusCode.Equals(HttpStatusCode.OK))
                {
                    while (true)
                    {
                        var instances = ec2Client.DescribeInstances();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            // TODO
            return null;
        }
コード例 #2
0
        public List<string> AssignAgentIps(AmazonEC2Client ec2Client, TestSuite testSuite)
        {
            int requiredInstances = testSuite.Tests.Count(testToGiveIp => string.IsNullOrWhiteSpace(testToGiveIp.Agent));

            if (requiredInstances > 0)
            {
                var runInstancesRequest = new RunInstancesRequest
                {
                    ImageId = "ami-df844ba8",
                    InstanceType = "t1.micro",
                    MinCount = requiredInstances,
                    MaxCount = requiredInstances,
                    KeyName = "Fourth"
                };
                runInstancesRequest.SecurityGroups.Add("Controller");
                RunInstancesResponse runResponse = ec2Client.RunInstances(runInstancesRequest);
                List<Instance> instances = runResponse.Reservation.Instances;
                List<string> instanceIDs = instances.Select(item => item.InstanceId).ToList();
                WaitForInstancesToBeRunning(ec2Client, instanceIDs);
                var instancesRequest = new DescribeInstancesRequest {InstanceIds = instanceIDs};
                DescribeInstancesResponse statusResponse = ec2Client.DescribeInstances(instancesRequest);
                List<string> ipAddresses =
                    statusResponse.Reservations[0].Instances.Select(x => x.PublicIpAddress).ToList();
                //we now have our running instances and we need to assign the ips to our tests
                foreach (Test test in testSuite.Tests.Where(test => string.IsNullOrWhiteSpace(test.Agent)))
                {
                    //assign the first free Id
                    test.Agent = ipAddresses.First();
                    //then remove it from the list
                    ipAddresses.RemoveAt(0);
                }

                //now we need to make sure all instances are ready
                MakeSureAgentsCanBeUsed(ec2Client, instanceIDs);
                return instanceIDs;
            }
            return null;
        }
コード例 #3
0
        public void LaunchInstance(string instanceName, InstanceSize size, string metadata)
        {
            var ec2Client = new AmazonEC2Client(_awsCredentials);

            string instanceType = "t1.micro";
            // WEB: string imageId = "ami-a9da0ec0";
            // Compute: string imageId = "ami-443fe32d";
            string imageId = "ami-09d96e60";

            switch (size)
            {
                case InstanceSize.Medium:
                    instanceType = "t1.small";
                    break;

                case InstanceSize.Small:
                default:
                    instanceType = "t1.micro";
                    break;
            }

            var request = new RunInstancesRequest()
                .WithInstanceType(instanceType)
                .WithPlacement(new Placement().WithAvailabilityZone("us-east-1d"))
                .WithImageId(imageId)
                .WithMinCount(1)
                .WithMaxCount(1)
                .WithSecurityGroup("default")
                .WithKeyName("IgorTest3");

            if (!string.IsNullOrEmpty(metadata))
                request = request.WithUserData(Convert.ToBase64String(Encoding.UTF8.GetBytes(metadata)));

            var runInstancesResponse = ec2Client.RunInstances(request);

            var instances = runInstancesResponse.RunInstancesResult.Reservation.RunningInstance;
            var index = 0;
            foreach (var instance in instances)
            {
                var name = instanceName;

                if (instances.Count > 0)
                    instanceName = instanceName + index;

                var createTagsRequest = new CreateTagsRequest();
                createTagsRequest
                    .WithResourceId(instance.InstanceId)
                    .WithTag(new Tag().WithKey("Name")
                    .WithValue(name));
                ec2Client.CreateTags(createTagsRequest);

                index++;
            }

            /*
            string rsaPrivateKey;
            using (var reader = new StreamReader(@"C:\Dev\BlueMetal\CloudAbstractions\SecurityStorage\IgorKeyPair.pem"))
            {
                rsaPrivateKey = reader.ReadToEnd();
            }
            var result = ec2Client.GetPasswordData(
                        new GetPasswordDataRequest().WithInstanceId(instanceId))
                .GetPasswordDataResult;

            Console.WriteLine(result.GetDecryptedPassword(rsaPrivateKey));
            */

            /*
                        AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(
                          appConfig["AWSAccessKey"],
                          appConfig["AWSSecretKey"],
                          new AmazonEC2Config().WithServiceURL("https://eu-west-1.ec2.amazonaws.com")
                          );

                        DescribeInstancesRequest ec2Request = new DescribeInstancesRequest();
            */
        }