summaryrefslogtreecommitdiffstats
path: root/ci/deploy.py
blob: 83992b4292050620f745d4383f578bd65859f1a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import yaml
import pprint
import socket
import fcntl
import struct
import os
import getpass

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

with open('labconfig.yaml', 'r') as labf:
    labcfg = yaml.load(labf)

with open('deployment.yaml', 'r') as opnfvf:
    opnfvcfg = yaml.load(opnfvf)

def setInDict(dataDict, mapList, value):
    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value

def getFromDict(dataDict, mapList):
    return reduce(lambda d, k: d[k], mapList, dataDict)


# lets modify the maas general settings:

updns = getFromDict(labcfg, ["labconfig","labsettings","upstream_dns"])
setInDict(opnfvcfg, ["demo-maas", "maas", "settings", "upstream_dns"], updns)
value = getFromDict(labcfg, ["labconfig","lab_location"])
setInDict(opnfvcfg, ["demo-maas", "maas", "settings", "maas_name"], value)
setInDict(opnfvcfg, ["demo-maas", "maas", "name"], "opnfv-"+value)

#lets figure out the interfaces data

ethbrAdm=""
ethbrAdmin=""

c=0
y=0
z=0

while c < len(labcfg["labconfig"]["bridges"]):
    brtype = getFromDict(labcfg, ["labconfig","bridges",c,"type"])
    brname = getFromDict(labcfg, ["labconfig","bridges",c,"bridge"])
    brcidr = getFromDict(labcfg, ["labconfig","bridges",c,"cidr"])
    if brtype == "admin":
        if c > 0:
            opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"].append({})
        ethbrAdmin = getFromDict(labcfg, ["labconfig","bridges",c,"bridge"])
        brgway = getFromDict(labcfg, ["labconfig","bridges",c,"gateway"])
        tmpcidr = brcidr[:-4]
        setInDict(opnfvcfg, ["demo-maas", "maas", "ip_address"], tmpcidr+"5")
        opnfvcfg["demo-maas"]["maas"]["interfaces"][y] = "bridge="+brname+",model=virtio" 

        nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
                   "broadcast_ip": tmpcidr+"255", "router_ip": brgway,\
                   "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
                   "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}

        opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"][y] = nodegroup

        opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"][z] = "bridge="+brname+",model=virtio" 

        ethbrAdm = ('auto lo\n'
                    '    iface lo inet loopback\n\n'
                    'auto eth'+str(y)+'\n'
                    '    iface eth'+str(y)+' inet static\n'
                    '    address '+tmpcidr+'5\n'
                    '    netmask 255.255.255.0\n'
                    '    gateway '+brgway+'\n'
                    '    dns-nameservers '+updns+' '+tmpcidr+'5 127.0.0.1\n')
        z=z+1
        y=y+1
    elif brtype:
        opnfvcfg["demo-maas"]["maas"]["interfaces"].append("bridge="+brname+",model=virtio")
        brgway = getFromDict(labcfg, ["labconfig","bridges",c,"gateway"])
        if brtype != "external":
            if c > 0:
                opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"].append({})
            tmpcidr = brcidr[:-4]
            if brgway:
                nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
                           "broadcast_ip": tmpcidr+"255", "management": 1, "router_ip": brgway,\
                           "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
                           "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}
            else:
                nodegroup={"device": "eth"+str(y), "ip": tmpcidr+"5","subnet_mask": "255.255.255.0", \
                           "broadcast_ip": tmpcidr+"255", "management": 1, \
                           "static_range":{"high":tmpcidr+"80","low":tmpcidr+"50"},\
                           "dynamic_range":{"high":tmpcidr+"250","low":tmpcidr+"81"}}

            opnfvcfg["demo-maas"]["maas"]["node_group_ifaces"][y] = nodegroup
            ethbrAdm  = (ethbrAdm+'\n'
                        'auto eth'+str(y)+'\n'
                        '    iface eth'+str(y)+' inet static\n'
                        '    address '+tmpcidr+'5\n'
                        '    netmask 255.255.255.0\n')
        if brtype == "public":
            opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"].append("bridge="+brname+",model=virtio")
            z=z+1
        if brtype == "external":
            ipaddress = getFromDict(labcfg, ["labconfig","bridges",c,"ipaddress"])
            ethbrAdm  = (ethbrAdm+'\n'
                        'auto eth'+str(y)+'\n'
                        '    iface eth'+str(y)+' inet static\n'
                        '    address '+ipaddress+'\n'
                        '    netmask 255.255.255.0\n')
            opnfvcfg["demo-maas"]["juju-bootstrap"]["interfaces"].append("bridge="+brname+",model=virtio")
            z=z+1
        y=y+1


    c=c+1

setInDict(opnfvcfg, ["demo-maas", "maas", "network_config"], ethbrAdm)

# lets modify the maas general settings:
value = get_ip_address(ethbrAdmin) 
value = "qemu+ssh://"+getpass.getuser()+"@"+value+"/system"
setInDict(opnfvcfg, ["demo-maas", "maas", "virsh", "uri"], value)

if len(labcfg["labconfig"]["nodes"]) < 1:
    print("looks like virtual deployment where nodes were not defined")
    opnfvcfg["demo-maas"]["maas"]["nodes"].remove()
    exit() 

#lets insert the node details here:
c=0

while c < len(labcfg["labconfig"]["nodes"]):
    # setup value of name and tags accordigly
    value = getFromDict(labcfg, ["labconfig","nodes",c, "type"])
    namevalue = "node" + str(c+1) + "-" + value 
    if c > 0:
        opnfvcfg["demo-maas"]["maas"]["nodes"].append({})

    opnfvcfg["demo-maas"]["maas"]["nodes"][c]["name"] = namevalue
    opnfvcfg["demo-maas"]["maas"]["nodes"][c]["tags"] = value

    # setup value of architecture
    value = getFromDict(labcfg, ["labconfig","nodes",c, "architecture"])
    if value == "x86_64":
        value="amd64/generic"
    opnfvcfg["demo-maas"]["maas"]["nodes"][c]["architecture"] = value
    
    # setup mac_addresses
    value = getFromDict(labcfg, ["labconfig","nodes",c, "pxe_mac_address"])
    opnfvcfg["demo-maas"]["maas"]["nodes"][c]["mac_addresses"] = value
    valuetype = getFromDict(labcfg, ["labconfig","nodes",c, "power", "type"])

    if valuetype == "wakeonlan":
        macvalue = getFromDict(labcfg, ["labconfig","nodes",c, "power", "mac_address"])
        power={"type": "ether_wake", "mac_address": macvalue}
        opnfvcfg["demo-maas"]["maas"]["nodes"][c]["power"] = power
    if valuetype == "ipmi":
        valueaddr = getFromDict(labcfg, ["labconfig","nodes",c, "power", "address"])
        valueuser = getFromDict(labcfg, ["labconfig","nodes",c, "power", "user"])
        valuepass = getFromDict(labcfg, ["labconfig","nodes",c, "power", "pass"])
        valuedriver = "LAN_2_0"
        power={"type": valuetype, "address": valueaddr,"user": valueuser, "pass": valuepass, "driver": valuedriver}
        opnfvcfg["demo-maas"]["maas"]["nodes"][c]["power"] = power


    c=c+1

with open('deployment.yaml', 'w') as opnfvf:
   yaml.dump(opnfvcfg, opnfvf, default_flow_style=False)