summaryrefslogtreecommitdiff
path: root/kpov_judge/tasks/dhcp_dns_predefined_ip/task.py
blob: 4822c2ab700327efe0bd92fbd8777c159e8bcf10 (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
# kpov_util should be imported by add_assignment.py

instructions = {
    'si':"""
<pre>Ustvari tri navidezne računalnike. Za enega (SimpleArbiter) uporabi sliko diska simpleArbiter. Na drugega (DHCP_server) postavi strežnika DHCP in DNS in poskrbi, da ta računalnik dobi IP naslov {IP_server}. Poskrbi, da bo preostali računalnik (DHCP_client) dobil naslov {IP_client}, ki mu ga določi DHCP strežnik. Poskrbi še,da DNS strežnik vrne za hostname {HOSTNAME_X} IP naslov {IP_X}.</pre>
"""
}

computers = {
    'maliNetworkManager': {
        'disks': [
            {   'name': 'maliNetworkManager',
            },
            #{   'name': 'CDROM',
            #    'options':{'readonly': True},
            #    'parts': [],# no parts, no mounting.
            #}
        ],
        'network_interfaces': [{'network': 'net1'}],
        'flavor': 'm1.tiny',
        'config_drive': False

    },
    'maliBrezNetworkManager': {
        'disks': [
            {   'name': 'maliBrezNetworkManager',
            },
            #{   'name': 'CDROM',
            #    'options':{'readonly': True},
            #    'parts': [],# no parts, no mounting.
            #}
        ],
        'network_interfaces': [{'network': 'net1'}],
        'flavor': 'm1.tiny',
        'config_drive': False

    },
    'SimpleArbiter': {
        'disks': [
            {   'name': 'simpleArbiterDhcp',
                # attempt automount
            },
            #{   'name': 'CDROM',
            #    'options': {'readonly': True},
            #    'parts': [{'dev': 'b1', 'path': '/cdrom'}],
            #},
        ],
        'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}],
        'flavor': 'm1.tiny',
        'config_drive': False
    }
}

networks = { 'net1': {'public': False}, 'test-net': {'public': True} }

params_meta = {
    'IP_server': {'descriptions': {'si': 'IP naslov DHCP streznika'}, 'w': False, 'public':True, 'type': 'IP', 'generated': True},
    'IP_client': {'descriptions': {'si': 'IP naslov DHCP klienta'}, 'w': False, 'public': True, 'type': 'IP', 'generated': True},
    'MAC_client': {'descriptions': {'si': 'MAC naslov DHCP klienta'}, 'w': True, 'public': True, 'type': 'MAC', 'generated': False},
    'HOSTNAME_X': {'descriptions': {'si': 'Hostname za DNS'}, 'w': False, 'public': True, 'type': 'short_text', 'generated': True},
    'IP_X': {'descriptions': {'si': 'Naslov, ki ga vrne DNS'}, 'w': False, 'public': False, 'type': 'IP', 'generated': True},
}

def task(IP_server, IP_client, MAC_client, HOSTNAME_X):
    tests = {
        IP_server: (
            ('dhcp_proces', 'sudo ps -A')),
        IP_client: (
            ('dhcp', 'sudo dhcping -s {} -h {} -c {}'.format(IP_server, MAC_client, IP_client)),
            ('dns_hostname', 'nslookup {}'.format(HOSTNAME_X)),
            ('client_IP', '/sbin/ifconfig')),
    }

    results = collections.defaultdict(str)
    for host, host_tests in tests.items():
        results.update(kpov_util.ssh_test(host, 'student', 'vaje', host_tests))
    return results

def gen_params(user_id, params_meta):
    params = dict()
    r = random.Random(user_id+"bla")
    net = kpov_util.IPv4_subnet_gen(r, '172.23.128.0/18', 24)
    params['IP_server'], params['IP_client'], params['IP_X'] = kpov_util.IPv4_addr_gen(r, net, 3)
    params['HOSTNAME_X'] = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ') for i in range(5)])
    return params

def task_check(results, params):
    import re
    score = 0
    hints = []
    if results['dhcp'].find("Got answer from: {}".format(params['IP_server'])) > -1:
        score += 2
    else:
        hints += ["dhcp server IP wrong"]
    if results['dhcp_proces'].find("dhcpd") > -1:
        score += 2
    else:
        hints += ["dhcp wrong"]
    if results['dns_hostname'].find("Address: {}".format(params['IP_X'])) >= 0:
        score += 2
    else:
        hints += ['dns  hostname IP wrong']
    if results['dns_hostname'].find("Server:\t\t{}".format(params['IP_server'])) > -1:
        score += 2
    else:
        hints += ['dns server IP wrong']
    if results['client_IP'].find('inet {}'.format(params['IP_client'])) > -1:
        score += 2
    else:
        hints += ['client IP wrong']
    return score, hints


def prepare_disks(templates, task_params, global_params):
    write_default_config(templates['simpleArbiterDhcp'], global_params)