OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 import os |
| 23 import boto |
| 24 from boto.utils import get_instance_metadata, get_instance_userdata |
| 25 from boto.pyami.config import Config, BotoConfigPath |
| 26 from boto.pyami.scriptbase import ScriptBase |
| 27 import time |
| 28 |
| 29 class Bootstrap(ScriptBase): |
| 30 """ |
| 31 The Bootstrap class is instantiated and run as part of the PyAMI |
| 32 instance initialization process. The methods in this class will |
| 33 be run from the rc.local script of the instance and will be run |
| 34 as the root user. |
| 35 |
| 36 The main purpose of this class is to make sure the boto distribution |
| 37 on the instance is the one required. |
| 38 """ |
| 39 |
| 40 def __init__(self): |
| 41 self.working_dir = '/mnt/pyami' |
| 42 self.write_metadata() |
| 43 ScriptBase.__init__(self) |
| 44 |
| 45 def write_metadata(self): |
| 46 fp = open(os.path.expanduser(BotoConfigPath), 'w') |
| 47 fp.write('[Instance]\n') |
| 48 inst_data = get_instance_metadata() |
| 49 for key in inst_data: |
| 50 fp.write('%s = %s\n' % (key, inst_data[key])) |
| 51 user_data = get_instance_userdata() |
| 52 fp.write('\n%s\n' % user_data) |
| 53 fp.write('[Pyami]\n') |
| 54 fp.write('working_dir = %s\n' % self.working_dir) |
| 55 fp.close() |
| 56 # This file has the AWS credentials, should we lock it down? |
| 57 # os.chmod(BotoConfigPath, stat.S_IREAD | stat.S_IWRITE) |
| 58 # now that we have written the file, read it into a pyami Config object |
| 59 boto.config = Config() |
| 60 boto.init_logging() |
| 61 |
| 62 def create_working_dir(self): |
| 63 boto.log.info('Working directory: %s' % self.working_dir) |
| 64 if not os.path.exists(self.working_dir): |
| 65 os.mkdir(self.working_dir) |
| 66 |
| 67 def load_boto(self): |
| 68 update = boto.config.get('Boto', 'boto_update', 'svn:HEAD') |
| 69 if update.startswith('svn'): |
| 70 if update.find(':') >= 0: |
| 71 method, version = update.split(':') |
| 72 version = '-r%s' % version |
| 73 else: |
| 74 version = '-rHEAD' |
| 75 location = boto.config.get('Boto', 'boto_location', '/usr/local/boto
') |
| 76 self.run('svn update %s %s' % (version, location)) |
| 77 elif update.startswith('git'): |
| 78 location = boto.config.get('Boto', 'boto_location', '/usr/share/pyth
on-support/python-boto/boto') |
| 79 num_remaining_attempts = 10 |
| 80 while num_remaining_attempts > 0: |
| 81 num_remaining_attempts -= 1 |
| 82 try: |
| 83 self.run('git pull', cwd=location) |
| 84 num_remaining_attempts = 0 |
| 85 except Exception, e: |
| 86 boto.log.info('git pull attempt failed with the following ex
ception. Trying again in a bit. %s', e) |
| 87 time.sleep(2) |
| 88 if update.find(':') >= 0: |
| 89 method, version = update.split(':') |
| 90 else: |
| 91 version = 'master' |
| 92 self.run('git checkout %s' % version, cwd=location) |
| 93 else: |
| 94 # first remove the symlink needed when running from subversion |
| 95 self.run('rm /usr/local/lib/python2.5/site-packages/boto') |
| 96 self.run('easy_install %s' % update) |
| 97 |
| 98 def fetch_s3_file(self, s3_file): |
| 99 try: |
| 100 from boto.utils import fetch_file |
| 101 f = fetch_file(s3_file) |
| 102 path = os.path.join(self.working_dir, s3_file.split("/")[-1]) |
| 103 open(path, "w").write(f.read()) |
| 104 except: |
| 105 boto.log.exception('Problem Retrieving file: %s' % s3_file) |
| 106 path = None |
| 107 return path |
| 108 |
| 109 def load_packages(self): |
| 110 package_str = boto.config.get('Pyami', 'packages') |
| 111 if package_str: |
| 112 packages = package_str.split(',') |
| 113 for package in packages: |
| 114 package = package.strip() |
| 115 if package.startswith('s3:'): |
| 116 package = self.fetch_s3_file(package) |
| 117 if package: |
| 118 # if the "package" is really a .py file, it doesn't have to |
| 119 # be installed, just being in the working dir is enough |
| 120 if not package.endswith('.py'): |
| 121 self.run('easy_install -Z %s' % package, exit_on_error=F
alse) |
| 122 |
| 123 def main(self): |
| 124 self.create_working_dir() |
| 125 self.load_boto() |
| 126 self.load_packages() |
| 127 self.notify('Bootstrap Completed for %s' % boto.config.get_instance('ins
tance-id')) |
| 128 |
| 129 if __name__ == "__main__": |
| 130 # because bootstrap starts before any logging configuration can be loaded fr
om |
| 131 # the boto config files, we will manually enable logging to /var/log/boto.lo
g |
| 132 boto.set_file_logger('bootstrap', '/var/log/boto.log') |
| 133 bs = Bootstrap() |
| 134 bs.main() |
OLD | NEW |