ymodfiied to include marks comment for labconfig for intel pod6.
[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
56 opnfvcfg['demo-maas']['maas']['apt_sources'].append("ppa:maas/stable") 
57 opnfvcfg['demo-maas']['maas']['apt_sources'].append("ppa:juju/stable") 
58
59 # lets modify the maas general settings:
60
61 updns = getFromDict(labcfg, ["labconfig","labsettings","upstream_dns"])
62 opnfvcfg["demo-maas"]["maas"]["settings"]["upstream_dns"]=updns
63
64 value = getFromDict(labcfg, ["labconfig","lab_location"])
65 opnfvcfg["demo-maas"]["maas"]["settings"]["maas_name"]=value
66 opnfvcfg["demo-maas"]["maas"]["name"]="opnfv-"+value
67
68 ethbrAdm=""
69 ethbrAdmin=""
70
71 c=0
72 y=0
73 #z=0
74
75 while c < len(labcfg["opnfv"]["spaces"]):
76     brtype = getFromDict(labcfg, ["opnfv","spaces",c,"type"])
77     brname = getFromDict(labcfg, ["opnfv","spaces",c,"bridge"])
78     brcidr = getFromDict(labcfg, ["opnfv","spaces",c,"cidr"])
79 #
80     if brtype == "admin":
81         ethbrAdmin = getFromDict(labcfg, ["opnfv","spaces",c,"bridge"])
82         brgway = getFromDict(labcfg, ["opnfv","spaces",c,"gateway"])
83         tmpcidr = brcidr[:-4]
84
85         nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
86                    "broadcast_ip": tmpcidr+"255", "router_ip": brgway,\
87                    "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
88                    "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
89
90         ethbrAdm = ('auto lo\n'
91                     '    iface lo inet loopback\n\n'
92                     'auto eth'+str(y)+'\n'
93                     '    iface eth'+str(y)+' inet static\n'
94                     '    address '+tmpcidr+'5\n'
95                     '    netmask 255.255.255.0\n'
96                     '    gateway '+brgway+'\n'
97                     '    dns-nameservers '+updns+' '+tmpcidr+'5 127.0.0.1\n')
98
99         opnfvcfg['demo-maas']['maas']['ip_address']=tmpcidr+"5"
100         opnfvcfg['demo-maas']['maas']['interfaces'].append("bridge="+brname+",model=virtio")
101         opnfvcfg['demo-maas']['juju-bootstrap']['interfaces'].append("bridge="+brname+",model=virtio")
102         opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"].append(nodegroup)
103         y=y+1
104     elif brtype:
105         opnfvcfg["demo-maas"]["maas"]["interfaces"].append("bridge="+brname+",model=virtio")
106         brgway = getFromDict(labcfg, ["opnfv","spaces",c,"gateway"])
107         if brtype != "external":
108             tmpcidr = brcidr[:-4]
109             if brgway:
110                 nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
111                            "broadcast_ip": tmpcidr+"255", "management": 1, "router_ip": brgway,\
112                            "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
113                            "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
114             else:
115                 nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
116                            "broadcast_ip": tmpcidr+"255", "management": 1, \
117                            "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
118                            "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
119             opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"].append(nodegroup)
120             ethbrAdm  = (ethbrAdm+'\n'
121                         'auto eth'+str(y)+'\n'
122                         '    iface eth'+str(y)+' inet static\n'
123                         '    address '+tmpcidr+'5\n'
124                         '    netmask 255.255.255.0\n')
125             y=y+1
126         if brtype == "public":
127             opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"].append("bridge="+brname+",model=virtio")
128         if brtype == "external":
129             ipaddress = getFromDict(labcfg, ["opnfv","spaces",c,"ipaddress"])
130             ethbrAdm  = (ethbrAdm+'\n'
131                         'auto eth'+str(y)+'\n'
132                         '    iface eth'+str(y)+' inet static\n'
133                         '    address '+ipaddress+'\n'
134                         '    netmask 255.255.255.0\n')
135             opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"].append("bridge="+brname+",model=virtio")
136
137     c=c+1
138
139 # lets modify the maas general settings:
140 value = get_ip_address(ethbrAdmin)
141 value = "qemu+ssh://"+getpass.getuser()+"@"+value+"/system"
142 opnfvcfg['demo-maas']['maas']['virsh']['uri']=value
143 opnfvcfg['demo-maas']['maas']['network_config']=ethbrAdm
144
145 if len(labcfg["labconfig"]["nodes"]) < 1:
146     print("looks like virtual deployment where nodes were not defined")
147     opnfvcfg["demo-maas"]["maas"]["nodes"].remove()
148     exit()
149
150 #lets insert the node details here:
151 c=0
152 #
153 while c < len(labcfg["labconfig"]["nodes"]):
154     valuemac=[]
155     y = 0
156     # setup value of name and tags accordigly
157     noderoleslist = labcfg["labconfig"]["nodes"][c]["roles"]
158     noderoles = " ".join(noderoleslist)
159
160     valuetype = getFromDict(labcfg, ["labconfig","nodes",c, "power", "type"])
161     namevalue = labcfg["labconfig"]["nodes"][c]["name"]
162     valuearc = getFromDict(labcfg, ["labconfig","nodes",c, "architecture"])
163     # setup value of architecture
164     if valuearc == "x86_64":
165         valuearc="amd64/generic"
166
167     if valuetype == "wakeonlan":
168         macvalue = getFromDict(labcfg, ["labconfig","nodes",c, "power", "mac_address"])
169         power={"type": "ether_wake", "mac_address": macvalue}
170     if valuetype == "ipmi":
171         valueaddr = getFromDict(labcfg, ["labconfig","nodes",c, "power", "address"])
172         valueuser = getFromDict(labcfg, ["labconfig","nodes",c, "power", "user"])
173         valuepass = getFromDict(labcfg, ["labconfig","nodes",c, "power", "pass"])
174         valuedriver = "LAN_2_0"
175         power={"type": valuetype, "address": valueaddr,"user": valueuser,\
176                "pass": valuepass, "driver": valuedriver}
177
178     opnfvcfg["demo-maas"]["maas"]["nodes"].append({"name": namevalue, \
179              "architecture":valuearc,"interfaces":[],"mac_addresses":[],\
180              "power":power,'tags':noderoles})
181     y = 0
182     while y < len(labcfg["labconfig"]["nodes"][c]["nics"]):
183         valueifname = labcfg["labconfig"]["nodes"][c]["nics"][y]["ifname"]
184         valueifmac = labcfg["labconfig"]["nodes"][c]["nics"][y]["mac"][0]
185         valuemac += labcfg["labconfig"]["nodes"][c]["nics"][y]["mac"]
186         opnfvcfg["demo-maas"]["maas"]["nodes"][c]["interfaces"]\
187                  .append({"name":valueifname,"mac_address":valueifmac,"mode": "auto"})
188
189         y=y+1
190
191     if valueifmac:
192        opnfvcfg["demo-maas"]["maas"]["nodes"][c]['mac_addresses']=valuemac
193
194     c=c+1
195
196 with open('deployment.yaml', 'wa') as opnfvf:
197    yaml.dump(opnfvcfg, opnfvf, default_flow_style=False)
198