1. Mike Bayer
  2. ec2control

Commits

zzzeek  committed f21529c

add support for elastic ip binding, public hostname

  • Participants
  • Parent commits 4b5a21e
  • Branches default

Comments (0)

Files changed (3)

File ec2control.ini.SAMPLE

View file
 # instance sections for each of the above names.
 
 [instance_my_instance_one]
+# instance id - this is the amazon id
 instance_id=i-12345678
+
+# public ip - if present, associate_address()
+# will be called when the instance is brought
+# online
+public_ip_address=xxx.xxx.xxx.xxx
+
+# public hostname - if you have a public_ip_address,
+# this value can be used in the "success!" email
+public_hostname=myhostname.com
+
+# descriptive name used in emails
 descriptive_name=My Instance
+
+# any custom key/value pairs can be
+# added here
 custom_key_one=Some Custom Value
 
 [instance_my_instance_two]

File ec2control/ec2.py

View file
 
     @property
     def ip_address(self):
+        """Return the dynamic IP address of the instance"""
         if self.instance:
             return self.instance.ip_address
         else:
             return None
 
+    @property
+    def public_hostname(self):
+        """Return the best internet name for the instance.
+
+        If public_hostname is set, returns that.
+        Else if public_ip_address is present, returns that.
+        Otherwise returns ip_address.
+        If the instance is not running, returns None
+        in all cases.
+        """
+
+        if self.instance:
+            if "public_hostname" in self.config:
+                return self.config['public_hostname']
+            elif "public_ip_address" in self.config:
+                return self.config['public_ip_address']
+            else:
+                return self.instance.ip_address
+        else:
+            return None
+
     def start(self):
         instance_id = self.config['instance_id']
         log.info("Starting instance id: %s", instance_id)
         self.status = inst.state
         log.info("Status: %s", self.status)
 
+        if self.status == "running":
+            public_ip_address = self.config.get('public_ip_address', None)
+            if public_ip_address is not None:
+                if self._conn.associate_address(instance_id, public_ip_address):
+                    self.public_ip_address = public_ip_address

File sample_templates/minecraft.mako

View file
 <%def name="subject()">
     % if status == 'started':
         % if instance.status == 'running':
-            ${instance.descriptive_name} is online!  ${instance.ip_address}:${instance.config['minecraft_port']}
+            ${instance.descriptive_name} is online!  ${instance.public_hostname}:${instance.config['minecraft_port']}
         % else:
             ${instance.descriptive_name} failed to start!
         % endif
 <%def name="body()">
 % if status == 'started':
     % if instance.status == 'running':
-Your presence is welcome at ${instance.descriptive_name}!  ${instance.ip_address}:${instance.config['minecraft_port']}
+Your presence is welcome at ${instance.descriptive_name}!  ${instance.public_hostname}:${instance.config['minecraft_port']}
     % else:
 ${instance.descriptive_name} wasn't able to start !   Status is ${instance.status}
     % endif