def get_hostid(): """Get host id from uuid file.""" hostid = "" file_path = "/etc/uuid" if os.path.isfile(file_path): try: with open(file_path, 'r') as fp: hostid = fp.readline() except Exception: LOG.warning("read uuid file fail, try hosts file") hostid = socket.gethostname() else: LOG.warning("uuid file not exist, use hosts file") hostid = socket.gethostname() return hostid
def get_myname(): try: with open('/etc/confluent/cfg/myname', 'r') as f: return f.read().strip() except IOError: myname = socket.gethostname() with open('/etc/confluent/cfg/myname', 'w') as f: f.write(myname) return myname
def testSockName(self): # Testing getsockname() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("0.0.0.0", PORT+1)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. my_ip_addr = socket.gethostbyname(socket.gethostname()) self.assert_(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], PORT+1)
def testSockName(self): # Testing getsockname() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("0.0.0.0", PORT + 1)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. my_ip_addr = socket.gethostbyname(socket.gethostname()) self.assert_(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], PORT + 1)
def get_myname(): if mycachedname[1] > time.time() - 15: return mycachedname[0] try: with open('/etc/confluent/cfg/myname', 'r') as f: mycachedname[0] = f.read().strip() mycachedname[1] = time.time() return mycachedname[0] except IOError: myname = socket.gethostname() with open('/etc/confluent/cfg/myname', 'w') as f: f.write(myname) mycachedname[0] = myname mycachedname[1] = time.time() return myname
def testHostnameRes(self): # Testing hostname resolution mechanisms hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except socket.error: # Probably name lookup wasn't set up right; skip this test return self.assert_(ip.find('.') >= 0, "Error resolving host to ip.") try: hname, aliases, ipaddrs = socket.gethostbyaddr(ip) except socket.error: # Probably a similar problem as above; skip this test return all_host_names = [hostname, hname] + aliases fqhn = socket.getfqdn(ip) if not fqhn in all_host_names: self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
def testHostnameRes(self): # Testing hostname resolution mechanisms hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except socket.error: # Probably name lookup wasn't set up right; skip this test return self.assert_(ip.find('.') >= 0, "Error resolving host to ip.") try: hname, aliases, ipaddrs = socket.gethostbyaddr(ip) except socket.error: # Probably a similar problem as above; skip this test return all_host_names = [hostname, hname] + aliases fqhn = socket.getfqdn(ip) if not fqhn in all_host_names: self.fail( "Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
def __init__(self, component_id=None, queue_hostname=None, queue_username=None, queue_password=None, queue_vhost=None): if not hasattr(self, 'component_type'): raise Exception("Component subclass must define 'component_type' attribute!") self.component_id = component_id self.hostname = socket.gethostname() self.queue_hostname = queue_hostname self.queue_username = queue_username self.queue_password = queue_password self.queue_vhost = queue_vhost if not component_id: self.component_id = self.hostname.split('.')[0] self.stats = collections.defaultdict(int) self.stats['stat_start_time'] = time.time() self.log = logging.getLogger(self.component_id) # Spin up the heartbeat thread self.heartbeat_frequency = HEARTBEAT_FREQUENCY self.heartbeat_thread = eventlet.spawn(self.heartbeat_loop) # Spin up the command thread self.command_thread = eventlet.spawn(self.command_loop) self.allowed_commands = ['registration_request', 'reset_stats'] # Load the previously cached checker state self.state = shelve.open(self.component_id + '.shelf', writeback=True) # Flag to indicate that the component should shut down self.should_shutdown = False # Container for any extra data that we want to send back with the heartbeat self.extra_heartbeat_data = {}
' heat_stack_name will be set to the name of the' ' resource\'s parent stack, heat_path_in_stack will' ' be set to a list of tuples, (stackresourcename,' ' stackname) with list[0] being (None, rootstackname),' ' heat_resource_name will be set to the resource\'s' ' name, and heat_resource_uuid will be set to the' ' resource\'s orchestration id.')), cfg.BoolOpt('encrypt_parameters_and_properties', default=False, help=_('Encrypt template parameters that were marked as' ' hidden and also all the resource properties before' ' storing them in database.'))] rpc_opts = [ cfg.StrOpt('host', default=socket.gethostname(), help=_('Name of the engine node. ' 'This can be an opaque identifier. ' 'It is not necessarily a hostname, FQDN, ' 'or IP address.'))] auth_password_group = cfg.OptGroup('auth_password') auth_password_opts = [ cfg.BoolOpt('multi_cloud', default=False, help=_('Allow orchestration of multiple clouds.')), cfg.ListOpt('allowed_auth_uris', default=[], help=_('Allowed keystone endpoints for auth_uri when ' 'multi_cloud is enabled. At least one endpoint needs ' 'to be specified.'))]
engine_opts = [ cfg.StrOpt('instance_user', default='ec2-user', help='The default user for new instances'), cfg.StrOpt('instance_driver', default='heat.engine.nova', help='Driver to use for controlling instances'), cfg.ListOpt('plugin_dirs', default=['/usr/lib64/heat', '/usr/lib/heat'], help='List of directories to search for Plugins') ] rpc_opts = [ cfg.StrOpt('host', default=socket.gethostname(), help='Name of the engine node. ' 'This can be an opaque identifier.' 'It is not necessarily a hostname, FQDN, or IP address.') ] auth_password_group = cfg.OptGroup('auth_password') auth_password_opts = [ cfg.BoolOpt('multi_cloud', default=False, help=_('Allow orchestration of multiple clouds')), cfg.ListOpt('allowed_auth_uris', default=[], help=_('Allowed targets for auth_uri when multi_cloud is ' 'enabled. If empty, all targets will be allowed.')) ]
def test_file(self): import rfc822 h = urllib2.FileHandler() o = h.parent = MockOpener() TESTFN = test_support.TESTFN urlpath = sanepathname2url(os.path.abspath(TESTFN)) towrite = "hello, world\n" urls = [ "file://localhost%s" % urlpath, "file://%s" % urlpath, "file://%s%s" % (socket.gethostbyname('localhost'), urlpath), ] try: localaddr = socket.gethostbyname(socket.gethostname()) except socket.gaierror: localaddr = '' if localaddr: urls.append("file://%s%s" % (localaddr, urlpath)) for url in urls: f = open(TESTFN, "wb") try: try: f.write(towrite) finally: f.close() r = h.file_open(Request(url)) try: data = r.read() headers = r.info() newurl = r.geturl() finally: r.close() stats = os.stat(TESTFN) modified = rfc822.formatdate(stats.st_mtime) finally: os.remove(TESTFN) self.assertEqual(data, towrite) self.assertEqual(headers["Content-type"], "text/plain") self.assertEqual(headers["Content-length"], "13") self.assertEqual(headers["Last-modified"], modified) for url in [ "file://localhost:80%s" % urlpath, # XXXX bug: these fail with socket.gaierror, should be URLError ## "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), ## os.getcwd(), TESTFN), ## "file://somerandomhost.ontheinternet.com%s/%s" % ## (os.getcwd(), TESTFN), ]: try: f = open(TESTFN, "wb") try: f.write(towrite) finally: f.close() self.assertRaises(urllib2.URLError, h.file_open, Request(url)) finally: os.remove(TESTFN) h = urllib2.FileHandler() o = h.parent = MockOpener() # XXXX why does // mean ftp (and /// mean not ftp!), and where # is file: scheme specified? I think this is really a bug, and # what was intended was to distinguish between URLs like: # file:/blah.txt (a file) # file://localhost/blah.txt (a file) # file:///blah.txt (a file) # file://ftp.example.com/blah.txt (an ftp URL) for url, ftp in [ ("file://ftp.example.com//foo.txt", True), ("file://ftp.example.com///foo.txt", False), # XXXX bug: fails with OSError, should be URLError ("file://ftp.example.com/foo.txt", False), ]: req = Request(url) try: h.file_open(req) # XXXX remove OSError when bug fixed except (urllib2.URLError, OSError): self.assert_(not ftp) else: self.assert_(o.req is req) self.assertEqual(req.type, "ftp")
from validator import version socket_opts = [ cfg.IntOpt('tcp_keepidle', default=600, help="Sets the value of TCP_KEEPIDLE in seconds for each " "server socket. Not supported on OS X."), ] CONF = cfg.CONF CONF.register_opts(socket_opts) LOG = logging.getLogger(__name__) # Local ip/port binding options bind_opts = [ cfg.StrOpt('bind_host', default=socket.gethostname(), help=_('Name of the engine node.')), cfg.StrOpt('bind_port', default=4041, help=_('Listening port of the engine node.')), ] CONF.register_cli_opts(bind_opts) # Paste/Deploy server options paste_deploy_group = cfg.OptGroup('paste_deploy') paste_deploy_opts = [ cfg.StrOpt('config_file', default="api-paste.ini", help=_("The API config file to use.")), cfg.StrOpt('flavor', help=_("The flavor to use.")) ] CONF.register_group(paste_deploy_group) CONF.register_opts(paste_deploy_opts, group='paste_deploy')
def get_hostname(): return socket.gethostname()
def thishost(): """Return the IP address of the current host.""" global _thishost if _thishost is None: _thishost = socket.gethostbyname(socket.gethostname()) return _thishost