def test_parse_ip(self): """Should return an IP when the given text contains one IPv4""" text = "127.0.0.1" self.assertEquals(lib.parse_ip(text), "127.0.0.1") text = "blabla(127.0.0.1)sdfasdf" self.assertEquals(lib.parse_ip(text), "127.0.0.1") text = "\nblabla 216.34.94.184 sdfasdf" self.assertEquals(lib.parse_ip(text), "216.34.94.184") text = "216.34.94" self.assertEquals(lib.parse_ip(text), None)
def get_ips(): """Ping all nodes and update their 'ipaddress' field""" import subprocess for node in lib.get_nodes(): # For each node, ping the hostname env.host_string = node['name'] proc = subprocess.Popen(['ping', '-c', '1', node['name']], stdout=subprocess.PIPE, stderr=subprocess.PIPE) resp, error = proc.communicate() if not error: # Split output into lines and parse the first line to get the IP ip = lib.parse_ip(resp.split("\n")[0]) if not ip: print "Warning: could not get IP address from node {0}".format( node['name']) continue print "Node {0} has IP {1}".format(node['name'], ip) # Update with the ipaddress field in the corresponding node.json del node['name'] node['ipaddress'] = ip os.remove(chef.save_config(node, ip)) else: print "Warning: could not resolve node {0}".format(node['name'])