-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitcherctl.py
156 lines (131 loc) · 6.38 KB
/
twitcherctl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import sys
import getpass
import argcomplete
import argparse
from twitcher.client import TwitcherService
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARN)
LOGGER = logging.getLogger(__name__)
class TwitcherCtl(object):
"""
Command line to interact with the xmlrpc interface of the ``twitcher`` service.
"""
def create_parser(self):
parser = argparse.ArgumentParser(
prog="twitcherctl",
description='twitcherctl -- control twitcher service from the cmd line.',
)
parser.add_argument("--debug",
help="Enable debug mode.",
action="store_true")
parser.add_argument('-s', '--serverurl',
metavar='URL',
default='https://localhost:5000',
help='URL on which twitcher server is listening (default "https://localhost:5000").')
parser.add_argument("-u", "--username",
help="Username to use for authentication with server.")
parser.add_argument("-p", "--password",
help="Password to use for authentication with server")
parser.add_argument("-k", "--insecure", # like curl
help="Don't validate the server's certificate.",
action="store_true")
# commands
subparsers = parser.add_subparsers(
dest='cmd',
title='command',
description='List of available commands',
)
# token managment
# ---------------
# gentoken
subparser = subparsers.add_parser('gentoken', help="Generates an access token.")
subparser.add_argument('-H', '--valid-in-hours', type=int, default=1,
help="Set how long the token is valid in hours (default: 1 hour).")
subparser.add_argument('-S', '--esgf-slcs-service-url', default="https://172.28.128.3",
help="URL of ESGF SLCS service (default: https://172.28.128.3).")
subparser.add_argument('-T', '--esgf-access-token',
help="ESGF access token to retrieve a certificate from ESGF SLCS service.")
subparser.add_argument('-C', '--esgf-credentials',
help="URL pointing to ESGF credentials.")
subparser.add_argument('-e', '--env', nargs='*', default=[],
help="Set environment variable (key=value).")
# revoke
subparser = subparsers.add_parser('revoke', help="Remove given access token.")
subparser.add_argument('token', nargs="?", help="access token")
subparser.add_argument('-A', '--all', action="store_true",
help="Remove all access tokens.")
# service registry
# ----------------
# list
subparser = subparsers.add_parser('list', help="Lists all registered OWS services used by OWS proxy.")
# clear
subparser = subparsers.add_parser('clear', help="Removes all OWS services from the registry.")
# register
subparser = subparsers.add_parser('register',
help="Adds OWS service to the registry to be used by the OWS proxy.")
subparser.add_argument('url', help="Service url.")
subparser.add_argument('--name', help="Service name. If not set then a name will be generated.")
subparser.add_argument('--type', default='wps',
help="Service type (wps, wms). Default: wps.")
subparser.add_argument('--public', action='store_true',
help="If set then service has no access restrictions.")
subparser.add_argument('--auth', default='token',
help="Authentication method (token, cert). Default: token.")
# unregister
subparser = subparsers.add_parser('unregister', help="Removes OWS service from the registry.")
subparser.add_argument('name', help="Service name.")
return parser
def run(self, args):
if args.debug:
LOGGER.setLevel(logging.DEBUG)
if args.insecure:
LOGGER.warn('disabled certificate verification!')
password = args.password
if args.username:
# username = raw_input('Username:')
if not password:
password = getpass.getpass(prompt='Password:')
verify_ssl = args.insecure is False
service = TwitcherService(url=args.serverurl,
username=args.username,
password=password,
verify=verify_ssl)
result = None
try:
if args.cmd == 'list':
result = service.list_services()
elif args.cmd == 'register':
result = service.register_service(
url=args.url,
data={'name': args.name, 'type': args.type, 'public': args.public, 'auth': args.auth})
elif args.cmd == 'unregister':
result = service.unregister_service(name=args.name)
elif args.cmd == 'clear':
result = service.clear_services()
elif args.cmd == 'gentoken':
data = {k: v for k, v in (x.split('=') for x in args.env)}
if args.esgf_access_token:
data['esgf_access_token'] = args.esgf_access_token
data['esgf_slcs_service_url'] = args.esgf_slcs_service_url
if args.esgf_credentials:
data['esgf_credentials'] = args.esgf_credentials
result = service.generate_token(valid_in_hours=args.valid_in_hours, data=data)
elif args.cmd == 'revoke':
if args.all is True:
result = service.revoke_all_tokens()
else:
result = service.revoke_token(token=args.token)
except Exception as e:
LOGGER.error("Error: %s", e.message)
else:
LOGGER.info("Result: %s", result)
return result
def main():
LOGGER.setLevel(logging.INFO)
ctl = TwitcherCtl()
parser = ctl.create_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args()
return ctl.run(args)
if __name__ == '__main__':
sys.exit(main())