Пример #1
0
        private Dictionary <string, Object> GetConfigOptions()
        {
            Amazon.S3.Model.GetObjectResponse rsp;
            IAWSContext ctx  = AppUtility.GetContext();
            var         name = AppUtility.GetContext().Bucket;
            var         key  = "config";
            string      content;

            using (var client = ctx.GetS3Client())
            {
                var req = new Amazon.S3.Model.GetObjectRequest()
                          .WithBucketName(name).WithKey(key);
                try
                {
                    rsp = client.GetObject(req);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Bucket {0} key {1}: {2}", name, key, ex.Message), GetType());
                    throw;
                }
                using (StreamReader r = new StreamReader(rsp.ResponseStream))
                {
                    content = r.ReadToEnd();
                }
            }
            var dict = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(content);

            return(dict);
        }
Пример #2
0
        public int GetNotifications()
        {
            int count = 0;
            // Check Queue For Shutdown Message
            var request = new Amazon.SQS.Model.ReceiveMessageRequest();

            request.WithQueueUrl(AppUtility.GetContext().ShutdownNotificationQueue);

            var rsp = queue.ReceiveMessage(request);

            if (rsp.IsSetReceiveMessageResult())
            {
                foreach (var msg in rsp.ReceiveMessageResult.Message)
                {
                    if (msg.IsSetMessageId())
                    {
                        Debug.WriteLine("Notification Instance shutdown: " + msg.Body);
                        var delMsg = new Amazon.SQS.Model.DeleteMessageRequest();
                        delMsg.WithQueueUrl(AppUtility.GetContext().ShutdownQueue);
                        delMsg.WithReceiptHandle(msg.ReceiptHandle);
                        var delRsp = queue.DeleteMessage(delMsg);
                        count += 1;
                    }
                }
            }

            return(count);
        }
Пример #3
0
        /*
         * Remove all messages from shutdown queue
         */
        private void ClearShutdownQueue()
        {
            var QueueURL = AppUtility.GetContext().ShutdownQueue;

            Amazon.SQS.Model.ReceiveMessageRequest request = new Amazon.SQS.Model.ReceiveMessageRequest();
            request.WithMaxNumberOfMessages(10);
            request.WithQueueUrl(QueueURL);
            var rsp = queue.ReceiveMessage(request);

            while (rsp.IsSetReceiveMessageResult() && rsp.ReceiveMessageResult.Message.Count > 0)
            {
                Debug.WriteLine("Remove Shutdown Messages: " + rsp.ReceiveMessageResult.Message.Count);
                var result = rsp.ReceiveMessageResult.Message;
                foreach (var msg in rsp.ReceiveMessageResult.Message)
                {
                    //Console.WriteLine("Message: " + msg);
                    if (msg.IsSetMessageId())
                    {
                        //Console.WriteLine("Body: " + msg.Body);
                        //if (msg.Body == "shutdown")
                        //{
                        //}
                        var delMsg = new Amazon.SQS.Model.DeleteMessageRequest();
                        delMsg.WithQueueUrl(QueueURL);
                        delMsg.WithReceiptHandle(msg.ReceiptHandle);
                        var delRsp = queue.DeleteMessage(delMsg);
                    }
                }
                rsp = queue.ReceiveMessage(request);
            }
        }
Пример #4
0
        public EC2ScaleContract()
        {
            IAWSContext ctx;

            ctx    = AppUtility.GetContext();
            client = ctx.GetEC2Client();
            queue  = ctx.GetQueueClient();
        }
Пример #5
0
        /// <summary>
        /// StartInstances utilizes EC2 interface to launch specified number of instances.
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public int StartInstances(int count)
        {
            var configOptions = GetConfigOptions();
            var ctx           = AppUtility.GetContext();

            Debug.WriteLine(String.Format("StartInstances: {0}", count), this.GetType());
            var runRequest = new RunInstancesRequest();

            runRequest.ImageId      = ctx.AMI;
            runRequest.MinCount     = count;
            runRequest.MaxCount     = count;
            runRequest.InstanceType = ctx.InstanceType;
            Object instanceType;

            if (configOptions.TryGetValue("instance", out instanceType))
            {
                Debug.WriteLine("InstanceType Requested: " + instanceType, GetType());
                var instanceList = new List <string>()
                {
                    "t1.micro", "m1.small", "c1.medium"
                };
                if (!instanceList.Contains(instanceType))
                {
                    var msg = String.Format("Requested instanceType {0} not in {1}",
                                            instanceType, string.Join(",", instanceList.ToArray()));
                    Debug.WriteLine(msg);
                }
                else
                {
                    runRequest.InstanceType = (string)instanceType;
                }
            }

            runRequest.SecurityGroup = new System.Collections.Generic.List <String>()
            {
                ctx.SecurityGroup
            };
            runRequest.WithInstanceInitiatedShutdownBehavior("terminate");
            if (!String.IsNullOrEmpty(ctx.AvailabilityZone))
            {
                runRequest.Placement = new Placement();
                runRequest.Placement.AvailabilityZone = ctx.AvailabilityZone;
            }
            RunInstancesResponse response = client.RunInstances(runRequest);

            return(response.RunInstancesResult.Reservation.RunningInstance.Count());
        }
Пример #6
0
        private void UpdateInstances(List <string> instances)
        {
            Amazon.S3.Model.PutObjectResponse rsp;
            IAWSContext ctx     = AppUtility.GetContext();
            var         name    = AppUtility.GetContext().Bucket;
            var         key     = "floor";
            var         request = new Amazon.S3.Model.PutObjectRequest();

            request.WithBucketName(name);
            request.WithKey("instances");
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(instances);

            request.WithContentBody(json);
            using (var client = ctx.GetS3Client())
            {
                rsp = client.PutObject(request);
            }
        }
Пример #7
0
        /// <summary>
        /// GetRunningInstances clears the terminate queue and returns number of currently
        /// running instances.
        /// </summary>
        /// <returns></returns>
        public int GetRunningInstances()
        {
            ClearShutdownQueue();

            var ctx    = AppUtility.GetContext();
            var req    = new DescribeInstancesRequest();
            var filter = new Filter();

            filter.Name  = "image-id";
            filter.Value = new System.Collections.Generic.List <String>()
            {
                ctx.AMI
            };
            req.Filter.Add(filter);

            filter       = new Filter();
            filter.Name  = "instance-state-name";
            filter.Value = new System.Collections.Generic.List <String>()
            {
                "running"
            };
            req.Filter.Add(filter);

            DescribeInstancesResponse rsp = client.DescribeInstances(req);
            var listRunningInstance       = new List <string>();

            foreach (var i in rsp.DescribeInstancesResult.Reservation)
            {
                Debug.WriteLine("== Reservation");
                foreach (var j in i.RunningInstance)
                {
                    Debug.WriteLine(String.Format("{0}) {1} -- {2}", listRunningInstance.Count, j.InstanceId, j.LaunchTime));
                    listRunningInstance.Add(j.InstanceId);
                }
            }
            UpdateInstances(listRunningInstance);
            return(listRunningInstance.Count);
        }
Пример #8
0
        /// <summary>
        /// TerminateInstances sends number of terminate messages to specified queue.
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public int TerminateInstances(int num)
        {
            Debug.WriteLine(String.Format("TerminateInstnaces: {0}", num), this.GetType());
            int count = 0;

            for (int i = 1; i <= num; i++)
            {
                var msg = new Amazon.SQS.Model.SendMessageRequest();
                msg.WithQueueUrl(AppUtility.GetContext().ShutdownQueue);
                msg.WithMessageBody("shutdown");

                var rsp = queue.SendMessage(msg);
                if (rsp.IsSetSendMessageResult())
                {
                    if (rsp.SendMessageResult.IsSetMessageId())
                    {
                        count += 1;
                    }
                }
            }

            return(count);
        }
Пример #9
0
 public void resetQueue()
 {
     IAWSOrchestratorContext ctx = AppUtility.GetContext();
 }