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