Пример #1
0
        /// <summary>
        /// List all volumes found in region
        /// </summary>
        public static void ListVolumes()
        {
            AmazonEC2 ec2 = Ec2Helper.CreateClient();

            DescribeVolumesRequest  rq = new DescribeVolumesRequest();
            DescribeVolumesResponse rs = ec2.DescribeVolumes(rq);

            foreach (Volume v in rs.DescribeVolumesResult.Volume)
            {
                Console.WriteLine(v.VolumeId);


                DescribeTagsRequest trq = new DescribeTagsRequest();
                trq.WithFilter(new Filter()
                {
                    Name = "resource-id", Value = new List <string>()
                    {
                        v.VolumeId
                    }
                });
                DescribeTagsResponse trs = ec2.DescribeTags(trq);
                foreach (ResourceTag t in trs.DescribeTagsResult.ResourceTag)
                {
                    Console.WriteLine("  " + t.Key + "=" + t.Value);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Describes the status of the indicated or, in lieu of any specified,  all volumes belonging to the caller. Volumes that have been deleted are not described.
        /// 
        /// </summary>
        /// <param name="service">Instance of AmazonEC2 service</param>
        /// <param name="request">DescribeVolumesRequest request</param>
        public static void InvokeDescribeVolumes(AmazonEC2 service, DescribeVolumesRequest request)
        {
            try 
            {
                DescribeVolumesResponse response = service.DescribeVolumes(request);
                
                
                Console.WriteLine ("Service Response");
                Console.WriteLine ("=============================================================================");
                Console.WriteLine ();

                Console.WriteLine("        DescribeVolumesResponse");
                if (response.IsSetDescribeVolumesResult())
                {
                    Console.WriteLine("            DescribeVolumesResult");
                    DescribeVolumesResult  describeVolumesResult = response.DescribeVolumesResult;
                    List<Volume> volumeList = describeVolumesResult.Volume;
                    foreach (Volume volume in volumeList)
                    {
                        Console.WriteLine("                Volume");
                        if (volume.IsSetVolumeId())
                        {
                            Console.WriteLine("                    VolumeId");
                            Console.WriteLine("                        {0}", volume.VolumeId);
                        }
                        if (volume.IsSetSize())
                        {
                            Console.WriteLine("                    Size");
                            Console.WriteLine("                        {0}", volume.Size);
                        }
                        if (volume.IsSetSnapshotId())
                        {
                            Console.WriteLine("                    SnapshotId");
                            Console.WriteLine("                        {0}", volume.SnapshotId);
                        }
                        if (volume.IsSetAvailabilityZone())
                        {
                            Console.WriteLine("                    AvailabilityZone");
                            Console.WriteLine("                        {0}", volume.AvailabilityZone);
                        }
                        if (volume.IsSetStatus())
                        {
                            Console.WriteLine("                    Status");
                            Console.WriteLine("                        {0}", volume.Status);
                        }
                        if (volume.IsSetCreateTime())
                        {
                            Console.WriteLine("                    CreateTime");
                            Console.WriteLine("                        {0}", volume.CreateTime);
                        }
                        List<Attachment> attachmentList = volume.Attachment;
                        foreach (Attachment attachment in attachmentList)
                        {
                            Console.WriteLine("                    Attachment");
                            if (attachment.IsSetVolumeId())
                            {
                                Console.WriteLine("                        VolumeId");
                                Console.WriteLine("                            {0}", attachment.VolumeId);
                            }
                            if (attachment.IsSetInstanceId())
                            {
                                Console.WriteLine("                        InstanceId");
                                Console.WriteLine("                            {0}", attachment.InstanceId);
                            }
                            if (attachment.IsSetDevice())
                            {
                                Console.WriteLine("                        Device");
                                Console.WriteLine("                            {0}", attachment.Device);
                            }
                            if (attachment.IsSetStatus())
                            {
                                Console.WriteLine("                        Status");
                                Console.WriteLine("                            {0}", attachment.Status);
                            }
                            if (attachment.IsSetAttachTime())
                            {
                                Console.WriteLine("                        AttachTime");
                                Console.WriteLine("                            {0}", attachment.AttachTime);
                            }
                        }
                    }
                }
                if (response.IsSetResponseMetadata())
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata  responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId())
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                }

            } 
            catch (AmazonEC2Exception ex) 
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Request ID: " + ex.RequestId);
                Console.WriteLine("XML: " + ex.XML);
            }
        }
Пример #3
0
        /// <summary>
        /// Check for any volumes that have a snapshot scheduled based on the schedule in the snapshotSchedule tag key.
        /// </summary>
        public static void CheckForScheduledSnapshots()
        {
            Console.WriteLine("Checking for scheduled snapshots...");

            AmazonEC2 ec2 = Ec2Helper.CreateClient();

            DescribeVolumesRequest rq = new DescribeVolumesRequest();

            rq.WithFilter(new Filter()
            {
                Name = "tag-key", Value = new List <string>()
                {
                    "snapshotSchedule"
                }
            });
            DescribeVolumesResponse rs = ec2.DescribeVolumes(rq);

            foreach (Volume v in rs.DescribeVolumesResult.Volume)
            {
                string[] sch2 = Ec2Helper.GetTagValue(v.Tag, "snapshotSchedule").Split(' ');

                string volumename = Ec2Helper.GetTagValue(v.Tag, "Name");


                DateTime lastSnap;     // date of last snapshot
                DateTime nextSnap;     // the next backup that should have occured based on last backup
                DateTime nextNextSnap; // the next backup that should occur assuming a backup runs now or ran at the last proper time

                DateTime now = DateTime.UtcNow;

                if (!DateTime.TryParse(Ec2Helper.GetTagValue(v.Tag, "lastSnapshot"), out lastSnap))
                {
                    lastSnap = Convert.ToDateTime("1/1/2010");
                }


                Console.WriteLine("Checking " + v.VolumeId + " / " + volumename + "...");
//sch2 = ("hourly 4 :30 x30days").Split(' ');
//lastSnap = Convert.ToDateTime("2/29/2012 6:00:15pm");
//now = Convert.ToDateTime("2/29/2012 10:00:14pm");



                switch (sch2[0])
                {
                case "hourly":     // hourly, hourly 1 :15, hourly :30, hourly 4 (pass it hours between backups & when on the hour to do it, any order; defaults to every hour on the hour)

                    int ah = GetAfterTheHour(sch2, 0);
                    int hi = GetInt(sch2, 1);

                    nextSnap = lastSnap.AddMinutes(-lastSnap.Minute).AddSeconds(-lastSnap.Second).AddMilliseconds(-lastSnap.Millisecond);
                    nextSnap = nextSnap.AddHours(hi).AddMinutes(ah);

                    // this is not right
                    nextNextSnap = now.AddMinutes(-now.Minute).AddSeconds(-now.Second).AddMilliseconds(-now.Millisecond);
                    nextNextSnap = nextNextSnap.AddMinutes(ah).AddHours(hi);

                    break;

                case "daily":     // daily, 3pm, daily 15:15, daily 3:30pm (times are UTC; defaults to midnight UTC)

                    DateTime hour = GetTime(sch2, Convert.ToDateTime("0:00"));

                    nextSnap = lastSnap.Date.AddDays(1).AddTicks(hour.TimeOfDay.Ticks);

                    nextNextSnap = now.Date.AddDays(1).AddTicks(hour.TimeOfDay.Ticks);

                    break;

                case "weekly":     // weekly, weekly sunday, weekly thursday 3pm (times are UTC; defaults to sunday midnight UTC)

                    DateTime  whour = GetTime(sch2, Convert.ToDateTime("0:00"));
                    DayOfWeek dow   = GetDow(sch2, DayOfWeek.Sunday);

                    if (lastSnap.DayOfWeek >= dow)
                    {
                        nextSnap = lastSnap.Date.AddDays(-(int)lastSnap.DayOfWeek).AddDays(7 + (int)dow).AddTicks(whour.TimeOfDay.Ticks);
                    }
                    else
                    {
                        nextSnap = lastSnap.Date.AddDays(-(int)lastSnap.DayOfWeek).AddDays((int)dow).AddTicks(whour.TimeOfDay.Ticks);
                    }

                    nextNextSnap = now.Date.AddDays(-(int)now.DayOfWeek).AddDays(7 + (int)dow).AddTicks(whour.TimeOfDay.Ticks);
                    if (nextSnap == nextNextSnap)
                    {
                        nextNextSnap = nextNextSnap.AddDays(7);
                    }

                    break;

                default:
                    lastSnap     = now.AddYears(1);
                    nextSnap     = lastSnap;
                    nextNextSnap = lastSnap;
                    break;
                }


//Console.WriteLine("last=" + lastSnap.ToString());
//Console.WriteLine("now=" + now);
//Console.WriteLine("next=" + nextSnap.ToString());
//Console.WriteLine("nextNext=" + nextNextSnap.ToString());
//Console.ReadKey();
//return;
                if (nextSnap <= now)
                {
                    // create snapshot of volume

                    string expires     = "";
                    int    expireHours = GetExpireHours(sch2, 0);
                    if (expireHours > 0)
                    {
                        expires = now.AddHours(expireHours).ToString();
                    }


                    Backup(volumename, "automatic", v.VolumeId, volumename, Ec2Helper.GetInstanceName(v.Attachment[0].InstanceId), expires);


                    // update volume tags

                    CreateTagsRequest rqq = new CreateTagsRequest();

                    rqq.WithResourceId(v.VolumeId);

                    nextSnap = nextSnap.AddSeconds(-nextSnap.Second).AddMilliseconds(-nextSnap.Millisecond);

                    rqq.WithTag(new Tag[] {
                        new Tag {
                            Key = "lastSnapshot", Value = now.ToString()
                        },
                        new Tag {
                            Key = "nextSnapshot", Value = nextNextSnap.ToString()
                        }
                    });

                    var createTagResponse = ec2.CreateTags(rqq);
                }
                else
                {
                    Console.WriteLine("    Next scheduled " + nextSnap.ToString());
                }
            }
        }