From e33085889f26c76587fdab2ad600ed336dc028b5 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 6 Oct 2018 23:35:00 +0200 Subject: Improve ssh error reporting in some tasks --- kpov_judge/tasks/set_ip_static_dhcp/task.py | 68 +++++++++++++---------------- 1 file changed, 31 insertions(+), 37 deletions(-) (limited to 'kpov_judge/tasks/set_ip_static_dhcp') diff --git a/kpov_judge/tasks/set_ip_static_dhcp/task.py b/kpov_judge/tasks/set_ip_static_dhcp/task.py index 0553585..f400b6e 100644 --- a/kpov_judge/tasks/set_ip_static_dhcp/task.py +++ b/kpov_judge/tasks/set_ip_static_dhcp/task.py @@ -84,37 +84,29 @@ params_meta = { } def task(IP_NM, DNS_NM, IP_static, DNS_static): + import collections from pexpect import pxssh - import pexpect - results = dict() - peer_user = 'student' - peer_passwd = 'vaje' - # sA - network manager - sA = pxssh.pxssh() - sA.login(IP_NM, peer_user, peer_passwd) - sA.sendline('nmcli -c no d') - sA.prompt() - results['NM_nmcli'] = sA.before - sA.sendline('nslookup www.arnes.si') - sA.prompt() - results['NM_nslookup'] = sA.before - sA.logout() - # sB - brez network manager - sB = pxssh.pxssh() - sB.login(IP_static, peer_user, peer_passwd) - sB.sendline('nmcli -c no d') - sB.prompt() - results['static_nmcli'] = sB.before - sB.sendline('nslookup www.arnes.si') - sB.prompt() - results['static_nslookup'] = sB.before - sB.logout() + + tests = ['nmcli -c no d', 'nslookup www.arnes.si'] + results = collections.defaultdict(str) + + for name, host in [('nm', IP_NM), ('static', IP_static)]: + try: + s = pxssh.pxssh(encoding='utf-8') + s.login(host, 'student', 'vaje') + results['ssh-'+name] = True + for test in tests: + s.sendline(test) + s.prompt() + results[test.split()[0]+'-'+name] = s.before + s.logout() + except Exception as ex: + results['ssh'] = str(ex) return results def gen_params(user_id, params_meta): params = dict() r = random.Random(user_id) - # IP_NM, DNS_NM, IP_static, DNS_static) dns_servers = ['193.2.1.66', '193.2.1.72', '8.8.8.8', '8.8.4.4', '208.67.222.222', '208.67.220.220'] net = kpov_random_helpers.IPv4_subnet_gen(r, '10.94.96.0/19', 25) params['DNS_NM'] = r.choice(dns_servers) @@ -126,29 +118,31 @@ def task_check(results, params): import re score = 0 hints = [] - #print results, params - if results['NM_nslookup'].find(params['DNS_NM']) > -1: - # print "NM_nslookup OK" + if results['ssh-nm'] is True: + score += 1 + else: + hints += ['mali ssh failed: ' + results['ssh-nm']] + if results['ssh-static'] is True: + score += 1 + else: + hints += ['malibrez ssh failed: ' + results['ssh-static']] + if params['DNS_NM'] in results['nslookup-nm']: score += 2 else: hints += ['NM nslookup incorrect'] - if results['static_nslookup'].find(params['DNS_static']) > -1: - # print "static_nslookup OK" + if params['DNS_static'] in results['nslookup-static']: score += 2 else: hints += ['static nslookup incorrect'] - if re.search(r'e(th0|np0s3|ns3) +ethernet +connected', results['NM_nmcli']): - # print "NM_nmcli OK" - score += 3 + if re.search(r'e(th0|np0s3|ns3) +ethernet +connected', results['nmcli-nm']): + score += 2 else: hints += ['nmcli incorrect'] - if re.search(r'e(th0|np0s3|ns3) +ethernet +unmanaged', results['static_nmcli']): - # print "static_nmcli OK" - score += 3 + if re.search(r'e(th0|np0s3|ns3) +ethernet +unmanaged', results['nmcli-static']): + score += 2 else: hints += ['nmcli on malibrez incorrect'] return score, hints def prepare_disks(templates, task_params, global_params): write_default_config(templates['simpleArbiterDhcpGW'], global_params) - -- cgit v1.2.1 From 9c51bbe5016495c714789f5df01806a6d366e11a Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 6 Oct 2018 23:56:03 +0200 Subject: Rename kpov_random_helpers to kpov_util --- kpov_judge/tasks/set_ip_static_dhcp/task.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'kpov_judge/tasks/set_ip_static_dhcp') diff --git a/kpov_judge/tasks/set_ip_static_dhcp/task.py b/kpov_judge/tasks/set_ip_static_dhcp/task.py index f400b6e..4cdbc14 100644 --- a/kpov_judge/tasks/set_ip_static_dhcp/task.py +++ b/kpov_judge/tasks/set_ip_static_dhcp/task.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# kpov_random_helpers should be imported by add_assignment.py +# kpov_util should be imported by add_assignment.py instructions = { 'si':""" Ustvari tri navidezne racunalnike. Za prvega @@ -108,9 +108,9 @@ def gen_params(user_id, params_meta): params = dict() r = random.Random(user_id) dns_servers = ['193.2.1.66', '193.2.1.72', '8.8.8.8', '8.8.4.4', '208.67.222.222', '208.67.220.220'] - net = kpov_random_helpers.IPv4_subnet_gen(r, '10.94.96.0/19', 25) + net = kpov_util.IPv4_subnet_gen(r, '10.94.96.0/19', 25) params['DNS_NM'] = r.choice(dns_servers) - params['IP_NM'], params['IP_static'] = kpov_random_helpers.IPv4_addr_gen(r, net, 2) + params['IP_NM'], params['IP_static'] = kpov_util.IPv4_addr_gen(r, net, 2) params['DNS_static'] = r.choice(dns_servers) return params -- cgit v1.2.1 From b5b35faea7f4205f353f57178ddc795b7dce5043 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:02:26 +0200 Subject: Refactor SSH testing code out of individual tasks Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes. --- kpov_judge/tasks/set_ip_static_dhcp/task.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'kpov_judge/tasks/set_ip_static_dhcp') diff --git a/kpov_judge/tasks/set_ip_static_dhcp/task.py b/kpov_judge/tasks/set_ip_static_dhcp/task.py index 4cdbc14..3a3fcc6 100644 --- a/kpov_judge/tasks/set_ip_static_dhcp/task.py +++ b/kpov_judge/tasks/set_ip_static_dhcp/task.py @@ -85,23 +85,17 @@ params_meta = { def task(IP_NM, DNS_NM, IP_static, DNS_static): import collections - from pexpect import pxssh - tests = ['nmcli -c no d', 'nslookup www.arnes.si'] - results = collections.defaultdict(str) + tests = ( + ('nmcli', 'nmcli -c no d'), + ('nslookup', 'nslookup www.arnes.si'), + ) + results = collections.defaultdict(str) for name, host in [('nm', IP_NM), ('static', IP_static)]: - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(host, 'student', 'vaje') - results['ssh-'+name] = True - for test in tests: - s.sendline(test) - s.prompt() - results[test.split()[0]+'-'+name] = s.before - s.logout() - except Exception as ex: - results['ssh'] = str(ex) + host_results = kpov_util.ssh_test(host, 'student', 'vaje', tests) + for key, value in host_results.items(): + results[key+'-'+name] = value return results def gen_params(user_id, params_meta): -- cgit v1.2.1 From 3091fc2877600f7926ed6a596c316183499125f8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:11:49 +0200 Subject: Remove coding: lines Python 3 source is utf-8 by default. --- kpov_judge/tasks/set_ip_static_dhcp/task.py | 1 - 1 file changed, 1 deletion(-) (limited to 'kpov_judge/tasks/set_ip_static_dhcp') diff --git a/kpov_judge/tasks/set_ip_static_dhcp/task.py b/kpov_judge/tasks/set_ip_static_dhcp/task.py index 3a3fcc6..16895c3 100644 --- a/kpov_judge/tasks/set_ip_static_dhcp/task.py +++ b/kpov_judge/tasks/set_ip_static_dhcp/task.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # kpov_util should be imported by add_assignment.py instructions = { -- cgit v1.2.1 From dbf585c2a59dbcb7f6d803d5756826b562cd7f43 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:38:40 +0200 Subject: Fix or remove hashbangs --- kpov_judge/tasks/set_ip_static_dhcp/task.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'kpov_judge/tasks/set_ip_static_dhcp') diff --git a/kpov_judge/tasks/set_ip_static_dhcp/task.py b/kpov_judge/tasks/set_ip_static_dhcp/task.py index 16895c3..aac93f7 100644 --- a/kpov_judge/tasks/set_ip_static_dhcp/task.py +++ b/kpov_judge/tasks/set_ip_static_dhcp/task.py @@ -1,6 +1,5 @@ -#!/usr/bin/env python - # kpov_util should be imported by add_assignment.py + instructions = { 'si':""" Ustvari tri navidezne racunalnike. Za prvega @@ -33,10 +32,6 @@ computers = { 'disks': [ { 'name': 'maliNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -47,10 +42,6 @@ computers = { 'disks': [ { 'name': 'maliBrezNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -60,12 +51,7 @@ computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiterDhcpGW', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', -- cgit v1.2.1