def downloadFile(self, uuid, fqdn, shortname, ips, macs, netmasks, path, bwlimit): # choose a way to perform the operation ip = chooseClientIP({ 'uuid': uuid, 'fqdn': fqdn, 'shortname': shortname, 'ips': ips, 'macs': macs, 'netmasks': netmasks }) if not ip or not NetUtils.is_ipv4_format(ip): logging.getLogger().warn("Ivalid IP address format: '%s'" % str(ip)) return fail(False) client = { 'host': ip, 'chosen_ip': ip, 'uuid': uuid, 'shortname': shortname, 'ip': ips, 'macs': macs, 'protocol': 'ssh' } client['client_check'] = getClientCheck(client) client['server_check'] = getServerCheck(client) client['action'] = getAnnounceCheck('download') return self.call_method('download_file', client, path, bwlimit)
def _host_detect(self, host): """ Network address detect callback. Invoked by correct IP address of machine. @param host: IP address @type host: str @return: network address @rtype: str """ if host : self.host = host for pref_net_ip, pref_netmask in self.config.preferred_network : if NetUtils.on_same_network(host, pref_net_ip, pref_netmask): return pref_net_ip if len(self.config.preferred_network) > 0 : self.logger.debug("Circuit #%s: network detect failed, assigned the first of scheduler" % (self.id)) (pref_net_ip, pref_netmask) = self.config.preferred_network[0] return pref_net_ip else: self.logger.warn("Circuit #%s: IP address detect failed" % (self.id)) if len(self.config.preferred_network) > 0 : self.logger.debug("Circuit #%s: network detect failed, assigned the first of scheduler" % (self.id)) (pref_net_ip, pref_netmask) = self.config.preferred_network[0] return pref_net_ip
def test04_on_same_network_false(self): """ test of co-existence in the same network""" ip = "10.1.22.5" network = "192.168.0.0" netmask = "255.255.255.0" result = NetUtils.on_same_network(ip, network, netmask) self.assertEqual(result, False)
def test03_on_same_network_true(self): """ test of co-existence in the same network""" ip = "10.1.22.5" network = "10.1.22.0" netmask = "255.255.255.0" result = NetUtils.on_same_network(ip, network, netmask) self.assertEqual(result, True)
def establish_proxy(self, uuid, fqdn, shortname, ips, macs, netmasks, requestor_ip, requested_port): def _finalize(result): if type(result) == list: # got expected struct (launcher, host, port, key) = result if key == '-': # Key not provided => TCP Proxy logging.getLogger().info( 'VNC Proxy: launcher "%s" created new TCP Proxy to "%s:%s"' % (launcher, host, str(port))) else: # Key provided => Websocket Proxy logging.getLogger().info( 'VNC Proxy: launcher "%s" created new WebSocket Proxy to "%s:%s" with key "%s"' % (str(launcher), str(host), str(port), str(key))) if host == '': host = SchedulerConfig().launchers[launcher]['host'] return (host, port, key) else: return False # choose a way to perform the operation ip = chooseClientIP({ 'uuid': uuid, 'fqdn': fqdn, 'shortname': shortname, 'ips': ips, 'macs': macs, 'netmasks': netmasks }) if not ip or not NetUtils.is_ipv4_format(ip): logging.getLogger().warn("Ivalid IP address format: '%s'" % str(ip)) return fail(False) client = { 'host': ip, 'chosen_ip': ip, 'uuid': uuid, 'shortname': shortname, 'ip': ips, 'macs': macs, 'protocol': 'tcpsproxy' } client['client_check'] = getClientCheck(client) client['server_check'] = getServerCheck(client) client['action'] = getAnnounceCheck('vnc') d = self.call_method('tcp_sproxy', client, requestor_ip, requested_port) d.addCallback(_finalize) @d.addErrback def _eb(failure): logging.getLogger().warn("VNC proxy open failed: %s" % str(failure)) return d
def test05_has_enough_info_true(self): """ test of info complexity """ iface_info = {"ip": "192.168.1.25", "mac": "00:55:47:f0:d4", "netmask": "255.255.255.0" , "gateway" : "192.168.1.1"} result = NetUtils.has_enough_info(iface_info) self.assertEqual(result, True)
def test06_has_enough_info_false(self): """ test of info complexity """ iface_info = {"ip": "192.168.1.25", "mac": None, "netmask": "255.255.255.0" , "gateway" : "192.168.1.1"} result = NetUtils.has_enough_info(iface_info) self.assertEqual(result, False)
def test06_has_enough_info_false(self): """ test of info complexity """ iface_info = { "ip": "192.168.1.25", "mac": None, "netmask": "255.255.255.0", "gateway": "192.168.1.1" } result = NetUtils.has_enough_info(iface_info) self.assertEqual(result, False)
def test05_has_enough_info_true(self): """ test of info complexity """ iface_info = { "ip": "192.168.1.25", "mac": "00:55:47:f0:d4", "netmask": "255.255.255.0", "gateway": "192.168.1.1" } result = NetUtils.has_enough_info(iface_info) self.assertEqual(result, True)
def get_netmask(self, networks, from_ip): for name, ip, mac, netmask in networks: if ip.startswith("127"): # exclude the loopack continue if ip == from_ip: if NetUtils.netmask_validate(netmask): return netmask else: return "255.0.0.0" else: return "255.0.0.0"
def chooseClientInfo(target): ips = target.getIps() if len(ips) > 0: # if at least one element from list of IPs is IP format if any([NetUtils.is_ipv4_format(ip) for ip in ips]): host_dict = { "uuid": target.getUUID(), "fqdn": target.getFQDN(), "shortname": target.getShortName(), "ips": ips, "macs": target.getMacs(), "netmasks": target.getNetmasks(), } return chooseClientIP(host_dict) return None
def allocate_port_couple(): """ Looking for two free ports to establish SSH proxy. @return: two free ports @rtype: list """ ret_ports = [] port_range = range(LauncherConfig().tcp_sproxy_port_range_start + 1, LauncherConfig().tcp_sproxy_port_range_end + 1) for port in port_range : if NetUtils.is_port_free(port): ret_ports.append(port) if len(ret_ports) == 2 : logging.getLogger().debug("Allocated ports to build TCP SSH Proxy: (%d:%d)" % tuple(ret_ports)) return ret_ports
def downloadFile(self, uuid, fqdn, shortname, ips, macs, netmasks, path, bwlimit): # choose a way to perform the operation ip = chooseClientIP({'uuid': uuid, 'fqdn': fqdn, 'shortname': shortname, 'ips': ips, 'macs': macs, 'netmasks': netmasks }) if not ip or not NetUtils.is_ipv4_format(ip): logging.getLogger().warn("Ivalid IP address format: '%s'" % str(ip)) return fail(False) client = {'host': ip, 'chosen_ip': ip, 'uuid': uuid, 'shortname': shortname, 'ip': ips, 'macs': macs, 'protocol': 'ssh' } client['client_check'] = getClientCheck(client) client['server_check'] = getServerCheck(client) client['action'] = getAnnounceCheck('download') return self.call_method('download_file', client, path, bwlimit)
def get_client(self, announce): client_group = "" if self.host : for pref_net_ip, pref_netmask in self.config.preferred_network : if NetUtils.on_same_network(self.host, pref_net_ip, pref_netmask): client_group = pref_net_ip break else : if len(self.config.preferred_network) > 0 : (pref_net_ip, pref_netmask) = self.config.preferred_network[0] client_group = pref_net_ip return {'host': self.host, 'uuid': self.target.getUUID(), 'maxbw': self.cmd.maxbw, 'protocol': 'ssh', 'client_check': getClientCheck(self.target), 'server_check': getServerCheck(self.target), 'action': getAnnounceCheck(announce), 'group': client_group }
def test01_get_netmask(self): """ test of getting of netmask """ netmask, gateway = NetUtils.get_netmask_and_gateway() self.assertEqual(self.my_nm, netmask)
def test07_is_ipv4_format(self): """ test of ipv4 format""" ip = "192.168.1.25" result = NetUtils.is_ipv4_format(ip) self.assertTrue(result)
def test08_is_not_ipv4_format(self): """ test of ipv4 format""" ip = "150.150.154.aaa" result = NetUtils.is_ipv4_format(ip) self.assertFalse(result)
def test04_on_same_network_false(self): """ test of co-existence in the same network""" ip1 = "10.1.20.5" ip2 = "10.1.41.2" result = NetUtils.on_same_network(ip1, ip2) self.assertEqual(result, False)
def establish_proxy(self, uuid, fqdn, shortname, ips, macs, netmasks, requestor_ip, requested_port): def _finalize(result): if type(result) == list: # got expected struct (launcher, host, port, key) = result if key == '-': # Key not provided => TCP Proxy logging.getLogger().info( 'VNC Proxy: launcher "%s" created new TCP Proxy to "%s:%s"' % (launcher, host, str(port))) else: # Key provided => Websocket Proxy logging.getLogger().info( 'VNC Proxy: launcher "%s" created new WebSocket Proxy to "%s:%s" with key "%s"' % (str(launcher), str(host), str(port), str(key))) if host == '': host = SchedulerConfig().launchers[launcher]['host'] return (host, port, key) else: return False # choose a way to perform the operation ip = chooseClientIP({'uuid': uuid, 'fqdn': fqdn, 'shortname': shortname, 'ips': ips, 'macs': macs, 'netmasks': netmasks }) if not ip or not NetUtils.is_ipv4_format(ip): logging.getLogger().warn("Ivalid IP address format: '%s'" % str(ip)) return fail(False) client = {'host': ip, 'chosen_ip': ip, 'uuid': uuid, 'shortname': shortname, 'ip': ips, 'macs': macs, 'protocol': 'tcpsproxy' } client['client_check'] = getClientCheck(client) client['server_check'] = getServerCheck(client) client['action'] = getAnnounceCheck('vnc') d = self.call_method('tcp_sproxy', client, requestor_ip, requested_port) d.addCallback(_finalize) @d.addErrback def _eb(failure): logging.getLogger().warn("VNC proxy open failed: %s" % str(failure)) return d
def test09_netmask_validate_false(self): """ correct format of netmask """ nmask = "200.255.128.0" result = NetUtils.netmask_validate(nmask) self.assertFalse(result)
def test01_get_netmask(self): """ test of getting of netmask """ netmask = NetUtils.get_netmask() self.assertEqual(self.my_nm, netmask)
def test02_get_gateway(self): """ test of getting of gateway """ netmask, gateway = NetUtils.get_netmask_and_gateway() self.assertEqual(self.my_gw, gateway)
def test03_on_same_network_true(self): """ test of co-existence in the same network""" ip1 = "10.1.22.5" ip2 = "10.1.22.211" result = NetUtils.on_same_network(ip1, ip2) self.assertEqual(result, True)