def verify_tx(tx: Tx) -> None: compiler = Compiler() input_sum = sum( sum(outpoint.amount for outpoint in tx_input.outpoints) for tx_input in tx.inputs) output_sum = sum(output.amount for output in tx.outputs) if output_sum > input_sum: raise ValueError('Output amounts exceeds input amounts') for input_idx, tx_input in enumerate(tx.inputs): witness = tx.witnesses[input_idx] loop_trees = parse_loop_trees(io.BytesIO(witness.loop_trees)) loop_stack = LoopStack(loop_trees) src = tx_input.bytecode.decode('ascii') compile_result = compiler.compile(src) vm = VM(loop_stack, compile_result.num_locals, witness.ram_size) instructions = Block(compile_result.instructions) instructions.run(vm) for preamble_idx, preamble in enumerate(tx.preambles): witness = tx.witnesses[len(tx.inputs) + preamble_idx] loop_trees = parse_loop_trees(io.BytesIO(witness.loop_trees)) loop_stack = LoopStack(loop_trees) src = preamble.decode('ascii') compile_result = compiler.compile(src) vm = VM(loop_stack, compile_result.num_locals, witness.ram_size) instructions = Block(compile_result.instructions) instructions.run(vm)
def get_vm(self, vmid=None, **kwargs): '''Retrieve specific vm or all''' if vmid is not None: req = self.get_request('v1/vms/{0}'.format(vmid)) return VM(**req.json()) req = self.get_request('v1/vms', params=(kwargs or None)) return list([VM(**res) for res in req.json()])
def get_vm(self, vmid=None, **kwargs): '''Retrieve specific vm or all''' if vmid is not None: req = self.get_request('v1/vms/{0}'.format(vmid)) return VM(**req.json()) # Get protected VMs prot_vms = self.get_request('v1/vms', params=(kwargs or None)) return [VM(**vm_resp) for vm_resp in prot_vms.json()]
def generate_cluster_info(self, cluster_name, ambari_server_fqdn_ip_pairs, service_server_fqdn_ip_pairs, ambari_agent_fqdn_ip_pairs, docker_num): """ generate VM and docker info for this cluster set up parameter of the class instance as this info :param cluster_name: the name of the cluster :param ambari_server_fqdn_ip_pairs: the domain name and IP pairs for Ambari-server :param service_server_fqdn_ip_pairs: the domain name and IP pairs for VMs with Ambari-agent installed :param ambari_agent_fqdn_ip_pairs: the domain name and IP pairs for VM with Docker containers :param docker_num: the number of Dockers inside each VMs :return: None """ weave_ip_base = Config.ATTRIBUTES["weave_ip_base"] weave_ip_mask = Config.ATTRIBUTES["weave_ip_mask"] current_ip = weave_ip_base for vm_domain_name, vm_ip in ambari_server_fqdn_ip_pairs: current_ip = self._increase_ip(current_ip, 1) weave_dns_ip = current_ip vm = VM(vm_ip, vm_domain_name, weave_dns_ip, weave_ip_mask) current_ip = self._increase_ip(current_ip, 1) vm.weave_internal_ip = current_ip self.ambari_server_vm.append(vm) for vm_domain_name, vm_ip in service_server_fqdn_ip_pairs: current_ip = self._increase_ip(current_ip, 1) weave_dns_ip = current_ip vm = VM(vm_ip, vm_domain_name, weave_dns_ip, weave_ip_mask) current_ip = self._increase_ip(current_ip, 1) vm.weave_internal_ip = current_ip self.service_server_vm_list.append(vm) vm_index = 0 for vm_domain_name, vm_ip in ambari_agent_fqdn_ip_pairs: current_ip = self._increase_ip(current_ip, 1) weave_dns_ip = current_ip vm = VM(vm_ip, vm_domain_name, weave_dns_ip, weave_ip_mask) for docker_index in range(0, docker_num): current_ip = self._increase_ip(current_ip, 1) docker_ip_str = current_ip total_docker_index = vm_index * docker_num + docker_index docker_domain_name = Docker.get_weave_domain_name(cluster_name, total_docker_index) docker = Docker(docker_ip_str, str(weave_ip_mask), docker_domain_name) vm.add_docker(docker) vm_index += 1 self.ambari_agent_vm_list.append(vm) self.cluster_name = cluster_name self.create_time = str(datetime.datetime.now()) self.state = Cluster.STATE_FREE # update config file. # This step makes the user avoid reconfiguring the IP for next cluster creation Config.update("weave", "weave_ip_base", current_ip)
def generate_cluster_info(self, VM_IP_list, cluster_name, docker_num): config = Config() config.load() Docker_IP_base = config.ATTRIBUTES["Docker_IP_base"].split(".") Docker_IP_mask = config.ATTRIBUTES["Docker_IP_mask"] VM_index = 0 for VM_IP in VM_IP_list: vm = VM(VM_IP) for Docker_index in range(0, docker_num): total_Docker_index = VM_index * docker_num + Docker_index docker_IP = self.__increase_IP__(Docker_IP_base, total_Docker_index) docker_IP_str = str(docker_IP[0]) + "." + str(docker_IP[1]) + "." + \ str(docker_IP[2]) + "." + str(docker_IP[3]) docker_hostname = cluster_name + "-" + str( VM_index) + "-" + str(Docker_index) docker = Docker(docker_IP_str, str(Docker_IP_mask), docker_hostname) # print docker vm.add_docker(docker) VM_index = VM_index + 1 self.VM_list.append(vm) self.VMs_num = len(VM_IP_list) self.cluster_name = cluster_name
def load_vm(self, name): show('Loading VM %s' % name) self.get_domain(name) # this fails if VM does not exist # TODO: check that it started, if not, wait self.start(name) # TODO: need a proper retry function ip = None timeout = 0 while ip is None: ip = self.get_ip(name) sleep(2) timeout += 2 if timeout > 20: raise RuntimeError("Could not determine IP of VM %s" % name) hostname = util.normalize_hostname(ip) show('IP determined: %s' % ip) return VM(name=name, backend=self, hostname=hostname, domain=locals.DOMAIN, ip=ip)
def check_missing_lvs(self): """ Perform a check on logicals volumes used by VMs. Return False if some are missing. """ log.info("Checking for missing LV...") safe = True # Get all LVs used by VMs used_lvs = list() for vm in self.get_possible_vm_names(): used_lvs.extend(VM(vm).get_lvs()) # Get all existent LVs existent_lvs = list() for line in self.run("lvs -o vg_name,name --noheading").readlines(): (vg, lv) = line.strip().split() existent_lvs.append("/dev/" + vg + "/" + lv) # Compute missing LVs missing_lvs = list(Set(used_lvs) - Set(existent_lvs)) if len(missing_lvs): log.info(" ** WARNING : Found missing LV :\n\t", "\n\t".join(missing_lvs)) safe = False return safe
def vm_addons(): data = {} payload = request.get_json() logging.debug(payload) field = {'Address', 'Username', 'Passwd', 'Port', 'Addons'} if field - set(payload.keys()): error_msg = "Input json missing some field! " + "It must include " + str( field) logging.error(error_msg) return jsonify({"error": error_msg}), 400 else: try: vm = VM(payload['Address'], payload['Username'], payload['Passwd'], "", port=payload['Port'], addons=payload['Addons']) logging.info("VM install addons " + str(vm.addons)) vm.install_addons() logging.info("VM install addons done") except Exception as e: logging.error(str(e)) return jsonify({"error": str(e)}), 500 return jsonify(data), 200
def solve(): prev_first_x = 0 rows = [] for y in count(500): first_x = None if prev_first_x == None: prev_first_x = 0 x = prev_first_x while True: inputs = deque([x, y]) vm = VM(ns, inputs) output = next(vm) if output: if first_x == None: first_x = x if rows: x = rows[-1][1] - 1 else: if first_x != None: rows.append((first_x, x)) if len(rows) > 100: smallest_x = rows[-1][0] biggest_x = rows[-100][1] if biggest_x - smallest_x >= 100: return smallest_x * 10000 + y - 100 + 1 break x += 1 prev_first_x = first_x
def compile_from_string(string, allowed_opcodes=None): """ :param allowed_opcodes: from opcode_pack """ allowed_opcodes = allowed_opcodes or \ OPCode.get_all_opcodes() vm = VM() print_opcodes = filter(lambda op: issubclass(op, PrintOPCode), allowed_opcodes) jump_opcodes = filter(lambda op: issubclass(op, JumpOPCodes), allowed_opcodes) nc = string_to_prt_opcodes(string, print_opcodes, vm) atoms = break_to_atoms(nc) if NOP in allowed_opcodes: add_random_nops(atoms) atoms.append(NodeChain(HLT())) if jump_opcodes: atoms = add_jmp_opcodes(atoms, jmp_opcode_classes=jump_opcodes) atoms = shuffle(atoms) nc = make_chain_from_atoms(atoms) link_to_real_address(nc) return nc
def info(user, password, machine, mongo, dt_string): server= machine["name"] owner = machine["owner"] print("working on server " + server) resource = Resources(server, owner, dt_string) conn = Connection(server, user, password) try: pythoncom.CoInitialize() conn.register_physical() except Exception as e: print("connection issues with physical namespace " + server) print(e) mongo.update("systems", {"server": server}, json.loads(str(resource))) return resource.physical_properties = PhysicalProperties(conn.physical_connection, get_login=True) print("got physical info of " + server) try: conn.register_virtual() resource.virtual_properties = VirtualProperties(conn.virtual_connection) for vm in resource.virtual_properties.vms_list: v = VM(conn.virtual_connection, vm) resource.virtual_properties.register_vm(v) print("got virtual info of " + server) resource.physical_properties.virtualization = resource.virtual_properties.virtualization except Exception as e: resource.physical_properties.virtualization = "disabled" print("Did not find virtual namespace on " + server) print(e) #print(resource) mongo.update("systems", {"server": server}, json.loads(str(resource)))
def run(fp): program_contents = "" while True: read = os.read(fp, 4096) if len(read) == 0: break program_contents += read os.close(fp) program = parse(program_contents) i = 0 functions = [None] * 1024 while i < len(program): ops = program[i] opcode = int(ops[0]) if opcode == OpCode.MAKE_FUNCTION: name = int(ops[1]) params = int(ops[2]) func, bytecodes_length = make_function(name, params, program, i + 1) functions[name] = func i += bytecodes_length i += 1 functions = [func for func in functions if func is not None] main_func = functions[0] init_frame = Frame(None, main_func, main_func.num_locals, main_func.literals, main_func.stack_size) vm = VM(functions) vm.run(init_frame)
def test_nested_loop(): vm = VM( LoopStack([ LoopTree.CARTESIAN(3, [ LoopTree.LEAF(3), LoopTree.LEAF(5), ]), ]), num_locals=0, ram_size=0, ) ins = InsLoopSpecified( Block([ InsLoopSpecified( Block([ InsArith([0], True, ArithMode.CHECKED, lambda n: n - 1), ])), InsLoopSpecified( Block([ InsArith([0], True, ArithMode.CHECKED, lambda n: n + 1), ])), InsArith([0], True, ArithMode.CHECKED, lambda n: n * 2), ])) ins.run(vm) result = vm.belt().get_num(0).value.expect_int() assert result == 28
def do_pt1(): with open('input') as f: intcode = f.read() intcode = VM.sanitise_memory(intcode) intcode[1] = 12 intcode[2] = 2 vm = VM() print(vm.compute(intcode))
def test_input(self): ops =[9, 32768, 32769, 4, 19, 32768] # - Store into register 0 the sum of 4 and the value contained in register 1. # - Output to the terminal the character with the ascii code contained in register 0. vm = VM(ops) vm.r[1] = 65 vm.run() self.assertEqual(vm.r[0], vm.r[1] + 4)
def __init__(self, sql_string): self.sql_string_array = sql_string.split() self.statement_type = self.sql_string_array[0] self.command_executions = { 'insert': self.handle_insert_command, 'select': self.handle_select_command } self.vm = VM()
def __init__(self, _id, _label, _isHost=False): Component.__init__(self, _id, _label) self.compType = CompType.DEVICE self.isHost = _isHost self.VMs = [] if _isHost: self.VMs = [VM(self.id) for x in range(cfg.VMsInHost)] self.links = []
def StartVM(): VM().execute("start") waiter = waiting() waiter.start() wait = "Waiting for VM |" while w.is_alive(): print(wait, end="\r") wait = change(wait) time.sleep(0.2) print("[+] Done")
def get_command(self): input_list = input().split() if input_list[0] == "create": c_v, m_v = int(input_list[1]), int(input_list[2]) vm = VM(c_v, m_v) self.create(vm) elif input_list[0] == "delete": pass else: pass
def load_vm(self, name, vm=None): if not vm: vm = self.get_vm(name) vm = self.start(name, vm) # Obtain the IP address. It can take a while for the guest agent # to start, so we wait 2 minutes here before giving up. show('Waiting to obtain IP address') show('Press CTRL+C to interrupt and enter manually.') counter = 0 ip = self.get_ip(vm) try: while ip is None: vm = self.get_vm(name) ip = self.get_ip(vm) counter = counter + 1 if counter > 40: break sleep(15) except KeyboardInterrupt: counter = 100000 if counter <= 40: fqdn = vm.get_guest_info().fqdn show("IP address of the VM is %s" % ip) show("FQDN of the VM is %s" % fqdn) else: notify('Enter the IP manually.') fqdn = '' last_ip_segment = '' while not (len(last_ip_segment) > 0 and len(last_ip_segment) < 4): last_ip_segment = raw_input( "IP address could not be " "determined. Enter the VM number (no leading zeros):") ip = locals.IP_BASE + last_ip_segment # Set the VM's description so that it can be identified in WebAdmin if fqdn: vm.set_description(fqdn) vm.update() show("Description set to %s" % fqdn) # Necessary because of RHEV bug show("Pinging the VM") output, errors, rc = util.run(['ping', '-c', '3', ip]) show.untab() return VM(name=name, backend=self, hostname=fqdn, domain=locals.DOMAIN, ip=ip)
def get_vm(self, vmname): """Return the VM instance corresponding to the given vmname.""" try: uuid = self.server.xenapi.VM.get_by_name_label(vmname)[0] except IndexError: raise NotRunningVmError(self.get_hostname(), vmname) vm_rec = self.server.xenapi.VM.get_record(uuid) vm = VM(vm_rec['name_label'], vm_rec['domid']) vm.metrics = self.server.xenapi.VM_metrics.get_record( vm_rec['metrics']) return vm
def get_vm(self, vmname): """Return the VM instance corresponding to the given vmname.""" if core.cfg['USESSH']: line = self.run("xm list | grep " + vmname + " | awk '{print $1,$2,$3,$4;}'").read() if len(line) < 1: raise ClusterNodeError(self.get_hostname(), ClusterNodeError.VM_NOT_RUNNING, vmname) (name, id, ram, vcpu) = line.strip().split() return VM(name, id, ram, vcpu) else: try: uuid = self.server.xenapi.VM.get_by_name_label(vmname)[0] except IndexError: raise ClusterNodeError(self.get_hostname(), ClusterNodeError.VM_NOT_RUNNING, vmname) vm_rec = self.server.xenapi.VM.get_record(uuid) vm = VM(vm_rec['name_label'], vm_rec['domid']) vm.metrics = self.server.xenapi.VM_metrics.get_record( vm_rec['metrics']) return vm
def deactivate_lv(self, vmname): """Deactivate the logicals volumes of the specified VM on this node. Raise a RunningVmError if the VM is running. """ if (self.is_vm_started(vmname)): raise RunningVmError(self.hostname, vmname) else: lvs = VM(vmname).get_lvs() if lvs: self.refresh_lvm(self.get_vgs(lvs)) self.run("lvchange -aln " + " ".join(lvs))
def get_vms(self): """Return the list of VM instance for each running vm.""" vms = list() if core.cfg['USESSH']: for line in self.run( "xm list | awk '{print $1,$2,$3,$4;}' | tail -n +3" ).readlines(): (name, id, ram, vcpu) = line.strip().split() vms.append(VM(name, id, ram, vcpu)) else: dom_recs = self.server.xenapi.VM.get_all_records() dom_metrics_recs = self.server.xenapi.VM_metrics.get_all_records() if core.cfg['DEBUG']: print "DEBUG Xen-Api: ", dom_recs for dom_rec in dom_recs.values(): if dom_rec['name_label'] != "Domain-0": vm = VM(dom_rec['name_label'], dom_rec['domid']) vm.metrics = dom_metrics_recs[dom_rec['metrics']] vms.append(vm) return vms
def create_vm(self, name, template=locals.TEMPLATE_NAME): # TODO: check if the VM with the name of name exists # Check whether template VM exists show('Checking for existence of template') template_domain = self.get_domain(template) # TODO: check if it is running, if is, print down warning and shut # it down if template_domain: show('Cloning..') # Find out next available MAC address in the pool new_mac = self.get_next_free_mac() output, errors, rc = util.run([ 'virt-clone', '-o', template, '--auto-clone', '-n', name, '-m', new_mac, ]) if rc != 0: raise RuntimeError("Could not clone VM %s" % template) show('Cloning successful') # TODO: check that it started, if not, wait show('Starting..') self.start(name) sleep(10) # Macs are tied to the IPs last_mac_segment = new_mac.split(':')[-1] ip = locals.IP_BASE + '%s' % int(last_mac_segment) show('IP determined: %s' % ip) hostname = util.normalize_hostname(ip) return VM(name=name, backend=self, hostname=hostname, domain=locals.DOMAIN, ip=ip)
def new(self, name: str, description: dict): self._logger.debug( f"Loading VM {name} from description: {description}") if name in self._vm_map.keys(): raise KeyError( "A virtual machine with this name already exists...") vm = VM(name, description) self._vms.append(vm) self._rebuild_map() self._save(vm) self._logger.info(f"New virtual machine created: {vm.get_name()}")
def create_vm(): # POST rancher DO_TOKEN = os.getenv("DO_TOKEN", "") create_vm_payload = { "type": "host", "digitaloceanConfig": { "accessToken": DO_TOKEN, "image": "ubuntu-16-04-x64", "region": "nyc3", "size": "2gb", "sshKeyFingerprint": "", "sshKeyPath": "", "sshPort": "22", "sshUser": "******", "tags": "", "userdata": "", "type": "digitaloceanConfig" }, "engineInstallUrl": "https://releases.rancher.com/install-docker/1.12.sh", "hostname": "dev-3", "labels": { "app": "instamaker" } } create_vm_url = "https://try.rancher.com/v2-beta/projects/1a1065894/host" rancher_creds = utils.get_rancher_creds() r = requests.post( create_vm_url, json=create_vm_payload, auth=HTTPBasicAuth(rancher_creds["username"], rancher_creds["password"]), ) if r.status_code == 201: # ONLY INSERT IF RANCHER RETURNS RESULT # INSERT INTO vms ip, rancher_id, blocked=false data = r.json() rancher_id = data["id"] ip = "" blocked = "0" vm = VM(ip=ip, rancher_id=rancher_id, blocked=False) session = Session() #if ip != '': session.add(vm) session.commit() return
def vm_action(): payload = request.get_json() logging.debug(payload) field = {'hostIp', 'hostUser', 'hostPass', 'vmName', 'vmAction'} if field - set(payload.keys()): error_msg = "Input json missing some field! " + "It must include " + str( field) logging.error(error_msg) return jsonify({"error": error_msg}), 400 else: action = payload['vmAction'] try: vm = VM(payload['hostIp'], payload['hostUser'], payload['hostPass'], payload['vmName']) if action == 'create': vm.vcpus = payload['vmVcpus'] vm.memory = payload['vmMemory'] vm.disk = payload['vmDisk'] vm.os_type = payload['vmType'] # optional params if "rootPass" in payload.keys() and payload['rootPass']: vm.root_pass = payload['rootPass'] if "vncPass" in payload.keys() and payload['vncPass']: vm.vnc_pass = payload['vncPass'] if "vmHostname" in payload.keys() and payload['vmHostname']: vm.hostname = payload['vmHostname'] else: vm.hostname = payload['vmName'] logging.info("VM creating") vm.create() logging.info("VM created") elif action == 'start': vm.start() elif action == 'shutdown': vm.shutdown() elif action == 'reboot': vm.reboot() elif action == 'delete': vm.delete() else: error_msg = "give vmAction: {} not support".format(action) return jsonify({"error": error_msg}), 400 except Exception as e: logging.error(str(e)) return jsonify({"error": str(e)}), 500 return jsonify(""), 202
def get_colors(initial): inputs = deque() vm = VM(ns, inputs) d = -1j loc = 0 colors = defaultdict(int) colors[0] = initial try: while True: inputs.append(colors[loc]) colors[loc] = next(vm) d *= 1j if next(vm) else -1j loc += d except StopIteration: return colors
def test_simple_loop(): vm = VM( LoopStack([ LoopTree.LEAF(8), ]), num_locals=0, ram_size=0, ) ins = InsLoopSpecified( Block([ InsArith([0], False, ArithMode.CHECKED, lambda n: n + 1), ])) ins.run(vm) result = vm.belt().get_num(0).value.expect_int() assert result == 8