Пример #1
0
        public void set_name_tag(AmazonEC2Client client, string res_id, string val)
        {
            var req = new CreateTagsRequest();

            req.Resources.Add(res_id);
            req.Tags.Add(new Tag("Name", val));
            client.CreateTags(req);
        }
Пример #2
0
        /****************************************** Other  ******************************************/
        private void AssignNameToResource(string resourceId, string name)
        {
            //string resourceName = AwsCommon.FormatresourceName(resourceTypeName);
            CreateTagsRequest reqCreateTag = new CreateTagsRequest();

            reqCreateTag.Resources = new List <string>();
            reqCreateTag.Resources.Add(resourceId);
            reqCreateTag.Tags = new List <Tag>();
            reqCreateTag.Tags.Add(new Tag("Name", name));

            client.CreateTags(reqCreateTag);
        }
Пример #3
0
        private static void AddTag()
        {
            AmazonEC2Client   client = new AmazonEC2Client();
            CreateTagsRequest tagReq = new CreateTagsRequest();

            tagReq.Resources = new System.Collections.Generic.List <string> {
                "instanceId"
            };
            tagReq.Tags = new System.Collections.Generic.List <Tag> {
                new Tag()
                {
                    Key = "tagKey", Value = "tagValue"
                }
            };
            CreateTagsResponse tagResp = client.CreateTags(tagReq);

            Console.WriteLine("EC2 Tag created- {0}", tagResp.ResponseMetadata);
        }
Пример #4
0
        public static void CreateInstance(AmazonEC2Client ec2Client)
        {
            var instanceId         = Args.Value("InstanceId");
            var instanceProfileArn = CreateInstanceProfile();

            // Create the name tag
            ec2Client.CreateTags(new CreateTagsRequest
            {
                Resources = new List <string> {
                    instanceId
                },
                Tags = new List <Amazon.EC2.Model.Tag> {
                    new Amazon.EC2.Model.Tag {
                        Key = "Name", Value = "Processor"
                    }
                }
            });
            Log.Debug("Adding Name Tag to instance");
        }
Пример #5
0
        public static void CreateInstance()
        {
            var bucketName = "ec2-sample-" + RESOURCDE_POSTFIX;

            var ec2Client = new AmazonEC2Client();

            // Get latest 2012 Base AMI
            var imageId = ImageUtilities.FindImage(ec2Client, ImageUtilities.WINDOWS_2012_BASE).ImageId;

            Console.WriteLine("Using Image ID: {0}", imageId);

            var runRequest = new RunInstancesRequest
            {
                ImageId      = imageId,
                MinCount     = 1,
                MaxCount     = 1,
                InstanceType = new InstanceType("t2.micro")
            };
            var instanceId = ec2Client.RunInstances(runRequest).Reservation.Instances[0].InstanceId;

            ec2Client.CreateTags(new CreateTagsRequest
            {
                Resources = new List <string> {
                    instanceId
                },
                Tags = new List <Amazon.EC2.Model.Tag> {
                    new Amazon.EC2.Model.Tag {
                        Key = "Name", Value = "Processor"
                    }
                }
            });
            Console.WriteLine("Adding Name Tag to instance");
            // Pause to be sure instance has started
            Thread.Sleep(45000);
            ec2Client.StopInstances(new StopInstancesRequest {
                InstanceIds = new List <string> {
                    instanceId
                }
            });
        }
Пример #6
0
        public static void Backup(string name, string description, string volumeid, string volumename, string instancename, string expires)
        {
            Console.WriteLine("    Creating snapshot of " + volumeid + " / " + volumename + " / " + instancename);

            AmazonEC2Client ec2 = Ec2Helper.CreateClient();

            CreateSnapshotRequest rq = new CreateSnapshotRequest();

            rq.VolumeId    = volumeid;
            rq.Description = description;

            CreateSnapshotResponse rs = ec2.CreateSnapshot(rq);

            string snapshotid = rs.Snapshot.SnapshotId;


            // build tags for snapshot

            CreateTagsRequest rqq = new CreateTagsRequest();

            rqq.Resources.Add(snapshotid);

            rqq.Tags.Add(new Tag {
                Key = "Name", Value = name
            });
            rqq.Tags.Add(new Tag {
                Key = "source", Value = "scheduler"
            });
            rqq.Tags.Add(new Tag {
                Key = "instance", Value = instancename
            });
            rqq.Tags.Add(new Tag {
                Key = "volume", Value = volumename
            });
            rqq.Tags.Add(new Tag {
                Key = "expires", Value = expires.ToString()
            });


            // get tags from volume to be applied to snapshot

            DescribeTagsRequest trq = new DescribeTagsRequest();

            trq.Filters.Add(new Filter()
            {
                Name = "resource-id", Values = new List <string>()
                {
                    volumeid
                }
            });
            DescribeTagsResponse trs = ec2.DescribeTags(trq);

            foreach (TagDescription t in trs.Tags)
            {
                if (t.Key != "nextSnapshot" && t.Key != "lastSnapshot" && t.Key != "Name")
                {
                    rqq.Tags.Add(new Tag {
                        Key = t.Key, Value = t.Value
                    });
                }
            }


            // apply tags to snapshopt

            var createTagResponse = ec2.CreateTags(rqq);
        }
Пример #7
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 in " + Program.options.Region + "...");

            AmazonEC2Client ec2 = Ec2Helper.CreateClient();

            DescribeVolumesRequest rq = new DescribeVolumesRequest();

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

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

                string volumename = Ec2Helper.GetTagValue(v.Tags, "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.Tags, "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.Attachments.First().InstanceId), expires);


                    // update volume tags

                    CreateTagsRequest rqq = new CreateTagsRequest();

                    rqq.Resources.Add(v.VolumeId);

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

                    rqq.Tags.Add(new Tag {
                        Key = "lastSnapshot", Value = now.ToString()
                    });
                    rqq.Tags.Add(new Tag {
                        Key = "nextSnapshot", Value = nextNextSnap.ToString()
                    });

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