예제 #1
0
    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)
예제 #2
0
 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))
예제 #3
0
    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
예제 #4
0
    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)
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
0
 def destroy(self):
     super(VirtualBoxNatNetwork, self).destroy()
     logged_exec(["VBoxManage", "natnetwork", "remove", "--netname", self._name], self.logger, check=False)
예제 #9
0
 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
예제 #10
0
 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))
예제 #11
0
 def destroy(self):
     super(VirtualBoxHostNetwork, self).destroy()
     logged_exec(["VBoxManage", "hostonlyif", "remove", self._if_name], self.logger, check=False)
예제 #12
0
 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
예제 #13
0
 def findall(cls):
     return re.findall(r"Name: *(vboxnet\d+) *", logged_exec([
         "VBoxManage", "list", "hostonlyifs"
     ], cls.logger, capture_stdout=True))
예제 #14
0
 def destroy(self):
     logged_exec(["VBoxManage", "dhcpserver", "remove", "--netname", self._name], self.logger, check=False)
예제 #15
0
 def _findall_dhcped(cls):
     return re.findall(r"NetworkName: *(NatNetwork\d+|HostInterfaceNetworking-vboxnet\d+) *", logged_exec([
         "VBoxManage", "list", "dhcpservers"
     ], cls.logger, capture_stdout=True))