/
Client.py
49 lines (43 loc) · 1.48 KB
/
Client.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
"""
Client module forms requests and process obtained responses.
"""
import getopt
import sys
import simplejson
import logging
import ConfigParser
from webob import Request
import confLogging
def printHelp():
print 'Usage:'
print 'Use -h to display help'
print 'Use --action to define action. Allowed actions:'
print '"crt" - create VM; "del" - delete existing VM;'
print '"on", "off", "reboot" - change state of existing VM;'
print 'Allowed arguments: --name - name of existing VM, '
print '--id - id of existing VM, --os - OS for new VM'
def main(argv):
args, opts = getopt.getopt(argv, 'h', ['action=', 'name=',
'id=', 'os='])
keys = [arg[0] for arg in args]
if '-h' in keys or not '--action' in keys:
return printHelp()
prc = ConfigParser.RawConfigParser()
prc.read('defConfig.ini')
actionMap = dict(prc.items('ACTIONMAP'))
usrConfig = {k[2:]: v for (k, v) in args}
if not usrConfig['action'] in actionMap:
return printHelp()
req = Request.blank('http://localhost:8080')
req.content_type = 'application/json'
req.body = simplejson.dumps(usrConfig)
resp = req.get_response()
res = simplejson.loads(resp.body)
logger = logging.getLogger('manageVM.client')
logger = confLogging.configLog(logger)
logger.info(res['result'])
if res['error'] != None:
logger.error(res['message'])
printHelp()
if __name__ == '__main__':
main(sys.argv[1:])