~soter-developers/soter/trunk

« back to all changes in this revision

Viewing changes to soter/clicommands.py

  • Committer: Damien Churchill
  • Date: 2008-06-20 14:39:53 UTC
  • Revision ID: damoxc@gmail.com-20080620143953-cxkvv86xn97svf46
convert files to using spaces over tabs to adhere to:
 http://www.python.org/dev/peps/pep-0008/
added backup/api.py, backup/rsync_backup.py and backup/common.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
write = sys.stdout.write
26
26
 
27
27
class Command:
28
 
        help = ''
29
 
        name = '[notset]'
30
 
        usage = '%prog [options]'
31
 
        
32
 
        def __init__(self):
33
 
                pass
34
 
 
35
 
        def run(self, args):
36
 
                self.parser = OptionParser(usage=self.usage,
37
 
                                                                   version='%prog ' + 
38
 
                                                                   '%s\n%s' % (__version__, __license__))
39
 
                self.before_parse()
40
 
                (self.options, self.args) = self.parser.parse_args(args)
41
 
                self.after_parse()
42
 
        
43
 
        def before_parse(self):
44
 
                pass
45
 
        
46
 
        def after_parse(self):
47
 
                pass
 
28
    help = ''
 
29
    name = '[notset]'
 
30
    usage = '%prog [options]'
 
31
 
 
32
    def __init__(self):
 
33
        pass
 
34
 
 
35
    def run(self, args):
 
36
        self.parser = OptionParser(usage=self.usage,
 
37
                                   version='%prog ' + 
 
38
                                   '%s\n%s' % (__version__, __license__))
 
39
        self.before_parse()
 
40
        (self.options, self.args) = self.parser.parse_args(args)
 
41
        self.after_parse()
 
42
 
 
43
    def before_parse(self):
 
44
        pass
 
45
 
 
46
    def after_parse(self):
 
47
        pass
48
48
 
49
49
class BackupCommand(Command):
50
 
        name = 'backup'
51
 
        help = 'Starts the backup process'
52
 
        usage = '%prog backup [options] host'
53
 
 
54
 
        def before_parse(self):
55
 
                self.parser.add_option('-a', '--all',
56
 
                                                  default=False,
57
 
                                                  action='store_true', dest='all',
58
 
                                                  #TRANSLATORS: command line option
59
 
                                                  help=('Backup all hosts'))
60
 
                self.parser.add_option('-f', '--force',
61
 
                                                  default=False,
62
 
                                                  action='store_true', dest='force',
63
 
                                                  #TRANSLATORS: command line option
64
 
                                                  help=('Force the backup to run'))
65
 
                
66
 
        def after_parse(self):
67
 
                main.backup(hosts=self.args,
68
 
                                   all=self.options.all,
69
 
                                   force=self.options.force)
 
50
    name = 'backup'
 
51
    help = 'Starts the backup process'
 
52
    usage = '%prog backup [options] host'
 
53
 
 
54
    def before_parse(self):
 
55
        self.parser.add_option('-a', '--all',
 
56
                               default=False,
 
57
                               action='store_true', dest='all',
 
58
                               #TRANSLATORS: command line option
 
59
                               help=('Backup all hosts'))
 
60
        self.parser.add_option('-f', '--force',
 
61
                               default=False,
 
62
                               action='store_true', dest='force',
 
63
                               #TRANSLATORS: command line option
 
64
                               help=('Force the backup to run'))
 
65
 
 
66
    def after_parse(self):
 
67
        main.backup(hosts=self.args,
 
68
                    all=self.options.all,
 
69
                    force=self.options.force)
70
70
__commands__.append(BackupCommand)
71
71
 
72
72
class DeployCommand(Command):
73
 
        name = 'deploy'
74
 
        help = 'Deploys configuration files'
75
 
        usage = '%prog deploy [options] host:path'
76
 
 
77
 
        def before_parse(self):
78
 
                self.parser.add_option('-a', '--all',
79
 
                                                  default=False,
80
 
                                                  action='store_true', dest='all',
81
 
                                                  #TRANSLATORS: command line option
82
 
                                                  help=('Deploy files for all hosts'))
83
 
                
84
 
        def after_parse(self):
85
 
                main.deploy(self.args, all=self.options.all)
 
73
    name = 'deploy'
 
74
    help = 'Deploys configuration files'
 
75
    usage = '%prog deploy [options] host:path'
 
76
 
 
77
    def before_parse(self):
 
78
        self.parser.add_option('-a', '--all',
 
79
                               default=False,
 
80
                               action='store_true', dest='all',
 
81
                               #TRANSLATORS: command line option
 
82
                               help=('Deploy files for all hosts'))
 
83
 
 
84
    def after_parse(self):
 
85
        main.deploy(self.args, all=self.options.all)
86
86
__commands__.append(DeployCommand)
87
87
 
88
88
class DisableCommand(Command):
89
 
        name = 'disable'
90
 
        help = 'Disables a host config file'
91
 
        usage = '%prog disable [options] [host]'
92
 
        
93
 
        def after_parse(self):
94
 
                if len(self.args) > 0:
95
 
                        for host in self.args:
96
 
                                self.disable_host(host)
97
 
                else:
98
 
                        enabled = os.listdir(config.enabled_path)
99
 
                        available = os.listdir(config.available_path)
100
 
                        
101
 
                        if len(available) == 0:
102
 
                                print 'No hosts exist!'
103
 
                                exit(0)
104
 
 
105
 
                        if len(enabled) == 0:
106
 
                                print 'No hosts enabled!'
107
 
                                exit(0)
108
 
                        
109
 
                        print 'Which host would you like to disable?'
110
 
                        write('Your choices are: ')
111
 
                        for host in enabled:
112
 
                                if enabled.index(host) == 0:
113
 
                                        write(host)
114
 
                                else:
115
 
                                        write(', ' + host)
116
 
                        write('\nHost name: ')
117
 
                        host = raw_input()
118
 
                        self.disable_host(host)
119
 
 
120
 
        def disable_host(self, name):
121
 
                available = os.path.join(config.available_path, name)
122
 
                enabled = os.path.join(config.enabled_path, name)
123
 
                if not os.path.exists(available):
124
 
                        print >> sys.stderr, 'This host does not exist!'
125
 
                        exit(1)
126
 
                        
127
 
                if not os.path.exists(enabled):
128
 
                        print 'This host is already disabled!'
129
 
                        exit(0)
130
 
                
131
 
                os.remove(enabled)              
132
 
                print 'Host %s disabled' % name         
 
89
    name = 'disable'
 
90
    help = 'Disables a host config file'
 
91
    usage = '%prog disable [options] [host]'
 
92
 
 
93
    def after_parse(self):
 
94
        if len(self.args) > 0:
 
95
            for host in self.args:
 
96
                self.disable_host(host)
 
97
        else:
 
98
            enabled = os.listdir(config.enabled_path)
 
99
            available = os.listdir(config.available_path)
 
100
 
 
101
            if len(available) == 0:
 
102
                print 'No hosts exist!'
 
103
                exit(0)
 
104
 
 
105
            if len(enabled) == 0:
 
106
                print 'No hosts enabled!'
 
107
                exit(0)
 
108
 
 
109
            print 'Which host would you like to disable?'
 
110
            write('Your choices are: ')
 
111
            for host in enabled:
 
112
                if enabled.index(host) == 0:
 
113
                    write(host)
 
114
                else:
 
115
                    write(', ' + host)
 
116
            write('\nHost name: ')
 
117
            host = raw_input()
 
118
            self.disable_host(host)
 
119
 
 
120
    def disable_host(self, name):
 
121
        available = os.path.join(config.available_path, name)
 
122
        enabled = os.path.join(config.enabled_path, name)
 
123
        if not os.path.exists(available):
 
124
            print >> sys.stderr, 'This host does not exist!'
 
125
            exit(1)
 
126
 
 
127
        if not os.path.exists(enabled):
 
128
            print 'This host is already disabled!'
 
129
            exit(0)
 
130
 
 
131
        os.remove(enabled)              
 
132
        print 'Host %s disabled' % name         
133
133
__commands__.append(DisableCommand)
134
134
 
135
135
class EnableCommand(Command):
136
 
        name = 'enable'
137
 
        help = 'Enables a host config file'
138
 
        usage = '%prog enable [options] [host]'
139
 
 
140
 
        def after_parse(self):
141
 
                if len(self.args) > 0:
142
 
                        for host in self.args:
143
 
                                self.enable_host(host)
144
 
                else:
145
 
                        enabled = os.listdir(config.enabled_path)
146
 
                        available = os.listdir(config.available_path)
147
 
                        
148
 
                        if len(available) == 0:
149
 
                                print 'No hosts exist!'
150
 
                                exit(0)
151
 
                        hosts = []
152
 
                        
153
 
                        for host in available:
154
 
                                if not host.startswith('.') and host not in enabled:
155
 
                                        hosts.append(host)
156
 
                        
157
 
                        print 'Which host would you like to enable?'
158
 
                        write('Your choices are: ')
159
 
                        for host in hosts:
160
 
                                if hosts.index(host) == 0:
161
 
                                        write(host)
162
 
                                else:
163
 
                                        write(', ' + host)
164
 
                        write('\nHost name: ')
165
 
                        host = raw_input()
166
 
                        self.enable_host(host)
167
 
 
168
 
        def enable_host(self, name):
169
 
                available = os.path.join(config.available_path, name)
170
 
                enabled = os.path.join(config.enabled_path, name)
171
 
                if not os.path.exists(available):
172
 
                        print >> sys.stderr, 'This host does not exist!'
173
 
                        exit(1)
174
 
 
175
 
                if os.path.exists(enabled):
176
 
                        print 'This host is already enabled!'
177
 
                        exit(0)
178
 
 
179
 
                os.symlink(available, enabled)
180
 
                print 'Host %s enabled' % name
 
136
    name = 'enable'
 
137
    help = 'Enables a host config file'
 
138
    usage = '%prog enable [options] [host]'
 
139
 
 
140
    def after_parse(self):
 
141
        if len(self.args) > 0:
 
142
            for host in self.args:
 
143
                self.enable_host(host)
 
144
        else:
 
145
            enabled = os.listdir(config.enabled_path)
 
146
            available = os.listdir(config.available_path)
 
147
 
 
148
            if len(available) == 0:
 
149
                print 'No hosts exist!'
 
150
                exit(0)
 
151
            hosts = []
 
152
 
 
153
            for host in available:
 
154
                if not host.startswith('.') and host not in enabled:
 
155
                    hosts.append(host)
 
156
 
 
157
            print 'Which host would you like to enable?'
 
158
            write('Your choices are: ')
 
159
            for host in hosts:
 
160
                if hosts.index(host) == 0:
 
161
                    write(host)
 
162
                else:
 
163
                    write(', ' + host)
 
164
            write('\nHost name: ')
 
165
            host = raw_input()
 
166
            self.enable_host(host)
 
167
 
 
168
    def enable_host(self, name):
 
169
        available = os.path.join(config.available_path, name)
 
170
        enabled = os.path.join(config.enabled_path, name)
 
171
        if not os.path.exists(available):
 
172
            print >> sys.stderr, 'This host does not exist!'
 
173
            exit(1)
 
174
 
 
175
        if os.path.exists(enabled):
 
176
            print 'This host is already enabled!'
 
177
            exit(0)
 
178
 
 
179
        os.symlink(available, enabled)
 
180
        print 'Host %s enabled' % name
181
181
__commands__.append(EnableCommand)
182
182
 
183
183
class RestoreCommand(Command):
184
 
        name = 'restore'
185
 
        help = 'Restores a hosts files'
186
 
        usage = '%prog restore [options] host:path ...'
187
 
 
188
 
        def before_parse(self):
189
 
                self.parser.add_option('-a', '--all',
190
 
                                                  default=False,
191
 
                                                  action='store_true', dest='all',
192
 
                                                  #TRANSLATORS: command line option
193
 
                                                  help=('Restore all files for all hosts'))
194
 
                
195
 
                self.parser.add_option('--all-files',
196
 
                                                  default=False,
197
 
                                                  action='store_true', dest='all_files',
198
 
                                                  #TRANSLATORS: command line option
199
 
                                                  help=('Restore all files for the host'))
200
 
 
201
 
                self.parser.add_option('-r', '--revision',
202
 
                                                  default=False,
203
 
                                                  action='store', dest='revision',
204
 
                                                  #TRANSLATORS: command line option
205
 
                                                  help=('Snapshot version to use for restore'))
206
 
                
207
 
        def after_parse(self):
208
 
                main.restore(self.args, all=self.options.all)
 
184
    name = 'restore'
 
185
    help = 'Restores a hosts files'
 
186
    usage = '%prog restore [options] host:path ...'
 
187
 
 
188
    def before_parse(self):
 
189
        self.parser.add_option('-a', '--all',
 
190
                               default=False,
 
191
                               action='store_true', dest='all',
 
192
                               #TRANSLATORS: command line option
 
193
                               help=('Restore all files for all hosts'))
 
194
 
 
195
        self.parser.add_option('--all-files',
 
196
                               default=False,
 
197
                               action='store_true', dest='all_files',
 
198
                               #TRANSLATORS: command line option
 
199
                               help=('Restore all files for the host'))
 
200
 
 
201
        self.parser.add_option('-r', '--revision',
 
202
                               default=False,
 
203
                               action='store', dest='revision',
 
204
                               #TRANSLATORS: command line option
 
205
                               help=('Snapshot version to use for restore'))
 
206
 
 
207
    def after_parse(self):
 
208
        main.restore(self.args, all=self.options.all)
209
209
__commands__.append(RestoreCommand)
210
210
 
211
211
class SetupCommand(Command):
212
 
        name = 'setup'
213
 
        help = 'Prepares the host for being backed up'
214
 
        usage = '%prog setup [options] host'
215
 
        
216
 
        def before_parse(self):
217
 
                self.parser.add_option('-a', '--all',
218
 
                                                           default=False,
219
 
                                                           action='store_true', dest='all',
220
 
                                                           #TRANSLATORS: command line option
221
 
                                                           help=('Deploy key for all hosts'))
222
 
                self.parser.add_option('-p', '--pass',
223
 
                                                           default=None,
224
 
                                                           action='store', dest='password',
225
 
                                                           #TRANSLATORS: command line option
226
 
                                                           help=('Set the password to use'))
227
 
        
228
 
        def after_parse(self):
229
 
                main.setup(self.args, self.options)
 
212
    name = 'setup'
 
213
    help = 'Prepares the host for being backed up'
 
214
    usage = '%prog setup [options] host'
 
215
 
 
216
    def before_parse(self):
 
217
        self.parser.add_option('-a', '--all',
 
218
                               default=False,
 
219
                               action='store_true', dest='all',
 
220
                               #TRANSLATORS: command line option
 
221
                               help=('Deploy key for all hosts'))
 
222
        self.parser.add_option('-p', '--pass',
 
223
                               default=None,
 
224
                               action='store', dest='password',
 
225
                               #TRANSLATORS: command line option
 
226
                               help=('Set the password to use'))
 
227
 
 
228
    def after_parse(self):
 
229
        main.setup(self.args, self.options)
230
230
__commands__.append(SetupCommand)
231
231
 
232
232
class StatusCommand(Command):
233
 
        name = 'status'
234
 
        help = 'Report some usage statistics'
235
 
        usage = '%prog status [options]'
236
 
        def after_parse(self):
237
 
                main.status(self.args, self.options)
 
233
    name = 'status'
 
234
    help = 'Report some usage statistics'
 
235
    usage = '%prog status [options]'
 
236
    def after_parse(self):
 
237
        main.status(self.args, self.options)
238
238
__commands__.append(StatusCommand)
239
239
 
240
240
def get_commands():
241
 
        __commands__.sort(key=attrgetter('name'))
242
 
        return __commands__
 
241
    __commands__.sort(key=attrgetter('name'))
 
242
    return __commands__
243
243
 
244
244
def get_command(name):
245
 
        if name == '[notset]':
246
 
                raise Exception('invalid command name')
 
245
    if name == '[notset]':
 
246
        raise Exception('invalid command name')
247
247
 
248
 
        for command in __commands__:
249
 
                if command.name == name:
250
 
                        return command
251
 
        raise Exception('command not found')
 
 
b'\\ No newline at end of file'
 
248
    for command in __commands__:
 
249
        if command.name == name:
 
250
            return command
 
251
    raise Exception('command not found')
 
 
b'\\ No newline at end of file'