1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
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
28
class Bootstrap(ScriptBase):
30
The Bootstrap class is instantiated and run as part of the PyAMI
31
instance initialization process. The methods in this class will
32
be run from the rc.local script of the instance and will be run
35
The main purpose of this class is to make sure the boto distribution
36
on the instance is the one required.
40
self.working_dir = '/mnt/pyami'
42
ScriptBase.__init__(self)
44
def write_metadata(self):
45
fp = open(os.path.expanduser(BotoConfigPath), 'w')
46
fp.write('[Instance]\n')
47
inst_data = get_instance_metadata()
49
fp.write('%s = %s\n' % (key, inst_data[key]))
50
user_data = get_instance_userdata()
51
fp.write('\n%s\n' % user_data)
53
fp.write('working_dir = %s\n' % self.working_dir)
55
# This file has the AWS credentials, should we lock it down?
56
# os.chmod(BotoConfigPath, stat.S_IREAD | stat.S_IWRITE)
57
# now that we have written the file, read it into a pyami Config object
58
boto.config = Config()
61
def create_working_dir(self):
62
boto.log.info('Working directory: %s' % self.working_dir)
63
if not os.path.exists(self.working_dir):
64
os.mkdir(self.working_dir)
67
update = boto.config.get('Boto', 'boto_update', 'svn:HEAD')
68
if update.startswith('svn'):
69
if update.find(':') >= 0:
70
method, version = update.split(':')
71
version = '-r%s' % version
74
location = boto.config.get('Boto', 'boto_location', '/usr/local/boto')
75
self.run('svn update %s %s' % (version, location))
77
# first remove the symlink needed when running from subversion
78
self.run('rm /usr/local/lib/python2.5/site-packages/boto')
79
self.run('easy_install %s' % update)
81
def fetch_s3_file(self, s3_file):
83
if s3_file.startswith('s3:'):
84
bucket_name, key_name = s3_file[len('s3:'):].split('/')
86
bucket = c.get_bucket(bucket_name)
87
key = bucket.get_key(key_name)
88
boto.log.info('Fetching %s/%s' % (bucket.name, key.name))
89
path = os.path.join(self.working_dir, key.name)
90
key.get_contents_to_filename(path)
92
boto.log.exception('Problem Retrieving file: %s' % s3_file)
96
def load_packages(self):
97
package_str = boto.config.get('Pyami', 'packages')
99
packages = package_str.split(',')
100
for package in packages:
101
package = package.strip()
102
if package.startswith('s3:'):
103
package = self.fetch_s3_file(package)
105
# if the "package" is really a .py file, it doesn't have to
106
# be installed, just being in the working dir is enough
107
if not package.endswith('.py'):
108
self.run('easy_install -Z %s' % package, exit_on_error=False)
111
self.create_working_dir()
114
self.notify('Bootstrap Completed for %s' % boto.config.get_instance('instance-id'))
116
if __name__ == "__main__":
117
# because bootstrap starts before any logging configuration can be loaded from
118
# the boto config files, we will manually enable logging to /var/log/boto.log
119
boto.set_file_logger('bootstrap', '/var/log/boto.log')