#!/usr/bin/python -tt
#
# Create a virtual machine from an XML image description
#
# Copyright 2007  Red Hat, Inc.
# David Lutterkort <dlutter@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.

import os, sys, string
from optparse import OptionParser, OptionValueError
import subprocess
import logging
import libxml2
import urlgrabber.progress as progress

import virtinst
import virtinst.ImageParser
import virtinst.CapabilitiesParser
import virtinst.cli as cli
from virtinst.cli import fail

import gettext
import locale

from virtinst import _virtinst as _
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain(virtinst.gettext_app, virtinst.gettext_dir)
gettext.install(virtinst.gettext_app, virtinst.gettext_dir, unicode=1)

### General input gathering functions

def get_name(name, image_name, guest):
    if name is None:
        name = image_name
    cli.get_name(name, guest)

def get_memory(memory, image_memory, guest):
    if memory is None and image_memory is not None:
        memory = int(image_memory)/1024
    cli.get_memory(memory, guest)

def get_vcpus(vcpus, image_vcpus, check_cpu, guest, conn):
    if vcpus is None:
        vcpus = int(image_vcpus)
    cli.get_vcpus(vcpus, check_cpu, guest, conn)

def get_networks(domain, macs, bridges, networks, guest):
    nnics = domain.interface
    (macs, networks) = cli.digest_networks(guest.conn, macs, bridges,
                                           networks, nnics)

    if len(networks) > nnics:
        print >> sys.stderr, (_("Warning: more networks were provided [%i] then nics required [%i]. All extras are ignored") % (len(networks),  nnics))
        networks = networks[0:nnics]
        macs = macs [0:nnics]
    elif nnics > len(networks):
        fail(_("The image requires %i network interface") % nnics)

    map(lambda m, n: cli.get_network(m, n, guest), macs, networks)

def get_graphics(domain, vnc, vncport, nographics, sdl, keymap, guest):
    if not domain.graphics:
        guest.graphics_dev = None
        return
    else:
        if not vnc and not sdl:
            vnc = True
        cli.get_graphics(vnc, vncport, nographics, sdl, keymap, guest)

### Option parsing
def parse_args():
    parser = cli.VirtOptionParser()
    parser.set_usage("%prog [options] image.xml")
    parser.add_option("-n", "--name", type="string", dest="name",
                      action="callback", callback=cli.check_before_store,
                      help=_("Name of the guest instance"))
    parser.add_option("-r", "--ram", type="int", dest="memory",
                      help=_("Memory to allocate for guest instance in megabytes"))
    parser.add_option("-u", "--uuid", type="string", dest="uuid",
                      action="callback", callback=cli.check_before_store,
                      help=_("UUID for the guest; if none is given a random UUID will be generated. If you specify UUID, you should use a 32-digit hexadecimal number."))
    parser.add_option("", "--vcpus", type="int", dest="vcpus",
                      help=_("Number of vcpus to configure for your guest"))
    parser.add_option("", "--check-cpu", action="store_true", dest="check_cpu",
                      help=_("Check that vcpus do not exceed physical CPUs and warn if they do."))

    # network options
    parser.add_option("-m", "--mac", type="string",
                      dest="mac", action="callback", callback=cli.check_before_append,
                      help=_("Fixed MAC address for the guest; if none or RANDOM is given a random address will be used"))
    parser.add_option("-b", "--bridge", type="string",
                      dest="bridge", action="callback", callback=cli.check_before_append,
                      help=_("Bridge to connect guest NIC to; if none given, will try to determine the default"))
    parser.add_option("-w", "--network", type="string",
                      dest="network", action="callback", callback=cli.check_before_append,
                      help=_("Connect the guest to a virtual network, forwarding to the physical network with NAT"))

    # graphics options
    parser.add_option("", "--vnc", action="store_true", dest="vnc",
                      help=_("Use VNC for graphics support"))
    parser.add_option("", "--vncport", type="int", dest="vncport",
                      help=_("Port to use for VNC"))
    parser.add_option("", "--sdl", action="store_true", dest="sdl",
                      help=_("Use SDL for graphics support"))
    parser.add_option("", "--nographics", action="store_true",
                      help=_("Don't set up a graphical console for the guest."))

    parser.add_option("-k", "--keymap", type="string", dest="keymap",
                      action="callback", callback=cli.check_before_store,
                      help=_("set up keymap for a graphical console"))

    parser.add_option("", "--connect", type="string", dest="connect",
                      action="callback", callback=cli.check_before_store,
                      help=_("Connect to hypervisor with URI"),
                      default=virtinst.util.default_connection())

    # fullvirt options
    parser.add_option("", "--noapic", action="store_true", dest="noapic", help=_("Disables APIC for fully virtualized guest (overrides value in os-type/os-variant db)"), default=False)
    parser.add_option("", "--noacpi", action="store_true", dest="noacpi", help=_("Disables ACPI for fully virtualized guest (overrides value in os-type/os-variant db)"), default=False)

    # Misc options
    parser.add_option("-d", "--debug", action="store_true", dest="debug",
                      help=_("Print debugging information"))
    parser.add_option("-p", "--print", action="store_true", dest="print_only",
                      help=_("Print the libvirt XML, but do not start the domain"))
    parser.add_option("", "--boot", type="int", dest="boot",
                      help=_("The zero-based index of the boot record to use"))
    parser.add_option("", "--force", action="store_true", dest="force",
                      help=_("Do not prompt for input. Answers yes where applicable, terminates for all other prompts"),
                      default=False)    
    parser.add_option("", "--replace",action="store_true", dest="replace",
                      help=_("Overwrite, or destroy, an existing image with the same name"),
                      default=False)                      

    (options,args) = parser.parse_args()
    if len(args) < 1:
        parser.error(_("You need to provide an image XML descriptor"))
    options.image = args[0]
    
    return options

def main():
    options = parse_args()

    cli.setupLogging("virt-image", options.debug)
    cli.set_force(options.force)

    conn = cli.getConnection(options.connect)
    type = None

    try:
    	image = virtinst.ImageParser.parse_file(options.image)
    except virtinst.ImageParser.ParserException, msg:
        fail( "%s '%s': %s" % (_("Cannot parse"),  options.image, msg))
    try:
        capabilities = virtinst.CapabilitiesParser.parse(conn.getCapabilities())
    except virtinst.CapabilitiesParser.CapabilitiesParserException, msg:
        fail( "%s : %s" % (_("Cannot parse capabilities"),  msg))

    if options.boot is not None:
        nboots = len(image.domain.boots)
        if options.boot < 0 or options.boot >= nboots:
            fail(_("The index for --boot must be between 0 and %d") % (nboots - 1))

    installer = virtinst.ImageInstaller(boot_index = options.boot,
                                        image = image,
                                        capabilities = capabilities)

    if installer.is_hvm():
        guest = virtinst.FullVirtGuest(connection=conn, installer=installer, arch=installer.arch)
    else:
        guest = virtinst.ParaVirtGuest(connection=conn, installer=installer)

    # now let's get some of the common questions out of the way
    get_name(options.name, image.name, guest)
    get_memory(options.memory, image.domain.memory, guest)
    cli.get_uuid(options.uuid, guest)
    get_vcpus(options.vcpus, image.domain.vcpu, options.check_cpu,
              guest, conn)
    # For now, we only allow one NIC
    get_networks(image.domain, options.mac, options.bridge,
                 options.network, guest)

    get_graphics(image.domain, options.vnc, options.vncport,
                 options.nographics, options.sdl, options.keymap, guest)

    if installer.is_hvm():
        if options.noacpi:
            guest.features["acpi"] = False
        if options.noapic:
            guest.features["apic"] = False

    progresscb = progress.TextMeter()

    # we've got everything -- try to start the install
    if options.print_only:
        # FIXME: Ugly remix of Guest.start_install/_do_install
        # Should be exposed by Guest in a different way
        meter = progress.BaseMeter()
        guest.validate_parms()
        guest._prepare_install(meter)
        guest._create_devices(meter)
        print guest.get_config_xml()
    else:
        try:
            print _("\n\nCreating guest %s...") % guest.name

            dom = guest.start_install(None, progresscb, options.replace)
            if dom is None:
                print _("Guest creation failed")
                sys.exit(1)

        except RuntimeError, e:
            fail(e)
        except Exception, e:
            print _("Domain creation may not have been\n"
                   "successful.  If it was, you can restart your domain\n"
                   "by running 'virsh start %s'; otherwise, please\n"
                   "restart your installation.") %(guest.name,)
            raise

if __name__ == "__main__":
    try:
        main()
    except SystemExit, e:
        sys.exit(e.code)
    except KeyboardInterrupt, e:
        print >> sys.stderr, _("Installation aborted at user request")
    except Exception, e:
        logging.exception(e)
        sys.exit(1)

