# Ubuntu Testing Automation Harness
# Copyright 2012 Canonical Ltd.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Command line parser for the utah server script."""
import argparse
import sys
import os
from utah.config import config
from utah.run import master_runlist_argument
from utah.url import url_argument
[docs]def parse_args(argv=None):
"""Parse command line arguments.
:param argv: Command line arguments to be parsed
:type argv: list
:returns: Parse arguments
:rtype: `argparse.Namespace`
"""
if argv is None:
argv = sys.argv[1:]
args = get_parser().parse_args(argv)
if args.gigabytes is None:
args.gigabytes = config.disksizes
if args.logpath:
config.logpath = args.logpath
config._set_log_files()
return args
[docs]def directory_argument(directory):
"""Argument passed through the command line which is a valid directory.
:param directory: A path to a directory passed through the command line.
:type directory: string
:returns: The same directory passed if it's valid.
:rtype: string
:raises argparse.ArgumentTypeError:
If path doesn't point to a directory.
"""
if not os.path.isdir(directory):
raise argparse.ArgumentTypeError(
"Invalid directory: {}".format(directory))
return directory
[docs]def get_parser():
"""Get command line parser.
:returns: Parser object
:rtype: `argparse.ArgumentParser`
"""
parser = argparse.ArgumentParser(
description='Provision a machine and run a runlist there.',
epilog=("For example:\n"
"Provision a VM using a precise server image "
"with i386 architecture and run the given runlist\n"
"\t%(prog)s -s precise -t server -a i386 \\\n"
"\t\t/usr/share/utah/client/examples/master.run"),
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-m', '--machinetype', metavar='MACHINETYPE',
choices=('physical', 'virtual'),
default=config.machinetype,
help='Type of machine to provision (%(choices)s) '
'(Default is %(default)s)')
parser.add_argument('-v', '--variant',
default=config.variant,
help='Variant of architecture, i.e., armel, armhf')
parser.add_argument('--skip-provisioning', action='store_true',
help='Reuse a system that is already provisioned '
'(name argument must be passed)')
parser.add_argument('runlist', metavar='runlist',
type=master_runlist_argument,
help='URLs of runlist files to run')
parser.add_argument('-s', '--series', metavar='SERIES',
choices=config.serieschoices,
default=config.series,
help='Series to use for installation (%(choices)s) '
'(Default is %(default)s)')
parser.add_argument('-t', '--type', metavar='TYPE',
choices=('desktop', 'server', 'mini', 'alternate'),
default=config.installtype,
help='Install type to use for installation '
'(%(choices)s) (Default is %(default)s)')
parser.add_argument('-a', '--arch', metavar='ARCH',
choices=('i386', 'amd64', 'arm'),
default=config.arch,
help='Architecture to use for installation '
'(%(choices)s) (Default is %(default)s)')
parser.add_argument('-n', '--no-destroy', action='store_true',
help='Preserve VM after tests have run')
parser.add_argument('-d', '--debug', action='store_true',
help='Enable debug logging')
parser.add_argument('-j', '--json', action='store_true',
help='Enable json logging (default is YAML)')
parser.add_argument('-f', '--files', action='append',
help='File or directory to copy from test system ')
parser.add_argument('-o', '--outdir',
help=('Directory to store locally copied files '
'(Default is {}/machine-name)'
.format(config.logpath)))
parser.add_argument('--dumplogs', action='store_true',
help='Write client output logs to standard out')
parser.add_argument('--outputpreseed', action='store_true',
help='Copy preseed to logs directory and list as '
'log file in output')
parser.add_argument('-i', '--image', type=url_argument,
default=config.image,
help='Image/ISO file to use for installation')
parser.add_argument('-p', '--preseed', type=url_argument,
default=config.preseed,
help='Preseed file to use for installation')
parser.add_argument('-b', '--boot',
default=config.boot,
help='Boot arguments for initial installation')
parser.add_argument('--rewrite',
choices=('all', 'minimal', 'casperonly', 'none'),
default=config.rewrite,
help='Set level of automatic configuration rewriting '
'(Default is %(default)s)')
parser.add_argument('-k', '--kernel', type=url_argument,
default=config.kernel,
help='Kernel file to use for installation')
parser.add_argument('-r', '--initrd', type=url_argument,
default=config.initrd,
help='InitRD file to use for installation')
parser.add_argument('--name',
default=config.name,
help='Name of machine to provision')
parser.add_argument('-e', '--emulator',
default=config.emulator,
help='Emulator to use (kvm and qemu are supported, '
'kvm will be favored if available)')
parser.add_argument('-x', '--xml', type=url_argument,
default=config.xml,
help='XML VM definition file '
'(Default is %(default)s)')
parser.add_argument('-g', '--gigabytes', action='append',
help='Size in gigabytes of virtual disk, '
'specify more than once for multiple disks '
'(Default is {})'.format(config.disksizes))
parser.add_argument('--diskbus', metavar='DISKBUS',
choices=('virtio', 'sata', 'ide'),
default=config.diskbus,
help='Disk bus to use for customvm installation '
'(%(choices)s) (Default is %(default)s)')
parser.add_argument('-l', '--logpath', type=directory_argument,
help='Directory used to write log files to')
return parser