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

Proposed by Gavin Lin
Status: Merged
Approved by: Gavin Lin
Approved revision: 27bebfaaaecab6b4a56dcb6cf8f21bb6b791b767
Merged at revision: 842848d85aea4d189477e0ba424007a6b99ccb2f
Proposed branch: ~gavin.lin/cc-lab-manager:platform-config
Merge into: cc-lab-manager:master
Diff against target: 185 lines (+77/-41)
1 file modified
cc_lab_manager/gen_config/gen_agent_tf_config.py (+77/-41)
Reviewer Review Type Date Requested Status
Kevin Yeh Approve
Review via email: mp+425130@code.launchpad.net

Commit message

Support generating customized agent config from platform name or device_id.
Fix typo for maas provision type.

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/gen_config/gen_agent_tf_config.py b/cc_lab_manager/gen_config/gen_agent_tf_config.py
2index 1292ce6..d72b0bf 100644
3--- a/cc_lab_manager/gen_config/gen_agent_tf_config.py
4+++ b/cc_lab_manager/gen_config/gen_agent_tf_config.py
5@@ -4,8 +4,8 @@ import yaml
6 import argparse
7 import re
8
9-from cc_lab_manager.cc_lab_manager_maptable import reboot_script_maptable, provision_type_maptable
10-from cc_lab_manager.cc_lab_manager_maptable import customized_agent_config_maptable
11+from cc_lab_manager.cc_lab_manager_maptable import lab_netboot_servers, provision_type_maptable, reboot_script_maptable
12+from cc_lab_manager.device_config_maptable import agent_config_platform_maptable, agent_config_device_maptable
13
14 agent_config = {"device_ip": None,
15 "secure_id": None,
16@@ -94,6 +94,8 @@ def read_data_from_db(cursor):
17
18
19 def device_id_to_agent_name(device_id):
20+ # Juju does not accept only numbers in any section divided by hyphen,
21+ # so generate agent name to comply that rule.
22 id_items = device_id.split('-')
23 if re.match('^[0-9]*$', id_items[0]):
24 print('Unsupported device id:', device_id)
25@@ -110,6 +112,19 @@ def device_id_to_agent_name(device_id):
26 return '-'.join(output)
27
28
29+def device_id_to_platform_name(device_id):
30+ # Platform name: Device_ID without the tailing increments or CID.
31+ id_items = device_id.split('-')
32+ if len(id_items) < 2:
33+ return device_id
34+ else:
35+ if re.match('^[0-9]{3}$', id_items[-1]) or re.match('^c[0-9]{4,}$', id_items[-1]):
36+ id_items.pop()
37+ return '-'.join(id_items)
38+ else:
39+ return device_id
40+
41+
42 def create_agent_config_dir(db_machine_list, cfg_path):
43
44 for machine in db_machine_list:
45@@ -122,64 +137,87 @@ def create_agent_config_dir(db_machine_list, cfg_path):
46 except:
47 print("dir exist for {}".format(agent_name))
48
49+
50+def insert_data_from_maptable(update_conf, device_data, maptable_name, device_key):
51+ maptable = globals()[maptable_name]
52+ if device_key in maptable:
53+ for key in maptable[device_key]:
54+ if isinstance(maptable[device_key][key], list):
55+ update_conf[key] = []
56+ for line in maptable[device_key][key]:
57+ try:
58+ update_conf[key].append(line.format(**device_data))
59+ except KeyError:
60+ update_conf[key].append(line)
61+ elif isinstance(maptable[device_key][key], (bool, int, float, complex)):
62+ update_conf[key] = maptable[device_key][key]
63+ else:
64+ try:
65+ update_conf[key] = maptable[device_key][key].format(**device_data)
66+ except KeyError:
67+ update_conf[key] = maptable[device_key][key]
68+ return update_conf
69+
70+
71 def generate_agent_config(db_machine_list, cfg_path):
72
73 for machine in db_machine_list:
74 if machine['lab'] == '' or machine['Customized_agent_config'] == 'TRUE':
75 continue
76
77+ # Copy data from sqlite3.Row object to a dict so we can add more keys later
78+ device_data = dict(machine)
79+
80 try:
81- provision = provision_type_maptable[machine["Provision"]]
82+ provision = provision_type_maptable[device_data["Provision"]]
83 except:
84- print("Unknown provision type for {}".format(machine['cid']))
85+ print("Unknown provision type for {}".format(device_data['Provision']))
86 continue
87
88+ # Update default configs apply to all devices.
89 agent_conf = agent_config.copy()
90- agent_name = device_id_to_agent_name(machine["Device_ID"])
91-
92- agent_conf["device_ip"] = machine["IP"]
93- agent_conf["secure_id"] = machine["SecureID"]
94+ agent_name = device_id_to_agent_name(device_data["Device_ID"])
95+ agent_conf["device_ip"] = device_data["IP"]
96+ agent_conf["secure_id"] = device_data["SecureID"]
97 agent_conf["agent_name"] = agent_name
98- agent_conf["env"]["HEXR_DEVICE_SECURE_ID"] = machine["SecureID"]
99- agent_conf["env"]["DEVICE_IP"] = machine["IP"]
100+ agent_conf["env"]["HEXR_DEVICE_SECURE_ID"] = device_data["SecureID"]
101+ agent_conf["env"]["DEVICE_IP"] = device_data["IP"]
102
103- if provision == 'maas':
104- agent_conf["node_id"] = machine["MAAS_Node_ID"]
105- agent_conf["node_name"] = machine["Device_ID"]
106+ # Update default configs for some provision types.
107+ if provision == 'maas2':
108+ agent_conf["node_id"] = device_data["MAAS_Node_ID"]
109+ agent_conf["node_name"] = device_data["Device_ID"]
110
111- if machine["Provision"] == 'cm3' or machine["Provision"] == 'muxpi' \
112- or machine["Provision"] == 'dragonboard':
113- agent_conf["control_host"] = machine["Controller_IP"]
114- agent_conf['serial_host'] = machine["Controller_IP"]
115+ if provision == 'netboot':
116+ device_data['Netboot_IP'] = lab_netboot_servers[device_data['Lab'].lower()]
117+
118+ if device_data["Provision"] == 'cm3' or device_data["Provision"] == 'muxpi' \
119+ or device_data["Provision"] == 'dragonboard':
120+ agent_conf["control_host"] = device_data["Controller_IP"]
121+ agent_conf['serial_host'] = device_data["Controller_IP"]
122 agent_conf['serial_port'] = '3000'
123
124- if machine["Provision"] == 'cm3' or machine["Provision"] == 'muxpi':
125+ if device_data["Provision"] == 'cm3' or device_data["Provision"] == 'muxpi':
126 agent_conf['test_device'] = '/dev/sda'
127 agent_conf['snappy_writable_partition'] = '/dev/sda2'
128- elif machine["Provision"] == 'dragonboard':
129+ elif device_data["Provision"] == 'dragonboard':
130 agent_conf['test_device'] = '/dev/mmcblk1'
131 agent_conf['snappy_writable_partition'] = '/dev/mmcblk1p9'
132 agent_conf['user_assertion'] = '/home/ubuntu/plars/auto-import.assert'
133 agent_conf['model_assertion'] = '/home/ubuntu/plars/dragon.model'
134
135+ # Update reboot script for known type.
136 agent_conf["reboot_script"] = []
137- power_template = {'PDU_IP' : machine["PDU_IP"],
138- 'PDU_Outlet' : machine["PDU_Outlet"]}
139- try:
140- reboot_script_template = reboot_script_maptable[machine["Power"]]
141- for line in reboot_script_template:
142- agent_conf["reboot_script"].append(line.format(**machine))
143- except:
144- print('No power template')
145+ if device_data["Power"] in reboot_script_maptable:
146+ for line in reboot_script_maptable[device_data["Power"]]:
147+ agent_conf["reboot_script"].append(line.format(**device_data))
148+
149+ # Insert or overwrite platform specific config from maptable if any.
150+ platform_name = device_id_to_platform_name(device_data['Device_ID'])
151+ agent_conf = insert_data_from_maptable(agent_conf, device_data, 'agent_config_platform_maptable', platform_name)
152
153- if machine["Device_ID"] in customized_agent_config_maptable:
154- for key in customized_agent_config_maptable[machine["Device_ID"]]:
155- if isinstance(customized_agent_config_maptable[machine["Device_ID"]][key], list):
156- agent_conf[key] = []
157- for line in customized_agent_config_maptable[machine["Device_ID"]][key]:
158- agent_conf[key].append(line.format(**machine))
159- else:
160- agent_conf[key] = customized_agent_config_maptable[machine["Device_ID"]][key]
161+ # Insert or overwrite device specific config from maptable if any.
162+ agent_conf = insert_data_from_maptable(agent_conf, device_data, 'agent_config_device_maptable', device_data["Device_ID"])
163
164 empty_keys = []
165 for key in agent_conf:
166@@ -211,15 +249,13 @@ def generate_tf_config(db_machine_list, cfg_path):
167 tf_conf["results_basedir"] = tf_conf["results_basedir"].format(agent_name)
168 tf_conf["provision_type"] = provision
169
170- # Default queues: CID, Device_ID, and Device_ID without tailing CID or increment
171+ # Default queues: CID, Device_ID, and platform name(Device_ID without tailing CID or increment)
172 tf_conf['job_queues'] = []
173 tf_conf['job_queues'].append(machine['CID'])
174 tf_conf['job_queues'].append(machine['Device_ID'])
175- id_items = machine['Device_ID'].split('-')
176- if len(id_items) > 1:
177- if re.match('^[0-9]{3}$', id_items[-1]) or re.match('^c[0-9]{4,}$', id_items[-1]):
178- id_items.pop()
179- tf_conf['job_queues'].append('-'.join(id_items))
180+ platform_name = device_id_to_platform_name(machine['Device_ID'])
181+ if platform_name != machine['Device_ID']:
182+ tf_conf['job_queues'].append(platform_name)
183
184 # Add customized queues if any
185 if machine['TF_Queue'] != '':

Subscribers

People subscribed via source and target branches

to all changes: