From e1d74134dfb6cc36a434042df04acc660d84c724 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Mon, 10 Aug 2009 14:02:01 +0200 Subject: 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__. --- ec2init/__init__.py | 23 +++++++++++++++-------- 1 file 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 . # -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] -- cgit v1.2.3