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