示例#1
0
def hostname():
    """
    Get the hostname from
    - config
    - unix internals
    - ec2
    """
    result = None

    config = context.app_config
    hostname_from_config = config['credentials']['hostname']
    if hostname_from_config and is_valid_hostname(hostname_from_config):
        result = hostname_from_config

    # then move on to os-specific detection
    if result is None:
        def _get_hostname_unix():
            try:
                # fqdn
                out, err = subp.call('/bin/hostname -f')
                return out[0]
            except Exception:
                return None

        if os_name() in ['mac', 'freebsd', 'linux', 'solaris']:
            unix_hostname = _get_hostname_unix()
            if unix_hostname and is_valid_hostname(unix_hostname):
                result = unix_hostname

    # if its ec2 default hostname, try to get instance_id
    if result is not None and True in [result.lower().startswith(p) for p in [u'ip-', u'domu']]:
        instance_id = AmazonEC2.instance_id()
        if instance_id:
            result = instance_id

    # fall back on socket.gethostname()
    if result is None:
        try:
            socket_hostname = socket.gethostname()
        except socket.error:
            socket_hostname = None
        if socket_hostname and is_valid_hostname(socket_hostname):
            result = socket_hostname

    if result is None:
        raise AmplifyCriticalException(
            message='Unable to determine host name. Define it in the config file'
        )
    else:
        return result
示例#2
0
    def __init__(self, **kwargs):
        super(SystemCommonMetaCollector, self).__init__(**kwargs)

        self.uuid = self.object.data['uuid']
        self.hostname = self.object.data['hostname']
        self.ec2_metadata = AmazonEC2.read_meta()