Beispiel #1
0
def build_base(packer_var_file, replace_existing, vmServer=None, prependString = ""):
    TEMP_DIR="tmp"

    vm_name = packer_var_file.strip("_packer.json")

    temp_path = os.path.join("..", "..", TEMP_DIR, prependString + vm_name)

    if not os.path.exists(temp_path):
        os.makedirs(temp_path)

    output = vm_name + "_vmware.box"

    only = ['vmware-iso']

    with open(os.path.join("..", "..", packer_var_file)) as packer_var_source:
        packer_vars = json.load(packer_var_source)

    packer_vars.update({
        "vm_name": prependString + vm_name,
        "output": os.path.join("..", "..", "box", output)
    })

    packerfile = "ubuntu.json"

    packer_obj = packerMod(packerfile)
    packer_obj.update_config(packer_vars)

    if vmServer.get_esxi() is not None:
        packer_vars.update(vmServer.get_config())
        packer_obj.use_esxi_config()

    packerfile = os.path.join(temp_path, vm_name + ".json")
    packer_obj.save_config(packerfile)

    out_file = os.path.join(temp_path, "output.log")
    err_file = os.path.join(temp_path, "error.log")

    p = packer.Packer(str(packerfile), only=only, vars=packer_vars,
                      out_iter=out_file, err_iter=err_file)

    vm = vmServer.get_vm(prependString + vm_name)
    if vm is not None:
        if replace_existing:
            vmServer.remove_vm(prependString + vm_name)
        else:
            return p  # just return without exec since ret value is not checked anyways

    p.build(parallel=True, debug=False, force=False)

    if vmServer.get_esxi() is not None:
        vm = vmServer.get_vm(prependString + vm_name)
        if vm is not None:
            vm.takeSnapshot(snapshotName='baseline')

    return p
def build_base(packer_var_file,
               root_path,
               replace_existing=False,
               vmServer=None,
               prependString=""):
    temp_dir = "tmp"
    abs_root = os.path.abspath(root_path)

    vm_name = packer_var_file.split('/').pop().strip(".json")

    temp_path = os.path.join(abs_root, temp_dir, prependString + vm_name)

    if not os.path.exists(temp_path):
        os.makedirs(temp_path)

    output = vm_name + "_virtualbox.box"

    only = ['virtualbox-iso']

    if vmServer.get_config() is not None:
        output = vm_name + "_vmware.box"
        only = ['vmware-iso']

    with open(os.path.join(root_path, packer_var_file)) as packer_var_source:
        packer_vars = json.load(packer_var_source)

    if packer_vars["run_in_path"]:
        os.chdir(packer_vars["packer_template_path"])
        packer_file = packer_vars["packer_template"]
    else:
        packer_file = os.path.join(packer_vars["packer_template_path"],
                                   packer_vars["packer_template"])

    packer_vars.update({
        "vm_name": prependString + vm_name,
        "output": os.path.join(abs_root, "box", output)
    })

    packer_obj = packerMod(packer_file)

    if vmServer.get_esxi() is not None:
        packer_vars.update(vmServer.get_config())
        packer_obj.use_esxi_config()

    packer_obj.update_config(packer_vars)

    packer_file = os.path.join(temp_path, vm_name + ".json")
    packer_obj.save_config(packer_file)

    out_file = os.path.join(temp_path, "output.log")
    err_file = os.path.join(temp_path, "error.log")

    p = packer.Packer(str(packer_file),
                      only=only,
                      vars=packer_vars,
                      out_iter=out_file,
                      err_iter=err_file)

    vm = vmServer.get_vm(prependString + vm_name)
    if vm is not None:
        if replace_existing:
            vmServer.remove_vm(prependString + vm_name)
        else:
            os.chdir(abs_root)
            return p  # just return without exec since ret value is not checked anyways

    try:
        p.build(parallel=True, debug=False, force=False)
    except sh.ErrorReturnCode:
        print "Error: build of " + prependString + vm_name + " returned non-zero"
    os.chdir(abs_root)

    return p
def build_base(iso, md5, replace_existing, vmServer=None, prependString=""):
    global esxi_file

    os_types_vmware = {
        "10": "windows9-64",
        "1709": "windows9srv-64",
        "1803": "windows9srv-64",
        "1809": "windows9srv-64",
        "2003": "winnetstandard",
        "2003r2": "winnetstandard-64",
        "2008": "longhorn-64",
        "2008r2": "windows7srv-64",
        "2012": "windows8srv-64",
        "2012r2": "windows8srv-64",
        "2016": "windows9srv-64",
        "2019": "windows9srv-64",
        "7": "windows7-64",
        "8": "windows8-64",
        "8.1": "windows8-64",
        "xp": "winXPPro-64",
    }

    os_parts = parse_iso(iso)

    vm_name = get_vm_name(os_parts)
    output = "windows_" + os_parts['version'] + "_" + os_parts['arch']

    if os_parts["patch_level"] is not None:
        output += "_" + os_parts["patch_level"]

    if os_parts["build_version"] is not None:
        output += "_" + os_parts["build_version"]

    temp_path = os.path.join(TEMP_DIR, prependString + vm_name)

    if not os.path.exists(temp_path):
        os.makedirs(temp_path)

    # building vmware only for now
    output += "_vmware.box"

    packerfile = './windows_packer.json'
    # TODO: create custom vagrant file for the box being created for now packages a generic file
    vagrant_template = 'vagrantfile-windows_packer.template'

    only = ['vmware-iso']
    packer_vars = {"iso_checksum_type": "md5"}

    packer_obj = packerMod(packerfile)

    # if an esxi_config file is found add to the packer file params needed for esxi
    if vmServer.get_esxi() is not None:
        packer_vars.update(vmServer.get_config())
        packer_obj.use_esxi_config()
    else:
        packer_obj.update_config({"output": "./../../box/" + output})

    packerfile = os.path.join(temp_path, "current_packer.json")
    packer_obj.save_config(packerfile)

    packer_vars.update({"vm_name": prependString + vm_name})

    autounattend = create_autounattend(vm_name,
                                       os_parts,
                                       prependString=prependString)

    os_type = os_types_vmware[os_parts['version']]
    if os_parts['arch'] == 'x86':
        os_type = os_type.replace("-64", "")

    packer_vars.update({
        "iso_url": "./iso/" + iso,
        "iso_checksum": md5,
        "autounattend": autounattend,
        "output": "./box/" + output,
        "vagrantfile_template": vagrant_template,
        "guest_os_type": os_type,
        "vm_name": prependString + vm_name
    })

    out_file = os.path.join(temp_path, "output.log")
    err_file = os.path.join(temp_path, "error.log")

    p = packer.Packer(str(packerfile),
                      only=only,
                      vars=packer_vars,
                      out_iter=out_file,
                      err_iter=err_file)
    vm = vmServer.get_vm(prependString + vm_name)
    if vm is not None:
        if replace_existing:
            vmServer.remove_vm(vm_name)
        else:
            return p  # just return without exec since ret value is not checked anyways

    try:
        p.build(parallel=True, debug=False, force=False)
    except sh.ErrorReturnCode:
        print "Error: build of " + prependString + vm_name + " returned non-zero"
        return p

    if vmServer.get_esxi() is not None:
        vm = vmServer.get_vm(prependString + vm_name)
        if vm is not None:
            vm.takeSnapshot(snapshotName='baseline')
            # possibly change the network in future
    return p
Beispiel #4
0
def build_base(packer_var_file,
               common_vars,
               packerfile,
               replace_existing,
               vmServer=None,
               prependString="",
               factory_image=false):
    TEMP_DIR = "tmp"

    vm_name = packer_var_file.strip(".json")
    if "-server" in vm_name:
        vm_name = vm_name[:vm_name.index("-server")]
    vm_name = "Linux" + vm_name.capitalize() + "x64"

    temp_path = os.path.join("..", "..", TEMP_DIR, prependString + vm_name)

    if not os.path.exists(temp_path):
        os.makedirs(temp_path)

    output = vm_name + "_vmware.box"

    only = ['vmware-iso']

    with open(os.path.join("", packer_var_file)) as packer_var_source:
        packer_vars = json.load(packer_var_source)

    packer_vars.update({
        "vm_name": prependString + vm_name,
        "output": os.path.join("..", "..", "box", output)
    })

    packer_vars.update(common_vars)
    if factory_image:
        del packer_vars["custom_script"]

    packer_obj = packerMod(packerfile)
    packer_obj.update_linux_config(packer_vars)

    request = requests.head(packer_vars['iso_url'])
    if request.status_code != 200:
        packer_obj.update_url(packer_vars)

    if vmServer.get_esxi() is not None:
        packer_vars.update(vmServer.get_config())
        packer_obj.use_esxi_config()
    else:
        packer_obj.update_config({"output": "./../../box/" + output})

    packerfile = os.path.join(temp_path, "current_packer.json")
    packer_obj.save_config(packerfile)

    out_file = os.path.join(temp_path, "output.log")
    err_file = os.path.join(temp_path, "error.log")

    p = packer.Packer(str(packerfile),
                      only=only,
                      vars=packer_vars,
                      out_iter=out_file,
                      err_iter=err_file)

    vm = vmServer.get_vm(prependString + vm_name)
    if vm is not None:
        if replace_existing:
            vm.powerOff
            vm.waitForTask(vm.vmObject.Destroy_Task())
        else:
            return p  # just return without exec since ret value is not checked anyways

    try:
        p.build(parallel=True, debug=False, force=False)
    except sh.ErrorReturnCode:
        print "Error: build of " + prependString + vm_name + " returned non-zero"
        return p

    if vmServer.get_esxi() is not None:
        vm = vmServer.get_vm(prependString + vm_name)
        if vm is not None:
            vm.takeSnapshot(snapshotName='baseline')

    return p