aboutsummaryrefslogtreecommitdiff
blob: 49dcb83399142c4340f1502d55e2714cd5f26d8e (plain)
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
#!/usr/bin/env python

import sys
import json
import argparse
import ConfigParser
import urllib, httplib
from gentoostats.payload import Payload

def getAuthInfo(auth):
    """
    Read auth config
    """
    config = ConfigParser.ConfigParser()
    if len(config.read(auth)) == 0:
        sys.stderr.write('Cannot read ' + auth)
        sys.exit(1)

    try:
        uuid = config.get('AUTH', 'UUID')
        passwd = config.get('AUTH', 'PASSWD')
        auth_info = {'UUID' : uuid, 'PASSWD' : passwd}
        return auth_info
    except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
        sys.stderr.write('Malformed auth config')
        sys.exit(1)

def serialize(object, human=False):
    """
    Encode using json
    """
    if human:
        indent = 2
        sort_keys = True
    else:
        indent = None
        sort_keys = False
    return json.JSONEncoder(indent=indent, sort_keys=sort_keys).encode(object)

def main():
    parser = argparse.ArgumentParser(description='Gentoostats client')
    parser.add_argument('-s', '--server', default='soc.dev.gentoo.org')
    parser.add_argument('-p', '--port', type = int, default=443)
    parser.add_argument('-u', '--url', default='/gentoostats')
    parser.add_argument('-a', '--auth', default='/etc/gentoostats/auth.cfg')
    parser.add_argument('-c', '--config', default='/etc/gentoostats/payload.cfg')
    parser.add_argument('-v', '--verbose', action='store_true')
    args = vars(parser.parse_args())

    pl = Payload(configfile=args['config'])

    if args['verbose']:
        pl.dump(human=True)

    post_data = pl.get()
    post_data['AUTH'] = getAuthInfo(auth=args['auth'])

    post_url = args['url'].strip('/')
    if not len(post_url) == 0:
        post_url = '/' + post_url
    post_url = post_url + '/host/' + post_data['AUTH']['UUID']

    post_body = serialize(post_data, human=True)
    post_headers = {'Content-type':'application/json'}

    conn = httplib.HTTPSConnection(args['server'] + ':' + str(args['port']))
    conn.request('POST', url=post_url, headers=post_headers, body=post_body)

    #TODO: Handle exceptions
    response = conn.getresponse()
    print response.status, response.reason
    print 'Server response: ' + response.read()

if __name__ == "__main__":
    main()