From d7dc9d1a23481f05b1634a49243e25b3d9c44e23 Mon Sep 17 00:00:00 2001 From: Ben Greear Date: Tue, 1 Feb 2022 07:14:06 -0800 Subject: [PATCH] vap-profile: Add logging and return code checking. And remove the logic to create a bridge within the vap profile, the vap profile does not have enough info to properly create a bridge (old code used hard-coded port names) Instead, logic would be to create vap, and then create a bridge object that holds the VAP using the bridge profile/script. Signed-off-by: Ben Greear --- py-json/vap_profile.py | 85 ++++++++++++++++------------------------ py-scripts/create_vap.py | 72 +++++++++++++++++++++------------- 2 files changed, 79 insertions(+), 78 deletions(-) diff --git a/py-json/vap_profile.py b/py-json/vap_profile.py index 9287e9ac..1273f15c 100644 --- a/py-json/vap_profile.py +++ b/py-json/vap_profile.py @@ -72,7 +72,6 @@ class VAPProfile(LFCliBase): "realm": None, "domain": None } - self.up = None def set_wifi_extra(self, key_mgmt="WPA-EAP", @@ -97,7 +96,7 @@ class VAPProfile(LFCliBase): def admin_up(self, resource): set_port_r = LFRequest.LFRequest(self.lfclient_url, "/cli-json/set_port", debug_=self.debug) req_json = LFUtils.portUpRequest(resource, None, debug_on=self.debug) - req_json["port"] = self.vap_name + req_json["port"] = LFUtils.name_to_eid(self.vap_name)[2] set_port_r.addPostData(req_json) set_port_r.jsonPost(self.debug) time.sleep(0.03) @@ -105,7 +104,7 @@ class VAPProfile(LFCliBase): def admin_down(self, resource): set_port_r = LFRequest.LFRequest(self.lfclient_url, "/cli-json/set_port", debug_=self.debug) req_json = LFUtils.port_down_request(resource, None, debug_on=self.debug) - req_json["port"] = self.vap_name + req_json["port"] = LFUtils.name_to_eid(self.vap_name)[2] set_port_r.addPostData(req_json) set_port_r.jsonPost(self.debug) time.sleep(0.03) @@ -219,9 +218,11 @@ class VAPProfile(LFCliBase): return result - def create(self, resource, radio, channel=None, up_=None, debug=False, use_ht40=True, use_ht80=True, + # NOTE: This method does not have enough knowledge of desired setup to build or modify + # a bridge, so work-flow would be to create vap, then create bridge to hold the VAP. + def create(self, resource, radio, channel=None, up=False, debug=False, use_ht40=True, use_ht80=True, use_ht160=False, - suppress_related_commands_=True, use_radius=False, hs20_enable=False, bridge=True): + suppress_related_commands_=True, use_radius=False, hs20_enable=False): eid = LFUtils.name_to_eid(radio) self.shelf = eid[0] self.resource = eid[1] @@ -229,14 +230,18 @@ class VAPProfile(LFCliBase): if resource: self.resource = resource + resource = self.resource - port_list = self.local_realm.json_get("port/1/1/list") + # Removes port if it exists already. + # TODO: Make this optional, that way this profile could modify existing VAP in place. + port_list = self.local_realm.json_get("port/1/%s/list" % (resource)) if port_list is not None: port_list = port_list['interfaces'] for port in port_list: for k, v in port.items(): - if v['alias'] == self.vap_name: - self.local_realm.rm_port(k, check_exists=True) + if v['alias'] == LFUtils.name_to_eid(self.vap_name)[2]: + self.local_realm.rm_port(k, check_exists=False) + if use_ht160: self.desired_add_vap_flags.append("enable_80211d") self.desired_add_vap_flags_mask.append("enable_80211d") @@ -279,9 +284,7 @@ class VAPProfile(LFCliBase): "frequency": self.local_realm.channel_freq(channel_=channel) } self.local_realm.json_post("/cli-json/set_wifi_radio", _data=data) - if up_ is not None: - self.up = up_ - if self.up: + if up: if "create_admin_down" in self.desired_add_vap_flags: del self.desired_add_vap_flags[self.desired_add_vap_flags.index("create_admin_down")] elif "create_admin_down" not in self.desired_add_vap_flags: @@ -311,8 +314,8 @@ class VAPProfile(LFCliBase): # pprint(self.station_names) # exit(1) - self.set_port_data["port"] = self.vap_name - self.add_vap_data["ap_name"] = self.vap_name + self.set_port_data["port"] = LFUtils.name_to_eid(self.vap_name)[2] + self.add_vap_data["ap_name"] = LFUtils.name_to_eid(self.vap_name)[2] add_vap_r.addPostData(self.add_vap_data) if debug: print("- 1502 - %s- - - - - - - - - - - - - - - - - - " % self.vap_name) @@ -322,57 +325,37 @@ class VAPProfile(LFCliBase): print("- ~1502 - - - - - - - - - - - - - - - - - - - ") add_vap_r.jsonPost(debug) - # time.sleep(0.03) - time.sleep(2) set_port_r.addPostData(self.set_port_data) set_port_r.jsonPost(debug) - time.sleep(0.03) self.wifi_extra_data["resource"] = resource - self.wifi_extra_data["port"] = self.vap_name + self.wifi_extra_data["port"] = LFUtils.name_to_eid(self.vap_name)[2] if self.wifi_extra_data_modified: wifi_extra_r.addPostData(self.wifi_extra_data) wifi_extra_r.jsonPost(debug) - port_list = self.local_realm.json_get("port/1/1/list") - if port_list is not None: - port_list = port_list['interfaces'] - for port in port_list: - for k, v in port.items(): - if v['alias'] == 'br0': - self.local_realm.rm_port(k, check_exists=True) - time.sleep(5) - - # create bridge - if bridge: - print("creating bridge") - data = { - "shelf": 1, - "resource": resource, - "port": "br0", - "network_devs": "eth1,%s" % self.vap_name - } - self.local_realm.json_post("cli-json/add_br", data) - - bridge_set_port = { - "shelf": 1, - "resource": resource, - "port": "br0", - "current_flags": 0x80000000, - "interest": 0x4000 # (0x2 + 0x4000 + 0x800000) # current, dhcp, down - } - self.local_realm.json_post("cli-json/set_port", bridge_set_port) - - if self.up: - self.admin_up(resource) + desired_ports = ["1.%s.%s" % (resource, LFUtils.name_to_eid(self.vap_name)[2])] + if LFUtils.wait_until_ports_appear(base_url=self.lfclient_url, port_list=desired_ports, debug=debug): + if up: + self.admin_up(resource) + + if LFUtils.wait_until_ports_admin_up(base_url=self.lfclient_url, port_list=desired_ports, debug_=debug): + return True + else: + return False # Ports did not go admin up + else: + return True # We are not trying to admin them up + else: + return False # Ports did not appear def cleanup(self, resource): - print("Cleaning up VAPs") - desired_ports = ["1.%s.%s" % (resource, self.vap_name), "1.%s.br0" % resource] + print("Cleaning up VAP") + + desired_ports = ["1.%s.%s" % (resource, LFUtils.name_to_eid(self.vap_name)[2])] # First, request remove on the list. for port_eid in desired_ports: self.local_realm.rm_port(port_eid, check_exists=True) # And now see if they are gone - LFUtils.wait_until_ports_disappear(base_url=self.lfclient_url, port_list=desired_ports) + return LFUtils.wait_until_ports_disappear(base_url=self.lfclient_url, port_list=desired_ports) diff --git a/py-scripts/create_vap.py b/py-scripts/create_vap.py index 3425449e..d9751030 100755 --- a/py-scripts/create_vap.py +++ b/py-scripts/create_vap.py @@ -7,9 +7,11 @@ import os import importlib import argparse import pprint +import logging +logger = logging.getLogger(__name__) if sys.version_info[0] != 3: - print("This script requires Python 3") + logger.critical("This script requires Python 3") exit(1) @@ -20,6 +22,7 @@ LFCliBase = lfcli_base.LFCliBase LFUtils = importlib.import_module("py-json.LANforge.LFUtils") realm = importlib.import_module("py-json.realm") Realm = realm.Realm +lf_logger_config = importlib.import_module("py-scripts.lf_logger_config") class CreateVAP(Realm): @@ -38,8 +41,6 @@ class CreateVAP(Realm): _radio=None, _channel=36, _country_code=0, - _nss=False, - _bridge=False, _proxy_str=None, _debug_on=False, _exit_on_error=False, @@ -69,7 +70,6 @@ class CreateVAP(Realm): self.number_template = _number_template self.debug = _debug_on self.dhcp = _dhcp - self.bridge = _bridge self.vap_profile = self.new_vap_profile() self.vap_profile.vap_name = self.vap_list self.vap_profile.ssid = self.ssid @@ -92,19 +92,23 @@ class CreateVAP(Realm): self.security, self.ssid, passwd=self.password) print("Creating VAPs") - self.vap_profile.create(resource=self.resource, + # TODO: Add cmd line arguments to control the various options of the VAP profile. + if self.vap_profile.create(resource=self.resource, radio=self.radio, channel=self.channel, - up_=True, - debug=False, + up=True, + debug=self.debug, use_ht40=True, use_ht80=True, use_ht160=False, suppress_related_commands_=True, use_radius=False, - hs20_enable=False, - bridge=self.bridge) - self._pass("PASS: VAP build finished") + hs20_enable=False): + self._pass("PASS: VAP build finished") + return True + else: + self._fail("VAP profile creation failed.") + return False def main(): @@ -141,23 +145,23 @@ Command example: required=False, default=None, action='append') - optional.add_argument( - '--bridge', - help='Create a bridge connecting the VAP to a port', - required=False, - default=False) optional.add_argument( '--mac', help='Custom mac address', default="xx:xx:xx:xx:*:xx") - optional.add_argument('--mode', default='AUTO') + optional.add_argument('--mode', default='0') # 0 means auto # TODO: Add help for other available modes. optional.add_argument('--channel', default=36) optional.add_argument('--country_code', default=0) - optional.add_argument('--nss', default=False) optional.add_argument('--resource', default=1) optional.add_argument('--start_id', default=0) - optional.add_argument('--vap_name', default=None) + optional.add_argument('--vap_suffix', default=None, help='The numeric suffix, like the 005 in vap005') args = parser.parse_args() + + logger_config = lf_logger_config.lf_logger_config() + # set the logger level to requested value + logger_config.set_level(level=args.log_level) + logger_config.set_json(json_file=args.lf_logger_config_json) + # if args.debug: # pprint.pprint(args) # time.sleep(5) @@ -174,7 +178,8 @@ Command example: # print(args.passwd) # print(args.ssid) - if args.vap_name is None: + create_vaps = [] + if args.vap_suffix is None: for vap in vap_list: create_vap = CreateVAP(_host=args.mgr, _port=args.mgr_port, @@ -188,15 +193,16 @@ Command example: _radio=args.radio, _channel=args.channel, _country_code=args.country_code, - _nss=args.nss, _proxy_str=args.proxy, - _bridge=args.bridge, _debug_on=args.debug) print('Creating VAP') - - create_vap.build() + if create_vap.build(): + create_vap._pass("VAP %s created." % (vap)) + else: + create_vap._fail("VAP %s was not created." % (vap)) + create_vaps.append(create_vap) else: - vap_name = "vap" + args.vap_name + vap_name = "vap" + args.vap_suffix create_vap = CreateVAP(_host=args.mgr, _port=args.mgr_port, _ssid=args.ssid, @@ -209,14 +215,26 @@ Command example: _radio=args.radio, _channel=args.channel, _country_code=args.country_code, - _nss=args.nss, _proxy_str=args.proxy, - _bridge=args.bridge, _debug_on=args.debug) print('Creating VAP') + if create_vap.build(): + create_vap._pass("VAP %s created." % (vap)) + else: + create_vap._fail("VAP %s was not created." % (vap)) + create_vaps.append(create_vap) - create_vap.build() + # TODO: Add logic to clean up vap, unless --noclean option is specified. + any_failed = False + for v in create_vaps: + if not v.passes(): + any_failed = True + v.print_pass_fail() + + if any_failed: + exit(1) + exit(0) if __name__ == "__main__": main()