~joetalbott/utah/cobbler-pidlock-updated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python

import argparse
import sys

from utah.exceptions import UTAHException
from utah.url import url_argument
from utah.group import check_user_group, print_group_error_message
from utah.provisioning.inventory.sqlite import TinySQLiteInventory
from utah.provisioning.vm.libvirtvm import CustomVM
from utah.run import run_tests


def get_parser():
    parser = argparse.ArgumentParser(
            description=('Provision a machine '
                         'and run one or more UTAH runlists there.'),
            epilog=("For example:\n"
                    "Provision a VM using a precise server image "
                    "with i386 architecture and run the two given runlists\n"
                    "\t%(prog)s -s precise -t server -a i386 \\\n"
                    "\t\t/usr/share/utah/client/examples/master.run \\\n"
                    "\t\t'http://people.canonical.com/~max/max_test.run'"),
            formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('runlists', metavar='runlist', nargs='+',
                        type=url_argument, help='URLs of runlist files to run')
    parser.add_argument('-m', '--machinetype', metavar='MACHINETYPE',
                        choices=('physical', 'virtual'), default='virtual',
                        help='Type of machine to provision (%(choices)s)')
    parser.add_argument('-e', '--emulator',
                        help=('Emulator to use (kvm and qemu are supported, '
                              'kvm will be favored if available)'))
    parser.add_argument('-i', '--image', type=url_argument,
                        help='Image/ISO file to use for installation')
    parser.add_argument('-k', '--kernel', type=url_argument,
                        help='Kernel file to use for installation')
    parser.add_argument('-r', '--initrd', type=url_argument,
                        help='InitRD file to use for installation')
    parser.add_argument('-p', '--preseed', type=url_argument,
                        help='Preseed file to use for installation')
    parser.add_argument('-b', '--boot',
                        help='Boot arguments for initial installation')
    parser.add_argument('-x', '--xml', type=url_argument,
                        help='XML VM definition file')
    parser.add_argument('-g', '--gigabytes', action='append',
                        help=('Size in gigabytes of virtual disk, '
                              'specify more than once for multiple disks'))
    parser.add_argument('-s', '--series', metavar='SERIES',
                        choices=('hardy', 'lucid', 'natty',
                                 'oneiric', 'precise', 'quantal'),
                        help='Series to use for installation (%(choices)s)')
    parser.add_argument('-t', '--type', metavar='TYPE',
                        choices=('desktop', 'server', 'mini', 'alternate'),
                        help=('Install type to use for installation '
                              '(%(choices)s)'))
    parser.add_argument('-a', '--arch', metavar='ARCH',
                        choices=('i386', 'amd64', 'arm'),
                        help=('Architecture to use for installation '
                              '(%(choices)s)'))
    parser.add_argument('-v', '--variant',
                        help='Variant of architecture, i.e., armel, armhf')
    parser.add_argument('-n', '--no-destroy', action='store_true',
                        help='Preserve machine 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('--diskbus', metavar='DISKBUS',
                        choices=('virtio', 'sata', 'ide'),
                        help=('Disk bus to use for customvm installation '
                              '(%(choices)s)'))
    return parser


def run_install_test(args=None):
    if not check_user_group():
        print_group_error_message(__file__)
        sys.exit(3)

    if args is None:
        args = get_parser().parse_args()

    locallogs = []
    exitstatus = 0
    machine = None

    try:
        inventory = TinySQLiteInventory()
        machine = inventory.request(CustomVM,
                arch=args.arch, boot=args.boot, debug=args.debug,
                diskbus=args.diskbus, disksizes=args.gigabytes,
                dlpercentincrement=10, emulator=args.emulator,
                image=args.image, initrd=args.initrd, installtype=args.type,
                kernel=args.kernel, new=True, preseed=args.preseed,
                series=args.series, xml=args.xml)
        exitstatus, locallogs = run_tests(args, machine)

    except UTAHException as error:
        sys.stderr.write('Exception: ' + str(error))
        exitstatus = 2

    finally:
        if not args.no_destroy and machine is not None:
            try:
                machine.destroy()
            except UTAHException as error:
                sys.stderr.write('Failed to destroy machine: ' + str(error))
            finally:
                try:
                    inventory.destroy(machine.machineid)
                except UTAHException as error:
                    sys.stderr.write('Failed to update inventory: '
                                     + str(error))
                finally:
                    del machine
        if len(locallogs) != 0:
            print('Test logs copied to the following files:')
            print("\t" + "\n\t".join(locallogs))

    sys.exit(exitstatus)


if __name__ == '__main__':
    run_install_test()