summaryrefslogtreecommitdiff
path: root/ec2-set-hostname.py
diff options
context:
space:
mode:
authorChuck Short <zulcss@ubuntu.com>2009-05-05 09:59:54 +0200
committerChuck Short <zulcss@ubuntu.com>2009-05-05 09:59:54 +0200
commit322bf4c779a378f113e6bf60714c66298aba876d (patch)
tree839d46c3211c2724ecafa32beaeac0cf0ab4519f /ec2-set-hostname.py
parentac53fe3d7599356b1f8c9851113212b68a5ab667 (diff)
downloadvyos-cloud-init-322bf4c779a378f113e6bf60714c66298aba876d.tar.gz
vyos-cloud-init-322bf4c779a378f113e6bf60714c66298aba876d.zip
* Add more smarts to ec2 instance bring up. (LP: #371936)
Diffstat (limited to 'ec2-set-hostname.py')
-rwxr-xr-xec2-set-hostname.py44
1 files changed, 32 insertions, 12 deletions
diff --git a/ec2-set-hostname.py b/ec2-set-hostname.py
index 6858a8b0..9abb8877 100755
--- a/ec2-set-hostname.py
+++ b/ec2-set-hostname.py
@@ -25,15 +25,35 @@ from Cheetah.Template import Template
api_ver = '2008-02-01'
metadata = None
-base_url = 'http://169.254.169.254/%s/meta-data' % api_ver
-my_hostname = urllib.urlopen('%s/local-hostname/' % base_url).read()
-os.system('hostname %s' % my_hostname)
-
-# replace the ubuntu hostname in /etc/hosts
-mp = {'hostname': my_hostname}
-t = Template(file="/etc/ec2-init/templates/hosts.tmpl", searchList=[mp])
-
-os.system("rm /etc/hosts")
-f = open("/etc/hosts", "w")
-f.write('%s' %(t))
-f.close()
+def get_ami_id():
+ api_ver = '2008-02-01'
+ metadata = None
+
+ url = 'http://169.254.169.254/%s/meta-data' % api_ver
+ ami_id = urllib.urlopen('%s/ami-id/' %url).read()
+ return ami_id
+
+def set_hostname(filename):
+ api_ver = '2008-02-01'
+ metadata = None
+
+ base_url = 'http://169.254.169.254/%s/meta-data' % api_ver
+ my_hostname = urllib.urlopen('%s/local-hostname/' % base_url).read()
+ os.system('hostname %s' % my_hostname)
+
+ # replace the ubuntu hostname in /etc/hosts
+ mp = {'hostname': my_hostname}
+ t = Template(file="/etc/ec2-init/templates/hosts.tmpl", searchList=[mp])
+
+ os.system("rm /etc/hosts")
+ f = open("/etc/hosts", "w")
+ f.write('%s' %(t))
+ f.close()
+ os.system('touch %s' %(filename))
+
+id = get_ami_id()
+filename = '/var/ec2/.hostname-already-ran.%s' %id
+if os.path.exists(filename):
+ print "hostname already set previously"
+else:
+ set_hostname(filename)