From 495b58abad330d5511c1f80b07088c53461579c6 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 7 Jan 2010 21:14:00 -0500 Subject: add setting of default locale and apt-sources --- ec2-init.py | 24 +++++++++++++++++++++++- ec2init/DataSourceEc2.py | 34 +++++++++++++++++++--------------- ec2init/__init__.py | 6 ++++++ 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/ec2-init.py b/ec2-init.py index c335e7ca..6944ff54 100755 --- a/ec2-init.py +++ b/ec2-init.py @@ -34,7 +34,11 @@ def main(): raise # set the defaults (like what ec2-set-defaults.py did) - # TODO: cloud.set_defaults() + try: + generate_sources_list(cloud.get_mirror()) + apply_locale(cloud.get_locale()) + except: + warn("failed to set defaults") # set the ssh keys up cloud.apply_credentials() @@ -44,5 +48,23 @@ def main(): sys.exit(0) +def render_to_file(template, outfile, searchList): + t = Template(file='/etc/ec2-init/templates/%s.tmpl' % template, searchList=[searchList]) + f = open(outfile, 'w') + f.write(t.respond()) + f.close() + +def apply_locale(locale): + subprocess.Popen(['locale-gen', locale]).communicate() + subprocess.Popen(['update-locale', locale]).communicate() + + render_to_file('default-locale', '/etc/default/locale', { 'locale' : locale }) + +def generate_sources_list(mirror): + stdout, stderr = subprocess.Popen(['lsb_release', '-cs'], stdout=subprocess.PIPE).communicate() + codename = stdout.strip() + + render_to_file('sources.list', '/etc/apt/sources.list', { 'mirror' : mirror, 'codename' : codename }) + if __name__ == '__main__': main() diff --git a/ec2init/DataSourceEc2.py b/ec2init/DataSourceEc2.py index 5d3bab88..cdea0b3e 100644 --- a/ec2init/DataSourceEc2.py +++ b/ec2init/DataSourceEc2.py @@ -14,17 +14,12 @@ class DataSourceEc2(DataSource.DataSource): location_locale_map = { 'us' : 'en_US.UTF-8', - 'eu' : 'en_GB.UTF-8' - } - - location_archive_map = { - 'us' : 'http://us.ec2.archive.ubuntu.com/ubuntu', - 'eu' : 'http://eu.ec2.archive.ubuntu.com/ubuntu' + 'eu' : 'en_GB.UTF-8', + 'default' : 'en_US.UTF-8', } def __init__(self): - self.meta_data_base_url = 'http://169.254.169.254/%s/meta-data' % self.api_ver - self.userdata_base_url = 'http://169.254.169.254/%s/user-data' % self.api_ver + pass def get_data(self): try: @@ -77,20 +72,29 @@ class DataSourceEc2(DataSource.DataSource): # self.instance_metadata = getattr(self, 'instance_metadata', boto.utils.get_instance_metadata()) # return self.instance_metadata - def get_ami_id(self): - return self.get_instance_metadata()['ami-id'] - def get_availability_zone(self): - conn = urllib2.urlopen('%s/placement/availability-zone' % self.meta_data_base_url) - return conn.read() + return(self.metadata['placement']['availability-zone']) + + def get_local_mirror(self): + return(self.get_mirror_from_availability_zone()) + + def get_locale(self): + az = self.metadata['placement']['availability-zone'] + if self.location_locale_map.has_key[az[0:2]]: + return(self.location_locale_map[az]) + else: + return(self.location_locale_map["default"]) def get_hostname(self): - hostname = self.get_instance_metadata()['local-hostname'] + hostname = self.metadata['local-hostname'] hostname = hostname.split('.')[0] return hostname - def get_mirror_from_availability_zone(self, availability_zone): + def get_mirror_from_availability_zone(self, availability_zone = None): # availability is like 'us-west-1b' or 'eu-west-1a' + if availability_zone == None: + availability_zone = self.get_availability_zone() + try: host="%s.ec2.archive.ubuntu.com" % availability_zone[:-1] socket.getaddrinfo(host, None, 0, socket.SOCK_STREAM) diff --git a/ec2init/__init__.py b/ec2init/__init__.py index e1ae87b0..7a9c8a95 100644 --- a/ec2init/__init__.py +++ b/ec2init/__init__.py @@ -230,6 +230,12 @@ class EC2Init: def get_public_ssh_keys(self): return(self.datasource.get_public_ssh_keys()) + def get_locale(self): + return(self.datasource.get_locale()) + + def get_mirror(self): + return(self.datasource.get_local_mirror()) + def apply_credentials(self): user = self.get_cfg_option_str('user') disable_root = self.get_cfg_option_bool('disable_root', True) -- cgit v1.2.3