~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/boto/boto/pyami/bootstrap.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
28
class Bootstrap(ScriptBase):
 
29
    """
 
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
 
33
    as the root user.
 
34
 
 
35
    The main purpose of this class is to make sure the boto distribution
 
36
    on the instance is the one required.
 
37
    """
 
38
 
 
39
    def __init__(self):
 
40
        self.working_dir = '/mnt/pyami'
 
41
        self.write_metadata()
 
42
        ScriptBase.__init__(self)
 
43
 
 
44
    def write_metadata(self):
 
45
        fp = open(os.path.expanduser(BotoConfigPath), 'w')
 
46
        fp.write('[Instance]\n')
 
47
        inst_data = get_instance_metadata()
 
48
        for key in inst_data:
 
49
            fp.write('%s = %s\n' % (key, inst_data[key]))
 
50
        user_data = get_instance_userdata()
 
51
        fp.write('\n%s\n' % user_data)
 
52
        fp.write('[Pyami]\n')
 
53
        fp.write('working_dir = %s\n' % self.working_dir)
 
54
        fp.close()
 
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()
 
59
        boto.init_logging()
 
60
 
 
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)
 
65
 
 
66
    def load_boto(self):
 
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
 
72
            else:
 
73
                version = '-rHEAD'
 
74
            location = boto.config.get('Boto', 'boto_location', '/usr/local/boto')
 
75
            self.run('svn update %s %s' % (version, location))
 
76
        else:
 
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)
 
80
 
 
81
    def fetch_s3_file(self, s3_file):
 
82
        try:
 
83
            if s3_file.startswith('s3:'):
 
84
                bucket_name, key_name = s3_file[len('s3:'):].split('/')
 
85
                c = boto.connect_s3()
 
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)
 
91
        except:
 
92
            boto.log.exception('Problem Retrieving file: %s' % s3_file)
 
93
            path = None
 
94
        return path
 
95
 
 
96
    def load_packages(self):
 
97
        package_str = boto.config.get('Pyami', 'packages')
 
98
        if package_str:
 
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)
 
104
                if 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)
 
109
 
 
110
    def main(self):
 
111
        self.create_working_dir()
 
112
        self.load_boto()
 
113
        self.load_packages()
 
114
        self.notify('Bootstrap Completed for %s' % boto.config.get_instance('instance-id'))
 
115
 
 
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')
 
120
    bs = Bootstrap()
 
121
    bs.main()