Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 3 | import string |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 4 | import sys |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 5 | import xmlrpclib |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 6 | |
| 7 | if __name__ == '__main__': |
Scott Baker | eaeabad | 2015-02-09 11:18:46 -0800 | [diff] [blame] | 8 | sys.path.append("/opt/xos") |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 10 | |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 11 | from xos.config import Config |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 12 | from core.models import Service |
| 13 | from hpc.models import HpcService |
| 14 | from requestrouter.models import RequestRouterService |
| 15 | from util.logger import Logger, logging |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 19 | class 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 | |
| 35 | class 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 Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 56 | class HpcLibrary: |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 57 | 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 Baker | 18ce715 | 2014-01-06 16:59:36 -0800 | [diff] [blame] | 66 | return y[:20] |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 67 | |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 68 | 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 Baker | be8f850 | 2014-08-19 17:22:30 -0700 | [diff] [blame] | 87 | if ("coblitz" in name) or ("hpc" in name): |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 88 | 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 Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 115 | 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 Baker | 5bee893 | 2014-01-03 12:00:59 -0800 | [diff] [blame] | 122 | 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 Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 129 | mapping = self.extract_slice_info(hpc_service) |
| 130 | rr_mapping = self.extract_slice_info(rr_service) |
| 131 | |
| 132 | mapping.update(rr_mapping) |
| 133 | |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 134 | fn = "/tmp/slices" |
| 135 | |
| 136 | f = open(fn, "w") |
| 137 | f.write(""" |
| 138 | ENABLE_PLC=True |
| 139 | ENABLE_PS=False |
| 140 | BASE_HRN="%(base_hrn)s" |
| 141 | RELEVANT_SERVICE_NAMES=['%(service_coblitz)s', '%(service_dnsredir)s', '%(service_dnsdemux)s'] |
| 142 | COBLITZ_SLICE_NAME="%(slice_coblitz)s" |
| 143 | COBLITZ_SLICE_ID=1 |
| 144 | COBLITZ_PS_SLICE_NAME="%(slice_coblitz)s" |
| 145 | DNSREDIR_SLICE_NAME="%(slice_dnsredir)s" |
| 146 | DNSREDIR_SLICE_ID=2 |
| 147 | DNSREDIR_PS_SLICE_NAME="%(slice_dnsredir)s" |
| 148 | DNSDEMUX_SLICE_NAME="%(slice_dnsdemux)s" |
| 149 | DNSDEMUX_SLICE_ID=3 |
| 150 | DNSDEMUX_PS_SLICE_NAME="%(slice_dnsdemux)s" |
| 151 | CMI_URL="http://%(hostname_cmi)s" |
| 152 | CMI_HTTP_PORT="8004" |
| 153 | CMI_HTTPS_PORT="8003" |
| 154 | PUPPET_MASTER_HOSTNAME="%(hostname_cmi)s" |
| 155 | PUPPET_MASTER_PORT="8140" |
| 156 | """ % mapping) |
| 157 | |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 158 | @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 Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 164 | if __name__ == '__main__': |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 165 | print "testing write_slices_file" |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 166 | lib = HpcLibrary() |
Scott Baker | 5bee893 | 2014-01-03 12:00:59 -0800 | [diff] [blame] | 167 | lib.write_slices_file() |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 168 | |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 169 | print "testing API connection" |
Scott Baker | e99e3df | 2014-01-06 16:05:46 -0800 | [diff] [blame] | 170 | lib.client.cob.GetNewObjects() |
| 171 | lib.client.onev.ListAll("CDN") |
Scott Baker | 9570303 | 2014-01-06 14:00:17 -0800 | [diff] [blame] | 172 | |
| 173 | |
| 174 | |
Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 175 | |