Clone of official tools
device_management.py@43:2a7da56ebd24, 2018-09-25 (annotated)
- Committer:
- theotherjimmy
- Date:
- Tue Sep 25 13:43:09 2018 -0500
- Revision:
- 43:2a7da56ebd24
Release 5.10.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
theotherjimmy |
43:2a7da56ebd24 | 1 | #! /usr/bin/env python2 |
theotherjimmy |
43:2a7da56ebd24 | 2 | """ |
theotherjimmy |
43:2a7da56ebd24 | 3 | mbed SDK |
theotherjimmy |
43:2a7da56ebd24 | 4 | Copyright (c) 2011-2013 ARM Limited |
theotherjimmy |
43:2a7da56ebd24 | 5 | |
theotherjimmy |
43:2a7da56ebd24 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
theotherjimmy |
43:2a7da56ebd24 | 7 | you may not use this file except in compliance with the License. |
theotherjimmy |
43:2a7da56ebd24 | 8 | You may obtain a copy of the License at |
theotherjimmy |
43:2a7da56ebd24 | 9 | |
theotherjimmy |
43:2a7da56ebd24 | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
theotherjimmy |
43:2a7da56ebd24 | 11 | |
theotherjimmy |
43:2a7da56ebd24 | 12 | Unless required by applicable law or agreed to in writing, software |
theotherjimmy |
43:2a7da56ebd24 | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
theotherjimmy |
43:2a7da56ebd24 | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
theotherjimmy |
43:2a7da56ebd24 | 15 | See the License for the specific language governing permissions and |
theotherjimmy |
43:2a7da56ebd24 | 16 | limitations under the License. |
theotherjimmy |
43:2a7da56ebd24 | 17 | |
theotherjimmy |
43:2a7da56ebd24 | 18 | |
theotherjimmy |
43:2a7da56ebd24 | 19 | device-management, dev-mgmt, and dm sub command |
theotherjimmy |
43:2a7da56ebd24 | 20 | """ |
theotherjimmy |
43:2a7da56ebd24 | 21 | from __future__ import print_function, absolute_import |
theotherjimmy |
43:2a7da56ebd24 | 22 | import logging |
theotherjimmy |
43:2a7da56ebd24 | 23 | import sys |
theotherjimmy |
43:2a7da56ebd24 | 24 | import argparse |
theotherjimmy |
43:2a7da56ebd24 | 25 | from os.path import join, abspath, dirname, basename |
theotherjimmy |
43:2a7da56ebd24 | 26 | from os import getenv |
theotherjimmy |
43:2a7da56ebd24 | 27 | |
theotherjimmy |
43:2a7da56ebd24 | 28 | from manifesttool import create, parse, verify, cert, init, update |
theotherjimmy |
43:2a7da56ebd24 | 29 | from manifesttool.argparser import MainArgumentParser |
theotherjimmy |
43:2a7da56ebd24 | 30 | from mbed_cloud import AccountManagementAPI, CertificatesAPI |
theotherjimmy |
43:2a7da56ebd24 | 31 | import colorama |
theotherjimmy |
43:2a7da56ebd24 | 32 | colorama.init() |
theotherjimmy |
43:2a7da56ebd24 | 33 | |
theotherjimmy |
43:2a7da56ebd24 | 34 | |
theotherjimmy |
43:2a7da56ebd24 | 35 | LOG = logging.getLogger(__name__) |
theotherjimmy |
43:2a7da56ebd24 | 36 | LOG_FORMAT = '[%(levelname)s] %(asctime)s - %(name)s - %(message)s' |
theotherjimmy |
43:2a7da56ebd24 | 37 | |
theotherjimmy |
43:2a7da56ebd24 | 38 | # Be sure that the tools directory is in the search path |
theotherjimmy |
43:2a7da56ebd24 | 39 | ROOT = abspath(join(dirname(__file__), "..")) |
theotherjimmy |
43:2a7da56ebd24 | 40 | sys.path.insert(0, ROOT) |
theotherjimmy |
43:2a7da56ebd24 | 41 | |
theotherjimmy |
43:2a7da56ebd24 | 42 | from tools.config import Config |
theotherjimmy |
43:2a7da56ebd24 | 43 | from tools.options import extract_mcus |
theotherjimmy |
43:2a7da56ebd24 | 44 | |
theotherjimmy |
43:2a7da56ebd24 | 45 | |
theotherjimmy |
43:2a7da56ebd24 | 46 | class MbedExtendedArgs(MainArgumentParser): |
theotherjimmy |
43:2a7da56ebd24 | 47 | def _addCreateArgs(self, parser, exclusions=[]): |
theotherjimmy |
43:2a7da56ebd24 | 48 | if 'payload' not in exclusions: |
theotherjimmy |
43:2a7da56ebd24 | 49 | parser.add_argument( |
theotherjimmy |
43:2a7da56ebd24 | 50 | '-p', '--payload', |
theotherjimmy |
43:2a7da56ebd24 | 51 | help='Supply a local copy of the payload file.' |
theotherjimmy |
43:2a7da56ebd24 | 52 | 'This option overrides any payload file supplied in a ' |
theotherjimmy |
43:2a7da56ebd24 | 53 | '`-i` argument.', |
theotherjimmy |
43:2a7da56ebd24 | 54 | metavar='FILE', |
theotherjimmy |
43:2a7da56ebd24 | 55 | type=argparse.FileType('rb') |
theotherjimmy |
43:2a7da56ebd24 | 56 | ) |
theotherjimmy |
43:2a7da56ebd24 | 57 | parser.add_argument('-m', '--mcu') |
theotherjimmy |
43:2a7da56ebd24 | 58 | parser.add_argument('-t', '--toolchain') |
theotherjimmy |
43:2a7da56ebd24 | 59 | parser.add_argument('--source', nargs='+', dest='source_dir') |
theotherjimmy |
43:2a7da56ebd24 | 60 | parser.add_argument('--build') |
theotherjimmy |
43:2a7da56ebd24 | 61 | exclusions.append('payload') |
theotherjimmy |
43:2a7da56ebd24 | 62 | super(MbedExtendedArgs, self)._addCreateArgs(parser, exclusions) |
theotherjimmy |
43:2a7da56ebd24 | 63 | |
theotherjimmy |
43:2a7da56ebd24 | 64 | |
theotherjimmy |
43:2a7da56ebd24 | 65 | def wrap_payload(func): |
theotherjimmy |
43:2a7da56ebd24 | 66 | def inner(options): |
theotherjimmy |
43:2a7da56ebd24 | 67 | if not options.payload and options.mcu and options.build: |
theotherjimmy |
43:2a7da56ebd24 | 68 | mcus = extract_mcus(MbedExtendedArgs(), options) |
theotherjimmy |
43:2a7da56ebd24 | 69 | sources = options.source_dir or ['.'] |
theotherjimmy |
43:2a7da56ebd24 | 70 | config = Config(mcus[0], sources) |
theotherjimmy |
43:2a7da56ebd24 | 71 | app_name = config.name or basename(abspath(sources[0])) |
theotherjimmy |
43:2a7da56ebd24 | 72 | output_ext = getattr(config.target, "OUTPUT_EXT", "bin") |
theotherjimmy |
43:2a7da56ebd24 | 73 | payload_name = join(options.build, "{}_application.{}".format( |
theotherjimmy |
43:2a7da56ebd24 | 74 | app_name, output_ext |
theotherjimmy |
43:2a7da56ebd24 | 75 | )) |
theotherjimmy |
43:2a7da56ebd24 | 76 | options.payload = open(payload_name, "rb") |
theotherjimmy |
43:2a7da56ebd24 | 77 | return func(options) |
theotherjimmy |
43:2a7da56ebd24 | 78 | return inner |
theotherjimmy |
43:2a7da56ebd24 | 79 | |
theotherjimmy |
43:2a7da56ebd24 | 80 | |
theotherjimmy |
43:2a7da56ebd24 | 81 | def wrap_init(func): |
theotherjimmy |
43:2a7da56ebd24 | 82 | def inner(options): |
theotherjimmy |
43:2a7da56ebd24 | 83 | if getattr(options, 'api_key', None): |
theotherjimmy |
43:2a7da56ebd24 | 84 | api_key = options.api_key |
theotherjimmy |
43:2a7da56ebd24 | 85 | else: |
theotherjimmy |
43:2a7da56ebd24 | 86 | api_key = getenv("MBED_CLOUD_SDK_API_KEY") |
theotherjimmy |
43:2a7da56ebd24 | 87 | if getattr(options, 'server_address', None): |
theotherjimmy |
43:2a7da56ebd24 | 88 | host_addr = options.server_address |
theotherjimmy |
43:2a7da56ebd24 | 89 | else: |
theotherjimmy |
43:2a7da56ebd24 | 90 | host_addr = getenv("MBED_CLOUD_SDK_HOST", |
theotherjimmy |
43:2a7da56ebd24 | 91 | "https://api.us-east-1.mbedcloud.com/") |
theotherjimmy |
43:2a7da56ebd24 | 92 | config = { |
theotherjimmy |
43:2a7da56ebd24 | 93 | "api_key": api_key, |
theotherjimmy |
43:2a7da56ebd24 | 94 | "host": host_addr, |
theotherjimmy |
43:2a7da56ebd24 | 95 | } |
theotherjimmy |
43:2a7da56ebd24 | 96 | accounts = AccountManagementAPI(config) |
theotherjimmy |
43:2a7da56ebd24 | 97 | certs = CertificatesAPI(config) |
theotherjimmy |
43:2a7da56ebd24 | 98 | api_key = accounts.list_api_keys(filter={ |
theotherjimmy |
43:2a7da56ebd24 | 99 | 'key': api_key |
theotherjimmy |
43:2a7da56ebd24 | 100 | }).next() |
theotherjimmy |
43:2a7da56ebd24 | 101 | certificates_owned = list(certs.list_certificates()) |
theotherjimmy |
43:2a7da56ebd24 | 102 | dev_cert_info = None |
theotherjimmy |
43:2a7da56ebd24 | 103 | for certif in certificates_owned: |
theotherjimmy |
43:2a7da56ebd24 | 104 | if certif.type == "developer" and (certif.owner_id == api_key.owner_id or |
theotherjimmy |
43:2a7da56ebd24 | 105 | certif.owner_id == api_key.id): |
theotherjimmy |
43:2a7da56ebd24 | 106 | dev_cert_info = certs.get_certificate(certif.id) |
theotherjimmy |
43:2a7da56ebd24 | 107 | LOG.info("Found developer certificate named %s", |
theotherjimmy |
43:2a7da56ebd24 | 108 | dev_cert_info.name) |
theotherjimmy |
43:2a7da56ebd24 | 109 | break |
theotherjimmy |
43:2a7da56ebd24 | 110 | else: |
theotherjimmy |
43:2a7da56ebd24 | 111 | LOG.warning( |
theotherjimmy |
43:2a7da56ebd24 | 112 | "Could not find developer certificate for this account." |
theotherjimmy |
43:2a7da56ebd24 | 113 | " Generting a new developer certificate." |
theotherjimmy |
43:2a7da56ebd24 | 114 | ) |
theotherjimmy |
43:2a7da56ebd24 | 115 | dev_cert_info = CertificatesAPI().add_developer_certificate( |
theotherjimmy |
43:2a7da56ebd24 | 116 | "mbed-cli-auto {}".format(api_key.name), |
theotherjimmy |
43:2a7da56ebd24 | 117 | description="cetificate auto-generated by Mbed CLI" |
theotherjimmy |
43:2a7da56ebd24 | 118 | ) |
theotherjimmy |
43:2a7da56ebd24 | 119 | LOG.info("Writing developer certificate %s into c file " |
theotherjimmy |
43:2a7da56ebd24 | 120 | "mbed_cloud_dev_credentials.c", dev_cert_info.name) |
theotherjimmy |
43:2a7da56ebd24 | 121 | with open("mbed_cloud_dev_credentials.c", "w") as fout: |
theotherjimmy |
43:2a7da56ebd24 | 122 | fout.write(dev_cert_info.header_file) |
theotherjimmy |
43:2a7da56ebd24 | 123 | return func(options) |
theotherjimmy |
43:2a7da56ebd24 | 124 | return inner |
theotherjimmy |
43:2a7da56ebd24 | 125 | |
theotherjimmy |
43:2a7da56ebd24 | 126 | |
theotherjimmy |
43:2a7da56ebd24 | 127 | def main(): |
theotherjimmy |
43:2a7da56ebd24 | 128 | options = MbedExtendedArgs().parse_args().options |
theotherjimmy |
43:2a7da56ebd24 | 129 | |
theotherjimmy |
43:2a7da56ebd24 | 130 | log_level = { |
theotherjimmy |
43:2a7da56ebd24 | 131 | 'debug': logging.DEBUG, |
theotherjimmy |
43:2a7da56ebd24 | 132 | 'info': logging.INFO, |
theotherjimmy |
43:2a7da56ebd24 | 133 | 'warning': logging.WARNING, |
theotherjimmy |
43:2a7da56ebd24 | 134 | 'exception': logging.CRITICAL, |
theotherjimmy |
43:2a7da56ebd24 | 135 | }[options.log_level] |
theotherjimmy |
43:2a7da56ebd24 | 136 | logging.basicConfig( |
theotherjimmy |
43:2a7da56ebd24 | 137 | level=log_level, |
theotherjimmy |
43:2a7da56ebd24 | 138 | format=LOG_FORMAT, |
theotherjimmy |
43:2a7da56ebd24 | 139 | datefmt='%Y-%m-%d %H:%M:%S', |
theotherjimmy |
43:2a7da56ebd24 | 140 | ) |
theotherjimmy |
43:2a7da56ebd24 | 141 | logging.addLevelName( |
theotherjimmy |
43:2a7da56ebd24 | 142 | logging.INFO, |
theotherjimmy |
43:2a7da56ebd24 | 143 | "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO) |
theotherjimmy |
43:2a7da56ebd24 | 144 | ) |
theotherjimmy |
43:2a7da56ebd24 | 145 | logging.addLevelName( |
theotherjimmy |
43:2a7da56ebd24 | 146 | logging.WARNING, |
theotherjimmy |
43:2a7da56ebd24 | 147 | "\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING) |
theotherjimmy |
43:2a7da56ebd24 | 148 | ) |
theotherjimmy |
43:2a7da56ebd24 | 149 | logging.addLevelName( |
theotherjimmy |
43:2a7da56ebd24 | 150 | logging.CRITICAL, |
theotherjimmy |
43:2a7da56ebd24 | 151 | "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL) |
theotherjimmy |
43:2a7da56ebd24 | 152 | ) |
theotherjimmy |
43:2a7da56ebd24 | 153 | LOG.debug('CLIDriver created. Arguments parsed and logging setup.') |
theotherjimmy |
43:2a7da56ebd24 | 154 | |
theotherjimmy |
43:2a7da56ebd24 | 155 | rc = { |
theotherjimmy |
43:2a7da56ebd24 | 156 | "create": wrap_payload(create.main), |
theotherjimmy |
43:2a7da56ebd24 | 157 | "parse": parse.main, |
theotherjimmy |
43:2a7da56ebd24 | 158 | "verify": verify.main, |
theotherjimmy |
43:2a7da56ebd24 | 159 | "cert": cert.main, |
theotherjimmy |
43:2a7da56ebd24 | 160 | "init": wrap_init(init.main), |
theotherjimmy |
43:2a7da56ebd24 | 161 | "update": wrap_payload(update.main), |
theotherjimmy |
43:2a7da56ebd24 | 162 | }[options.action](options) or 0 |
theotherjimmy |
43:2a7da56ebd24 | 163 | |
theotherjimmy |
43:2a7da56ebd24 | 164 | sys.exit(rc) |
theotherjimmy |
43:2a7da56ebd24 | 165 | |
theotherjimmy |
43:2a7da56ebd24 | 166 | if __name__ == "__main__": |
theotherjimmy |
43:2a7da56ebd24 | 167 | main() |