Esempio n. 1
0
def public_ip_func(context, func_logger):
    """
    OCID thread function for refreshing the instance metadata.

    Parameters
    ----------
    context: dict
        THe thread context.
        # GT not used, kept to avoid to break the function call.
    func_logger: logger

    Returns
    -------
    dict
        Dictionary containiong the external IP address.
    """

    try:
        sess = oci_utils.oci_api.OCISession()
        instance = sess.this_instance()
        if instance is None:
            raise Exception('Cannot get instance')
        return {'publicIp': instance.get_public_ip()}
    except Exception as e:
        func_logger.exception('failed to retrieve public ip information: %s' % str(e))

    # fallback
    external_ip = get_ip_info()[1]

    return {'publicIp': external_ip}
Esempio n. 2
0
def main():
    """
    Main

    Returns
    -------
        int
            0 on success, 1 otherwise.
    """
    args = parse_args()

    if args.list_servers:
        # print the list of known STUN servers and exit
        for server in STUN_SERVERS:
            print server
        sys.exit(0)

    external_ip = None
    external_ips = []
    if args.instance_id is None and args.sourceip == '0.0.0.0' and \
       args.stun_server is None:
        try:
            _found = False
            if oci_api.HAVE_OCI_SDK:
                _this_instance = oci_api.OCISession().this_instance()
                _all_p_ips = _this_instance.all_public_ips()
                if len(_all_p_ips) > 0:
                    _found = True
                    external_ips.append(_all_p_ips[0])
            if not _found:
                # fall back to STUN if we did not find any
                _p_ip = get_ip_info(source_ip='0.0.0.0')[1]
                if _p_ip is not None:
                    external_ips.append(_p_ip)
        except Exception as e:
            stun_log.error("%s\n" % e)

    if (external_ip is None or args.all) \
            and (oci_api.HAVE_OCI_SDK or args.instance_id is not None):
        # try the OCI APIs first

        try:
            sess = oci_api.OCISession()
            if args.instance_id is not None:
                inst = sess.get_instance(args.instance_id)
            else:
                inst = sess.this_instance()

            if inst is None:
                stun_log.error("Instance not found: %s\n" % args.instance_id)
                sys.exit(1)
            for vnic in inst.all_vnics():
                external_ip = vnic.get_public_ip()
                if vnic.is_primary() and external_ip not in external_ips:
                    external_ips.insert(0, external_ip)
                else:
                    if external_ip is None:
                        continue
                    if args.all:
                        if external_ip in external_ips:
                            continue
                        external_ips.append(external_ip)
                    else:
                        break
        except OCISDKError:
            if args.instance_id is not None:
                stun_log.error(
                    "The OCI Python SDK must be installed and configured when "
                    "using the --instance-id option.\n")
                sys.exit(1)

    if external_ip is None or args.all:
        # fall back to pystun
        external_ip = get_ip_info(source_ip=args.sourceip,
                                  stun_host=args.stun_server)[1]

        if args.all and external_ip not in external_ips:
            external_ips.append(external_ip)

    if args.all:
        if args.json:
            print json.dumps({'publicIps': external_ips})
        elif args.get:
            print external_ips
        else:
            print "Public IP addresses: "
            print "  Primary public IP: %s " % external_ips[0]
            if len(external_ips) < 2:
                print "  Other public IP(s): None"
                return 0
            print "  Other public IP(s):"
            for ip in external_ips[1:]:
                print "    %s" % ip
        return 0
    elif external_ip is not None:
        if args.json:
            print json.dumps({'publicIp': external_ip})
        elif args.get:
            print external_ip
        else:
            print "Public IP address: %s" % external_ip

        return 0
    else:
        stun_log.info("No public IP address found.\n")
        return 1
Esempio n. 3
0
def main():
    """
    Main

    Returns
    -------
        int
            0 on success, 1 otherwise.
    """
    args = parse_args()

    # deal with deprecated options
    if args.human_readable:
        args.output_mode = 'table'
    if args.get:
        args.output_mode = 'parsable'
    if args.json:
        args.output_mode = 'json'

    if args.list_servers:
        # print the list of known STUN servers and exit
        for server in STUN_SERVERS:
            print(server)
        return 0

    _instance = None
    try:
        sess = oci_api.OCISession()
        if args.instance_id is not None:
            _instance = sess.get_instance(args.instance_id)
        else:
            _instance = sess.this_instance()
    except Exception as e:
        if args.instance_id is not None:
            # user specified a remote instance, there is no fallback to stun
            # we treat this as an error now
            stun_log.error("Error getting information of instance [%s]: %s" %
                           (args.instance_id, str(e)))
            return 1
        # in that case, just issue a debug info
        stun_log.debug("Error getting information of current instance: %s",
                       str(e))

    _all_p_ips = []

    if _instance is None:
        if args.instance_id is not None:
            # user specified a remote instance, there is no fallback to stun
            stun_log.error("Instance not found: %s" % args.instance_id)
            return 1
        # can we really end up here ?
        stun_log.debug("current Instance not found")
        # fall back to pystun
        stun_log.debug('No ip found , fallback to STUN')
        _ip = get_ip_info(source_ip=args.sourceip,
                          stun_host=args.stun_server)[1]
        stun_log.debug('STUN gave us : %s' % _ip)
        if _ip:
            _all_p_ips.append({'ip': _ip})
    else:
        _all_p_ips = [{
            'ip': v.get_public_ip(),
            'vnic_name': v.get_display_name(),
            'vnic_ocid': v.get_ocid()
        } for v in _instance.all_vnics() if v.get_public_ip()]
        stun_log.debug('%s ips retreived from sdk information' %
                       len(_all_p_ips))

    if len(_all_p_ips) == 0:
        # none of the methods give us information
        stun_log.info("No public IP address found.\n")
        return 1

    _display_ip_list(_all_p_ips, args.all, args.output_mode, args.details)
    return 0
def main():
    """
    Main

    Returns
    -------
        int
            0 on success, 1 otherwise.
    """
    args = parse_args()

    if args.list_servers:
        # print the list of known STUN servers and exit
        for server in STUN_SERVERS:
            print(server)
        return(0)

    _instance = None
    try:
        sess = oci_api.OCISession()
        if args.instance_id is not None:
            _instance = sess.get_instance(args.instance_id)
        else:
            _instance = sess.this_instance()
    except Exception as e:
        if args.instance_id is not None:
            # user specified a remote instance, there is no fallback to stun
            # we treat this as an error now
            stun_log.error(
                "Error getting information of instance [%s]: %s" % (args.instance_id, str(e)))
            return(1)
        else:
            # in that case, just issue a debug info
            stun_log.debug(
                "Error getting information of current instance: %s" % str(e))

    _all_p_ips = []

    if _instance is None:
        if args.instance_id is not None:
            # user specified a remote instance, there is no fallback to stun
            stun_log.error(
                "Instance not found: %s" % args.instance_id)
            return(1)
        else:
            # can we really end up here ?
            stun_log.debug(
                "current Instance not found")
    else:
        _all_p_ips = _instance.all_public_ips()
        stun_log.debug('%s ips retreived from sdk information' % len(_all_p_ips))

    if len(_all_p_ips) == 0:
        # fall back to pystun
        stun_log.debug('No ip found , fallback to STUN')
        _ip = get_ip_info(source_ip=args.sourceip,
                          stun_host=args.stun_server)[1]
        stun_log.debug('STUN gave us : %s' % _ip)
        if _ip:
            _all_p_ips.append(_ip)

    if len(_all_p_ips) == 0:
        # none of the methods give us information
        stun_log.info("No public IP address found.\n")
        return 1
    else:
        _display_ip_list(_all_p_ips, args.all, args.json, args.get)
        return 0