/
swarmtoolscore.py
94 lines (82 loc) · 3.45 KB
/
swarmtoolscore.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
#!/usr/bin/python
import ConfigParser
import os.path
import httplib
import base64
import json
my_working_directory = os.path.abspath(os.path.dirname(__file__)+"/../")
def get_server_info():
config = ConfigParser.ConfigParser()
raw_data = config.read("%s/swarm.cfg"%(my_working_directory))
hostname = config.get("Server Information", "hostname")
return {"hostname": hostname}
def get_user_info():
config = ConfigParser.ConfigParser()
raw_data = config.read("%s/swarm.cfg"%(my_working_directory))
user_id = config.get("User Information", "user_id")
return {"user_id": user_id}
def get_keys():
config = ConfigParser.ConfigParser()
raw_data = config.read("%s/swarm.cfg"%(my_working_directory))
configuration = config.get("Keys", "configuration")
participation = config.get("Keys", "participation")
return {"configuration": configuration, "participation": participation}
def set_server_info(hostname):
config = ConfigParser.ConfigParser()
config.read("%s/swarm.cfg"%(my_working_directory))
config.set("Server Information", "hostname", hostname)
with open("swarm.cfg", "wb") as configfile:
config.write(configfile)
def set_user_info(user_id):
config = ConfigParser.ConfigParser()
config.read("%s/swarm.cfg"%(my_working_directory))
config.set("User Information", "user_id", user_id)
with open("swarm.cfg", "wb") as configfile:
config.write(configfile)
def set_keys(hostname, user_id, password):
config = ConfigParser.ConfigParser()
config.read("%s/swarm.cfg"%(my_working_directory))
resp = get_keys_from_server(hostname, user_id, password)
if resp.status == 404:
print "Status is 404"
create_key(hostname, user_id, password, "configuration")
create_key(hostname, user_id, password, "participation")
resp = get_keys_from_server(hostname, user_id, password)
txt = resp.read()
if resp.status >= 400:
print "Something bad happened: "
print resp.status, txt
json_obj = json.loads(txt)
for key_obj in json_obj:
key_type = key_obj["type"]
key_value = key_obj["key"]
config.set("Keys", key_type, key_value)
if config.has_option("Keys", "configuration") == False:
key_obj = create_key(hostname, user_id, password, "configuration")
config.set("Keys", "configuration", key_obj["key"])
if config.has_option("Keys", "participation") == False:
key_obj = create_key(hostname, user_id, password, "participation")
config.set("Keys", "participation", key_obj["key"])
with open("swarm.cfg", "wb") as configfile:
config.write(configfile)
def create_key(hostname, user_id, password, key_type):
conn = httplib.HTTPConnection(hostname)
auth_hash = user_id + ":" + password
auth_header = "Basic " + base64.b64encode(auth_hash)
if (key_type != None):
conn.request("POST", "/keys/" + key_type, None, {"Authorization":auth_header})
else:
conn.request("POST", "/keys", None, {"Authorization":auth_header})
resp = conn.getresponse()
txt = resp.read()
conn.close()
json_obj = json.loads(txt)
return json_obj
def get_keys_from_server(hostname, user_id, password):
conn = httplib.HTTPConnection(hostname)
auth_hash = user_id + ":" + password
auth_header = "Basic " + base64.b64encode(auth_hash)
conn.request("GET", "/keys", None, {"Authorization":auth_header})
resp = conn.getresponse()
conn.close()
return resp