blob: 798654086acd74b1c74d9bd65e56a86ec6267883 [file] [log] [blame]
Scott Bakerb30aa082014-01-03 08:36:00 -08001import os
2import base64
Scott Bakere99e3df2014-01-06 16:05:46 -08003import string
Scott Bakerb30aa082014-01-03 08:36:00 -08004import sys
Scott Baker95703032014-01-06 14:00:17 -08005import xmlrpclib
Scott Bakerb30aa082014-01-03 08:36:00 -08006
7if __name__ == '__main__':
Scott Baker166f4b82015-02-09 11:18:46 -08008 sys.path.append("/opt/xos")
Scott Bakerb30aa082014-01-03 08:36:00 -08009 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
10
11from planetstack.config import Config
12from core.models import Service
13from hpc.models import HpcService
14from requestrouter.models import RequestRouterService
15from util.logger import Logger, logging
16
17logger = Logger(level=logging.INFO)
18
Scott Baker95703032014-01-06 14:00:17 -080019class APIHelper:
20 def __init__(self, proxy, auth, method=None):
21 self.proxy = proxy
22 self.auth = auth
23 self.method = method
24
25 def __getattr__(self, name):
26 if name.startswith("_"):
27 return getattr(self, name)
28 else:
29 return APIHelper(self.proxy, self.auth, name)
30
31 def __call__(self, *args):
32 method = getattr(self.proxy, self.method)
33 return method(self.auth, *args)
34
35class CmiClient:
36 def __init__(self, hostname, port=8003, username="apiuser", password="apiuser"):
37 self.connect_api(hostname, port, username, password)
38
39 def connect_api(self, hostname, port=8003, username="apiuser", password="apiuser"):
40 #print "https://%s:%d/COAPI/" % (hostname, port)
41 cob = xmlrpclib.ServerProxy("https://%s:%d/COAPI/" % (hostname, port), allow_none=True)
42 cob_auth = {}
43 cob_auth["Username"] = username
44 cob_auth["AuthString"] = password
45 cob_auth["AuthMethod"] = "password"
46
47 onev = xmlrpclib.ServerProxy("https://%s:%d/ONEV_API/" % (hostname, port), allow_none=True)
48 onev_auth = {}
49 onev_auth["Username"] = username
50 onev_auth["AuthString"] = password
51 onev_auth["AuthMethod"] = "password"
52
53 self.cob = APIHelper(cob, cob_auth)
54 self.onev = APIHelper(onev, onev_auth)
55
Scott Bakerb30aa082014-01-03 08:36:00 -080056class HpcLibrary:
Scott Bakere99e3df2014-01-06 16:05:46 -080057 def __init__(self):
58 self._client = None
59
60 def make_account_name(self, x):
61 x=x.lower()
62 y = ""
63 for c in x:
64 if (c in (string.lowercase + string.digits)):
65 y = y + c
Scott Baker18ce7152014-01-06 16:59:36 -080066 return y[:20]
Scott Bakere99e3df2014-01-06 16:05:46 -080067
Scott Bakerb30aa082014-01-03 08:36:00 -080068 def extract_slice_info(self, service):
69 """ Produce a dict that describes the slices for the CMI
70
71 slice_coblitz = <name of coblitz slice>
72 service_coblitz = <name of coblitz service>
73 hostname_coblitz = <name of first coblitz slice>
74 hostnames_coblitz = <name_of_first_cob_slice>,<name_of_second_cob_slice>,...
75
76 slice_cmi = <name of cmi slice>
77 ...
78 """
79
80 slicenames = {}
81 slicehosts = {}
82 for slice in service.service.all():
83 name = slice.name
84 if not ("_" in name):
85 continue
86
Scott Bakerbe8f8502014-08-19 17:22:30 -070087 if ("coblitz" in name) or ("hpc" in name):
Scott Bakerb30aa082014-01-03 08:36:00 -080088 slicenames["coblitz"] = name
89 slicehosts["coblitz"] = [sliver.node.name for sliver in slice.slivers.all()]
90 elif "cmi" in name:
91 slicenames["cmi"] = name
92 slicehosts["cmi"] = [sliver.node.name for sliver in slice.slivers.all()]
93 elif "dnsredir" in name:
94 slicenames["dnsredir"] = name
95 slicehosts["dnsredir"] = [sliver.node.name for sliver in slice.slivers.all()]
96 elif "dnsdemux" in name:
97 slicenames["dnsdemux"] = name
98 slicehosts["dnsdemux"] = [sliver.node.name for sliver in slice.slivers.all()]
99
100 base_hrn = None
101 if "coblitz" in slicenames:
102 base_hrn = slicenames["coblitz"].split("_")[0]
103
104 mapping = {}
105 mapping["base_hrn"] = base_hrn
106 for (k,v) in slicenames.items():
107 mapping["slice_" + k] = v
108 mapping["service_" + k] = v.split("_",1)[1]
109 for (k,v) in slicehosts.items():
110 mapping["hostname_" + k] = v[0]
111 mapping["hostnames_" + k] = ",".join(v)
112
113 return mapping
114
Scott Baker95703032014-01-06 14:00:17 -0800115 def get_cmi_hostname(self, hpc_service=None):
116 if (hpc_service is None):
117 hpc_service = HpcService.objects.get()
118
119 slice_info = self.extract_slice_info(hpc_service)
120 return slice_info["hostname_cmi"]
121
Scott Baker5bee8932014-01-03 12:00:59 -0800122 def write_slices_file(self, hpc_service=None, rr_service=None):
123 if (hpc_service is None):
124 hpc_service = HpcService.objects.get()
125
126 if (rr_service is None):
127 rr_service = RequestRouterService.objects.get()
128
Scott Bakerb30aa082014-01-03 08:36:00 -0800129 mapping = self.extract_slice_info(hpc_service)
130 rr_mapping = self.extract_slice_info(rr_service)
131
132 mapping.update(rr_mapping)
133
Scott Bakerb30aa082014-01-03 08:36:00 -0800134 fn = "/tmp/slices"
135
136 f = open(fn, "w")
137 f.write("""
138ENABLE_PLC=True
139ENABLE_PS=False
140BASE_HRN="%(base_hrn)s"
141RELEVANT_SERVICE_NAMES=['%(service_coblitz)s', '%(service_dnsredir)s', '%(service_dnsdemux)s']
142COBLITZ_SLICE_NAME="%(slice_coblitz)s"
143COBLITZ_SLICE_ID=1
144COBLITZ_PS_SLICE_NAME="%(slice_coblitz)s"
145DNSREDIR_SLICE_NAME="%(slice_dnsredir)s"
146DNSREDIR_SLICE_ID=2
147DNSREDIR_PS_SLICE_NAME="%(slice_dnsredir)s"
148DNSDEMUX_SLICE_NAME="%(slice_dnsdemux)s"
149DNSDEMUX_SLICE_ID=3
150DNSDEMUX_PS_SLICE_NAME="%(slice_dnsdemux)s"
151CMI_URL="http://%(hostname_cmi)s"
152CMI_HTTP_PORT="8004"
153CMI_HTTPS_PORT="8003"
154PUPPET_MASTER_HOSTNAME="%(hostname_cmi)s"
155PUPPET_MASTER_PORT="8140"
156""" % mapping)
157
Scott Bakere99e3df2014-01-06 16:05:46 -0800158 @property
159 def client(self):
160 if self._client is None:
161 self._client = CmiClient(self.get_cmi_hostname())
162 return self._client
163
Scott Bakerb30aa082014-01-03 08:36:00 -0800164if __name__ == '__main__':
Scott Baker95703032014-01-06 14:00:17 -0800165 print "testing write_slices_file"
Scott Bakerb30aa082014-01-03 08:36:00 -0800166 lib = HpcLibrary()
Scott Baker5bee8932014-01-03 12:00:59 -0800167 lib.write_slices_file()
Scott Bakerb30aa082014-01-03 08:36:00 -0800168
Scott Baker95703032014-01-06 14:00:17 -0800169 print "testing API connection"
Scott Bakere99e3df2014-01-06 16:05:46 -0800170 lib.client.cob.GetNewObjects()
171 lib.client.onev.ListAll("CDN")
Scott Baker95703032014-01-06 14:00:17 -0800172
173
174
Scott Bakerb30aa082014-01-03 08:36:00 -0800175