-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgonjacutils.py
135 lines (108 loc) · 3.53 KB
/
gonjacutils.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
import os
import sys
import re
import ConfigParser
import random
import string
import gonjacerrors
try:
import psutil
except Exception:
sys.stderr.write(gonjacerrors.INSTALL_PSUTIL)
sys.exit(-1)
import gonjacserver
import gonjaccore
import gonjacreqs
class GonjacConfig(object):
confpath = gonjaccore.CONF
remote_ip = None
sysinfo = None
conf = None
@classmethod
def read(cls):
cls.conf = ConfigParser.ConfigParser()
cls.conf.read(cls.confpath)
cls.remote_ip = cls.conf.get("remote", "ip")
cls.sysinfo = cls.conf.get("sysinfo", "platform")
@classmethod
def set_remote_ip(cls, ip):
cls.read()
cls.conf.set("remote", "ip", ip)
cls.conf.write(open(gonjaccore.CONF, 'w'))
cls.read()
@classmethod
def get_remote_ip(cls):
cls.read()
return cls.conf.get("remote", "ip").strip('"')
@classmethod
def get_platform(cls):
cls.read()
return cls.conf.get("sysinfo", "platform").strip('"')
def get_random_id(size=10):
chars = string.ascii_uppercase
chars = chars + string.digits
return ''.join(random.choice(chars) for x in range(size))
def is_valid_ip(str_ip):
if re.match(gonjaccore.IP_REGEX, str_ip):
return True
else:
return False
def get_new_config():
try:
os.remove(gonjaccore.CONF)
except IOException:
pass
return dict()
def get_config():
config = ConfigParser.ConfigParser()
config.read(gonjaccore.CONF)
return config
def set_config_opt(config, section, key, value):
config.set(section, key, value)
def flush_config():
pass
def is_req_regack(req):
if req.type == gonjacreqs.REMOTE_REGACK:
return True
else:
return False
def validate_local_keys(req):
for k in req.get_dict().keys():
if k not in gonjacreqs.VALID_LOCAL_REQ_KEYS:
raise ValueError("Invalid local request key: %s" % k)
def validate_local_type(req):
if not req.type in gonjacreqs.VALID_LOCAL_REQ_TYPES:
raise ValueError("Invalid local request type: %s" % req.type)
def validate_register(req):
if not req[gonjacreqs.GENERIC_KEY_IP]:
req[gonjacreqs.GENERIC_KEY_IP] = \
GonjacConfig.get_remote_ip()
if not req[gonjacreqs.GENERIC_KEY_PLATFORM]:
req[gonjacreqs.GENERIC_KEY_PLATFORM] = \
GonjacConfig.get_platform()
def validate_unregister(req):
if not req[gonjacreqs.GENERIC_KEY_IP]:
req[gonjacreqs.GENERIC_KEY_IP] = \
GonjacConfig.get_remote_ip()
def validate_testconn(req):
if not req[gonjacreqs.GENERIC_KEY_IP]:
req[gonjacreqs.GENERIC_KEY_IP] = \
GonjacConfig.get_remote_ip()
def validate_remote_keys(req, remote_client):
for k in req.get_dict().keys():
if k not in gonjacreqs.VALID_REMOTE_REQ_KEYS:
errstring = "Invalid remote request key: %s" % k
remote_client.send(errstring)
raise ValueError(errstring)
def validate_remote_type(req, remote_client):
errstring = "Invalid remote request type: %s" % req.type
if not req.type in gonjacreqs.VALID_REMOTE_REQ_TYPES:
if remote_client:
remote_client.send(errstring)
raise ValueError(errstring)
def validate_remote_newjob(req, remote_client):
if not req[gonjacreqs.GENERIC_REPO]:
remote_client.send("No repo specified")
if not req[gonjacreqs.GENERIC_BRANCH]:
remote_client.send("No branch specified")
raise ValueError("No branch specified")