~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/mapreduce/pdb_revert

  • 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
#!/usr/bin/env python
 
2
# Copyright (c) 2006-2008 Mitch Garnaat http://garnaat.org/
 
3
#
 
4
# Permission is hereby granted, free of charge, to any person obtaining a
 
5
# copy of this software and associated documentation files (the
 
6
# "Software"), to deal in the Software without restriction, including
 
7
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
8
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
9
# persons to whom the Software is furnished to do so, subject to the fol-
 
10
# lowing conditions:
 
11
#
 
12
# The above copyright notice and this permission notice shall be included
 
13
# in all copies or substantial portions of the Software.
 
14
#
 
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
16
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
17
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
18
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
19
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
21
# IN THE SOFTWARE.
 
22
import queuetools, os, signal, sys
 
23
import subprocess
 
24
import time
 
25
from optparse import OptionParser
 
26
from boto.mapreduce.partitiondb import PartitionDB, Partition, Version
 
27
from lqs import LQSServer, PersistHandler
 
28
from boto.exception import SDBPersistenceError
 
29
from boto.sdb.persist import get_manager
 
30
 
 
31
USAGE = """
 
32
  SYNOPSIS
 
33
    %prog [options] [command]
 
34
  DESCRIPTION
 
35
    Revert to the previous Version in a PartitionDB.
 
36
"""
 
37
class Client:
 
38
 
 
39
    def __init__(self, queue_name):
 
40
        self.q = queuetools.get_queue(queue_name)
 
41
        self.q.connect()
 
42
        self.manager = get_manager()
 
43
        self.process()
 
44
 
 
45
    def process(self):
 
46
        m = self.q.get()
 
47
        while m['item']:
 
48
            print 'Deleting: %s' % m['item']
 
49
            p = Partition(id=m['item'], manager=self.manager)
 
50
            p.delete()
 
51
            self.q.delete(m)
 
52
            m = self.q.get()
 
53
        print 'client processing complete'
 
54
 
 
55
class Server:
 
56
 
 
57
    def __init__(self, pdb_name, domain_name=None):
 
58
        self.pdb_name = pdb_name
 
59
        self.manager = get_manager(domain_name)
 
60
        self.pdb = PartitionDB.get(name=self.pdb_name)
 
61
        self.serve()
 
62
 
 
63
    def serve(self):
 
64
        v = self.pdb.revert()
 
65
        args = {'v_id' : v.id}
 
66
        rs = v.partitions()
 
67
        s = LQSServer('', PersistHandler, rs, args)
 
68
        s.serve_forever()
 
69
    
 
70
class Revert:
 
71
 
 
72
    Commands = {'client' : 'Start a Revert client',
 
73
                'server' : 'Start a Revert server'}
 
74
 
 
75
    def __init__(self):
 
76
        self.parser = OptionParser(usage=USAGE)
 
77
        self.parser.add_option("--help-commands", action="store_true", dest="help_commands",
 
78
                               help="provides help on the available commands")
 
79
        self.parser.add_option('-d', '--domain-name', action='store', type='string',
 
80
                               help='name of the SimpleDB domain where PDB objects are stored')
 
81
        self.parser.add_option('-n', '--num-processes', action='store', type='int', dest='num_processes',
 
82
                               help='the number of client processes launched')
 
83
        self.parser.set_defaults(num_processes=5)
 
84
        self.parser.add_option('-p', '--pdb-name', action='store', type='string',
 
85
                               help='name of the PDB in which to store files (will create if necessary)')
 
86
        self.options, self.args = self.parser.parse_args()
 
87
        self.prog_name = sys.argv[0]
 
88
 
 
89
    def print_command_help(self):
 
90
        print '\nCommands:'
 
91
        for key in self.Commands.keys():
 
92
            print '  %s\t\t%s' % (key, self.Commands[key])
 
93
 
 
94
    def do_server(self):
 
95
        if not self.options.pdb_name:
 
96
            self.parser.error('No PDB name provided')
 
97
        s = Server(self.options.pdb_name, self.options.domain_name)
 
98
 
 
99
    def do_client(self):
 
100
        c = Client('localhost')
 
101
        
 
102
    def main(self):
 
103
        if self.options.help_commands:
 
104
            self.print_command_help()
 
105
            sys.exit(0)
 
106
        if len(self.args) == 0:
 
107
            if not self.options.pdb_name:
 
108
                self.parser.error('No PDB name provided')
 
109
            server_command = '%s -p %s ' % (self.prog_name, self.options.pdb_name)
 
110
            server_command += ' server'
 
111
            client_command = '%s client' % self.prog_name
 
112
            server = subprocess.Popen(server_command, shell=True)
 
113
            print 'server pid: %s' % server.pid
 
114
            time.sleep(5)
 
115
            clients = []
 
116
            for i in range(0, self.options.num_processes):
 
117
                client = subprocess.Popen(client_command, shell=True)
 
118
                clients.append(client)
 
119
            print 'waiting for clients to finish'
 
120
            for client in clients:
 
121
                client.wait()
 
122
            os.kill(server.pid, signal.SIGTERM)
 
123
        elif len(self.args) == 1:
 
124
            self.command = self.args[0]
 
125
            if hasattr(self, 'do_%s' % self.command):
 
126
                method = getattr(self, 'do_%s' % self.command)
 
127
                method()
 
128
            else:
 
129
                self.parser.error('command (%s) not recognized' % self.command)
 
130
        else:
 
131
            self.parser.error('unrecognized commands')
 
132
            
 
133
if __name__ == "__main__":
 
134
    revert = Revert()
 
135
    revert.main()