コード例 #1
0
        public string EnsureSecurityGroupExists(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Find the VPC ID for this region
            var vpcId = client.DescribeVpcs().Vpcs.Single().VpcId;

            // Does the security group with our preset name exist?
            var matchingGroups = client.DescribeSecurityGroups().SecurityGroups.Where(x => x.GroupName == securityGroupName).ToList();
            if (matchingGroups.Any())
            {
                // If it exists, assert that it has the same VPC ID as the one we found earlier
                if (matchingGroups.Single().VpcId != vpcId)
                {
                    throw new Exception("Security Group already exists with invalid VPC.");
                }
                return matchingGroups.Single().GroupId;
            }
            else
            {
                // It does not exist, so create it.
                return CreateSecurityGroup(client, vpcId, securityGroupName);
            }
        }
コード例 #2
0
        private void RefreshWorkers(AwsRegionLocations region)
        {
            // Use canRefreshWorkers to ensure that we don't try to refresh workers while a refresh is in progress
            if (canRefreshWorkers)
            {
                canRefreshWorkers = false;

                var regionControl = controlRegionDictionary[region];
                var instances     = regionControl.RegionDetails.Instances;

                // Go into the UI thread to clear out the current listview and re-add all the instances to it
                InstancesInRegionListView.UIThread(() =>
                {
                    InstancesInRegionListView.Items.Clear();
                    foreach (var instance in instances)
                    {
                        instance.WorkerInfo = null;
                        InstancesInRegionListView.Items.Add(instance.ToListViewItem());
                    }
                });

                var updateInstanceTasks = instances.Select(RefreshWorker);

                Task.WhenAll(updateInstanceTasks.ToArray())
                .ContinueWith(t =>
                {
                    // Once we're all done refreshing all of the workers allow the refresh to happen again
                    canRefreshWorkers = true;
                });
            }
        }
コード例 #3
0
        public static RegionEndpoint ToAwsRegionEndpoint(this AwsRegionLocations awsRegionLocation)
        {
            switch (awsRegionLocation)
            {
            case AwsRegionLocations.UsEast:
                return(RegionEndpoint.USEast1);

            case AwsRegionLocations.UsWest1:
                return(RegionEndpoint.USWest1);

            case AwsRegionLocations.UsWest2:
                return(RegionEndpoint.USWest2);

            case AwsRegionLocations.EuWest:
                return(RegionEndpoint.EUWest1);

            case AwsRegionLocations.SaEast:
                return(RegionEndpoint.SAEast1);

            case AwsRegionLocations.ApNortheast:
                return(RegionEndpoint.APNortheast1);

            case AwsRegionLocations.ApSoutheast1:
                return(RegionEndpoint.APSoutheast1);

            case AwsRegionLocations.ApSoutheast2:
                return(RegionEndpoint.APSoutheast2);

            default:
                throw new ArgumentException("Unmapped region type. Add mapping to RegionExtensions class", "awsRegionLocation");
            }
        }
コード例 #4
0
        private string FindNewestInstanceId(AwsRegionLocations region)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" state.
            request.Filters.Add(new Filter("instance-state-code", new List <string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            var instances = reservations.SelectMany(x => x.Instances);

            if (instances.Any())
            {
                var newestInstance = instances.OrderByDescending(i => i.LaunchTime).First();
                return(newestInstance.InstanceId);
            }
            return(null);
        }
コード例 #5
0
        private void CreateAndLaunchInstance(AwsRegionLocations region)
        {
            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var securityGroupId      = EnsureSecurityGroupExists(region);
            var availableSubnets     = client.DescribeSubnets().Subnets.OrderByDescending(x => x.AvailableIpAddressCount);
            var networkSpecification = new InstanceNetworkInterfaceSpecification()
            {
                DeviceIndex = 0,
                SubnetId    = availableSubnets.First().SubnetId,
                Groups      = new List <string>()
                {
                    securityGroupId
                },
                AssociatePublicIpAddress = true
            };
            var networkSpecifications = new List <InstanceNetworkInterfaceSpecification>()
            {
                networkSpecification
            };

            var launchRequest = new RunInstancesRequest()
            {
                ImageId           = GetAmiId(client, amiName),
                InstanceType      = "t2.micro",
                MinCount          = 1,
                MaxCount          = 1,
                KeyName           = keyPairName,
                NetworkInterfaces = networkSpecifications
            };

            client.RunInstances(launchRequest);
        }
コード例 #6
0
        public RegionDetails GetRegionData(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" or "Pending" state.
            request.Filters.Add(new Filter("instance-state-code", new List <string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
                ((ushort)InstanceStatuses.Pending).ToString()
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            return(new RegionDetails()
            {
                Name = regionEndpoint.SystemName,
                Region = region,
                Instances = reservations.SelectMany(x => x.Instances).Select(x => x.ToInstanceInfo()).ToList()
            });
        }
コード例 #7
0
        public RegionDetails GetRegionData(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" or "Pending" state.
            request.Filters.Add(new Filter("instance-state-code", new List<string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
                ((ushort)InstanceStatuses.Pending).ToString()
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            return new RegionDetails()
            {
                Name = regionEndpoint.SystemName,
                Region = region,
                Instances = reservations.SelectMany(x => x.Instances).Select(x => x.ToInstanceInfo()).ToList()
            };
        }
コード例 #8
0
        public string EnsureSecurityGroupExists(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Find the VPC ID for this region
            var vpcId = client.DescribeVpcs().Vpcs.Single().VpcId;

            // Does the security group with our preset name exist?
            var matchingGroups = client.DescribeSecurityGroups().SecurityGroups.Where(x => x.GroupName == securityGroupName).ToList();

            if (matchingGroups.Any())
            {
                // If it exists, assert that it has the same VPC ID as the one we found earlier
                if (matchingGroups.Single().VpcId != vpcId)
                {
                    throw new Exception("Security Group already exists with invalid VPC.");
                }
                return(matchingGroups.Single().GroupId);
            }
            else
            {
                // It does not exist, so create it.
                return(CreateSecurityGroup(client, vpcId, securityGroupName));
            }
        }
コード例 #9
0
        private void ShowRegionDetails(AwsRegionLocations region)
        {
            currentRegionBeingViewed = region;
            MainTabControl.SelectTab(1);
            RegionNameLabel.Text = controlRegionDictionary[region].RegionDetails.Name;

            RefreshWorkers(region);
        }
コード例 #10
0
 private void TerminateInstance(AwsRegionLocations region)
 {
     Task.Factory.StartNew(() => ec2Caller.TerminateInstance(region, launchedRegion =>
     {
         Thread.Sleep(1000);
         UpdateRegionControl(controlRegionDictionary[launchedRegion]);
     }));
 }
コード例 #11
0
        private void ShowRegionDetails(AwsRegionLocations region)
        {
            currentRegionBeingViewed = region;
            MainTabControl.SelectTab(1);
            RegionNameLabel.Text = controlRegionDictionary[region].RegionDetails.Name;

            RefreshWorkers(region);
        }
コード例 #12
0
 public RegionDetails GetRegionData(AwsRegionLocations region)
 {
     return new RegionDetails()
     {
         Name = regionNames[region],
         Region = region,
         Instances = CreateSomeRandomInstances().ToList()
     };
 }
コード例 #13
0
        protected virtual void OnRemoveInstanceButtonClicked(AwsRegionLocations e)
        {
            var handler = RemoveInstanceButtonClicked;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #14
0
        protected virtual void OnRegionIdLabelClicked(AwsRegionLocations e)
        {
            var handler = RegionIdLabelClicked;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #15
0
 public RegionDetails GetRegionData(AwsRegionLocations region)
 {
     return(new RegionDetails()
     {
         Name = regionNames[region],
         Region = region,
         Instances = CreateSomeRandomInstances().ToList()
     });
 }
コード例 #16
0
        public void TerminateInstance(AwsRegionLocations region, Action <AwsRegionLocations> instanceTerminated)
        {
            var newestInstanceInRegion = FindNewestInstanceId(region);

            if (newestInstanceInRegion != null)
            {
                TerminateInstance(region, newestInstanceInRegion);
                instanceTerminated(region);
            }
        }
コード例 #17
0
        public byte[] DownloadData(string bucketName, string keyName, AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = s3Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonS3Client(credentials, regionEndpoint));

            // download the data
            using (var stream = client.GetObject(bucketName, keyName).ResponseStream)
            {
                return ReadFully(stream);
            }
        }
コード例 #18
0
        public byte[] DownloadData(string bucketName, string keyName, AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = s3Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonS3Client(credentials, regionEndpoint));

            // download the data
            using (var stream = client.GetObject(bucketName, keyName).ResponseStream)
            {
                return(ReadFully(stream));
            }
        }
コード例 #19
0
        private void TerminateInstance(AwsRegionLocations region, string instanceId)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var deleteRequest = new TerminateInstancesRequest()
            {
                InstanceIds = new List <string>()
                {
                    instanceId
                }
            };

            client.TerminateInstances(deleteRequest);
        }
コード例 #20
0
 public void LaunchInstance(AwsRegionLocations region, Action <AwsRegionLocations> instanceLaunched)
 {
     // Wrap instance creation to ensure that only one is being launched simultaneously. This should be refactored but will not be due to time constraints.
     if (!instanceCurrentlyBeingLaunched)
     {
         instanceCurrentlyBeingLaunched = true;
         CreateAndLaunchInstance(region);
         instanceLaunched(region);
         while (!instancesToBeLaunched.IsEmpty)
         {
             AwsRegionLocations nextRegion;
             if (instancesToBeLaunched.TryDequeue(out nextRegion))
             {
                 CreateAndLaunchInstance(nextRegion);
                 instanceLaunched(nextRegion);
             }
         }
         instanceCurrentlyBeingLaunched = false;
     }
     else
     {
         instancesToBeLaunched.Enqueue(region);
     }
 }
コード例 #21
0
        private void RefreshWorkers(AwsRegionLocations region)
        {
            // Use canRefreshWorkers to ensure that we don't try to refresh workers while a refresh is in progress
            if (canRefreshWorkers)
            {
                canRefreshWorkers = false;

                var regionControl = controlRegionDictionary[region];
                var instances = regionControl.RegionDetails.Instances;

                // Go into the UI thread to clear out the current listview and re-add all the instances to it
                InstancesInRegionListView.UIThread(() =>
                {
                    InstancesInRegionListView.Items.Clear();
                    foreach (var instance in instances)
                    {
                        instance.WorkerInfo = null;
                        InstancesInRegionListView.Items.Add(instance.ToListViewItem());
                    }
                });

                var updateInstanceTasks = instances.Select(RefreshWorker);

                Task.WhenAll(updateInstanceTasks.ToArray())
                    .ContinueWith(t =>
                    {
                        // Once we're all done refreshing all of the workers allow the refresh to happen again
                        canRefreshWorkers = true;
                    });
            }
        }
コード例 #22
0
 public void LaunchInstance(AwsRegionLocations region, Action<AwsRegionLocations> instanceLaunched)
 {
     instanceLaunched(region);
 }
コード例 #23
0
 public void LaunchInstance(AwsRegionLocations region, Action <AwsRegionLocations> instanceLaunched)
 {
     instanceLaunched(region);
 }
コード例 #24
0
 private void TerminateInstance(AwsRegionLocations region)
 {
     Task.Factory.StartNew(() => ec2Caller.TerminateInstance(region, launchedRegion =>
     {
         Thread.Sleep(1000);
         UpdateRegionControl(controlRegionDictionary[launchedRegion]);
     }));
 }
コード例 #25
0
 public void TerminateInstance(AwsRegionLocations region, Action <AwsRegionLocations> instanceTerminated)
 {
     instanceTerminated(region);
 }
コード例 #26
0
        private void TerminateInstance(AwsRegionLocations region, string instanceId)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var deleteRequest = new TerminateInstancesRequest()
            {
                InstanceIds = new List<string>() { instanceId }
            };

            client.TerminateInstances(deleteRequest);
        }
コード例 #27
0
 public void LaunchInstance(AwsRegionLocations region, Action<AwsRegionLocations> instanceLaunched)
 {
     // Wrap instance creation to ensure that only one is being launched simultaneously. This should be refactored but will not be due to time constraints.
     if (!instanceCurrentlyBeingLaunched)
     {
         instanceCurrentlyBeingLaunched = true;
         CreateAndLaunchInstance(region);
         instanceLaunched(region);
         while (!instancesToBeLaunched.IsEmpty)
         {
             AwsRegionLocations nextRegion;
             if (instancesToBeLaunched.TryDequeue(out nextRegion))
             {
                 CreateAndLaunchInstance(nextRegion);
                 instanceLaunched(nextRegion);
             }
         }
         instanceCurrentlyBeingLaunched = false;
     }
     else
     {
         instancesToBeLaunched.Enqueue(region);
     }
 }
コード例 #28
0
 public void TerminateInstance(AwsRegionLocations region, Action<AwsRegionLocations> instanceTerminated)
 {
     var newestInstanceInRegion = FindNewestInstanceId(region);
     if (newestInstanceInRegion != null)
     {
         TerminateInstance(region, newestInstanceInRegion);
         instanceTerminated(region);
     }
 }
コード例 #29
0
        private string FindNewestInstanceId(AwsRegionLocations region)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" state.
            request.Filters.Add(new Filter("instance-state-code", new List<string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            var instances = reservations.SelectMany(x => x.Instances);
            if (instances.Any())
            {
                var newestInstance = instances.OrderByDescending(i => i.LaunchTime).First();
                return newestInstance.InstanceId;
            }
            return null;
        }
コード例 #30
0
 protected virtual void OnRemoveInstanceButtonClicked(AwsRegionLocations e)
 {
     var handler = RemoveInstanceButtonClicked;
     if (handler != null) handler(this, e);
 }
コード例 #31
0
 protected virtual void OnRegionIdLabelClicked(AwsRegionLocations e)
 {
     var handler = RegionIdLabelClicked;
     if (handler != null) handler(this, e);
 }
コード例 #32
0
 public void TerminateInstance(AwsRegionLocations region, Action<AwsRegionLocations> instanceTerminated)
 {
     instanceTerminated(region);
 }
コード例 #33
0
        private void CreateAndLaunchInstance(AwsRegionLocations region)
        {
            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var securityGroupId = EnsureSecurityGroupExists(region);
            var availableSubnets = client.DescribeSubnets().Subnets.OrderByDescending(x => x.AvailableIpAddressCount);
            var networkSpecification = new InstanceNetworkInterfaceSpecification()
            {
                DeviceIndex = 0,
                SubnetId = availableSubnets.First().SubnetId,
                Groups = new List<string>() { securityGroupId },
                AssociatePublicIpAddress = true
            };
            var networkSpecifications = new List<InstanceNetworkInterfaceSpecification>() { networkSpecification };

            var launchRequest = new RunInstancesRequest()
            {
                ImageId = GetAmiId(client, amiName),
                InstanceType = "t2.micro",
                MinCount = 1,
                MaxCount = 1,
                KeyName = keyPairName,
                NetworkInterfaces = networkSpecifications
            };

            client.RunInstances(launchRequest);
        }