summaryrefslogtreecommitdiff
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
commit9d82957478e99356fe367627a34b0ca5146027a0 (patch)
tree839d46c3211c2724ecafa32beaeac0cf0ab4519f
parentea2681ea30d397b7df3f67e12a86f18f0e623d21 (diff)
downloadcloud-init-git-9d82957478e99356fe367627a34b0ca5146027a0.tar.gz
* Add more smarts to ec2 instance bring up. (LP: #371936)0.3.4ubuntu3
-rw-r--r--debian/changelog6
-rwxr-xr-xec2-fetch-credentials.py27
-rwxr-xr-xec2-set-apt-sources.py24
-rwxr-xr-xec2-set-defaults.py16
-rwxr-xr-xec2-set-hostname.py44
5 files changed, 91 insertions, 26 deletions
diff --git a/debian/changelog b/debian/changelog
index b45b28c5..a99b17fc 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+ec2-init (0.3.4ubuntu1) karmic; urgency=low
+
+ * Add more smarts to ec2 instance bring up. (LP: #371936)
+
+ -- Chuck Short <zulcss@ubuntu.com> Tue, 05 May 2009 08:59:54 -0400
+
ec2-init (0.3.3ubuntu12) jaunty; urgency=low
* ec2-run-user-data.py: Fix error.
diff --git a/ec2-fetch-credentials.py b/ec2-fetch-credentials.py
index 68922878..05bc7a9c 100755
--- a/ec2-fetch-credentials.py
+++ b/ec2-fetch-credentials.py
@@ -38,7 +38,7 @@ def get_ssh_keys():
keyids = [line.split('=')[0] for line in data.split('\n')]
return [urllib.urlopen('%s/public-keys/%d/openssh-key' % (base_url, int(keyid))).read().rstrip() for keyid in keyids]
-def setup_user_keys(k,user):
+def setup_user_keys(k,user,filename):
if not os.path.exists('/home/%s/.ssh' %(user)):
os.mkdir('/home/%s/.ssh' %(user))
@@ -47,6 +47,7 @@ def setup_user_keys(k,user):
fp.write(''.join(['%s\n' % key for key in keys]))
fp.close()
os.system('chown -R %s:%s /home/%s/.ssh' %(user,user,user))
+ os.system('touch %s' %(filename))
def setup_root_user(k,root_config):
if root_config == "1":
@@ -72,11 +73,21 @@ def checkServer():
print "!!! Unable to connect to %s" % address
sys.exit(0)
-os.umask(077)
-if user == "":
- print "User must exist in %s" %(filename)
- sys.exit(0)
+def get_ami_id():
+ url = 'http://169.254.169.254/%s/meta-data' % api_ver
+ ami_id = urllib.urlopen('%s/ami-id/' %url).read()
+ return ami_id
-keys = get_ssh_keys()
-setup_user_keys(keys,user)
-setup_root_user(keys,config_root)
+amid = get_ami_id()
+filename = '/var/ec2/.ssh-keys-ran.%s' %amid
+if os.path.exists(filename):
+ print "ec2-fetch-credentials already ran....skipping."
+else:
+ os.umask(077)
+ if user == "":
+ print "User must exist in %s" %(filename)
+ sys.exit(0)
+
+ keys = get_ssh_keys()
+ setup_user_keys(keys,user,filename)
+ setup_root_user(keys,config_root)
diff --git a/ec2-set-apt-sources.py b/ec2-set-apt-sources.py
index 46ca0a48..8efb5896 100755
--- a/ec2-set-apt-sources.py
+++ b/ec2-set-apt-sources.py
@@ -37,8 +37,7 @@ def checkServer():
sys.exit(0)
def detectZone():
- api_ver = '2008-02-01'
- metadat = None
+ api_ver = '2008-02-01'
base_url = 'http://169.254.169.254/%s/meta-data' % api_ver
zone = urllib.urlopen('%s/placement/availability-zone' % base_url).read()
@@ -49,7 +48,7 @@ def detectZone():
return(archive)
-def updateList():
+def updateList(filename):
mirror = detectZone()
if not os.path.exists("/var/run/ec2/sources.lists"):
t = os.popen("lsb_release -c").read()
@@ -69,5 +68,22 @@ def updateList():
prog = apt.progress.FetchProgress()
cache.update(prog)
+ os.system('touch %s' %(filename))
+
+def get_ami_id():
+ api_ver = '2008-02-01'
+
+ url = 'http://169.254.169.254/%s/meta-data' % api_ver
+ ami_id = urllib.urlopen('%s/ami-id/' %url).read()
+ return ami_id
+
+
checkServer()
-updateList()
+
+ami_id = get_ami_id()
+filename = '/var/ec2/.apt-already-ran.%s' %ami_id
+
+if os.path.exists(filename):
+ print "ec2-set-apt-sources already ran....skipping."
+else:
+ updateList(filename)
diff --git a/ec2-set-defaults.py b/ec2-set-defaults.py
index 7d410031..09234d0d 100755
--- a/ec2-set-defaults.py
+++ b/ec2-set-defaults.py
@@ -33,7 +33,7 @@ if zone.startswith("us"):
elif zone.startswith("eu"):
archive = "http://eu.ec2.archive.ubuntu.com/ubuntu"
-def set_language(location):
+def set_language(location,filename):
if location.startswith("us"):
lang='en_US.UTF-8'
os.system('locale-gen %s 2>&1 > /dev/null' %(lang))
@@ -42,5 +42,17 @@ def set_language(location):
lang='en_GB.UTF-8'
os.system('locale-gen %s 2>&1 > /dev/null' %(lang))
os.system('update-locale %s 2>&1 > /dev/null' %(lang))
+ os.system('touch %s' %(filename))
-set_language(zone)
+def get_amid():
+ url = 'http://169.254.169.254/%s/meta-data' % api_ver
+ ami_id = urllib.urlopen('%s/ami-id/' %url).read()
+ return ami_id
+
+ami = get_amid()
+filename = '/var/ec2/.defaults-already-ran.%s' %ami
+
+if os.path.exists(filename):
+ print "ec2-set-defaults already ran...skipping"
+else:
+ set_language(zone,filename)
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)