File: oz-install

package info (click to toggle)
oz 0.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,620 kB
  • sloc: python: 6,993; xml: 654; perl: 614; makefile: 45; sh: 21
file content (193 lines) | stat: -rwxr-xr-x 6,834 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python

# Copyright (C) 2010,2011  Chris Lalancette <clalance@redhat.com>
# Copyright (C) 2012-2016  Chris Lalancette <clalancette@gmail.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import getopt
import logging
import time

import oz.TDL
import oz.GuestFactory
import oz.ozutil

def usage():
    print("Usage: oz-install [OPTIONS] <tdl>")
    print(" OPTIONS:")
    print("  -a <auto>\tUse the guest configuration tool from <auto> instead")
    print("\t\tof using the hard-coded one.  This is useful if you want to pass")
    print("\t\ta custom kickstart/preseed/sif/etc for installation")
    print("  -b <disk_bus>\tUse the <disk_bus> for attaching guest storage instead of")
    print("\t\tthe built-in Oz default")
    print("  -c <config>\tGet config from <config> (default is /etc/oz/oz.cfg)")
    print("  -d <level>\tTurn up logging level.  The levels are:")
    print("\t\t\t0 - errors only (this is the default)")
    print("\t\t\t1 - errors and warnings")
    print("\t\t\t2 - errors, warnings, and information")
    print("\t\t\t3 - all messages")
    print("\t\t\t4 - all messages, prepended with the level and classname")
    print("  -f\t\tForce download of installation media even if already cached")
    print("  -g\t\tGenerate the ICICLE after installation")
    print("  -h\t\tPrint this help message")
    print("  -i <icicle>\tWrite the ICICLE to <icicle> (only valid with -g)")
    print("  -m <mac_address>\tUse <mac_address> for the network interface instead of an autogenerated value")
    print("  -n <net_dev>\tUse <net_dev> for the network instead of the built-in Oz default")
    print("  -p\t\tCleanup old guests with the same name before installation")
    print("  -s <disk>\tWrite the output to <disk> (default is the TDL name tag)")
    print("  -t <timeout>\tWait <timeout> seconds for installation, rather than the default")
    print("  -u\t\tAfter installation, do the customization")
    print("  -x <xmlfile>\tWrite libvirt XML to <xmlfile>")
    print(" Currently supported architectures are:")
    print("   i386, x86_64")
    print(" Currently supported operating systems are:")
    oz.GuestFactory.distrolist()
    sys.exit(1)

try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'a:b:c:d:fghi:m:n:ps:t:ux:',
                                   ['auto', 'disk-bus', 'config', 'debug',
                                    'force-download', 'generate-icicle', 'help',
                                    'icicle', 'mac-address', 'network-device',
                                    'cleanup', 'disk', 'timeout', 'customize',
                                    'xmlfile'])
except getopt.GetoptError as err:
    print(str(err))
    usage()

loglevel = logging.ERROR
logformat = "%(message)s"
force_download = False
config_file = None
generate_icicle = False
filename = None
customize = False
cleanup = False
auto = None
timeout = None
icicle_file = None
output_disk = None
diskbus = None
netdev = None
macaddress = None
for o, a in opts:
    if o in ("-a", "--auto"):
        auto = a
    elif o in ("-b", "--disk-bus"):
        diskbus = a
    elif o in ("-c", "--config"):
        config_file = a
    elif o in ("-d", "--debug"):
        try:
            d_int = int(a)
        except ValueError:
            usage()
        if d_int == 0:
            loglevel = logging.ERROR
        elif d_int == 1:
            loglevel = logging.WARNING
        elif d_int == 2:
            loglevel = logging.INFO
        elif d_int == 3:
            loglevel = logging.DEBUG
        elif d_int >= 4:
            loglevel = logging.DEBUG
            logformat = logging.BASIC_FORMAT
    elif o in ("-f", "--force-download"):
        force_download = True
    elif o in ("-g", "--generate-icicle"):
        generate_icicle = True
    elif o in ("-h", "--help"):
        usage()
    elif o in ("-i", "--icicle"):
        icicle_file = a
    elif o in ("-n", "--network-device"):
        netdev = a
    elif o in ("-m", "--mac-address"):
        macaddress = a
    elif o in ("-p", "--cleanup"):
        cleanup = True
    elif o in ("-s", "--disk"):
        output_disk = a
    elif o in ("-t", "--timeout"):
        timeout = int(a)
    elif o in ("-u", "--customize"):
        customize = True
    elif o in ("-x", "--xmlfile"):
        filename = a
    else:
        assert False, "unhandled option"

if len(args) != 1:
    usage()

if not generate_icicle and icicle_file is not None:
    print("The -i option must be combined with the -g option")
    sys.exit(3)

try:
    config = oz.ozutil.parse_config(config_file)

    logging.basicConfig(level=loglevel, format=logformat)

    tdl = oz.TDL.TDL(open(args[0], 'r').read())

    guest = oz.GuestFactory.guest_factory(tdl, config, auto, output_disk,
                                          netdev, diskbus, macaddress)

    if cleanup:
        guest.cleanup_old_guest()
    else:
        guest.check_for_guest_conflict()

    try:
        guest.generate_install_media(force_download,
                                     customize or generate_icicle)
        try:
            guest.generate_diskimage(size=guest.disksize, force=force_download)
            libvirt_xml = guest.install(timeout, force_download)
        except:
            guest.cleanup_old_guest()
            raise
    finally:
        guest.cleanup_install()

    if customize and generate_icicle:
        print(guest.customize_and_generate_icicle(libvirt_xml))
    elif customize:
        guest.customize(libvirt_xml)
    elif generate_icicle:
        icicle_xml = guest.generate_icicle(libvirt_xml)
        if icicle_file is None:
            print(icicle_xml)
        else:
            open(icicle_file, 'w').write(icicle_xml)
            print("ICICLE XML was written to " + icicle_file)

    if filename is None:
        filename = guest.name + time.strftime("%b_%d_%Y-%H:%M:%S")
    open(filename, 'w').write(libvirt_xml)
    print("Libvirt XML was written to " + filename)
except Exception as exc:
    if loglevel > logging.DEBUG:
        print("")
        print("ERROR: %s" % (str(exc)))
        print("")
        print("(use -d3 to get the full backtrace)")
        print("")
    else:
        raise