1import os
2import sys
3from boto.utils import ShellCommand, get_ts
4import boto
5import boto.utils
6
7class ScriptBase(object):
8
9    def __init__(self, config_file=None):
10        self.instance_id = boto.config.get('Instance', 'instance-id', 'default')
11        self.name = self.__class__.__name__
12        self.ts = get_ts()
13        if config_file:
14            boto.config.read(config_file)
15
16    def notify(self, subject, body=''):
17        boto.utils.notify(subject, body)
18
19    def mkdir(self, path):
20        if not os.path.isdir(path):
21            try:
22                os.mkdir(path)
23            except:
24                boto.log.error('Error creating directory: %s' % path)
25
26    def umount(self, path):
27        if os.path.ismount(path):
28            self.run('umount %s' % path)
29
30    def run(self, command, notify=True, exit_on_error=False, cwd=None):
31        self.last_command = ShellCommand(command, cwd=cwd)
32        if self.last_command.status != 0:
33            boto.log.error('Error running command: "%s". Output: "%s"' % (command, self.last_command.output))
34            if notify:
35                self.notify('Error encountered',
36                            'Error running the following command:\n\t%s\n\nCommand output:\n\t%s' % \
37                            (command, self.last_command.output))
38            if exit_on_error:
39                sys.exit(-1)
40        return self.last_command.status
41
42    def main(self):
43        pass
44