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