Passed
Pull Request — master (#3276)
by Marek
03:32 queued 58s
created

install_vm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A parse_args() 0 44 1
A main() 0 17 4
1
#!/usr/bin/python
2
import argparse
3
import os
4
5
def parse_args():
6
    parser = argparse.ArgumentParser()
7
8
    parser.add_argument(
9
        "--distro",
10
        dest="distro",
11
        default="fedora",
12
        choices=("fedora", "centos7"),
13
        help="What type of distribution to install"
14
    )
15
    parser.add_argument(
16
        "--domain",
17
        dest="domain",
18
        required=True,
19
        help="What name should the new domain have."
20
    )
21
    parser.add_argument(
22
        "--disk-dir",
23
        dest="disk_dir",
24
        default="/var/lib/libvirt/images/",
25
        help="Location of the VM qcow file."
26
    )
27
    parser.add_argument(
28
        "--ram",
29
        dest="ram",
30
        default=2048,
31
        type=int,
32
        help="Amount of RAM configured for the VM."
33
    )
34
    parser.add_argument(
35
        "--cpu",
36
        dest="cpu",
37
        default=2,
38
        type=int,
39
        help="Number of CPU cores configured for the VM."
40
    )
41
    parser.add_argument(
42
        "--dry",
43
        dest="dry",
44
        action="store_true",
45
        help="Print command line instead of triggering command."
46
    )
47
48
    return parser.parse_args()
49
50
def main():
51
    data = parse_args()
52
    data.kickstart = "https://raw.githubusercontent.com/ComplianceAsCode/content/master/tests/kickstarts/rhel_centos_7.cfg"
53
    data.disk_path = os.path.join(data.disk_dir, data.domain) + ".qcow"
54
55
    if data.distro == "fedora":
56
        data.variant = "fedora27" # this is for support in RHEL7, where fedora28 is not known yet
57
        data.url = "https://download.fedoraproject.org/pub/fedora/linux/releases/28/Everything/x86_64/os"
58
    elif data.distro == "centos7":
59
        data.variant = "centos7"
60
        data.url = "http://mirror.centos.org/centos/7/os/x86_64"
61
62
    command = 'virt-install -n {domain} -r {ram} --vcpus={cpu} --os-variant={variant} --accelerate --disk path={disk_path},size=12 -x "inst.ks={kickstart}" --location {url}'.format(**data.__dict__)
63
    if data.dry:
64
        print(command)
65
    else:
66
        os.system(command)
67
68
if __name__ == '__main__':
69
    main()
70