示例#1
0
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