def _table(self, format, exact=True): left_mask = self.__packer.parse_mask(format) matched = self.__packer.parse_bits(format) assert (matched & left_mask) == 0 ops = {} for opcod, op in self.__ops.iteritems(): if (opcod & ~left_mask) != matched or opcod == 'ill': continue lid = opcod & left_mask # & op.mask() ops[lid] = op used_bits = 0 for k, op in ops.iteritems(): for l, op in ops.iteritems(): used_bits |= (k ^ l) if exact: used_bits |= left_mask else: if used_bits != left_mask: print "Using less bits than it seems: %x -> %x" % (left_mask, used_bits) pack = packer.Packer([(self.__base_width - 1, 0)], used_bits) rops = {} for k, op in ops.iteritems(): rops[pack.mop_to_plain(k)] = op for i in range(1 << pack.size()): if i not in rops: rops[i] = self.__ill return OpTable(pack, *[rops[i] for i in range(1 << pack.size())])
def test_packer(): """Test Packer methods """ only = ['virtualbox-iso'] pi = packer.Packer(PACKERFILE, only=only) pi.version() pi.validate() pi.build(force=True)
def render_metadata(self): stream = packer.Packer() stream.pack("<I", METADATA_MAGIC) stream.pack("<I", len(self.metadata)) for id, data in self.metadata: stream.pack("<HH", id, len(data)) stream.write(data) return bytes(stream)
def reduced_table(self, format): if format == None: format = self.__base_format format = ''.join(filter(lambda x: x in '01X', format)) left_mask = self.__packer.parse_mask(format) useful_mask = self.__packer.mask() nleft_mask = ~left_mask & useful_mask matched = self.__packer.parse_bits(format) # print "Reduced_table %x %x"%(left_mask, matched) assert (matched & left_mask) == 0 ops = {} for opcod, op in self.__ops.iteritems(): # print "%x %x %x %x"%(opcod, (opcod & nleft_mask), (opcod & left_mask), matched) if (opcod & nleft_mask) != matched: continue lid = opcod & left_mask ops[lid] = op pack = packer.Packer([(self.__base_width - 1, 0)], matched) rops = {} for k, op in ops.iteritems(): rops[pack.mop_to_plain(k)] = op ill = Op("ill", 0, 0, self.__func('ill')) table = [rops.get(i, ill) for i in range(1 << pack.size())] useless_bits = get_useless_bits(pack.slices(), table) if useless_bits: for n, bit in useless_bits: matched &= ~(1 << bit) pack = packer.Packer([(self.__base_width - 1, 0)], matched) rops = {} for k, op in ops.iteritems(): rops[pack.mop_to_plain(k)] = op ill = Op("ill", 0, 0, self.__func('ill')) table = [rops.get(i, ill) for i in range(1 << pack.size())] return OpTable(pack, *table)
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 packer(self, packerfile, exc=None, only=None, variables=None, vars_file=None): # Cast as string from unicode to appease upstream module _packerfile = str(packerfile) return packer.Packer(_packerfile, exc=exc, only=only, vars=self._get_vars(variables), vars_file=vars_file, exec_path=self._exec_path)
def __init__(self, packer_file_path=DEFAULT_FILES_LOCATION, packer_file_name="salt_packer"): access_key, secret_key = self._get_credentials() packer_vars = { "aws_access_key": access_key, "aws_secret_key": secret_key, } if not os.path.exists(packer_file_path): os.makedirs(packer_file_path) self.packer_file_path = os.path.join(packer_file_path, packer_file_name + '.yml') open(self.packer_file_path, 'a').close() # creates an empty file self.packer = packer.Packer(self.packer_file_path, vars=packer_vars, exec_path=PACKER_EXECUTABLE)
def test_build(self): p = packer.Packer(TEST_PACKERFILE) p.build()
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): global esxi_file os_types_vmware = { "10": "windows9-64", "2003": "winnetstandard", "2003r2": "winnetstandard-64", "2008": "longhorn-64", "2008r2": "windows7srv-64", "2012": "windows8srv-64", "2012r2": "windows8srv-64", "2016": "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, 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" } # if an esxi_config file is found add to the packer file params needed for esxi if vmServer is not None: with open(esxi_file) as config_file: esxi_config = json.load(config_file) packer_vars.update(esxi_config) with open(packerfile) as packer_source: packer_config = json.load(packer_source) for builder in packer_config['builders']: if builder['type'] == "vmware-iso": builder.update({ "remote_type": "esx5", "remote_host": "{{user `esxi_host`}}", "remote_datastore": "{{user `esxi_datastore`}}", "remote_username": "******", "remote_password": "******", "keep_registered": True, "vnc_port_min": "5900", "vnc_port_max": "5911", "vnc_bind_address": "0.0.0.0", "vnc_disable_password": True, "disk_type_id": "thin", "output_directory": vm_name }) if esxi_config['esxi_cache_datastore'] is not None: builder.update({ "remote_cache_datastore": "{{user `esxi_cache_datastore`}}" }) builder['vmx_data'].update({ "ethernet0.networkName": "{{user `esxi_network`}}" }) packer_config["post-processors"] = [] packerfile = os.path.join(temp_path, "current_packer.json") with open(packerfile, "w") as packer_current: json.dump(packer_config, packer_current) autounattend = create_autounattend(vm_name, os_parts) os_type = os_types_vmware[os_parts['version']] if os_parts['arch'] == 'x86': os_type.replace("64", "32") packer_vars.update({ "iso_url": "./iso/" + iso, "iso_checksum": md5, "autounattend": autounattend, "output": "./box/" + output, "vagrantfile_template": vagrant_template, "guest_os_type": os_types_vmware[os_parts['version']], "vm_name": 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 = get_vm(vmServer, 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 p.build(parallel=True, debug=False, force=False) if vmServer is not None: vm = get_vm(vmServer, vm_name) if vm is not None: vm.takeSnapshot(snapshotName='baseline') # possibly change the network in future return p
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
import packer packerfile = 'packer/tests/resources/packerfile.json' # exc = ['z', 't'] exc = [] # only = ['x', 'y'] only = [] # vars = {"variable1": "y", "variable2": "value"} vars = {} vars_file = '/x' p = packer.Installer('packer_executables/', 'packer_0.7.5_linux_amd64.zip') # If we installed packer using the provided installer, it will return # packer's executable path. We can use it below: # packer_exec = p.install() packer_exec = 'packer' p = packer.Packer(packerfile, exc=exc, only=only, vars=vars, exec_path=packer_exec) # print(p.version()) # validation = p.validate(syntax_only=True) # print(validation.succeeded) # print(validation.error) # result = p.inspect(mrf=True) # print result.parsed_output # print(p.fix('TEST.json').fixed) # print(p.build())
def __init__(self, base_width, mask, format, *ops): """ Constructor Parameters:: base_width: base width of instruction word, in bits mask: mask to AND with instruction word to get the useful bits format: format string of subsequent instructions, with spaces and X *ops: instruction set definition Instruction set definition elements:: a tuple: ("ins_format", "function") ins_format: a string corresponding to format above, with some 1 and 0 imposed function: a function name. if there are some [key]-enclosed strings in function name string, they get expanded to if_false or if_true strings defined below a dict: {"key": (bit, "if_false", "if_true")} key: an arbitrary name bit: bit (0-based little endian) in instruction word (corresponding bit in ins_format will probably be X) """ format_re = re.compile(r'\[([a-z A-Z_0-9-]*)\]') self.__base_width = base_width self.__packer = packer.Packer(((self.__base_width - 1, 0), ), mask) assert self.__packer.size() == len(filter(lambda x: x == 'X', format)) self.__base_format = format self.__format = format.replace("X", "%d") self.__ops = {} self.__funcs = {} self.__ops_by_mop = {} self.__func('ill') self.__ill = Op("ill", 0, 0, self.__func('ill')) cor = {} for op in ops: if isinstance(op, dict): cor.update(op) continue try: format, func, name = op except: format, func = op name = func x = '%(\\1)s' name = format_re.sub(x, name) func = format_re.sub(x, func) mask = self.__packer.parse_mask(format) plain = self.__packer.parse_bits(format) assert (mask & plain) == 0 for mid in self.__packer.ids_for_op_mask(mask): opcod = mid | plain fdict = self.fdict(opcod, cor) mname = name % fdict mfunc = func % fdict op = self.__mkop(mname, opcod, mask, self.__func(mfunc))
import lib import evo import packer import requests as rq ind1 = lib.run() Evo = evo.Evo(200) ind2 = Evo.run() Packer = packer.Packer() payload = Packer.pack(ind1, ind2) response = rq.post("https://cit-home1.herokuapp.com/api/ga_homework", json=payload) print(response.content)
def test_validate(self): p = packer.Packer(TEST_PACKERFILE) p.validate()
def test_fix(self): p = packer.Packer(TEST_PACKERFILE) p.fix()
def test_push(self): p = packer.Packer(TEST_PACKERFILE) p.push()
def test_inspect(self): p = packer.Packer(TEST_PACKERFILE) p.inspect()
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
def test_version(self): p = packer.Packer(TEST_PACKERFILE) p.version()
def packer_build(packerfile): p = packer.Packer(packerfile) output = p.build(parallel=False, debug=False, force=False) found = re.findall('ami-[a-z0-9]*', str(output)) return found[-1]