Пример #1
0
    def clean_all(self, config_file, region=None, profile_name=None):
        """
        Clean all provisioned artifacts from both the local file and the AWS
        Greengrass service.

        :param config_file: config file containing the group to clean
        :param region: the region in which the group should be cleaned.
            [default: us-west-2]
        :param profile_name: the name of the `awscli` profile to use.
            [default: None]
        """
        logging.info('[begin] Cleaning all provisioned artifacts')
        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh() is True:
            raise ValueError("Config is already clean.")

        if region is None:
            region = self._region

        self._delete_group(
            config_file, region=region, profile_name=profile_name)
        self.clean_core(config_file, region=region)
        self.clean_devices(config_file, region=region)
        self.clean_file(config_file)

        logging.info('[end] Cleaned all provisioned artifacts')
Пример #2
0
    def deploy(self, config_file, region=None, profile_name=None):
        """
        Deploy the configuration and Lambda functions of a Greengrass group to
        the Greengrass core contained in the group.

        :param config_file: config file of the group to deploy
        :param region: the region from which to deploy the group.
        :param profile_name: the name of the `awscli` profile to use.
            [default: None]
        """
        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh():
            raise ValueError("Config not yet tracking a group. Cannot deploy.")

        if region is None:
            region = self._region

        gg_client = _get_gg_session(region=region, profile_name=profile_name)

        dep_req = gg_client.create_deployment(
            GroupId=config['group']['id'],
            GroupVersionId=config['group']['version'],
            DeploymentType="NewDeployment"
        )
        print("Group deploy requested for deployment_id:{0}".format(
            dep_req['DeploymentId'],
        ))
Пример #3
0
    def create_core(self,
                    thing_name,
                    config_file,
                    region=None,
                    cert_dir=None,
                    account_id=None,
                    policy_name='ggc-default-policy'):
        """
        Using the `thing_name` value, creates a Thing in AWS IoT, attaches and
        downloads new keys & certs to the certificate directory, then records
        the created information in the local config file for inclusion in the
        Greengrass Group as a Greengrass Core.

        :param thing_name: the name of the thing to create and use as a
            Greengrass Core
        :param config_file: config file used to track the Greengrass Core in the
            group
        :param region: the region in which to create the new core.
            [default: us-west-2]
        :param cert_dir: the directory in which to store the thing's keys and
            certs. If `None` then use the current directory.
        :param account_id: the account_id in which to create the new core.
            [default: None]
        :param policy_name: the name of the policy to associate with the device.
            [default: 'ggc-default-policy']
        """
        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh() is False:
            raise ValueError(
                "Config file already tracking previously created core or group"
            )
        if region is None:
            region = self._region
        if account_id is None:
            account_id = self._account_id
        keys_cert, thing = self.create_thing(thing_name, region, cert_dir)

        cert_arn = keys_cert['certificateArn']
        config['core'] = {
            'thing_arn': thing['thingArn'],
            'cert_arn': cert_arn,
            'cert_id': keys_cert['certificateId'],
            'thing_name': thing_name
        }
        logging.debug("create_core cfg:{0}".format(config))
        logging.info("Thing:'{0}' associated with cert:'{1}'".format(
            thing_name, cert_arn))
        core_policy = self.get_core_policy(core_name=thing_name,
                                           account_id=account_id,
                                           region=region)
        iot_client = _get_iot_session(region=region)
        self._create_attach_thing_policy(cert_arn,
                                         core_policy,
                                         iot_client=iot_client,
                                         policy_name=policy_name)
        misc = config['misc']
        misc['policy_name'] = policy_name
        config['misc'] = misc
Пример #4
0
    def clean_file(config_file):
        """
        Clean all provisioned artifacts from the local config file.

        :param config_file: config file of the group to clean
        """
        logging.info('[begin] Cleaning config file')
        config = GroupConfigFile(config_file=config_file)

        if config.is_fresh() is True:
            raise ValueError("Config is already clean.")
        config.make_fresh()
        logging.info('[end] Cleaned config file:{0}'.format(config_file))
Пример #5
0
    def clean_all(self, config_file, region='us-west-2'):
        """
        Clean all provisioned artifacts from both the local file and the AWS
        Greengrass service.

        :param config_file: config file of the group to clean
        :param region: the region in which to clean the group
        """
        logging.info('[begin] Cleaning all provisioned artifacts')
        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh() is True:
            raise ValueError("Config is already clean.")

        self._delete(config_file, region=region)
        self.clean_file(config_file)

        logging.info('[end] Cleaned all provisioned artifacts')
Пример #6
0
    def deploy(self, config_file, region='us-west-2'):
        """
        Deploy the configuration and Lambda functions of a Greengrass group to
        the Greengrass core contained in the group.

        :param config_file: config file of the group to deploy
        :param region: the region from which to deploy the group.
        """
        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh():
            raise ValueError("Config not yet tracking a group. Cannot deploy.")

        gg_client = boto3.client("greengrass", region_name=region)
        dep_req = gg_client.create_deployment(
            GroupId=config['group']['id'],
            GroupVersionId=config['group']['version'],
            DeploymentType="NewDeployment"
        )
        print("Group deploy requested for deployment_id:{0}".format(
            dep_req['DeploymentId'],
        ))
Пример #7
0
    def create(self, group_type, config_file, group_name=None,
               region=None, profile_name=None):
        """
        Create a Greengrass group in the given region.

        :param group_type: the type of group to create. Must match a `key` in
            the `group_types` dict
        :param config_file: config file of the group to create
        :param group_name: the name of the group. If no name is given, then
            group_type will be used.
        :param region: the region in which to create the new group.
            [default: us-west-2]
        :param profile_name: the name of the `awscli` profile to use.
            [default: None]
        """
        logging.info("[begin] create command using group_types:{0}".format(
            self.group_types))

        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh() is False:
            raise ValueError(
                "Config file already tracking previously created group"
            )

        if group_type not in self.group_types.keys():
            raise ValueError("Can only create {0} groups.".format(
                self.group_types)
            )

        if region is None:
            region = self._region

        # create an instance of the requested group type that uses the given
        # config file and region
        gt = self.group_types[group_type](config=config, region=region)

        # get and store the account's IoT endpoint for future use
        ep = _get_iot_session(region=region).describe_endpoint()
        misc = config['misc']
        misc['iot_endpoint'] = ep['endpointAddress']
        config['misc'] = misc

        # Create a Group
        logging.info("[begin] Creating a Greengrass Group")
        if group_name is None:
            group_name = group_type

        gg_client = _get_gg_session(region=region, profile_name=profile_name)

        group_info = gg_client.create_group(Name="{0}".format(group_name))
        config['group'] = {"id": group_info['Id']}

        # setup the policies and roles
        gt.create_and_attach_thing_policy()
        gt.create_and_attach_iam_role()

        cl_arn = self._create_core_definition(
            gg_client=gg_client, group_type=gt,
            config=config, group_name=group_name
        )
        dl_arn = self._create_device_definition(
            gg_client=gg_client, group_type=gt,
            config=config, group_name=group_name
        )
        lv_arn = self._create_function_definition(
            gg_client=gg_client, group_type=gt,
            config=config
        )
        log_arn = self._create_logger_definition(
            gg_client=gg_client, group_type=gt,
            config=config
        )
        sub_arn = self._create_subscription_definition(
            gg_client=gg_client, group_type=gt,
            config=config
        )

        logging.info(
            'Group details, core_def:{0} device_def:{1} func_def:{2} '
            'logger_def:{3} subs_def:{4}'.format(
                cl_arn, dl_arn, lv_arn, log_arn, sub_arn)
        )

        # Add all the constituent parts to the Greengrass Group
        group_args = {'GroupId': group_info['Id']}
        if cl_arn:
            group_args['CoreDefinitionVersionArn'] = cl_arn
        if dl_arn:
            group_args['DeviceDefinitionVersionArn'] = dl_arn
        if lv_arn:
            group_args['FunctionDefinitionVersionArn'] = lv_arn
        if log_arn:
            group_args['LoggerDefinitionVersionArn'] = log_arn
        if sub_arn:
            group_args['SubscriptionDefinitionVersionArn'] = sub_arn
        grp = gg_client.create_group_version(
            **group_args
        )

        # store info about the provisioned artifacts into the local config file
        config['group'] = {
            "id": group_info['Id'],
            "version_arn": grp['Arn'],
            "version": grp['Version'],
            "name": group_name
        }
        logging.info(
            "[end] Created Greengrass Group {0}".format(group_info['Id']))
Пример #8
0
    def create(self, group_type, config_file,
               group_name=None, region='us-west-2'):
        """
        Create a Greengrass group in the given region.

        :param group_type: the type of group to create. Must match a `key` in
            the `group_types` dict
        :param config_file: config file of the group to create
        :param group_name: the name of the group. If no name is given, then
            group_type will be used.
        :param region: the region in which to create the new group. [default: us-west-2]
        """
        logging.info("[begin] create command using group_types:{0}".format(
            self.group_types))

        config = GroupConfigFile(config_file=config_file)
        if config.is_fresh() is False:
            raise ValueError(
                "Config file already tracking previously created group"
            )

        if group_type not in self.group_types.keys():
            raise ValueError("Can only create {0} groups.".format(
                self.group_types)
            )

        # create an instance of the requested group type that uses the given
        # config file and region
        gt = self.group_types[group_type](config=config, region=region)

        # Create a Group
        logging.info("[begin] Creating a Greengrass Group")
        if group_name is None:
            group_name = group_type

        gg_client = boto3.client("greengrass", region_name=region)

        group_info = gg_client.create_group(Name="{0}_group".format(group_name))
        config['group'] = {"id": group_info['Id']}

        # setup the policies and roles
        gt.create_and_attach_thing_policy()
        gt.create_and_attach_iam_role()

        cl_arn = self._create_core_definition(
            gg_client=gg_client, group_type=gt,
            config=config, group_name=group_name
        )
        dl_arn = self._create_device_definition(
            gg_client=gg_client, group_type=gt,
            config=config, group_name=group_name
        )
        lv_arn = self._create_function_definition(
            gg_client=gg_client, group_type=gt, config=config
        )
        log_arn = self._create_logger_definition(
            gg_client=gg_client, group_type=gt, config=config
        )
        sub_arn = self._create_subscription_definition(
            gg_client=gg_client, group_type=gt, config=config
        )

        # Add all the constituent parts to the Greengrass Group
        grp = gg_client.create_group_version(
            GroupId=group_info['Id'],
            CoreDefinitionVersionArn=cl_arn,
            DeviceDefinitionVersionArn=dl_arn,
            FunctionDefinitionVersionArn=lv_arn,
            LoggerDefinitionVersionArn=log_arn,
            SubscriptionDefinitionVersionArn=sub_arn
        )
        # store info about the provisioned artifacts into the local config file
        config['group'] = {
            "id": group_info['Id'],
            "version_arn": grp['Arn'],
            "version": grp['Version']
        }
        logging.info(
            "[end] Created Greengrass Group {0}".format(group_info['Id']))