]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_quick_start/SetupAWS.py
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_IoT_Libraries / tools / aws_config_quick_start / SetupAWS.py
diff --git a/FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_quick_start/SetupAWS.py b/FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_quick_start/SetupAWS.py
new file mode 100644 (file)
index 0000000..dade6b7
--- /dev/null
@@ -0,0 +1,235 @@
+#!/usr/bin/env python\r
+\r
+import os\r
+import sys\r
+import json\r
+import pprint\r
+import argparse\r
+import boto3\r
+import misc\r
+import certs\r
+import thing\r
+import policy\r
+\r
+pp = pprint.PrettyPrinter(indent=4)\r
+\r
+\r
+def check_aws_configuration():\r
+    mysession = boto3.session.Session()\r
+    if not mysession._session._config['profiles']:\r
+        print("AWS not configured. Please run `aws configure`.")\r
+        sys.exit(1)\r
+\r
+\r
+def prereq():\r
+    with open('configure.json') as configure_file:\r
+        json_text = json.load(configure_file)\r
+\r
+    # Create a Thing\r
+    thing_name = json_text['thing_name']\r
+    thing_obj = thing.Thing(thing_name)\r
+    if not thing_obj.create():\r
+\r
+        # Create a Certificate\r
+        cert_obj = certs.Certificate()\r
+        result = cert_obj.create()\r
+\r
+        # Store certId\r
+        cert_id = result['certificateId']\r
+        cert_id_filename = thing_name + '_cert_id_file.txt'\r
+        cert_id_file = open(cert_id_filename, 'w')\r
+        cert_id_file.write(cert_id)\r
+        cert_id_file_path = os.path.abspath(cert_id_filename)\r
+        os.chmod(cert_id_file_path, 0o444)\r
+        cert_id_file.close()\r
+\r
+        # Store cert_pem as file\r
+        cert_pem = result['certificatePem']\r
+        cert_pem_filename = thing_name + '_cert_pem_file.pem'\r
+        cert_pem_file = open(cert_pem_filename, 'w')\r
+        cert_pem_file.write(cert_pem)\r
+        cert_pem_file_path = os.path.abspath(cert_pem_filename)\r
+        os.chmod(cert_pem_file_path, 0o444)\r
+        cert_pem_file.close()\r
+\r
+        # Store private key PEM as file\r
+        private_key_pem = result['keyPair']['PrivateKey']\r
+        private_key_pem_filename = thing_name + '_private_key_pem_file.pem'\r
+        private_key_pem_file = open(private_key_pem_filename, 'w')\r
+        private_key_pem_file.write(private_key_pem)\r
+        private_key_pem_file_path = os.path.abspath(private_key_pem_filename)\r
+        os.chmod(private_key_pem_file_path, 0o444)\r
+        private_key_pem_file.close()\r
+\r
+        # Create a Policy\r
+        policy_document = misc.create_policy_document()\r
+        policy_name = thing_name + '_amazon_freertos_policy'\r
+        policy_obj = policy.Policy(policy_name, policy_document)\r
+        policy_obj.create()\r
+\r
+        # Attach certificate to Thing\r
+        cert_obj.attach_thing(thing_name)\r
+\r
+        # Attach policy to certificate\r
+        cert_obj.attach_policy(policy_name)\r
+\r
+\r
+def update_credential_file():\r
+    with open('configure.json') as configure_file:\r
+        json_text = json.load(configure_file)\r
+\r
+    source_dir = os.path.expanduser(json_text['FreeRTOS_source_dir'])\r
+    thing_name = json_text['thing_name']\r
+\r
+    # Read cert_pem from file\r
+    cert_pem_filename = thing_name + '_cert_pem_file.pem'\r
+    try:\r
+        cert_pem_file = open(cert_pem_filename, 'r')\r
+    except IOError:\r
+        print("{} file not found. Run prerequisite step"\r
+              .format(cert_pem_filename))\r
+        sys.exit(1)\r
+    else:\r
+        cert_pem = cert_pem_file.read()\r
+\r
+    # Read private_key_pem from file\r
+    private_key_pem_filename = thing_name + '_private_key_pem_file.pem'\r
+    try:\r
+        private_key_pem_file = open(private_key_pem_filename, 'r')\r
+    except IOError:\r
+        print("{} file not found. Run prerequisite step"\r
+              .format(private_key_pem_filename))\r
+        sys.exit(1)\r
+    else:\r
+        private_key_pem = private_key_pem_file.read()\r
+\r
+    # Modify 'iot_clientcredential.h' file\r
+    misc.write_client_credentials(\r
+        source_dir,\r
+        thing_name=thing_name,\r
+        client_certificate_pem=cert_pem,\r
+        client_private_key_pem=private_key_pem,\r
+        cleanup=False)\r
+\r
+\r
+def delete_prereq():\r
+    with open('configure.json') as configure_file:\r
+        json_text = json.load(configure_file)\r
+\r
+    # Delete Thing\r
+    thing_name = json_text['thing_name']\r
+    thing_obj = thing.Thing(thing_name)\r
+    if thing_obj.exists():\r
+        thing_obj.delete()\r
+\r
+    # Delete certificate\r
+    cert_id_filename = thing_name + '_cert_id_file.txt'\r
+    if os.path.exists(cert_id_filename):\r
+        cert_id_file = open(cert_id_filename, 'r')\r
+        cert_id = cert_id_file.read()\r
+        cert_obj = certs.Certificate(cert_id)\r
+        cert_obj.delete()\r
+        cert_id_file.close()\r
+        cert_id_file_path = os.path.abspath(cert_id_filename)\r
+        os.chmod(cert_id_file_path, 0o666)\r
+        os.remove(cert_id_filename)\r
+\r
+    # Delete cert_pem file and private_key_pem file\r
+    cert_pem_filename = thing_name + '_cert_pem_file.pem'\r
+    if os.path.exists(cert_pem_filename):\r
+        cert_pem_file_path = os.path.abspath(cert_pem_filename)\r
+        os.chmod(cert_pem_file_path, 0o666)\r
+        os.remove(cert_pem_filename)\r
+\r
+    private_key_pem_filename = thing_name + '_private_key_pem_file.pem'\r
+    if os.path.exists(private_key_pem_filename):\r
+        private_key_pem_file_path = os.path.abspath(private_key_pem_filename)\r
+        os.chmod(private_key_pem_file_path, 0o666)\r
+        os.remove(private_key_pem_filename)\r
+\r
+    # Delete policy\r
+    policy_name = thing_name + '_amazon_freertos_policy'\r
+    policy_obj = policy.Policy(policy_name)\r
+    if policy_obj.exists():\r
+        policy_obj.delete()\r
+\r
+\r
+def cleanup_creds():\r
+    with open('configure.json') as file:\r
+        json_text = json.load(file)\r
+\r
+    source_dir = os.path.expanduser(json_text['FreeRTOS_source_dir'])\r
+\r
+    # Cleanup 'iot_clientcredential.h' file\r
+    misc.write_client_credentials(source_dir, cleanup=True)\r
+\r
+\r
+def setup():\r
+    prereq()\r
+    update_credential_file()\r
+    print("Setup Completed")\r
+\r
+\r
+def cleanup():\r
+    delete_prereq()\r
+    cleanup_creds()\r
+    print("Cleanup Completed")\r
+\r
+\r
+def list_certificates():\r
+    client = boto3.client('iot')\r
+    certs = client.list_certificates()['certificates']\r
+    pp.pprint(certs)\r
+\r
+\r
+def list_things():\r
+    client = boto3.client('iot')\r
+    things = client.list_things()['things']\r
+    pp.pprint(things)\r
+\r
+\r
+def list_policies():\r
+    client = boto3.client('iot')\r
+    policies = client.list_policies()['policies']\r
+    pp.pprint(policies)\r
+\r
+\r
+if __name__ == "__main__":\r
+\r
+    arg_parser = argparse.ArgumentParser()\r
+    subparsers = arg_parser.add_subparsers(help='Available commands',\r
+                                           dest='command')\r
+    subparsers.add_parser('setup', help='Setup AWS IoT')\r
+    subparsers.add_parser('cleanup', help='Cleanup AWS IoT')\r
+    subparsers.add_parser('list_certificates', help='List certificates')\r
+    subparsers.add_parser('list_things', help='List things')\r
+    subparsers.add_parser('list_policies', help='List policies')\r
+    subparsers.add_parser('prereq', help='Setup prerequisites for AWS IoT')\r
+    subparsers.add_parser('update_creds', help='Update credential files')\r
+    subparsers.add_parser('delete_prereq', help='Delete prerequisites created')\r
+    subparsers.add_parser('cleanup_creds', help='Cleanup credential files')\r
+    args = arg_parser.parse_args()\r
+    check_aws_configuration()\r
+\r
+    if args.command == 'setup':\r
+        setup()\r
+    elif args.command == 'cleanup':\r
+        cleanup()\r
+    elif args.command == 'list_certificates':\r
+        list_certificates()\r
+    elif args.command == 'list_things':\r
+        list_things()\r
+    elif args.command == 'list_policies':\r
+        list_policies()\r
+    elif args.command == 'prereq':\r
+        prereq()\r
+    elif args.command == 'update_creds':\r
+        update_credential_file()\r
+    elif args.command == 'delete_prereq':\r
+        delete_prereq()\r
+    elif args.command == 'cleanup_creds':\r
+        cleanup_creds()\r
+    else:\r
+        print("Command does not exist")\r
+\r
+    sys.exit(1)
\ No newline at end of file