Initial commit
[ta/lockcli.git] / src / lockcli / handlers.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from __future__ import print_function
16 import sys
17 import inspect
18 import etcd
19
20
21 class VerboseLogger(object):
22     def __call__(self, msg):
23         print(msg)
24
25
26 class CLIHandler(object):
27     def __init__(self):
28         self.sock = None
29         self.verbose_logger = VerboseLogger()
30         self.client = None
31
32     def _init_api(self, server, port, verbose):
33         logger = None
34         if verbose:
35             logger = self.verbose_logger
36
37         if server == 'none':
38             import socket
39             server = socket.gethostname()
40
41         self.client = etcd.Client(server, port)
42
43     def set_handler(self, subparser):
44         subparser.set_defaults(handler=self)
45
46     def init_subparser(self, subparsers):
47         raise Error('Not implemented')
48
49     def __call__(self, args):
50         raise Error('Not implemented')
51
52
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',
57                                required=True,
58                                dest='id',
59                                metavar='ID',
60                                type=str,
61                                action='store')
62
63         subparser.add_argument('--timeout',
64                                required=True,
65                                dest='timeout',
66                                metavar='TIMEOUT',
67                                type=int,
68                                action='store')
69
70         self.set_handler(subparser)
71
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)
76         if not result:
77             raise Exception('Lock taken!')
78
79         print('Lock acquired successfully!')
80         print('uuid=%s' % lock.uuid)
81
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',
86                                required=True,
87                                dest='id',
88                                metavar='ID',
89                                type=str,
90                                action='store')
91
92         subparser.add_argument('--uuid',
93                                required=True,
94                                dest='uuid',
95                                metavar='UUID',
96                                type=str,
97                                action='store')
98         self.set_handler(subparser)
99
100     def __call__(self, args):
101         self._init_api(args.server, args.port, args.verbose)
102         lock = etcd.Lock(self.client, args.id)
103         try:
104             lock._uuid = args.uuid
105             lock.release()
106             print('Lock released successfully!')
107         except ValueError:
108             print('Lock is not held!')
109
110 def get_handlers_list():
111     handlers = []
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())
117     return handlers
118
119
120 def main():
121     handlers = get_handlers_list()
122     for handler in handlers:
123         print('handler is ', handler)
124
125
126 if __name__ == '__main__':
127     main()