Move YAML parsing into cmd.py; default to env

This moves the YAML parameter parsing into the command-line driver.
It makes the argument optional so it can be taken from the environment
variable directly.  The parsed YAML is passed to the BlockDevice
object.

Change-Id: I6fa5e5b7d1fccfc7cf47d6e4a1fa6e560734680d
This commit is contained in:
Ian Wienand 2017-05-02 12:08:33 +10:00
parent a9d8e51ec2
commit a06c610a8c
2 changed files with 33 additions and 8 deletions

View file

@ -149,13 +149,18 @@ class BlockDevice(object):
return json.load(fd)
return None
def __init__(self, args):
def __init__(self, params, args):
"""Create BlockDevice object
Arguments:
:param params: YAML file from --params
:param args: arguments from cmd argparse
"""
logger.debug("Creating BlockDevice object")
logger.debug("Param file [%s]" % args.params)
self.args = args
with open(self.args.params) as param_fd:
self.params = yaml.safe_load(param_fd)
self.params = params
logger.debug("Params [%s]" % self.params)
self.state_dir = os.path.join(

View file

@ -14,6 +14,9 @@
import argparse
import logging
import os
import sys
import yaml
from diskimage_builder.block_device.blockdevice import BlockDevice
from diskimage_builder import logging_config
@ -42,18 +45,35 @@ def main():
epilog="Available phases:\n" + phase_doc)
parser.add_argument('--phase', required=True,
help="phase to execute")
parser.add_argument('--params', required=True,
help="parameters for block device handling")
parser.add_argument('--params', required=False,
help="YAML file containing parameters for block-device"
"handling. Default is DIB_BLOCK_DEVICE_PARAMS_YAML")
parser.add_argument('--symbol', required=False,
help="symbol to query for getval")
args = parser.parse_args()
# Find, open and parse the parameters file
if not args.params:
if 'DIB_BLOCK_DEVICE_PARAMS_YAML' in os.environ:
param_file = os.environ['DIB_BLOCK_DEVICE_PARAMS_YAML']
else:
parser.error("DIB_BLOCK_DEVICE_PARAMS_YAML or --params not set")
else:
param_file = args.params
logger.info("params [%s]" % param_file)
try:
with open(param_file) as f:
params = yaml.safe_load(f)
except Exception:
logger.exception("Failed to open parameter YAML")
sys.exit(1)
logger.info("phase [%s]" % args.phase)
logger.info("params [%s]" % args.params)
if args.symbol:
logger.info("symbol [%s]" % args.symbol)
bd = BlockDevice(args)
bd = BlockDevice(params, args)
# Check if the method is available
method = getattr(bd, "cmd_" + args.phase, None)