Пример #1
0
 def test_get_config_dir_valid(self):
     """Test valid host OS platform returns a non empty directory"""
     dir_name = EdgeDefault.get_config_dir('windows')
     self.assertIsNotNone(dir_name)
     self.assertGreater(len(dir_name), 0)
     dir_name = EdgeDefault.get_config_dir('linux')
     self.assertIsNotNone(dir_name)
     self.assertGreater(len(dir_name), 0)
     dir_name = EdgeDefault.get_config_dir('darwin')
     self.assertIsNotNone(dir_name)
     self.assertGreater(len(dir_name), 0)
Пример #2
0
    def _get_host_config_dir():
        edge_config_dir = None
        result = None
        host = platform.system()
        log.debug('Searching Edge config dir in env var %s', EC.ENV_EDGECONFIGDIR)
        env_config_dir = os.getenv(EC.ENV_EDGECONFIGDIR, None)
        if env_config_dir and env_config_dir.strip() != '':
            edge_config_dir = os.path.realpath(env_config_dir)
        else:
            meta_config_file_path = EdgeDefault.get_meta_conf_file_path()
            log.debug('Searching Edge config dir in config file %s', meta_config_file_path)
            if meta_config_file_path and os.path.exists(meta_config_file_path):
                data = EdgeHostPlatform._read_json_config_file(meta_config_file_path)
                config_dir = data[EC.CONFIG_DIR_KEY]
                if config_dir != '':
                    edge_config_dir = os.path.realpath(config_dir)
            else:
                edge_config_dir = os.path.realpath(EdgeDefault.get_config_dir(host))
                log.debug('Using default Edge config dir %s', edge_config_dir)

        if edge_config_dir and os.path.isdir(edge_config_dir):
            log.debug('Found config directory: %s', edge_config_dir)
            result = edge_config_dir

        return result
Пример #3
0
    def choose_platform_config_dir(user_input_path, user_input_option):
        """
        Utility function that chooses a Edge config directory in the
        precedence order of:
        1) Env variable EDGECONFIGDIR
        2) User input via user_input_path
        3) Default Path

        Args:
            user_input_path: (string) A user supplied config dir path. Can be None or ''.
            user_input_option: (enum EdgeConfigDirInputSource member):
                               Use NONE when user_input_path is None or empty

        Return:
            Tuple:
               [0]: Path to the Edge config dir.
               [1]: An EdgeConfigDirInputSource enum member indicating which dir was chosen
        """
        edge_config_dir = None
        choice = None

        env_config_dir = os.getenv(EC.ENV_EDGECONFIGDIR, None)
        if env_config_dir and env_config_dir.strip() != '':
            edge_config_dir = os.path.realpath(env_config_dir)
            log.info('Using environment variable %s as IoT Edge configuration dir: %s',
                     EC.ENV_EDGECONFIGDIR, edge_config_dir)
            choice = EdgeConfigDirInputSource.ENV
        elif user_input_path and user_input_path.strip() != '':
            edge_config_dir = os.path.realpath(user_input_path)
            log.info('Using user configured IoT Edge configuration dir: %s', edge_config_dir)
            choice = user_input_option
        else:
            edge_config_dir = os.path.realpath(EdgeDefault.get_config_dir(platform.system()))
            log.info('Using default IoT Edge configuration dir: %s', edge_config_dir)
            choice = EdgeConfigDirInputSource.DEFAULT

        return (edge_config_dir, choice)
Пример #4
0
 def test_get_config_dir_invalid(self):
     """Test invalid host OS platform returns None"""
     dir_name = EdgeDefault.get_config_dir('blah')
     self.assertIsNone(dir_name)
     dir_name = EdgeDefault.get_config_dir('')
     self.assertIsNone(dir_name)
Пример #5
0
 def test_get_config_dir_exception(self):
     """Test None as host OS raises edgectl.errors.EdgeInvalidArgument"""
     with self.assertRaises(edgectl.errors.EdgeInvalidArgument):
         EdgeDefault.get_config_dir(None)
Пример #6
0
    def _process_cli_args(self):
        parser = argparse.ArgumentParser(prog=EdgeCLI._prog(),
                                         formatter_class=argparse.RawTextHelpFormatter,
                                         description='Azure IoT Edge Runtime Control Interface (DEPRECATED).\n' +
                                                     'For more information visit: https://docs.microsoft.com/en-us/azure/iot-edge/',
                                         epilog='''''')
        parser.add_argument('--version',
                            action='version',
                            version='{0} {1}'.format(EdgeCLI._prog(), self._version()))

        verbose_help_str = 'Set verbosity. Levels: ' \
                           + ', '.join(EdgeCLI._supported_log_levels()) \
                           + '. Default: ' + EdgeCLI._default_log_level()

        parser.add_argument('--verbose', dest='verbose_level',
                            choices=EdgeCLI._log_level_choices(),
                            default=EdgeCLI._default_log_level(),
                            help=verbose_help_str, metavar='')

        subparsers = parser.add_subparsers(title='commands',
                                           description='Azure IoT Edge Commands',
                                           help='sub-command help',
                                           dest='subparser_name')

        cmd_setup = subparsers.add_parser('setup',
                                          description='Setup the runtime. This must be run before starting.',
                                          help='Setup the runtime. This must be run before starting.',
                                          formatter_class=argparse.RawTextHelpFormatter)

        cmd_setup.add_argument('--config-file',
                               help='Setup the runtime using the specified configuration file. Optional.\n'
                               + 'If specified, all other command line inputs will be ignored.',
                               metavar='')

        cmd_setup.add_argument('--connection-string',
                               help='Set the Azure IoT Hub device connection string. Required.\n'
                               + 'Note: Use double quotes when supplying this input.',
                               metavar='')

        cmd_setup.add_argument('--edge-config-dir',
                               help='Set runtime configuration directory. Optional.\n'
                               + 'Note: If the configuration directory value is provided, it is saved in\n'
                               + 'the ' + EdgeCLI._prog() + ' configuration file located in the following directories:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_edge_ctl_diagnostic_path(EC.DOCKER_HOST_LINUX) + '\n'
                               + '   Windows Hosts - ' + EdgeDefault.get_edge_ctl_diagnostic_path(EC.DOCKER_HOST_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_edge_ctl_diagnostic_path(EC.DOCKER_HOST_DARWIN) + '\n'
                               + 'Instead of using this option, an environment variable "' + EC.ENV_EDGECONFIGDIR +'"\n'
                               + 'can be set with an absolute path to a home directory.\n'
                               + 'If environment variable "' + EC.ENV_EDGECONFIGDIR +'" is set and this option is specified,\n'
                               + 'the environment variable will take precedence and the supplied directory value will be ignored.\n'
                               + 'If none of these are provided, the following directories will be used as the default\n'
                               + 'IoT Edge configuration directory:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_config_dir(EC.DOCKER_HOST_LINUX) + '\n'
                               + '   Windows Hosts - ' + EdgeDefault.get_config_dir(EC.DOCKER_HOST_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_config_dir(EC.DOCKER_HOST_DARWIN),
                               metavar='')

        cmd_setup.add_argument('--edge-home-dir',
                               help='Set runtime home directory. Optional.\n'
                               + 'Default:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_home_dir(EC.DOCKER_HOST_LINUX) + '\n'
                               + '   Windows Hosts - ' + EdgeDefault.get_home_dir(EC.DOCKER_HOST_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_home_dir(EC.DOCKER_HOST_DARWIN),
                               metavar='')

        cmd_setup.add_argument('--edge-hostname',
                               help='Set the runtime hostname (FQDN). Optional.\n'
                               + 'Used when operating the runtime as a \'Gateway\' for leaf devices.',
                               metavar='')

        log_levels = EdgeDefault.get_runtime_log_levels()
        log_levels = ", ".join(log_levels)
        cmd_setup.add_argument('--runtime-log-level',
                               help='Set runtime log level. Optional.\n'
                               + 'Levels:  ' + log_levels + '\n'
                               + 'Default: ' + EdgeDefault.get_default_runtime_log_level(),
                               metavar='')

        cmd_setup.add_argument('--image',
                               help='Set the Edge Agent image. Optional.\n',
                               metavar='')

        cmd_setup.add_argument('--docker-registries',
                               help='Set a list of registries and their credentials. Optional.\n'
                               + 'Specified as triples of registry address, username, password.\n'
                               + 'Example: --docker-registries reg1 user1 pass1'
                               , nargs='+', metavar='')

        cmd_setup.add_argument('--docker-uri', '--edge-runtime-docker-uri',
                               help='Set docker endpoint URI for the IoT Edge runtime. Optional.\n'
                               + 'Default:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_LINUX, EC.DOCKER_ENGINE_LINUX) + '\n'
                               + '   Windows Hosts (Linux VM) - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_WINDOWS, EC.DOCKER_ENGINE_LINUX) + '\n'
                               + '   Windows Hosts (Native) - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_WINDOWS, EC.DOCKER_ENGINE_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_DARWIN, EC.DOCKER_ENGINE_LINUX) + '\n'
                               + 'Note: This is strictly the URI that the Edge runtime will use to interact with docker daemon.\n'
                               + 'The URI is determined by the underlying container technology being used by the docker daemon.\n'
                               + '  - Specifically, these could be Linux based containers or Windows based containers.\n'
                               + 'Sub note: Windows hosts are able to run both Linux and Windows containers but not vice versa.\n'
                               + 'In most cases, the Edge runtime docker URI is the same as the host docker daemon URI\n'
                               + 'but that may not always be the case. For example, if the docker host is Windows or\n'
                               + 'Windows Subsystem for Linux (WSL), the docker URI could be tcp://x.y.z.w:2375.\n'
                               + 'However, the underlying container technology could be Linux based and thus\n'
                               + 'the Edge runtime docker URI would be: ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_LINUX, EC.DOCKER_ENGINE_LINUX),
                               metavar='')

        cmd_setup.add_argument('--upstream-protocol',
                               help='Set the protocol that the edge runtime should use to communicate with the IoTHub. Optional.\n'
                               + 'Permitted values are Amqp (Amqp over TCP), AmqpWs (Amqp over WebSockets), \n'
                               + 'Mqtt (Mqtt over TCP) and MqttWs (Mqtt over WebSockets)\n',
                               metavar='')

        cmd_setup.add_argument('--auto-cert-gen-force-no-passwords', '--nopass',
                               help='Do not prompt for passwords when generating private keys. Optional.',
                               action='store_true')

        cmd_setup.add_argument('--owner-ca-cert-file',
                               help='Owner CA certificate in X.509 PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-cert-file',
                               help='Device CA certificate in X.509 PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-chain-cert-file',
                               help='Device CA chain certificate in X.509 PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-private-key-file',
                               help='Device CA certificate private key file in PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-passphrase-file',
                               help='Device CA certificate private key passphrase file in ascii text.\n' \
                               'Either provide the passphrase file or use the --device-ca-passphrase option but not both.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-passphrase',
                               help='Device CA certificate private key passphrase in ascii text.\n' \
                               'Use this option to provide the passphrase or use the\n' \
                               '--device-ca-passphrase-file option but not both.',
                               metavar='')

        cmd_setup.add_argument('--agent-ca-passphrase-file',
                               help='Agent CA certificate private key passphrase file in ascii text.\n' \
                               'Either provide the passphrase or use the --agent-ca-passphrase option but not both.',
                               metavar='')

        cmd_setup.add_argument('--agent-ca-passphrase',
                               help='Agent CA certificate private key passphrase in ascii text.\n' \
                               'Use this option to provide the passphrase or use the\n' \
                               '--agent-ca-passphrase-file option but not both.',
                               metavar='')

        subj = EdgeDefault.get_certificate_subject_dict()
        cmd_setup.add_argument('-C', '--country',
                               help='Two letter country code. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_COUNTRY_KEY]), metavar='')

        cmd_setup.add_argument('-ST', '--state',
                               help='State. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_STATE_KEY]), metavar='')

        cmd_setup.add_argument('-L', '--locality',
                               help='Locality or city. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_LOCALITY_KEY]), metavar='')

        cmd_setup.add_argument('-OR', '--organization',
                               help='Organization name. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_ORGANIZATION_KEY]), metavar='')

        cmd_setup.add_argument('-OU', '--organization-unit',
                               help='Organization unit name. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_ORGANIZATION_UNIT_KEY]), metavar='')

        cmd_setup.add_argument('-CN', '--common-name',
                               help='Common name used for the Device CA certificate. This parameter is used when autogenerating\n' \
                               'certificates. Optional.\n' \
                               'Default: \'{0}\'. ' \
                               .format(subj[EC.SUBJECT_COMMON_NAME_KEY]),
                               metavar='')

        cmd_setup.set_defaults(func=self._parse_edge_command)

        cmd_start = subparsers.add_parser('start', description="Start the runtime.", help='Start the runtime.')
        cmd_start.set_defaults(func=self._parse_edge_command)

        cmd_restart = subparsers.add_parser('restart', description='Restart the runtime.', help='Restart the runtime.')
        cmd_restart.set_defaults(func=self._parse_edge_command)

        cmd_stop = subparsers.add_parser('stop', description='Stop the runtime.', help='Stop the runtime.')
        cmd_stop.set_defaults(func=self._parse_edge_command)

        cmd_status = subparsers.add_parser('status', description='Report the status of the runtime.', help='Report the status of the runtime.')
        cmd_status.set_defaults(func=self._parse_edge_command)

        cmd_update = subparsers.add_parser('update', description='Update the Edge Agent image.', help='Update the Edge Agent image.')
        cmd_update.add_argument('--image', help='Specify the Edge Agent image', metavar='')
        cmd_update.set_defaults(func=self._parse_edge_command)

        cmd_uninstall = subparsers.add_parser('uninstall', description='Remove all modules and generated files.',
                                              help='Remove all modules and generated files.')
        cmd_uninstall.set_defaults(func=self._parse_edge_command)

        cmd_login = subparsers.add_parser('login', description="Log in to a container registry.",
                                          help='Log in to a container registry.',
                                          formatter_class=argparse.RawTextHelpFormatter)
        cmd_login.add_argument('--address', help='Specify the container registry. (e.g. example.azurecr.io)\nDefault: Docker Hub',
                               required=False, default=EdgeCLI._DEFAULT_REGISTRY_ADDRESS)
        cmd_login.add_argument('--username', help='Specify the username of the container registry', required=True)
        cmd_login.add_argument('--password', help='Specify the password of the container registry', required=True)
        cmd_login.set_defaults(func=self._parse_edge_command)

        args = parser.parse_args()
        if 'func' in vars(args):
            return args.func(args)
        parser.print_usage()
        return (False, False)