PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/boto-2.5.2/boto/pyami/installers/ubuntu/installer.py

#
Python | 96 lines | 72 code | 1 blank | 23 comment | 0 complexity | 40777b8c78716980c2feaf64fd0af714 MD5 | raw file
 1# Copyright (c) 2006,2007,2008 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#
22import boto.pyami.installers
23import os
24import os.path
25import stat
26import boto
27import random
28from pwd import getpwnam
29
30class Installer(boto.pyami.installers.Installer):
31    """
32    Base Installer class for Ubuntu-based AMI's
33    """
34    def add_cron(self, name, command, minute="*", hour="*", mday="*", month="*", wday="*", who="root", env=None):
35        """
36        Write a file to /etc/cron.d to schedule a command
37            env is a dict containing environment variables you want to set in the file
38            name will be used as the name of the file
39        """
40        if minute == 'random':
41            minute = str(random.randrange(60))
42        if hour == 'random':
43            hour = str(random.randrange(24))
44        fp = open('/etc/cron.d/%s' % name, "w")
45        if env:
46            for key, value in env.items():
47                fp.write('%s=%s\n' % (key, value))
48        fp.write('%s %s %s %s %s %s %s\n' % (minute, hour, mday, month, wday, who, command))
49        fp.close()
50
51    def add_init_script(self, file, name):
52        """
53        Add this file to the init.d directory
54        """
55        f_path = os.path.join("/etc/init.d", name)
56        f = open(f_path, "w")
57        f.write(file)
58        f.close()
59        os.chmod(f_path, stat.S_IREAD| stat.S_IWRITE | stat.S_IEXEC)
60        self.run("/usr/sbin/update-rc.d %s defaults" % name)
61
62    def add_env(self, key, value):
63        """
64        Add an environemnt variable
65        For Ubuntu, the best place is /etc/environment.  Values placed here do
66        not need to be exported.
67        """
68        boto.log.info('Adding env variable: %s=%s' % (key, value))
69        if not os.path.exists("/etc/environment.orig"):
70            self.run('cp /etc/environment /etc/environment.orig', notify=False, exit_on_error=False)
71        fp = open('/etc/environment', 'a')
72        fp.write('\n%s="%s"' % (key, value))
73        fp.close()
74        os.environ[key] = value
75
76    def stop(self, service_name):
77        self.run('/etc/init.d/%s stop' % service_name)
78
79    def start(self, service_name):
80        self.run('/etc/init.d/%s start' % service_name)
81
82    def create_user(self, user):
83        """
84        Create a user on the local system
85        """
86        self.run("useradd -m %s" % user)
87        usr =  getpwnam(user)
88        return usr
89
90
91    def install(self):
92        """
93        This is the only method you need to override
94        """
95        raise NotImplementedError
96