def create(cls, state, defn): def new_name(): exists = cls.findall() return cls._pattern.format(int(exists[-1][len(cls._pattern.format("")):])+1 if exists else 1) name = new_name() logged_exec([ "VBoxManage", "natnetwork", "add", "--netname", name, "--network", defn.network_cidr ], cls.logger) return cls(name).update(state, defn)
def set_static_ip(machine, address, vm_id, nic_num): try: if ipaddress.ip_address(unicode(address)) not in subnet: raise Exception("cannot assign a static IP out of the network CIDR") logged_exec([ "VBoxManage", "dhcpserver", "modify", "--netname" , self._name, "--vm" , vm_id, "--nic" , str(nic_num), "--fixed-address", address ], self.logger) except: state.warn("cannot assign static IP '{0}' to machine '{1}' in subnet '{2}'".format(address, machine, defn.network_cidr))
def setup_dhcp_server(self, state, defn): subnet = ipaddress.ip_network(unicode(defn.network_cidr), strict=False) if self._name in self._findall_dhcped(): logged_exec([ "VBoxManage" , "dhcpserver", "remove", "--netname" , self._name ], self.logger, check=False) logged_exec([ "VBoxManage" , "dhcpserver", "add", "--netname" , self._name, "--netmask" , str(subnet.netmask), "--ip" , str(subnet[2]), "--lowerip" , str(subnet[3]), "--upperip" , str(subnet[-2]), "--enable" ], self.logger) def set_static_ip(machine, address, vm_id, nic_num): try: if ipaddress.ip_address(unicode(address)) not in subnet: raise Exception("cannot assign a static IP out of the network CIDR") logged_exec([ "VBoxManage", "dhcpserver", "modify", "--netname" , self._name, "--vm" , vm_id, "--nic" , str(nic_num), "--fixed-address", address ], self.logger) except: state.warn("cannot assign static IP '{0}' to machine '{1}' in subnet '{2}'".format(address, machine, defn.network_cidr)) for machine, address in defn.static_ips.iteritems(): mstate = state.depl.resources.get(machine) mdefn = state.depl.definitions.get(machine) if isinstance(mstate, VirtualBoxState) and isinstance(mdefn, VirtualBoxDefinition): for i, net in enumerate(mdefn.config["virtualbox"]["networks"], start=1): if net.get("_name") == defn.name and net.get("type") == defn.network_type: if mstate.vm_id: set_static_ip(machine, address, mstate.vm_id, i) else: mstate.add_hook("set_static_ip_after_createvm", (set_static_ip, machine, address, i)) break; else: state.warn("cannot assign static IP '{0}' to non-attached machine '{1}'".format(address, machine)) else: state.warn("cannot assign static IP '{0}' to non-existent machine '{1}'".format(address, machine)) return subnet
def create(cls, state, defn): name = re.match(r"^.*'(vboxnet\d+)'.*$", logged_exec([ "VBoxManage", "hostonlyif", "create" ], cls.logger, capture_stdout=True)).group(1) return cls(name).update(state, defn)
def test_assert_logged_exec_stdin_none(self): msg = "hello" ret = util.logged_exec( command=["echo", msg], logger=self.logger, capture_stdout=True, ) self.assertEqual(ret.strip(), msg)
def test_assert_logged_exec(self): msg = "hello" ret = util.logged_exec( command=["cat"], logger=self.logger, stdin_string=msg, capture_stdout=True, ) self.assertEqual(ret, msg)
def test_assert_logged_exec_stdin_none(self): msg = "hello" ret = util.logged_exec( command=["echo", msg], logger=self.logger, capture_stdout=True, ) if not isinstance(ret, str): raise ValueError("Wrong return type!") self.assertEqual(ret.strip(), msg)
def destroy(self): super(VirtualBoxNatNetwork, self).destroy() logged_exec(["VBoxManage", "natnetwork", "remove", "--netname", self._name], self.logger, check=False)
def update(self, state, defn): logged_exec(["VBoxManage", "natnetwork", "modify", "--netname", self._name, "--network", defn.network_cidr, "--enable", "--dhcp", "on"], self.logger) self.setup_dhcp_server(state, defn) return self
def findall(cls): return re.findall(r"Name: *("+cls._pattern.format("")+r"\d+) *", logged_exec([ "VBoxManage", "natnetwork", "list", cls._pattern.format("*") ], cls.logger, capture_stdout=True))
def destroy(self): super(VirtualBoxHostNetwork, self).destroy() logged_exec(["VBoxManage", "hostonlyif", "remove", self._if_name], self.logger, check=False)
def update(self, state, defn): subnet = self.setup_dhcp_server(state, defn) logged_exec(["VBoxManage", "hostonlyif", "ipconfig", self._if_name, "--ip", str(subnet[1]), "--netmask", str(subnet.netmask)], self.logger) return self
def findall(cls): return re.findall(r"Name: *(vboxnet\d+) *", logged_exec([ "VBoxManage", "list", "hostonlyifs" ], cls.logger, capture_stdout=True))
def destroy(self): logged_exec(["VBoxManage", "dhcpserver", "remove", "--netname", self._name], self.logger, check=False)
def _findall_dhcped(cls): return re.findall(r"NetworkName: *(NatNetwork\d+|HostInterfaceNetworking-vboxnet\d+) *", logged_exec([ "VBoxManage", "list", "dhcpservers" ], cls.logger, capture_stdout=True))