summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-config/mv_network.py
blob: 3978f9558ba4f9f803080979bc76432e35e5d19b (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# -*- coding: utf-8 -*-
import sys , os, commands , glob, time, re
import logging
import mv_common
global etcnetdir
etcnetdir = "/etc/net/ifaces"


def setup_MYTH_IP(systemconfig):
    default_interface = systemconfig["default_interface"]
    try:
        defaultip = systemconfig["Hostip"+default_interface]
    except:
        logging.debug("    Error occured finding the defaultip")
        defaultip = "127.0.0.1"
    logging.debug("    Using %s as default ip", defaultip)
    return defaultip

def setup_MYTH_DHCP(systemconfig):
    default_interface = systemconfig["default_interface"]
    try:
        defaultdhcp = systemconfig["HostUSEDHCP"+default_interface]
    except:
        logging.critical("    *Error occured finding default dhcp")
        defaultdhcp = "0"
    logging.debug("    Using %s as dhcp value for %s", default_interface, defaultdhcp)
    return defaultdhcp

def flush(netdev):
    logging.debug("    Flushing %s",netdev)
    cmd = '''ip address flush dev %s''' %netdev
    mv_common.runcmd(cmd)

def kill_dhcp(basedir):
    logging.debug("    _Start of kill_dhcp")
    logging.debug("    Killing off dhcpd")
    stddir = os.getcwd()
    piddir = ("%s/var/run/") %basedir
    try:
        os.chdir(piddir)
        for FILE in glob.glob("dhcpcd-*.pid"):
            f = open(FILE,'r')
            pid = f.readline()
            f.close
            cmd = "kill -9 %s" %pid
            mv_common.runcmd(cmd)
            os.remove(FILE)
        os.chdir(stddir)
    except:
        pass

def setup_nameserver(dns):
    logging.info("    Adding %s for DNS", dns)
    cmd = '''grep -q %s /etc/resolv.conf''' %dns
    status = mv_common.runcmd(cmd)
    if  not status == 0 :
        cmd =  ''' echo "nameserver %s" >> /etc/resolv.conf ''' %dns
        mv_common.runcmd(cmd)



def setup_hostname(systemconfig):
    logging.debug("   _Start of setup_hostname")
    try:
        hostname = systemconfig["hostname"]
    except:
        logging.critical("    *Hostname could not be set")
        logging.info("    Using default value of me")
        hostname = "me"
    logging.info("    Setting the hostname to %s", hostname)
    cmd = "cat /etc/hosts | grep -v 127.0.0.1 | grep -v %s > /tmp/hosts" %hostname
    mv_common.runcmd(cmd)
    if systemconfig["mythdhcp"] == "1" :
        logging.debug("    not using dhcp")
        cmd = ''' echo 127.0.0.1 localhost  > /etc/hosts '''
        mv_common.runcmd(cmd)
        cmd = ''' echo %s %s  > /etc/hosts ''' %(systemconfig["mythip"], systemconfig["hostname"])
        mv_common.runcmd(cmd)
    else:
        cmd = ''' echo 127.0.0.1 %s localhost  > /etc/hosts ''' %systemconfig["hostname"]
        mv_common.runcmd(cmd)
    cmd = '''cat /tmp/hosts >> /etc/hosts '''
    mv_common.runcmd(cmd)


def write_wpafile(line,netdev):
    logging.debug("    writing out wpa_supplicant.conf file")
    logging.debug(line)
    wpafile = etcnetdir+"/"+netdev+"/wpa_supplicant.conf"
    try:
        f = open(wpafile,'w')
        f.write(line)
        f.close()
    except:
        logging.debug("    Couldn't write to %s",wpafile)

def setup_wpa(netinfo,netdev):
    logging.info("    %s will use wpa for encryption",netdev)
    line = '''
#This file was automaticly generated, any changes may be lost
ctrl_interface=/var/run/wpa_supplicant
network={
    ssid="%s"
    # Preshared key as an ASCII passphrase
    psk="%s"
    scan_ssid=1
    key_mgmt=WPA-EAP WPA-PSK NONE
    pairwise=CCMP TKIP
    proto=WPA RSN
}''' %(netinfo["ESSID"],netinfo["KEY"])
    write_wpafile(line,netdev)

def setup_wep(netinfo,netdev):
    logging.info("    %s will use wep for encryption",netdev)
    logging.debug("    When a packet comes along, you must wep it")
    line='''
#This file was automaticly generated, any changes may be lost
ctrl_interface=/var/run/wpa_supplicant
network={
        ssid="%s"
        key_mgmt=NONE
        wep_key0=%s
        wep_tx_keyidx=0
        scan_ssid=1
        auth_alg=SHARED
}
''' %(netinfo["ESSID"],netinfo["KEY"])
    write_wpafile(line,netdev)


def setup_none(netinfo,netdev):
    logging.info("    %s will not use encryption",netdev)
    line = '''
#This file was automaticly generated, any changes may be lost
ctrl_interface=/var/run/wpa_supplicant
network={
        ssid="%s"
        key_mgmt=NONE
}
''' %netinfo["ESSID"]
    write_wpafile(line,netdev)

def setup_interface(netdev,systemconfig):
    logging.debug("    _Start of setup_interface for %s",netdev)
    nettrans = {'Hostip':'ip',
                'Hostnetmask':'netmask',
                'HostActive':'isactive',
                'HostDNS':'dns',
                'HostUSEDHCP':'UseDHCP',
                'HostGW':'GW',
                'HostMTU':'mtu',
                'HOST_iswireless':"wireless",
                'HostESSID':'ESSID',
                'HostKey':'KEY',
                'HostUseEncryption':'ENCRYPT'}
    netinfo = {}
    #populate the netinfo dict
    for netitem in nettrans:
        if systemconfig[netitem+netdev]:
            netinfo[nettrans[netitem]] = systemconfig[netitem+netdev]
        else:
            netinfo[nettrans[netitem]] = False
        logging.debug("     %s:%s",netitem,netinfo[nettrans[netitem]])
    logging.info("    %s wireless: %s", netdev,netinfo["wireless"])
    try:
        os.makedirs(etcnetdir)
    except:
        logging.debug("   Could not create %s",etcnetdir)

    optionfile=etcnetdir+"/"+netdev+"/options"
    if not os.path.exists(optionfile):
        os.makedirs(etcnetdir+"/"+netdev)
        mv_common.cp_and_log(systemconfig["TEMPLATES"]+"/etcnet/eth/options", optionfile)

    if netinfo["isactive"] == "1" :
        change_iface_state(netdev,"enabled")
    else:
        change_iface_state(netdev,"disabled")

    if netinfo["wireless"] == "1" :

        logging.info("    Enabling wireless extensions")
        cmd = ''' sed -i -e 's/^CONFIG_WIRELESS=.*$/CONFIG_WIRLESS=yes/g'  %s/%s/options''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)
        if netinfo["ENCRYPT"] == "WEP":
            setup_wep(netinfo,netdev)
        elif netinfo["ENCRYPT"] == "WPA":
            setup_wpa(netinfo,netdev)
        elif netinfo["ENCRYPT"] == "none":
            setup_none(netinfo,netdev)

    else:
        logging.info("    Disabling wireless extensions")
        cmd = '''sed -i -e 's/^CONFIG_WIRLESS=.*$/CONFIG_WIRLESS=no/g' %s/%s/options''' %(etcnetdir, netdev)
        mv_common.runcmd(cmd)
        try:
            wpafile=etcnetdir+"/"+netdev+"/wpa_supplicant.conf"
            os.remove(wpafile)
        except:
            logging.debug("    Couldn't remove %s",wpafile)

    if netinfo["UseDHCP"] == "0" :
        logging.info("    Enabling DHCP support")
        cmd = ''' sed -i -e 's/^BOOTPROTO=.*$/BOOTPROTO=dhcp/g' %s/%s/options''' %(etcnetdir, netdev)
        mv_common.runcmd(cmd)
    else:
        logging.info("    Using static ip address of %s",netinfo["ip"])
        cmd = ''' sed -i -e 's/^BOOTPROTO=.*$/BOOTPROTO=static/g' %s/%s/options''' %(etcnetdir, netdev)
        mv_common.runcmd(cmd)
        cmd = '''echo "default via %s" > %s/%s/ipv4route''' %(netinfo["GW"], etcnetdir, netdev)
        mv_common.runcmd(cmd)
        cmd = '''echo "%s%s" > %s/%s/ipv4address ''' %(netinfo["ip"], netinfo["netmask"], etcnetdir, netdev)
        mv_common.runcmd(cmd)
        setup_nameserver(netinfo["dns"])

    if netinfo["mtu"] :
        logging.info("    Setting mtu to %s", netinfo["mtu"])
        cmd = '''sed -i '/^mtu.*$/d' %s/%s/iplink''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)
        cmd = ''' echo "mtu %s" >> %s/%s/iplink''' %(netinfo["mtu"], etcnetdir,netdev)
        mv_common.runcmd(cmd)
    else:
        logging.info("    Using default mtu value")
        cmd = '''sed -i '/^mtu.*$/d' %s/%s/iplink''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)


def change_iface_state(netdev, state):
    if state == "enabled":
        logging.info("    Activating %s", netdev)
        cmd = '''sed -i -e 's/^ONBOOT=.*$/ONBOOT=yes/g' %s/%s/options''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)
        cmd = '''sed -i -e 's/^DISABLED=.*$/DISABLED=no/g' %s/%s/options ''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)
    else:
        logging.info("    Disabling %s", netdev)
        cmd = '''sed -i -e 's/^ONBOOT=.*$/ONBOOT=no/g' %s/%s/options''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)
        cmd = '''sed -i -e 's/^DISABLED=.*$/DISABLED=yes/g' %s/%s/options ''' %(etcnetdir,netdev)
        mv_common.runcmd(cmd)
        cmd = "/sbin/ifconfig %s down" %netdev
        mv_common.runcmd(cmd)
        cmd = "ip address flush %s" %netdev
        mv_common.runcmd(cmd)

def devcheck(netdev):
    cmd = ''' /sbin/ifconfig %s''' %netdev
    rc = mv_common.runcmd(cmd)
    if rc == 0 :
        return True
    else:
        return False

def find_active(systemconfig):
    interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1', 'ath0')
    logging.debug("    _Start of find_active")

    if systemconfig["mythdhcp"] == "1":
        try:
            os.remove("/etc/resolv.conf")
        except:
            logging.debug("    couldn't remove /etc/resolv.conf")
        cmd = ''' echo search lan > /etc/resolv.conf '''
        mv_common.runcmd(cmd)
        cmd = ''' echo nameserver 127.0.0.1 >> /etc/resolv.conf '''
        mv_common.runcmd(cmd)

    for netdev in interfacelist:
        currentnet = "HostActive" + netdev
        try:
            systemconfig[currentnet]
        except:
            logging.debug("    %s is not defined",currentnet)
            change_iface_state(netdev, "disabled")
            continue
        if systemconfig[currentnet] == "1" :
            #check if device is present
            if devcheck(netdev) :
                setup_interface(netdev,systemconfig)
            else:
                logging.debug("    Interface %s not found in config", netdev)
                #change_iface_state(netdev, "disabled")
        else:
            change_iface_state(netdev, "disabled")

def vnc_netboot_check():
    logging.debug("    Start of VNC/NETBOOT check")
    vnc = False
    netboot = False
    try:
        f = open('/proc/cmdline', 'r')
        bootoptions = f.readlines()
        f.close()
    except:
        logging.critical("    *Couldn't open /proc/cmdline")
        logging.debug("    Assuming it's ok to fiddle with the network")
        return True
    bootoptions =  bootoptions[0]
    if re.match("vnc",bootoptions) != None :
        logging.debug("    Found VNC option")
        vnc = True
    if re.match("nfsroot",bootoptions) != None :
        logging.debug("    Found netboot option")
        netboot = True

    return (vnc or netboot)

def start_network():
    if not vnc_netboot_check():
        logging.info("    Restarting network")
        cmd ="/etc/net/scripts/network.init reload"
        mv_common.runcmd(cmd)
        cmd ="/etc/net/scripts/network.init restart"
        mv_common.runcmd(cmd)
    else:
        logging.info("    Will not restart network due to netboot/vnc")


def stop_network():
    if not vnc_netboot_check():
        logging.info("    Stopping network")
        cmd ="/etc/net/scripts/network.init stop"
        mv_common.runcmd(cmd)
        kill_dhcp("")
        interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1', 'ath0')
        for i in interfacelist:
            flush(i)
    else:
        logging.info("    Will not stop network due to netboot/vnc")
    pass



def setup_network (systemconfig):
    logging.info("____Start of network____")
    logging.info("    Setting up the network")
    setup_hostname(systemconfig)
    find_active(systemconfig)
    start_network()
    logging.info("__End of network\n")



##this is used by the install process to start the network
def install_network_setup(systemconfig):
    logging.info("____Start of network_install____")
    logging.info("    Setting up the network")
    #setup_MYTH_vars
    setup_hostname(systemconfig)
    stop_network()
    find_active(systemconfig)
    start_network()
    logging.info("__End of network install \n")