def main(): """Authenticate, and download file from MAAS metadata API.""" import argparse parser = argparse.ArgumentParser( description="GET file from MAAS metadata API.") parser.add_argument( "--config", metavar="file", help="Config file containing MAAS API credentials", default=None) parser.add_argument("--apiver", metavar="version", help="Use given API version", default=MD_VERSION) parser.add_argument('path') args = parser.parse_args() creds = { 'consumer_key': None, 'token_key': None, 'token_secret': None, 'consumer_secret': '', 'metadata_url': None, } read_config(args.config, creds) url = "%s/%s/%s" % ( creds['metadata_url'], args.apiver, args.path, ) sys.stdout.write(geturl(url, creds))
def main(): """Authenticate, and download file from MAAS metadata API.""" import argparse parser = argparse.ArgumentParser( description="GET file from MAAS metadata API.") parser.add_argument("--config", metavar="file", help="Config file containing MAAS API credentials", default=None) parser.add_argument("--apiver", metavar="version", help="Use given API version", default=MD_VERSION) parser.add_argument('path') args = parser.parse_args() creds = { 'consumer_key': None, 'token_key': None, 'token_secret': None, 'consumer_secret': '', 'metadata_url': None, } read_config(args.config, creds) url = "%s/%s/%s" % ( creds['metadata_url'], args.apiver, args.path, ) sys.stdout.write(geturl(url, creds))
def download_and_extract_tar(url, creds, scripts_dir): """Download and extract a tar from the given URL. The URL may contain a compressed or uncompressed tar. """ binary = BytesIO(geturl(url, creds)) with tarfile.open(mode='r|*', fileobj=binary) as tar: tar.extractall(scripts_dir)
def download_and_extract_tar(url, creds, scripts_dir): """Download and extract a tar from the given URL. The URL may contain a compressed or uncompressed tar. Returns false when there is no content. """ sys.stdout.write("Downloading and extracting %s to %s\n" % (url, scripts_dir)) sys.stdout.flush() ret = geturl(url, creds) if ret.status == int(http.client.NO_CONTENT): return False binary = BytesIO(ret.read()) with tarfile.open(mode="r|*", fileobj=binary) as tar: tar.extractall(scripts_dir) return True
def main(): """ Call with single argument of directory or http or https url. If url is given additional arguments are allowed, which will be interpreted as consumer_key, token_key, token_secret, consumer_secret. """ import argparse parser = argparse.ArgumentParser( description='Send signal operation and optionally post files to MAAS') parser.add_argument( "--config", metavar="file", help="Specify config file", default=None) parser.add_argument( "--ckey", metavar="key", help="The consumer key to auth with", default=None) parser.add_argument( "--tkey", metavar="key", help="The token key to auth with", default=None) parser.add_argument( "--csec", metavar="secret", help="The consumer secret (likely '')", default="") parser.add_argument( "--tsec", metavar="secret", help="The token secret to auth with", default=None) parser.add_argument( "--apiver", metavar="version", help="The apiver to use (\"\" can be used)", default=MD_VERSION) parser.add_argument( "--url", metavar="url", help="The data source to query", default=None) parser.add_argument( "--file", dest='files', help="File to post", action='append', default=[]) parser.add_argument( "--post", dest='posts', help="name=value pairs to post", action='append', default=[]) parser.add_argument( "--power-type", dest='power_type', help="Power type.", choices=POWER_TYPES, default=None) parser.add_argument( "--power-parameters", dest='power_parms', help="Power parameters.", default=None) parser.add_argument( "--script-result", metavar="retval", type=int, dest='script_result', help="Return code of a commissioning script.") parser.add_argument( "status", help="Status", choices=VALID_STATUS, action='store') parser.add_argument( "message", help="Optional message", default="", nargs='?') args = parser.parse_args() creds = { 'consumer_key': args.ckey, 'token_key': args.tkey, 'token_secret': args.tsec, 'consumer_secret': args.csec, 'metadata_url': args.url, } if args.config: read_config(args.config, creds) url = creds.get('metadata_url', None) if not url: fail("URL must be provided either in --url or in config\n") url = "%s/%s/" % (url, args.apiver) params = { "op": "signal", "status": args.status, "error": args.message, } if args.script_result is not None: params['script_result'] = args.script_result for ent in args.posts: try: (key, val) = ent.split("=", 2) except ValueError: sys.stderr.write("'%s' had no '='" % ent) sys.exit(1) params[key] = val if args.power_parms is not None: params["power_type"] = args.power_type if params["power_type"] == "moonshot": user, passwd, address, hwaddress = args.power_parms.split(",") power_parms = dict( power_user=user, power_pass=passwd, power_address=address, power_hwaddress=hwaddress ) else: user, passwd, address, driver = args.power_parms.split(",") power_parms = dict( power_user=user, power_pass=passwd, power_address=address, power_driver=driver ) params["power_parameters"] = json.dumps(power_parms) files = {} for fpath in args.files: files[os.path.basename(fpath)] = open(fpath, "r") data, headers = encode_multipart_data(params, files) exc = None msg = "" try: payload = geturl(url, creds=creds, headers=headers, data=data) if payload != "OK": raise TypeError("Unexpected result from call: %s" % payload) else: msg = "Success" except urllib2.HTTPError as exc: msg = "http error [%s]" % exc.code except urllib2.URLError as exc: msg = "url error [%s]" % exc.reason except socket.timeout as exc: msg = "socket timeout [%s]" % exc except TypeError as exc: msg = exc.message except Exception as exc: msg = "unexpected error [%s]" % exc sys.stderr.write("%s\n" % msg) sys.exit((exc is None))
def main(): """ Call with single argument of directory or http or https url. If url is given additional arguments are allowed, which will be interpreted as consumer_key, token_key, token_secret, consumer_secret. """ import argparse parser = argparse.ArgumentParser( description='Send signal operation and optionally post files to MAAS') parser.add_argument("--config", metavar="file", help="Specify config file", default=None) parser.add_argument("--ckey", metavar="key", help="The consumer key to auth with", default=None) parser.add_argument("--tkey", metavar="key", help="The token key to auth with", default=None) parser.add_argument("--csec", metavar="secret", help="The consumer secret (likely '')", default="") parser.add_argument("--tsec", metavar="secret", help="The token secret to auth with", default=None) parser.add_argument("--apiver", metavar="version", help="The apiver to use (\"\" can be used)", default=MD_VERSION) parser.add_argument("--url", metavar="url", help="The data source to query", default=None) parser.add_argument("--file", dest='files', help="File to post", action='append', default=[]) parser.add_argument("--post", dest='posts', help="name=value pairs to post", action='append', default=[]) parser.add_argument("--power-type", dest='power_type', help="Power type.", choices=POWER_TYPES, default=None) parser.add_argument("--power-parameters", dest='power_parms', help="Power parameters.", default=None) parser.add_argument("--script-result", metavar="retval", type=int, dest='script_result', help="Return code of a commissioning script.") parser.add_argument("status", help="Status", choices=VALID_STATUS, action='store') parser.add_argument("message", help="Optional message", default="", nargs='?') args = parser.parse_args() creds = { 'consumer_key': args.ckey, 'token_key': args.tkey, 'token_secret': args.tsec, 'consumer_secret': args.csec, 'metadata_url': args.url, } if args.config: read_config(args.config, creds) url = creds.get('metadata_url', None) if not url: fail("URL must be provided either in --url or in config\n") url = "%s/%s/" % (url, args.apiver) params = { "op": "signal", "status": args.status, "error": args.message, } if args.script_result is not None: params['script_result'] = args.script_result for ent in args.posts: try: (key, val) = ent.split("=", 2) except ValueError: sys.stderr.write("'%s' had no '='" % ent) sys.exit(1) params[key] = val if args.power_parms is not None: params["power_type"] = args.power_type if params["power_type"] == "moonshot": user, passwd, address, hwaddress = args.power_parms.split(",") power_parms = dict(power_user=user, power_pass=passwd, power_address=address, power_hwaddress=hwaddress) else: user, passwd, address, driver = args.power_parms.split(",") power_parms = dict(power_user=user, power_pass=passwd, power_address=address, power_driver=driver) params["power_parameters"] = json.dumps(power_parms) files = {} for fpath in args.files: files[os.path.basename(fpath)] = open(fpath, "r") data, headers = encode_multipart_data(params, files) exc = None msg = "" try: payload = geturl(url, creds=creds, headers=headers, data=data) if payload != "OK": raise TypeError("Unexpected result from call: %s" % payload) else: msg = "Success" except urllib2.HTTPError as exc: msg = "http error [%s]" % exc.code except urllib2.URLError as exc: msg = "url error [%s]" % exc.reason except socket.timeout as exc: msg = "socket timeout [%s]" % exc except TypeError as exc: msg = exc.message except Exception as exc: msg = "unexpected error [%s]" % exc sys.stderr.write("%s\n" % msg) sys.exit((exc is None))