Example #1
0
        /// <summary>
        /// Query the market for product spot pricing.
        /// </summary>
        protected override void AmazonExecute()
        {
            var request = new DescribeSpotPriceHistoryRequest
            {
                InstanceType = new List <string> {
                    this.InstanceType.Get(this.ActivityContext)
                },
                ProductDescription = new List <string> {
                    this.ProductDescription.Get(this.ActivityContext)
                },
                StartTime = DateTime.Now.ToAmazonDateTime(),
            };

            try
            {
                var response = EC2Client.DescribeSpotPriceHistory(request);

                // Get the first price in the price history array
                decimal price = decimal.Parse(response.DescribeSpotPriceHistoryResult.SpotPriceHistory[0].SpotPrice);
                this.CurrentSpotPrice.Set(this.ActivityContext, price);
            }
            catch (EndpointNotFoundException ex)
            {
                LogBuildMessage(ex.Message);
            }
        }
        /// <summary>
        /// Query EC2 for spot request information.
        /// </summary>
        protected override void AmazonExecute()
        {
            // Get a list of requests to monitor
            var requestIds = new List <string>();

            foreach (var spotRequest in this.SpotRequests.Get(this.ActivityContext))
            {
                requestIds.Add(spotRequest.SpotInstanceRequestId);
            }

            // Create a monitoring request
            var request = new DescribeSpotInstanceRequestsRequest
            {
                SpotInstanceRequestId = requestIds
            };

            try
            {
                var response = EC2Client.DescribeSpotInstanceRequests(request);

                this.UpdatedSpotRequests.Set(this.ActivityContext, response.DescribeSpotInstanceRequestsResult.SpotInstanceRequest);
            }
            catch (EndpointNotFoundException ex)
            {
                LogBuildMessage(ex.Message);
            }
        }
        public async Task <IActionResult> SubmitCreateTemplate([Bind("serverID", "Name", "TemplateDescription")] ServerTemplateCreationFormModel submission)
        {
            Server given = await _context.Servers.FindAsync(int.Parse(submission.serverID));

            if (given != null)
            {
                Template newlyCreated = new Template
                {
                    Name = submission.Name,
                    TemplateDescription = submission.TemplateDescription,
                    DateCreated         = DateTime.Now,
                    OperatingSystem     = given.OperatingSystem,
                    SpecificMinimumSize = true,
                    MinimumStorage      = given.StorageAssigned,
                    Type = TemplateType.Custom
                };
                CreateImageResponse response = await EC2Client.CreateImageAsync(new CreateImageRequest
                {
                    BlockDeviceMappings = new List <BlockDeviceMapping> {
                        new BlockDeviceMapping {
                            DeviceName = "/dev/xvda",
                            Ebs        = new EbsBlockDevice {
                                VolumeSize = given.StorageAssigned
                            }
                        }
                    },
                    Name        = submission.Name + " - Created on Platform",
                    Description = submission.TemplateDescription,
                    NoReboot    = false,
                    InstanceId  = given.AWSEC2Reference
                });

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    newlyCreated.AWSAMIReference = response.ImageId;
                    _context.Templates.Add(newlyCreated);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(""));
                }
                else
                {
                    return(StatusCode(500));
                }
            }
            else
            {
                return(StatusCode(500));
            }
        }
        /// <summary>
        /// Create a new instance using the Amazon spot market.
        /// </summary>
        protected override void AmazonExecute()
        {
            try
            {
                // Set the Amazon instance request times
                //// var localValidFrom = DateTime.Now.ToAmazonDateTime();
                //// var localValidUntil = DateTime.Now.AddMinutes(this.ValidDurationMinutes.Get(this.ActivityContext)).ToAmazonDateTime();

                var request = new RequestSpotInstancesRequest
                {
                    //// AvailabilityZoneGroup = ,
                    InstanceCount = this.InstanceCount.Get(this.ActivityContext),
                    //// LaunchGroup = ,
                    LaunchSpecification = new LaunchSpecification()
                    {
                        ImageId       = this.ImageId.Get(this.ActivityContext),
                        InstanceType  = this.InstanceType.Get(this.ActivityContext),
                        SecurityGroup = new List <string>()
                        {
                            this.SecurityGroupName.Get(this.ActivityContext)
                        }
                    },
                    SpotPrice = this.SpotPrice.Get(this.ActivityContext).ToString(),
                    Type      = this.RequestType.Get(this.ActivityContext),
                    //// ValidFrom = localValidFrom,
                    //// ValidUntil = localValidUntil
                };

                try
                {
                    var response = EC2Client.RequestSpotInstances(request);
                    this.SpotRequests.Set(this.ActivityContext, response.RequestSpotInstancesResult.SpotInstanceRequest);
                }
                catch (EndpointNotFoundException ex)
                {
                    this.LogBuildMessage(ex.Message);
                }
            }
            catch (Exception ex)
            {
                this.LogBuildError("Amazon error: " + ex.Message + " Stack Trace: " + ex.StackTrace);
                if (ex.InnerException != null)
                {
                    this.LogBuildError("Inner exception: " + ex.InnerException.Message);
                }
            }
        }
        /// <summary>
        /// Terminate an active EC2 instance.
        /// </summary>
        protected override void AmazonExecute()
        {
            var request = new TerminateInstancesRequest
            {
                InstanceId = this.InstanceIds.Get(this.ActivityContext)
            };

            try
            {
                var response = EC2Client.TerminateInstances(request);
                this.InstanceChanges.Set(this.ActivityContext, response.TerminateInstancesResult.TerminatingInstance);
            }
            catch (EndpointNotFoundException ex)
            {
                LogBuildMessage(ex.Message);
            }
        }
        /// <summary>
        /// Connect to an Amazon subscription and obtain information about instance reservations.
        /// </summary>
        protected override void AmazonExecute()
        {
            var request = new DescribeInstancesRequest
            {
                InstanceId = this.InstanceIds.Get(this.ActivityContext)
            };

            try
            {
                var response = EC2Client.DescribeInstances(request);
                this.Reservations.Set(this.ActivityContext, response.DescribeInstancesResult.Reservation);
            }
            catch (EndpointNotFoundException ex)
            {
                this.LogBuildMessage(ex.Message);
            }
        }
Example #7
0
        /// <summary>
        /// Connect to an EC2 instance and associate a public IP address with it.
        /// </summary>
        protected override void AmazonExecute()
        {
            var request = new AssociateAddressRequest
            {
                InstanceId = this.InstanceId.Get(this.ActivityContext),
                PublicIp   = this.PublicAddress.Get(this.ActivityContext)
            };

            try
            {
                EC2Client.AssociateAddress(request);
            }
            catch (EndpointNotFoundException ex)
            {
                LogBuildMessage(ex.Message);
            }
        }
Example #8
0
        /// <summary>
        /// Query EC2 for the list of owned AMIs.
        /// </summary>
        protected override void AmazonExecute()
        {
            var request = new DescribeImagesRequest
            {
                Owner = new List <string> {
                    this.Owner.Get(this.ActivityContext)
                }
            };

            try
            {
                var response = EC2Client.DescribeImages(request);
                this.Images.Set(this.ActivityContext, response.DescribeImagesResult.Image);
            }
            catch (EndpointNotFoundException ex)
            {
                this.LogBuildMessage(ex.Message);
            }
        }
Example #9
0
        /// <summary>
        /// Query EC2 for the spot pricing history for a specified instance type.
        /// </summary>
        protected override void AmazonExecute()
        {
            var request = new DescribeSpotPriceHistoryRequest
            {
                InstanceType = new List <string> {
                    this.InstanceType.Get(this.ActivityContext)
                },
                StartTime = this.StartTime.Get(this.ActivityContext).ToAmazonDateTime(),
                EndTime   = this.EndTime.Get(this.ActivityContext).ToAmazonDateTime()
            };

            try
            {
                var response = EC2Client.DescribeSpotPriceHistory(request);
                this.PriceHistory.Set(this.ActivityContext, response.DescribeSpotPriceHistoryResult.SpotPriceHistory);
            }
            catch (EndpointNotFoundException ex)
            {
                this.LogBuildMessage(ex.Message);
            }
        }
        public async Task <IActionResult> Delete(String TemplateID)
        {
            Template deleted = await _context.Templates.FindAsync(int.Parse(TemplateID));

            if (deleted != null)
            {
                DeregisterImageResponse response = await EC2Client.DeregisterImageAsync(new DeregisterImageRequest
                {
                    ImageId = deleted.AWSAMIReference
                });

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    if (!String.IsNullOrEmpty(deleted.AWSSnapshotReference))
                    {
                        Backgroundqueue.QueueBackgroundWorkItem(async token =>
                        {
                            _logger.LogInformation("Deletion of snapshot scheduled");
                            await Task.Delay(TimeSpan.FromMinutes(3), token);
                            await EC2Client.DeleteSnapshotAsync(new DeleteSnapshotRequest(deleted.AWSSnapshotReference));
                            _logger.LogInformation("Deletion of snapshot done!");
                        });
                    }
                    _context.Templates.Remove(deleted);
                    await _context.SaveChangesAsync();

                    TempData["Result"] = "Deleted Sucessfully!";
                    return(RedirectToAction(""));
                }
                else
                {
                    TempData["Result"] = "Delete Failed!";
                    return(RedirectToAction(""));
                }
            }
            else
            {
                return(NotFound());
            }
        }