diff options
author | Soren Hansen <soren@canonical.com> | 2009-08-10 14:02:01 +0200 |
---|---|---|
committer | Soren Hansen <soren@canonical.com> | 2009-08-10 14:02:01 +0200 |
commit | e1d74134dfb6cc36a434042df04acc660d84c724 (patch) | |
tree | 01ca9bbd2694ebd72ad341c8436a616d023c327b /ec2init/__init__.py | |
parent | 19023c5df38964a53b63f77943e3ac1d02d95d6c (diff) | |
download | vyos-cloud-init-e1d74134dfb6cc36a434042df04acc660d84c724.tar.gz vyos-cloud-init-e1d74134dfb6cc36a434042df04acc660d84c724.zip |
Use urllib2 instead of urllib so that HTTP errors appear as exceptions.
Add a new function: wait_or_bail which will wait for the meta-data service
or call the bailout command if the meta-data service never shows up.
Remove the wait_or_bail code from __init__.
Diffstat (limited to 'ec2init/__init__.py')
-rw-r--r-- | ec2init/__init__.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/ec2init/__init__.py b/ec2init/__init__.py index e75e6cbe..729e3a48 100644 --- a/ec2init/__init__.py +++ b/ec2init/__init__.py @@ -17,24 +17,30 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -from configobj import ConfigObj import os import socket +import urllib2 +from configobj import ConfigObj import boto.utils class EC2Init(): api_ver = '2008-02-01' - filename = '/etc/ec2-init/ec2-config.cfg' + conffile = '/etc/ec2-init/ec2-config.cfg' def __init__(self): self.meta_data_base_url = 'http://169.254.169.254/%s/meta-data' % self.api_ver self.user_data_base_url = 'http://169.254.169.254/%s/user-data' % self.api_ver - self.config = ConfigObj(filename) - self.wait_for_metadata_service() - bailout_command = self.get_cfg_option_str('bailout_command') - if bailout_command: - os.system(bailout_command) + self.config = ConfigObj(self.conffile) + + def wait_or_bail(self): + if self.wait_for_metadata_service(): + return True + else: + bailout_command = self.get_cfg_option_str('bailout_command') + if bailout_command: + os.system(bailout_command) + return False def get_cfg_option_bool(self, key): val = self.config[key] @@ -46,7 +52,8 @@ class EC2Init(): return config[key] def get_ssh_keys(self): - data = urllib.urlopen('%s/public-keys/' % self.meta_data_base_url).read() + conn = urllib2.urlopen('%s/public-keys/' % self.meta_data_base_url) + data = conn.read() keyids = [line.split('=')[0] for line in data.split('\n')] return [urllib.urlopen('%s/public-keys/%d/openssh-key' % (self.meta_data_base_url, int(keyid))).read().rstrip() for keyid in keyids] |