def __init__(self, service_connection_string, edge_hub_device_id=None): self.service_connection_string = service_connection_string self.helper = Helper(self.service_connection_string) if not edge_hub_device_id: self._createNewHub() else: self._useExistingHub(edge_hub_device_id)
def horton_delete_identities_and_containers(self, save_manifest_file): init(autoreset=True) print(Fore.GREEN + "Deleting Devices/Modules/Containers in IotHub and Docker from: {}".format(save_manifest_file)) deployment_json = self.get_deployment_model_json(save_manifest_file) hub_connect_string = self.get_env_connect_string() helper = Helper(hub_connect_string) try: identity_json = deployment_json['containers'] for containers in identity_json: container_json = identity_json[containers] container_name = container_json['name'] self.delete_container(container_name) except: print(Fore.RED + "Exception Processing HortonManifest Containers: " + save_manifest_file, file=sys.stderr) traceback.print_exc() sys.exit(-1) try: identity_json = deployment_json['identities'] for azure_device in identity_json: device_json = identity_json[azure_device] objectType = device_json['objectType'] if objectType in ["iothub_device", "iotedge_device"]: device_id = device_json['deviceId'] print("deleting device {}".format(device_id)) helper.try_delete_device(device_id) except: print(Fore.RED + "Exception Processing HortonManifest Identities: " + save_manifest_file, file=sys.stderr) traceback.print_exc() sys.exit(-1) return True
def remove_edgehub_device(): print("Removing edgehub device") if "IOTHUB_E2E_CONNECTION_STRING" not in os.environ: print( "ERROR: Iothub connection string not set in IOTHUB_E2E_CONNECTION_STRING environment variable." ) sys.exit(1) service_connection_string = os.environ["IOTHUB_E2E_CONNECTION_STRING"] config_file = ConfigFile() device_connection_string = config_file.contents["provisioning"][ "device_connection_string"] if device_connection_string: cn = connection_string_to_dictionary(device_connection_string) old_edgehub_device_id = cn["DeviceId"] old_edgehub_leaf_device = "{}_leaf_device".format( old_edgehub_device_id) helper = Helper(service_connection_string) if helper.try_delete_device(old_edgehub_device_id): print("deleted {}".format(old_edgehub_device_id)) if helper.try_delete_device(old_edgehub_leaf_device): print("deleted {}".format(old_edgehub_leaf_device)) print("updating config.yaml to remove strings") config_file.contents["provisioning"]["device_connection_string"] = "" config_file.save() print("config.yaml updated") print("edgehub test devices removed") else: print("no devices to remove")
def create_device_module(self, connect_string, device_name, module_name): mod_connect = None try: helper = Helper(connect_string) helper.create_device_module(device_name, module_name) print("create_device_module: ({}/{}) returned: ({})".format(device_name, module_name, 'OK')) mod_connect = helper.get_module_connection_string(device_name, module_name) except: print(Fore.RED + "Exception creating device: {}/{}".format(device_name, module_name), file=sys.stderr) traceback.print_exc() print(Fore.RESET, file=sys.stderr) return mod_connect
def create_iot_device(self, connect_string, device_name): dev_connect = "" try: helper = Helper(connect_string) helper.create_device(device_name) print("create_iot_device: ({}) returned: ({})".format(device_name, 'OK')) dev_connect = helper.get_device_connection_string(device_name) except: print(Fore.RED + "Exception creating device: " + device_name, file=sys.stderr) traceback.print_exc() print(Fore.RESET, file=sys.stderr) return dev_connect
def create_device_module(self, connect_string, device_name, module_name): mod_connect = "" try: helper = Helper(connect_string) helper.create_device_module(device_name, module_name) mod_connect = helper.get_module_connection_string( device_name, module_name) except: print(Fore.RED + "Exception creating module: {}/{}".format( device_name, module_name), file=sys.stderr) traceback.print_exc() sys.exit(-1) return mod_connect
def create_iot_device(self, hub_connect_string, device_name, is_edge=False): dev_connect = "" try: helper = Helper(hub_connect_string) helper.create_device(device_name, is_edge) dev_connect = helper.get_device_connection_string(device_name) except: print(Fore.RED + "Exception creating device: " + device_name, file=sys.stderr) traceback.print_exc() sys.exit(-1) return dev_connect
def get_edge_modules_connect_string(self, save_manifest_file): deployment_json = self.get_deployment_model_json(save_manifest_file) hub_connect_string = self.get_env_connect_string() json_updated = False try: identity_json = deployment_json['identities'] service_helper = Helper(hub_connect_string) for azure_device in identity_json: device_json = identity_json[azure_device] if device_json['objectType'] == "iotedge_device": device_id = device_json['deviceId'] if 'modules' in device_json: modules = device_json['modules'] for module_name in modules: module_json = modules[module_name] try: module_connect_string = service_helper.get_module_connection_string( device_id, module_name) except: module_connect_string = "Module connect Not Found" module_json[ 'connectionString'] = module_connect_string json_updated = True device_json['modules'][module_name] = module_json deployment_json['identities'][azure_device] = device_json if json_updated: with open(save_manifest_file, 'w') as f: f.write( json.dumps(deployment_json, default=lambda x: x.__dict__, sort_keys=False, indent=2)) except: print(Fore.RED + "Exception Processing HortonManifest: " + save_manifest_file, file=sys.stderr) traceback.print_exc() sys.exit(-1)
print("Removing edgehub device") verifyEnvironmentVariables() if "IOTHUB_E2E_CONNECTION_STRING" not in os.environ: print( "ERROR: Iothub connection string not set in IOTHUB_E2E_CONNECTION_STRING environment variable." ) sys.exit(1) service_connection_string = os.environ["IOTHUB_E2E_CONNECTION_STRING"] if "IOTHUB_E2E_EDGEHUB_DEVICE_ID" in os.environ: old_edgehub_device_id = os.environ["IOTHUB_E2E_EDGEHUB_DEVICE_ID"] old_edgehub_leaf_device = "{}_leaf_device".format(old_edgehub_device_id) helper = Helper(service_connection_string) if helper.try_delete_device(old_edgehub_device_id): print("deleted {}".format(old_edgehub_device_id)) if helper.try_delete_device(old_edgehub_leaf_device): print("deleted {}".format(old_edgehub_leaf_device)) print("updating config.yaml to remove strings") config_file = ConfigFile() config_file.contents["provisioning"]["device_connection_string"] = "" config_file.save() print("config.yaml updated") print("edgehub test devices removed") else: print("no devices to remove")
class EdgeHub: def __init__(self, service_connection_string, edge_hub_device_id=None): self.service_connection_string = service_connection_string self.helper = Helper(self.service_connection_string) if not edge_hub_device_id: self._createNewHub() else: self._useExistingHub(edge_hub_device_id) def _createNewHub(self): self.edge_hub_device_id = get_random_device_name().lower() self.helper.create_device(self.edge_hub_device_id, True) self._finishHubSetup() def _useExistingHub(self, edge_hub_device_id): self.edge_hub_device_id = edge_hub_device_id self._finishHubSetup() def _finishHubSetup(self): self.edge_hub_connection_string = self.helper.get_device_connection_string( self.edge_hub_device_id) self.leaf_device_id = self.edge_hub_device_id + "_leaf_device" try: self.leaf_device_connection_string = self.helper.get_device_connection_string( self.leaf_device_id) except: self.helper.create_device(self.leaf_device_id) self.leaf_device_connection_string = self.helper.get_device_connection_string( self.leaf_device_id) self._discoverAllExistingModules() def _discoverAllExistingModules(self): self.modules = [] for name in all_containers: mod = all_containers[name] try: thisModString = self.helper.get_module_connection_string( self.edge_hub_device_id, mod.module_id) except: pass else: mod.connection_string = thisModString self.modules.append(mod) def deployModules(self, modules): configuration = EdgeConfiguration() configuration.add_module(all_containers["friend"]) for name in modules.split(","): name = name.strip() if name in all_containers: configuration.add_module(all_containers[name]) else: print("module {} is invalid".format(name)) print("valid modules are {0}".format(", ".join( all_containers.keys()))) raise Exception("module " + name + " not defined") self.helper.apply_configuration(self.edge_hub_device_id, configuration.get_module_config())
def horton_create_identities(self, save_manifest_file): init(autoreset=True) deployment_json = self.get_deployment_model_json(save_manifest_file) hub_connect_string = self.get_env_connect_string() id_prefix = "horton_{}_{}".format(self.get_random_num_string(9999), self.get_random_num_string(9999)) device_count = 0 module_count = 0 other_count = 0 try: identity_json = deployment_json['identities'] for azure_device in identity_json: device_json = identity_json[azure_device] objectType = device_json['objectType'] objectName = device_json['objectName'] device_id = "{}_{}".format(id_prefix, objectName) if objectType == "iothub_device": device_json['deviceId'] = device_id device_json['connectionString'] = self.create_iot_device( hub_connect_string, device_id) device_count += 1 if 'modules' in device_json: modules = device_json['modules'] for module_name in modules: module_json = modules[module_name] module_json['moduleId'] = module_name module_json['deviceId'] = device_id module_json[ 'connectionString'] = self.create_device_module( hub_connect_string, device_id, module_name) device_json['modules'][module_name] = module_json module_count += 1 elif objectType in ["iothub_service", "iothub_registry"]: print("creating service {}".format(device_id)) device_json['connectionString'] = hub_connect_string other_count += 1 elif objectType == "iotedge_device": device_json['deviceId'] = device_id device_connect_string = self.create_iot_device( hub_connect_string, device_id, True) device_json['connectionString'] = device_connect_string device_count += 1 service_helper = Helper(hub_connect_string) edge_config = EdgeConfiguration() if 'modules' in device_json: modules = device_json['modules'] for module_name in modules: module_json = modules[module_name] module_json['moduleId'] = module_name module_json['deviceId'] = device_id mod = containers.Container() mod.module_id = module_name full_module_name = "{}/{}".format( device_id, module_name) print("creating Edge module {}".format( full_module_name)) mod.name = module_name mod.image_to_deploy = module_json[ 'image'] + ':' + module_json['imageTag'] mod.host_port = self.get_int_from_string( module_json['tcpPort']) mod.container_port = self.get_int_from_string( module_json['containerPort']) edge_config.add_module(mod) device_json['modules'][module_name] = module_json module_count += 1 module_edge_config = edge_config.get_module_config() service_helper.apply_configuration(device_id, module_edge_config) deployment_json['identities'][azure_device] = device_json except: print(Fore.RED + "Exception Processing HortonManifest: " + save_manifest_file, file=sys.stderr) traceback.print_exc() sys.exit(-1) print(Fore.GREEN + "Created {} Devices, {} Modules and {} Others".format( device_count, module_count, other_count)) try: with open(save_manifest_file, 'w') as f: f.write( json.dumps(deployment_json, default=lambda x: x.__dict__, sort_keys=False, indent=2)) except: print(Fore.RED + "ERROR: writing JSON manifest to: " + save_manifest_file, file=sys.stderr) traceback.print_exc() sys.exit(-1) self.get_edge_modules_connect_string(save_manifest_file) return True