From 03fa626edab0ce835416a076ee67b202a298f7b6 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 13 Aug 2018 17:55:42 +0100 Subject: [PATCH 1/8] Import manifest-tool --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 93caa818de..30f407b581 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ pyelftools>=0.24 jsonschema>=2.6 future>=0.16.0 six>=1.11.0 +git+https://github.com/armmbed/manifest-tool.git@v1.4.5 From ebb016e2708bd1be9381b5c1a8a854e2924e40bd Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Mon, 13 Aug 2018 17:56:04 +0100 Subject: [PATCH 2/8] Create a "device-management" subcommand --- tools/device_management.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tools/device_management.py diff --git a/tools/device_management.py b/tools/device_management.py new file mode 100644 index 0000000000..1567ae078e --- /dev/null +++ b/tools/device_management.py @@ -0,0 +1,38 @@ +#! /usr/bin/env python2 +""" +mbed SDK +Copyright (c) 2011-2013 ARM Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +device-management, dev-mgmt, and dm sub command +""" +from __future__ import print_function +from builtins import str +import sys +import json +from time import sleep +from shutil import copy +from os.path import join, abspath, dirname +from json import load, dump + +# Be sure that the tools directory is in the search path +ROOT = abspath(join(dirname(__file__), "..")) +sys.path.insert(0, ROOT) + +def main(): + print("device management!") + +if __name__ == "__main__": + main() From 563ee0d871741f473ffca2bccef7bfdf87ff781d Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Tue, 14 Aug 2018 16:17:08 +0100 Subject: [PATCH 3/8] Implement device management subcommand --- requirements.txt | 1 + tools/device_management.py | 94 +++++++++++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 30f407b581..48ee33d6ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ jsonschema>=2.6 future>=0.16.0 six>=1.11.0 git+https://github.com/armmbed/manifest-tool.git@v1.4.5 +mbed-cloud-sdk==2.0.0 diff --git a/tools/device_management.py b/tools/device_management.py index 1567ae078e..173774312a 100644 --- a/tools/device_management.py +++ b/tools/device_management.py @@ -20,19 +20,101 @@ device-management, dev-mgmt, and dm sub command """ from __future__ import print_function from builtins import str +import logging import sys -import json -from time import sleep -from shutil import copy -from os.path import join, abspath, dirname -from json import load, dump +import argparse +from os.path import join, abspath, dirname, basename + +from manifesttool import create, parse, verify, cert, init, update +from manifesttool.argparser import MainArgumentParser +import colorama +colorama.init() + + +LOG = logging.getLogger(__name__) +LOG_FORMAT='[%(levelname)s] %(asctime)s - %(name)s - %(message)s' # Be sure that the tools directory is in the search path ROOT = abspath(join(dirname(__file__), "..")) sys.path.insert(0, ROOT) +from tools.config import Config +from tools.options import extract_mcus + +class MbedExtendedArgs(MainArgumentParser): + def _addCreateArgs(self, parser, exclusions=[]): + if 'payload' not in exclusions: + parser.add_argument('-p', '--payload', + help='Supply a local copy of the payload file.' + 'This option overrides any payload file supplied in a `-i` argument.', + metavar='FILE', + type=argparse.FileType('rb') + ) + parser.add_argument('-m', '--mcu') + parser.add_argument('-t', '--toolchain') + parser.add_argument('--source', nargs='+', dest='source_dir') + parser.add_argument('--build') + exclusions.append('payload') + super(MbedExtendedArgs, self)._addCreateArgs(parser, exclusions) + + +def wrap_payload(func): + def inner(options): + if (not options.payload and + options.mcu and + options.build + ): + mcus = extract_mcus(MbedExtendedArgs(), options) + sources = options.source_dir or ['.'] + config = Config(mcus[0], sources) + app_name = config.name or basename(abspath(sources[0])) + output_ext = getattr(config.target, "OUTPUT_EXT", "bin") + payload_name = join(options.build, "{}_application.{}".format( + app_name, output_ext + )) + options.payload = open(payload_name, "rb") + return func(options) + return inner + + def main(): - print("device management!") + options = MbedExtendedArgs().parse_args().options + + log_level = { + 'debug': logging.DEBUG, + 'info': logging.INFO, + 'warning': logging.WARNING, + 'exception': logging.CRITICAL, + }[options.log_level] + logging.basicConfig( + level=log_level, + format=LOG_FORMAT, + datefmt='%Y-%m-%d %H:%M:%S', + ) + logging.addLevelName( + logging.INFO, + "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO) + ) + logging.addLevelName( + logging.WARNING, + "\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING) + ) + logging.addLevelName( + logging.CRITICAL, + "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL) + ) + LOG.debug('CLIDriver created. Arguments parsed and logging setup.') + + rc = { + "create": wrap_payload(create.main), + "parse": parse.main, + "verify": verify.main, + "cert": cert.main, + "init": init.main, + "update" : wrap_payload(update.main), + }[options.action](options) or 0 + + sys.exit(rc) if __name__ == "__main__": main() From 93309cd134ef07deb0a35e88129c73919c334ba5 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Thu, 16 Aug 2018 16:05:21 +0100 Subject: [PATCH 4/8] Extend init to maybe create and always DL dev cert --- tools/device_management.py | 41 +++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/tools/device_management.py b/tools/device_management.py index 173774312a..8091d64456 100644 --- a/tools/device_management.py +++ b/tools/device_management.py @@ -18,15 +18,16 @@ limitations under the License. device-management, dev-mgmt, and dm sub command """ -from __future__ import print_function -from builtins import str +from __future__ import print_function, absolute_import import logging import sys import argparse from os.path import join, abspath, dirname, basename +from os import getenv from manifesttool import create, parse, verify, cert, init, update from manifesttool.argparser import MainArgumentParser +from mbed_cloud import AccountManagementAPI, CertificatesAPI import colorama colorama.init() @@ -77,6 +78,40 @@ def wrap_payload(func): return inner +def wrap_init(func): + def inner(options): + accounts = AccountManagementAPI() + certs = CertificatesAPI() + api_key = accounts.list_api_keys(filter={ + 'key': getenv("MBED_CLOUD_SDK_API_KEY") + }).next() + user = accounts.get_user(api_key.owner_id) + certificates_owned = list(certs.list_certificates()) + dev_cert_info = None + for certif in certificates_owned: + if certif.type == "developer" and (certif.owner_id == user.id or + certif.owner_id == api_key.id): + dev_cert_info = certs.get_certificate(certif.id) + LOG.info("Found developer certificate onwed by %s named %s", + user.full_name, dev_cert_info.name) + break + else: + LOG.warning( + "Could not find developer certificate for this account." + " Generting a new developer certificate." + ) + dev_cert_info = CertificatesAPI().add_developer_certificate( + "mbed-cli-auto {}".format(user.full_name), + description="cetificate auto-generated by Mbed CLI" + ) + LOG.info("Writing developer certificate %s into c file " + "mbed_cloud_dev_credentials.c", dev_cert_info.name) + with open("mbed_cloud_dev_credentials.c", "w") as fout: + fout.write(dev_cert_info.header_file) + return func(options) + return inner + + def main(): options = MbedExtendedArgs().parse_args().options @@ -110,7 +145,7 @@ def main(): "parse": parse.main, "verify": verify.main, "cert": cert.main, - "init": init.main, + "init": wrap_init(init.main), "update" : wrap_payload(update.main), }[options.action](options) or 0 From a6163cb14fc323f3396a258d067893c094a8ef38 Mon Sep 17 00:00:00 2001 From: Jimmy Brisson Date: Tue, 21 Aug 2018 10:35:27 -0500 Subject: [PATCH 5/8] Clean up formatting of device_management.py --- tools/device_management.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/device_management.py b/tools/device_management.py index 8091d64456..14733f2c02 100644 --- a/tools/device_management.py +++ b/tools/device_management.py @@ -33,7 +33,7 @@ colorama.init() LOG = logging.getLogger(__name__) -LOG_FORMAT='[%(levelname)s] %(asctime)s - %(name)s - %(message)s' +LOG_FORMAT = '[%(levelname)s] %(asctime)s - %(name)s - %(message)s' # Be sure that the tools directory is in the search path ROOT = abspath(join(dirname(__file__), "..")) @@ -42,12 +42,15 @@ sys.path.insert(0, ROOT) from tools.config import Config from tools.options import extract_mcus + class MbedExtendedArgs(MainArgumentParser): def _addCreateArgs(self, parser, exclusions=[]): if 'payload' not in exclusions: - parser.add_argument('-p', '--payload', + parser.add_argument( + '-p', '--payload', help='Supply a local copy of the payload file.' - 'This option overrides any payload file supplied in a `-i` argument.', + 'This option overrides any payload file supplied in a ' + '`-i` argument.', metavar='FILE', type=argparse.FileType('rb') ) @@ -61,10 +64,7 @@ class MbedExtendedArgs(MainArgumentParser): def wrap_payload(func): def inner(options): - if (not options.payload and - options.mcu and - options.build - ): + if not options.payload and options.mcu and options.build: mcus = extract_mcus(MbedExtendedArgs(), options) sources = options.source_dir or ['.'] config = Config(mcus[0], sources) @@ -146,7 +146,7 @@ def main(): "verify": verify.main, "cert": cert.main, "init": wrap_init(init.main), - "update" : wrap_payload(update.main), + "update": wrap_payload(update.main), }[options.action](options) or 0 sys.exit(rc) From c89147ea300164feb0f175cc1a4e62d936d8837d Mon Sep 17 00:00:00 2001 From: Cruz Monrreal Date: Wed, 29 Aug 2018 16:21:15 -0500 Subject: [PATCH 6/8] Added secure urllib3 python requirement This pulls in additional packages requiured for SSL verification for Py2 Ref: https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl-py2 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 48ee33d6ae..96e681fedb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,5 @@ jsonschema>=2.6 future>=0.16.0 six>=1.11.0 git+https://github.com/armmbed/manifest-tool.git@v1.4.5 +urllib3[secure] mbed-cloud-sdk==2.0.0 From f46bcf1abcd66b702cec8cee416f143f88b0a9d8 Mon Sep 17 00:00:00 2001 From: Cruz Monrreal Date: Wed, 29 Aug 2018 16:24:47 -0500 Subject: [PATCH 7/8] Replace urllib3[secure] with discrete packages --- requirements.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 96e681fedb..16044153a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,5 +16,8 @@ jsonschema>=2.6 future>=0.16.0 six>=1.11.0 git+https://github.com/armmbed/manifest-tool.git@v1.4.5 -urllib3[secure] +pyOpenSSL +cryptography +idna +certifi mbed-cloud-sdk==2.0.0 From ebdad75518bcd68f88c1f28aa32c0233383f0bfb Mon Sep 17 00:00:00 2001 From: Cruz Monrreal Date: Wed, 29 Aug 2018 17:57:28 -0500 Subject: [PATCH 8/8] Remove extra package used for CI debugging --- requirements.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 16044153a0..48ee33d6ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,8 +16,4 @@ jsonschema>=2.6 future>=0.16.0 six>=1.11.0 git+https://github.com/armmbed/manifest-tool.git@v1.4.5 -pyOpenSSL -cryptography -idna -certifi mbed-cloud-sdk==2.0.0