Beispiel #1
0
        /// <summary>
        /// Actions on service start
        /// </summary>
        private static void OnStartEvent()
        {
            try
            {
                List <RunningInstance> instances = ServersWatcher.Instances;

                EC2Helper       ec2Helper = EC2Helper.Make();
                RunningInstance instance  = ec2Helper.GetCurrentInstance(instances);

                if (instance == null)
                {
                    return;
                }

                // Assign elastic IP if it is free and tag exists
                var tag = ec2Helper.GetElasticIpTag(instance);
                if (tag != null)
                {
                    ec2Helper.AssignElasticIpByTag(instance, tag);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message, EventLogEntryType.Error);
                throw new Exception(ex.Message, ex);
            }
        }
Beispiel #2
0
        private static void ConsoleStartTest()
        {
            try
            {
                List <RunningInstance> instances = ServersWatcher.Instances;

                EC2Helper       ec2Helper = EC2Helper.Make();
                RunningInstance instance  = ec2Helper.GetCurrentInstance(instances);

                if (instance == null)
                {
                    return;
                }

                Console.WriteLine("Instance: {0}", ServersWatcher.GetInstanceName(instance));

                // Assign elastic IP if it is free and tag exists
                var tag = ec2Helper.GetElasticIpTag(instance);
                if (tag != null)
                {
                    Console.WriteLine("Tag value: {0}", tag.Value);
                    ec2Helper.AssignElasticIpByTag(instance, tag);
                }
                else
                {
                    Console.WriteLine("Tag not found");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 /// <summary>
 /// Access helper instance
 /// </summary>
 /// <returns>instance referance</returns>
 public static EC2Helper Make()
 {
     //create instance if there is no one yet
     if (_singleton == null)
     {
         _singleton = new EC2Helper();
     }
     //return instance referance
     return(_singleton);
 }
Beispiel #4
0
        /// <summary>
        /// Actions on timer event
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private static void OnTimerEvent(object source, ElapsedEventArgs e)
        {
            try
            {
                List <RunningInstance> instances = ServersWatcher.Instances;

                // Write running instances to hosts file
                HostsManager hostsMgr = new HostsManager();
                hostsMgr.OpenHostsFile();
                foreach (RunningInstance inst in instances)
                {
                    hostsMgr.AddRecord(inst.PrivateIpAddress, ServersWatcher.GetInstanceName(inst));
                }
                hostsMgr.CommitHostsFile();

                //Apply ElasticIP for current host:
                EC2Helper ec2Helper = EC2Helper.Make();

                RunningInstance instance = ec2Helper.GetCurrentInstance(instances);
                if (instance == null)
                {
                    return;
                }

                if (ec2Helper.IsElasticIpAssigned(instance))
                {
                    // ElasticIP assigned to instance, update tags
                    Tag tag = new Tag().WithKey(EC2Helper.TAG_ELASTIC_IP).WithValue(instance.IpAddress);
                    ec2Helper.AddTagToInstance(instance.InstanceId, tag);
                }
                else
                {
                    var tag = ec2Helper.GetElasticIpTag(instance);

                    // Delete tag if exists
                    if (tag != null)
                    {
                        ec2Helper.DeleteTag(instance.InstanceId, tag);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message, EventLogEntryType.Error);
                throw new Exception(ex.Message, ex);
            }
        }
Beispiel #5
0
        private static void ConsoleInstanceTest()
        {
            try
            {
                List <RunningInstance> instances = ServersWatcher.Instances;

                //Apply ElasticIP for current host:
                EC2Helper ec2Helper = EC2Helper.Make();

                RunningInstance instance = ec2Helper.GetCurrentInstance(instances);
                if (instance == null)
                {
                    return;
                }

                Console.WriteLine("Instance: {0}", ServersWatcher.GetInstanceName(instance));

                if (ec2Helper.IsElasticIpAssigned(instance))
                {
                    Console.WriteLine("ElasticIP assigned to instance: {0}", instance.IpAddress);

                    // ElasticIP assigned to instance update tags
                    Tag tag = new Tag().WithKey(EC2Helper.TAG_ELASTIC_IP).WithValue(instance.IpAddress);
                    ec2Helper.AddTagToInstance(instance.InstanceId, tag);
                }
                else
                {
                    var tag = ec2Helper.GetElasticIpTag(instance);

                    // Delete tag if exists
                    if (tag != null)
                    {
                        Console.WriteLine("Deleting tad value: {0}", tag.Value);
                        ec2Helper.DeleteTag(instance.InstanceId, tag);
                    }
                    else
                    {
                        Console.WriteLine("Tag not found");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }