3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from __future__ import print_function
21 class VerboseLogger(object):
22 def __call__(self, msg):
26 class CLIHandler(object):
29 self.verbose_logger = VerboseLogger()
32 def _init_api(self, server, port, verbose):
35 logger = self.verbose_logger
39 server = socket.gethostname()
41 self.client = etcd.Client(server, port)
43 def set_handler(self, subparser):
44 subparser.set_defaults(handler=self)
46 def init_subparser(self, subparsers):
47 raise Error('Not implemented')
49 def __call__(self, args):
50 raise Error('Not implemented')
53 class CLILockHandler(CLIHandler):
54 def init_subparser(self, subparsers):
55 subparser = subparsers.add_parser('lock', help='Acquire a lock')
56 subparser.add_argument('--id',
63 subparser.add_argument('--timeout',
70 self.set_handler(subparser)
72 def __call__(self, args):
73 self._init_api(args.server, args.port, args.verbose)
74 lock = etcd.Lock(self.client, args.id)
75 result = lock.acquire(blocking=False, lock_ttl=args.timeout)
77 raise Exception('Lock taken!')
79 print('Lock acquired successfully!')
80 print('uuid=%s' % lock.uuid)
82 class CLIUnlockHandler(CLIHandler):
83 def init_subparser(self, subparsers):
84 subparser = subparsers.add_parser('unlock', help='Release a lock')
85 subparser.add_argument('--id',
92 subparser.add_argument('--uuid',
98 self.set_handler(subparser)
100 def __call__(self, args):
101 self._init_api(args.server, args.port, args.verbose)
102 lock = etcd.Lock(self.client, args.id)
104 lock._uuid = args.uuid
106 print('Lock released successfully!')
108 print('Lock is not held!')
110 def get_handlers_list():
112 for name, obj in inspect.getmembers(sys.modules[__name__]):
113 if inspect.isclass(obj):
114 if name is not 'CLIHandler':
115 if issubclass(obj, CLIHandler):
116 handlers.append(obj())
121 handlers = get_handlers_list()
122 for handler in handlers:
123 print('handler is ', handler)
126 if __name__ == '__main__':