modfied to include the os qualified domain name for public API.
[joid.git] / ci / deploy.py
1 import yaml
2 import pprint
3 import socket
4 import fcntl
5 import struct
6 import os
7 import getpass
8
9 def get_ip_address(ifname):
10     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
11     return socket.inet_ntoa(fcntl.ioctl(
12         s.fileno(),
13         0x8915,  # SIOCGIFADDR
14         struct.pack('256s', ifname[:15])
15     )[20:24])
16
17 with open('labconfig.yaml', 'r') as labf:
18     labcfg = yaml.load(labf)
19
20 opnfvcfg={}
21 opnfvlabcfg={}
22
23 def getFromDict(dataDict, mapList):
24     return reduce(lambda d, k: d[k], mapList, dataDict)
25
26 #lets define the bootstrap section
27 opnfvcfg['demo-maas']={'juju-bootstrap':{'memory': 4096,'name': "bootstrap",\
28                                          'pool': "default", 'vcpus': 4,\
29                                          'disk_size': "60G", 'arch': "amd64",\
30                                          'interfaces':[]},\
31                        'maas':{'memory': 4096,'pool': "default", 'vcpus': 4,\
32                                'disk_size': "160G", 'arch': "amd64", 'interfaces':[],\
33                                'name':"",'network_config':[],'node_group_ifaces':[],\
34                                'nodes':[],'password': 'ubuntu', 'user':'ubuntu',\
35                                'release': 'trusty', 'apt_sources':[],'ip_address':'',\
36                                'boot_source':{'keyring_filename':\
37                                                 "/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg",\
38                                              'url': \
39                                              "http://maas.ubuntu.com/images/ephemeral-v2/releases/",\
40                                              'selections':{'1':{'arches':'amd64','labels':'release',\
41                                                                 'os':'ubuntu','release':'xenial',\
42                                                                 'subarches':'*'
43                                                                }\
44                                                           }\
45                                              },\
46                               'settings':{'maas_name':'','upstream_dns':'',\
47                                           'main_archive':"http://us.archive.ubuntu.com/ubuntu"\
48                                          },\
49                               'virsh':{'rsa_priv_key':'/home/ubuntu/.ssh/id_rsa',
50                                        'rsa_pub_key':'/home/ubuntu/.ssh/id_rsa.pub',
51                                        'uri':''
52                                       }\
53                               }\
54                       }
55
56 opnfvlabcfg['opnfv']={'ext-port':'','floating-ip-range':'','dataNetwork':'','ceph-disk':'/srv/',\
57                       'storageNetwork':'','interface-enable':'','publicNetwork':'',\
58                       'os-domain-name':''}
59
60 opnfvcfg['demo-maas']['maas']['apt_sources'].append("ppa:maas/stable")
61 opnfvcfg['demo-maas']['maas']['apt_sources'].append("ppa:juju/stable")
62
63 # lets modify the maas general settings:
64
65 updns = getFromDict(labcfg, ["lab","racks",0,"dns"])
66 opnfvcfg["demo-maas"]["maas"]["settings"]["upstream_dns"]=updns
67
68 location = getFromDict(labcfg, ["lab","location"])
69 rack = getFromDict(labcfg, ["lab","racks",0,"rack"])
70 value=location+rack
71
72 opnfvcfg["demo-maas"]["maas"]["settings"]["maas_name"]=value
73 opnfvcfg["demo-maas"]["maas"]["name"]="opnfv-"+value
74
75 ethbrAdm=""
76 ethbrAdmin=""
77
78 c=0
79 y=0
80 #z=0
81
82 while c < len(labcfg["opnfv"]["spaces"]):
83     brtype = getFromDict(labcfg, ["opnfv","spaces",c,"type"])
84     brname = getFromDict(labcfg, ["opnfv","spaces",c,"bridge"])
85     brcidr = getFromDict(labcfg, ["opnfv","spaces",c,"cidr"])
86 #
87     if brtype == "admin":
88         ethbrAdmin = getFromDict(labcfg, ["opnfv","spaces",c,"bridge"])
89         brgway = getFromDict(labcfg, ["opnfv","spaces",c,"gateway"])
90         tmpcidr = brcidr[:-4]
91         opnfvlabcfg["opnfv"]["admNetwork"]=tmpcidr+"2"
92         opnfvlabcfg["opnfv"]["admNetgway"]=brgway
93
94         nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
95                    "broadcast_ip": tmpcidr+"255", "router_ip": brgway,\
96                    "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
97                    "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
98
99         ethbrAdm = ('auto lo\n'
100                     '    iface lo inet loopback\n\n'
101                     'auto eth'+str(y)+'\n'
102                     '    iface eth'+str(y)+' inet static\n'
103                     '    address '+tmpcidr+'5\n'
104                     '    netmask 255.255.255.0\n'
105                     '    gateway '+brgway+'\n'
106                     '    dns-nameservers '+updns+' '+tmpcidr+'5 127.0.0.1\n')
107
108         opnfvcfg['demo-maas']['maas']['ip_address']=tmpcidr+"5"
109         opnfvcfg['demo-maas']['maas']['interfaces'].append("bridge="+brname+",model=virtio")
110         opnfvcfg['demo-maas']['juju-bootstrap']['interfaces'].append("bridge="+brname+",model=virtio")
111         opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"].append(nodegroup)
112         y=y+1
113     elif brtype:
114         opnfvcfg["demo-maas"]["maas"]["interfaces"].append("bridge="+brname+",model=virtio")
115         brgway = getFromDict(labcfg, ["opnfv","spaces",c,"gateway"])
116         if brtype != "external":
117             tmpcidr = brcidr[:-4]
118             if brgway:
119                 nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
120                            "broadcast_ip": tmpcidr+"255", "management": 1, "router_ip": brgway,\
121                            "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
122                            "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
123             else:
124                 nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
125                            "broadcast_ip": tmpcidr+"255", "management": 1, \
126                            "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
127                            "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
128             opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"].append(nodegroup)
129             ethbrAdm  = (ethbrAdm+'\n'
130                         'auto eth'+str(y)+'\n'
131                         '    iface eth'+str(y)+' inet static\n'
132                         '    address '+tmpcidr+'5\n'
133                         '    netmask 255.255.255.0\n')
134             y=y+1
135         if brtype == "public":
136             opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"].append("bridge="+brname+",model=virtio")
137             opnfvlabcfg["opnfv"]["publicNetwork"]=brcidr
138         if brtype == "external":
139             ipaddress = getFromDict(labcfg, ["opnfv","spaces",c,"ipaddress"])
140             ethbrAdm  = (ethbrAdm+'\n'
141                         'auto eth'+str(y)+'\n'
142                         '    iface eth'+str(y)+' inet static\n'
143                         '    address '+ipaddress+'\n'
144                         '    netmask 255.255.255.0\n')
145             opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"].append("bridge="+brname+",model=virtio")
146         if brtype == "data":
147             opnfvlabcfg["opnfv"]["dataNetwork"]=brcidr
148         if brtype == "storage":
149             opnfvlabcfg["opnfv"]["storageNetwork"]=brcidr
150
151     c=c+1
152
153 # lets modify the maas general settings:
154 value = get_ip_address(ethbrAdmin)
155 value = "qemu+ssh://"+getpass.getuser()+"@"+value+"/system"
156 opnfvcfg['demo-maas']['maas']['virsh']['uri']=value
157 opnfvcfg['demo-maas']['maas']['network_config']=ethbrAdm
158
159 if len(labcfg["lab"]["racks"][0]["nodes"]) < 1:
160     print("looks like virtual deployment where nodes were not defined")
161     opnfvcfg["demo-maas"]["maas"]["nodes"].remove()
162     exit()
163
164 #lets insert the node details here:
165 c=0
166 ifnamelist=[]
167 #
168 while c < len(labcfg["lab"]["racks"][0]["nodes"]):
169     valuemac=[]
170     y = 0
171     # setup value of name and tags accordigly
172     noderoleslist = labcfg["lab"]["racks"][0]["nodes"][c]["roles"]
173     noderoles = " ".join(noderoleslist)
174
175     valuetype = getFromDict(labcfg, ["lab","racks",0,"nodes",c, "power", "type"])
176     namevalue = labcfg["lab"]["racks"][0]["nodes"][c]["name"]
177     valuearc = getFromDict(labcfg, ["lab","racks",0,"nodes",c, "architecture"])
178     # setup value of architecture
179     if valuearc == "x86_64":
180         valuearc="amd64/generic"
181
182     if valuetype == "wakeonlan":
183         macvalue = getFromDict(labcfg, ["lab","racks",0,"nodes",c, "power", "mac_address"])
184         power={"type": "ether_wake", "mac_address": macvalue}
185     if valuetype == "ipmi":
186         valueaddr = getFromDict(labcfg, ["lab","racks",0,"nodes",c, "power", "address"])
187         valueuser = getFromDict(labcfg, ["lab","racks",0,"nodes",c, "power", "user"])
188         valuepass = getFromDict(labcfg, ["lab","racks",0,"nodes",c, "power", "pass"])
189         valuedriver = "LAN_2_0"
190         power={"type": valuetype, "address": valueaddr,"user": valueuser,\
191                "pass": valuepass, "driver": valuedriver}
192
193     opnfvcfg["demo-maas"]["maas"]["nodes"].append({"name": namevalue, \
194              "architecture":valuearc,"interfaces":[],"mac_addresses":[],\
195              "power":power,'tags':noderoles})
196     y = 0
197     while y < len(labcfg["lab"]["racks"][0]["nodes"][c]["nics"]):
198         valuespaces = labcfg["lab"]["racks"][0]["nodes"][c]["nics"][y]["spaces"]
199         valueifname = labcfg["lab"]["racks"][0]["nodes"][c]["nics"][y]["ifname"]
200         if "admin" not in valuespaces:
201             ifnamelist += [valueifname]
202         valueifmac = labcfg["lab"]["racks"][0]["nodes"][c]["nics"][y]["mac"][0]
203         valuemac += labcfg["lab"]["racks"][0]["nodes"][c]["nics"][y]["mac"]
204         opnfvcfg["demo-maas"]["maas"]["nodes"][c]["interfaces"]\
205                  .append({"name":valueifname,"mac_address":valueifmac,"mode": "auto"})
206
207         y=y+1
208
209     if valueifmac:
210        opnfvcfg["demo-maas"]["maas"]["nodes"][c]['mac_addresses']=valuemac
211
212     c=c+1
213
214 opnfvlabcfg["opnfv"]["floating-ip-range"]=labcfg["lab"]["racks"][0]["floating-ip-range"]
215 opnfvlabcfg["opnfv"]["ext-port"]=labcfg["lab"]["racks"][0]["ext-port"]
216 opnfvlabcfg["opnfv"]["ceph-disk"]=labcfg["opnfv"]["storage"][0]["disk"]
217 opnfvlabcfg["opnfv"]["interface-enable"]=",".join(list(set(ifnamelist)))
218 opnfvlabcfg["opnfv"]["os-domain-name"] = labcfg["lab"]["racks"][0]["osdomainname"]
219
220 with open('deployment.yaml', 'wa') as opnfvf:
221    yaml.dump(opnfvcfg, opnfvf, default_flow_style=False)
222
223 with open('deployconfig.yaml', 'wa') as opnfvf:
224    yaml.dump(opnfvlabcfg, opnfvf, default_flow_style=False)