def create_centos(options):
    template = 'CentOS 6 (64-bit)'

    xenhost = Server(options.xshost, options.xsuser, options.xspass)
    xenhost.disable_known_hosts = True

    template_uuid = lib.template_uuid(xenhost, template)

    vm = lib.clone_template(xenhost, template_uuid, options.vmname)

    if options.kickstart is None:
        virt_kickstart_path = os.path.join(
            os.path.dirname(boxes.__file__), "virt-kickstart")
    else:
        virt_kickstart_path = options.kickstart

    with open(virt_kickstart_path, 'rb') as virt_kickstart_file:
        virt_kickstart_contents = virt_kickstart_file.read()

    virt_kickstart_contents = virt_kickstart_contents.replace(
        '@install_repo@', options.install_repo).replace(
        '@rootpwd@', options.rootpwd)

    kickstart_fname = '{0}.kickstart'.format(vm)

    xenhost.put(
        StringIO.StringIO(virt_kickstart_contents),
        "/opt/xensource/www/{0}".format(kickstart_fname))

    kickstart_url = "http://{0}/{1}".format(options.xshost, kickstart_fname)

    xenhost.run(
        'xe vm-param-set uuid={0} is-a-template=false'
        .format(vm))

    xenhost.run(
        'xe vm-param-set uuid={0} name-description="{1}"'
        .format(vm, options.vmname))

    xenhost.run(
        'xe vm-memory-limits-set static-min={0}MiB static-max={0}MiB dynamic-min={0}MiB dynamic-max={0}MiB uuid={1}'
        .format(options.memsize, vm))

    pool = xenhost.run(
        'xe pool-list --minimal')

    sr = xenhost.run(
        'xe pool-param-get uuid={0} param-name=default-SR'
        .format(pool))

    vdi = xenhost.run(
        'xe vdi-create name-label="boot-{vmname}" '
        'sr-uuid={sr} type=system virtual-size={hddsize}GiB'
        .format(sr=sr, hddsize=options.hddsize, vmname=options.vmname))

    xenhost.run(
        'xe vbd-create vm-uuid={0} vdi-uuid={1} device=0 bootable=true'
        .format(vm, vdi))

    xenhost.run(
        'xe vm-param-set uuid={0} other-config:install-repository={1}'
        .format(vm, options.install_repo))

    xenhost.run(
        'xe vm-param-set uuid={0} PV-args="ks={1} ksdevice=eth0"'.format(
            vm, kickstart_url)
    )

    net = xenhost.run(
        'xe network-list name-label="{0}" --minimal'
        .format(options.networklabel))

    xenhost.run(
        'xe vif-create network-uuid={0} vm-uuid={1} device=0'
        .format(net, vm))

    xenhost.run(
        'xe vm-start uuid={0}'
        .format(vm))
def command(
    user,
    xspass,
    host,
    suite,
    install_repo,
    preseed_file,
    vmname,
    hddsize,
    mac,
    fstype,
    usrpwd,
    packages,
    timezone,
    ntpserver,
    username,
    httpmirrorhost,
    httpmirrordirectory,
    memsize,
    bootoptions,
    httpmirrorproxy,
    networklabel,
    domain,
):
    template = "Ubuntu Lucid Lynx 10.04 (64-bit)"

    xenhost = Server(host, user, xspass)
    xenhost.disable_known_hosts = True

    template_uuid = lib.template_uuid(xenhost, template)

    vm = lib.clone_template(xenhost, template_uuid, vmname)

    xenhost.put(preseed_file, "/opt/xensource/www/{0}.preseed".format(vm))

    preseed_url = "{0}/{1}.preseed".format(host, vm)

    xenhost.run("xe vm-param-set uuid={0} is-a-template=false".format(vm))

    xenhost.run('xe vm-param-set uuid={0} name-description="{1}"'.format(vm, vmname))

    xenhost.run(
        "xe vm-memory-limits-set static-min={0}MiB static-max={0}MiB dynamic-min={0}MiB dynamic-max={0}MiB uuid={1}".format(
            memsize, vm
        )
    )

    pool = xenhost.run("xe pool-list --minimal")

    sr = xenhost.run("xe pool-param-get uuid={0} param-name=default-SR".format(pool))

    vdi = xenhost.run(
        'xe vdi-create name-label="boot-{vmname}" '
        "sr-uuid={sr} type=system virtual-size={hddsize}GiB".format(sr=sr, hddsize=hddsize, vmname=vmname)
    )

    xenhost.run("xe vbd-create vm-uuid={0} vdi-uuid={1} device=0 bootable=true".format(vm, vdi))

    xenhost.run("xe vm-param-set uuid={0} other-config:install-repository={1}".format(vm, install_repo))

    if httpmirrorproxy:
        xenhost.run("xe vm-param-set uuid={0} other-config:install-proxy={1}".format(vm, httpmirrorproxy))

    xenhost.run("xe vm-param-set uuid={0} other-config:debian-release={1}".format(vm, suite))

    xenhost.run(
        textwrap.dedent(
            r"""
        xe vm-param-set uuid={0} PV-args="-- quiet console=hvc0 \
        locale=en_GB \
        keyboard-configuration/layoutcode=gb \
        netcfg/choose_interface=eth0 \
        netcfg/get_hostname={1} \
        netcfg/get_domain={2} \
        mirror/suite={4} \
        mirror/udeb/suite={4} \
        partman/default_filesystem={5} \
        passwd/user-password={6} \
        passwd/user-password-again={6} \
        pkgsel/include={7} \
        time/zone={8} \
        clock-setup/ntp-server={9} \
        passwd/username={10} \
        mirror/http/hostname={11} \
        mirror/http/directory={12} \
        {13} \
        auto url={3}"
        """.format(
                vm,
                vmname,
                domain,
                preseed_url,
                suite,
                fstype,
                usrpwd,
                packages,
                timezone,
                ntpserver,
                username,
                httpmirrorhost,
                httpmirrordirectory,
                bootoptions,
            )
        )
    )

    net = xenhost.run('xe network-list name-label="{0}" --minimal'.format(networklabel))

    additional_net_options = "mac={0}".format(mac) if mac else ""

    xenhost.run("xe vif-create network-uuid={0} vm-uuid={1} device=0 {2}".format(net, vm, additional_net_options))

    xenhost.run("xe vm-start uuid={0}".format(vm))