Merge ~gavin.lin/cc-lab-manager:restart_dhcp into cc-lab-manager:master

Proposed by Gavin Lin
Status: Merged
Approved by: Gavin Lin
Approved revision: cc4c4a8fe5c6edca8cb97498eaa3aa4bc9d83f66
Merged at revision: bb1d338b8b8da1526df291ec6fa0c0e283052c4d
Proposed branch: ~gavin.lin/cc-lab-manager:restart_dhcp
Merge into: cc-lab-manager:master
Diff against target: 77 lines (+46/-3)
2 files modified
cc_lab_manager/commands/cc_lab_manager.py (+4/-3)
cc_lab_manager/dhcp/restart_dhcp_servers.py (+42/-0)
Reviewer Review Type Date Requested Status
Kevin Yeh Approve
Review via email: mp+423343@code.launchpad.net

Description of the change

Apply DHCP config automatically.

To post a comment you must log in.
Revision history for this message
Kevin Yeh (kevinyeh) wrote :

lgtm +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cc_lab_manager/commands/cc_lab_manager.py b/cc_lab_manager/commands/cc_lab_manager.py
2index a8324d2..3b78d8e 100644
3--- a/cc_lab_manager/commands/cc_lab_manager.py
4+++ b/cc_lab_manager/commands/cc_lab_manager.py
5@@ -6,9 +6,11 @@ from cc_lab_manager.gsheet_db import gsheet_db
6 from cc_lab_manager.gsheet_db import db_gsheet
7 from cc_lab_manager.c3_db import c3_db
8 from cc_lab_manager.dhcp import dhcp_config_generator
9+from cc_lab_manager.dhcp import restart_dhcp_servers
10 from cc_lab_manager.maas import create_maas_node
11 from cc_lab_manager.gen_config import gen_agent_tf_config
12
13+
14 logger = logging.getLogger('cc-lab-manager')
15
16 def main():
17@@ -18,10 +20,9 @@ def main():
18 # Get needed information from c3
19 c3_db.main()
20
21- # Generate DHCP config for each subnets
22- # TODO: Currently these config files need to be copied
23- # manually, then MAAS will apply it automatically
24+ # Generate and apply DHCP config for each subnet
25 dhcp_config_generator.main()
26+ restart_dhcp_servers.main()
27
28 # Create MAAS nodes for DUTs
29 create_maas_node.main()
30diff --git a/cc_lab_manager/dhcp/restart_dhcp_servers.py b/cc_lab_manager/dhcp/restart_dhcp_servers.py
31new file mode 100644
32index 0000000..22986f7
33--- /dev/null
34+++ b/cc_lab_manager/dhcp/restart_dhcp_servers.py
35@@ -0,0 +1,42 @@
36+import logging
37+import argparse
38+import os
39+import re
40+import time
41+import subprocess
42+
43+from cc_lab_manager.cc_lab_manager_maptable import lab_dhcp_servers
44+
45+logger = logging.getLogger('cc-lab-manager')
46+
47+def apply_dhcp_conf(cc_tool_box):
48+ dhcp_conf_location = os.path.join(cc_tool_box, 'config', 'dhcp')
49+ r = re.compile('cert-.*l-l.*-dhcpd-pool.conf')
50+ for dirpath, _, files in os.walk(dhcp_conf_location):
51+ for file in files:
52+ if r.match(file) is not None:
53+ lab_id = file.split('-')[1] + '-' + file.split('-')[2]
54+ if lab_id in lab_dhcp_servers:
55+ subprocess.run(['scp', os.path.join(dirpath, file), lab_dhcp_servers[lab_id] + ':~'])
56+ time.sleep(1)
57+ subprocess.run(['ssh', lab_dhcp_servers[lab_id], 'sudo systemctl restart maas-dhcpd.service'])
58+ time.sleep(5)
59+ subprocess.run(['ssh', lab_dhcp_servers[lab_id], 'sudo systemctl status maas-dhcpd.service'])
60+
61+
62+def environ_or_required(key):
63+ if os.environ.get(key):
64+ return {'default': os.environ.get(key)}
65+ else:
66+ return {'required': True}
67+
68+
69+def main():
70+ parser = argparse.ArgumentParser()
71+ parser.add_argument('--cctoolbox', help="Path to cc-tool-box for storing dhcp config", **environ_or_required('CCLM_CC_TOOL_BOX'))
72+ args = parser.parse_args()
73+
74+ apply_dhcp_conf(args.cctoolbox)
75+
76+if __name__ == '__main__':
77+ main()

Subscribers

People subscribed via source and target branches

to all changes: