Exemplo n.º 1
0
def main():
    """
    Main

    Returns
    -------
        int
            0 on success;
            1 on failure.
    """
    args = parse_args()

    if os.geteuid() != 0:
        _logger.error("You must run this program with root privileges")
        return 1

    if args.create_vnic:
        if args.add_private_ip:
            _logger.error(
                "Cannot use --create-vnic and --add-private-ip at the "
                "same time")
            return 1
        try:
            do_create_vnic(args)
        except Exception as e:
            _logger.debug('cannot create the VNIC', exc_info=True)
            _logger.error('cannot create the VNIC: %s' % str(e))
            return 1
    try:
        vnic_utils = VNICUtils()
        vnic_info = vnic_utils.get_vnic_info()[1]
    except Exception as e:
        _logger.warning("Cannot get vNIC information: %s" % str(e))
        _logger.debug('OCI SDK Error', exc_info=True)
        return 1

    shape = None
    if args.detach_vnic:
        try:
            shape = do_detach_vnic(args, vnic_utils)
            time.sleep(10)
        except Exception as e:
            _logger.error('Cannot detach vNIC: %s' % str(e))
            return 1

    if args.ns:
        vnic_utils.set_namespace(args.ns)

    if args.sshd:
        vnic_utils.set_sshd(args.sshd)

    excludes = vnic_info['exclude']
    if excludes is None:
        excludes = []

    ret = 0
    out = ""
    if args.add_private_ip:
        try:
            (ip, vnic_id) = do_add_private_ip(vnic_utils, args)
            _logger.info("IP %s has been assigned to vnic %s." % (ip, vnic_id))
        except Exception as e:
            _logger.error('failed ot add private ip: %s' % str(e))
            return 1

    elif args.del_private_ip:
        try:
            (ret, out) = do_del_private_ip(vnic_utils, args)
        except Exception as e:
            _logger.error('failed ot delete private ip: %s' % str(e))
            return 1

    if args.exclude:
        for exc in args.exclude:
            if args.include and exc in args.include:
                _logger.error(
                    "Invalid arguments: item both included and excluded: %s"
                    % exc)
            vnic_utils.exclude(exc)
        excludes = vnic_info['exclude']
    if args.include:
        for inc in args.include:
            vnic_utils.include(inc)
        excludes = vnic_info['exclude']

    if excludes and not args.quiet:
        if _logger.isEnabledFor(logging.INFO):
            _logger.info(
                "Info: Addresses excluded from automatic configuration: %s" %
                ", ".join(excludes))

    if args.auto or args.create_vnic or args.add_private_ip:
        (ret, out) = vnic_utils.auto_config(args.sec_ip)
    elif args.detach_vnic and shape and shape.startswith("BM"):
        (ret, out) = vnic_utils.auto_config(args.sec_ip)
    elif args.deconfigure:
        (ret, out) = vnic_utils.auto_deconfig(args.sec_ip)

    if ret:
        _logger.error("Failed to execute the VNIC configuration script.")
    if out:
        _logger.debug(str(out))

    if not args.quiet or args.show:
        do_show_network_config(vnic_utils)

    return 0
Exemplo n.º 2
0
def main():
    """
    Main

    Returns
    -------
        int
            0 on success;
            1 on failure.
    """
    parser = get_arg_parser()
    args = parser.parse_args()

    if args.quiet:
        _logger.setLevel(logging.WARNING)

    if not args.command:
        parser.print_help()
        return 1

    if args.command == 'usage':
        parser.print_help()
        return 0

    if os.geteuid() != 0:
        _logger.error("You must run this program with root privileges")
        return 1

    try:
        vnic_utils = VNICUtils()
    except IOError as e:
        _logger.warning("Cannot get vNIC information: %s" % str(e))
        _logger.debug('Cannot get vNIC information', exc_info=True)
        return 1

    if 'exclude' in args and args.exclude:
        for exc in args.exclude:
            vnic_utils.exclude(exc)

    if 'include' in args and args.include:
        for inc in args.include:
            vnic_utils.include(inc)


    if _logger.isEnabledFor(logging.INFO) and not args.quiet:
        excludes = vnic_utils.get_vnic_info()[1]['exclude']
        if excludes:
            _logger.info(
                "Info: Addresses excluded from automatic configuration: %s" %
                ", ".join(excludes))


    if args.command == 'show':
        if args.compat_output:
            compat_show_vnics_information()
            compat_show_network_config(vnic_utils)
        else:
            try:
                do_show_information(vnic_utils,args.output_mode, args.details)
            except Exception as e:
                _logger.debug('cannot show  information', exc_info=True)
                _logger.error('cannot show information: %s' % str(e))
                return 1
        return 0

    if args.command == 'show-vnics':
        sess = get_oci_api_session()
        if sess is None:
            _logger.error("Failed to get API session.")
            return 1
        vnics = set()
        _vnics = sess.this_instance().all_vnics()
        if not args.ocid and not args.name and not args.ip_address:
            vnics.update(_vnics)
        else:
            if args.ocid:
                for v in _vnics:
                    if v.get_ocid() == args.ocid:
                        vnics.add(v)
            if args.name:
                for v in _vnics:
                    if v.get_display_name() == args.name:
                        vnics.add(v)
            if args.ip_address:
                for v in _vnics:
                    if v.get_private_ip() == args.ip_address:
                        vnics.add(v)
        do_show_vnics_information(vnics,args.output_mode, args.details)

        return 0


    if args.command == 'attach-vnic':
        if 'nic_index' in args and args.nic_index != 0:
            if not get_oci_api_session().this_shape().startswith("BM"):
                _logger.error('--nic-index option ignored when not runnig on Bare Metal type of shape')
                return 1
        try:
            do_create_vnic(args)
        except Exception as e:
            _logger.debug('cannot create the VNIC', exc_info=True)
            _logger.error('cannot create the VNIC: %s' % str(e))
            return 1
        # apply config of newly created vnic
        vnic_utils.auto_config(None)


    if args.command == 'detach-vnic':
        try:
            do_detach_vnic(args, vnic_utils)
        except Exception as e:
            _logger.debug('cannot detach VNIC', exc_info=True)
            _logger.error('cannot detach vNIC: %s' % str(e))
            return 1
        # if we are here session is alive: no check
        if get_oci_api_session().this_shape().startswith("BM"):
            # in runnning on BM some cleanup is needed on the host
            vnic_utils.auto_config(None)


    if args.command == "add-secondary-addr":
        try:
            (ip, vnic_id) = do_add_private_ip(vnic_utils, args)
            _logger.info("IP %s has been assigned to vnic %s." % (ip, vnic_id))
        except Exception as e:
            _logger.error('failed to add private ip: %s' % str(e))
            return 1


    if args.command == "remove-secondary-addr":
        try:
            (ret, out) = do_del_private_ip(vnic_utils, args)
            if ret != 0:
                raise Exception('cannot deleet ip: %s' % out)
        except Exception as e:
            _logger.error('failed to delete private ip: %s' % str(e))
            return 1


    if 'namespace' in args and args.namespace:
        vnic_utils.set_namespace(args.namespace)

    if 'start_sshd' in args and args.start_sshd:
        vnic_utils.set_sshd(args.start_sshd)

    if args.command == 'configure':
        vnic_utils.auto_config(args.sec_ip)

    if args.command == 'unconfigure':
        vnic_utils.auto_deconfig(args.sec_ip)

    return 0
def main():
    """
    Main

    Returns
    -------
        int
            0 on success;
            1 on failure.
    """
    parser = get_arg_parser()
    args = parser.parse_args()

    if args.quiet:
        _logger.setLevel(logging.WARNING)

    if not args.command:
        parser.print_help()
        return 1

    if args.command == 'usage':
        parser.print_help()
        return 0

    if os.geteuid() != 0:
        _logger.error("You must run this program with root privileges")
        return 1

    vnic_utils = VNICUtils()

    if 'exclude' in args and args.exclude:
        for exc in args.exclude:
            vnic_utils.exclude(exc)

    if 'include' in args and args.include:
        for inc in args.include:
            vnic_utils.include(inc)

    if args.command == 'show':
        #
        # for compatibility mode, oci-network-config show should provide the same output as oci-network-config --show;
        # if output-mode is specified, compatiblity requirement is dropped.
        showerror = False
        if args.compat_output:
            compat_show_vnics_information()
        else:
            try:
                do_show_information(vnic_utils, args.output_mode, args.details)
            except Exception as e:
                _logger.debug('Cannot show information', exc_info=True)
                _logger.error('Cannot show information: %s', str(e))
                showerror = True
        if args.output_mode == 'table':
            show_network_config(vnic_utils)
        return 1 if showerror else 0

    if args.command == 'show-vnics':
        sess = get_oci_api_session()
        if sess is None:
            _logger.error("Failed to get API session.")
            return 1
        vnics = set()
        _vnics = sess.this_instance().all_vnics()
        if not args.ocid and not args.name and not args.ip_address:
            vnics.update(_vnics)
        else:
            if args.ocid:
                for v in _vnics:
                    if v.get_ocid() == args.ocid:
                        vnics.add(v)
            if args.name:
                for v in _vnics:
                    if v.get_display_name() == args.name:
                        vnics.add(v)
            if args.ip_address:
                for v in _vnics:
                    if v.get_private_ip() == args.ip_address:
                        vnics.add(v)
        do_show_vnics_information(vnics, args.output_mode, args.details)

        return 0

    if args.command == 'attach-vnic':
        if 'nic_index' in args and args.nic_index != 0:
            if not get_oci_api_session().this_shape().startswith("BM"):
                _logger.error(
                    '--nic-index option ignored when not runnig on Bare Metal type of shape'
                )
                return 1
        try:
            do_create_vnic(args)
        except Exception as e:
            _logger.debug('Cannot create the VNIC', exc_info=True)
            _logger.error('Cannot create the VNIC: %s', str(e))
            return 1
        # apply config of newly created vnic
        time.sleep(25)
        vnic_utils = VNICUtils()
        vnic_utils.auto_config(None)

    if args.command == 'detach-vnic':
        try:
            do_detach_vnic(args)
        except Exception as e:
            _logger.debug('Cannot detach VNIC', exc_info=True, stack_info=True)
            _logger.error('Cannot detach VNIC: %s', str(e))
            return 1
        # if we are here session is alive: no check
        if get_oci_api_session().this_shape().startswith("BM"):
            # in runnning on BM some cleanup is needed on the host
            vnic_utils.auto_config(None)

    if args.command == "add-secondary-addr":
        try:
            (ip, vnic_id) = do_add_private_ip(vnic_utils, args)
            _logger.info("IP %s has been assigned to vnic %s.", ip, vnic_id)
        except Exception as e:
            _logger.error('Failed to add private IP: %s', str(e))
            return 1

    if args.command == "remove-secondary-addr":
        try:
            (ret, out) = do_del_private_ip(vnic_utils, args)
            if ret != 0:
                raise Exception('Cannot delete ip: %s' % out)
        except Exception as e:
            _logger.error('Failed to delete private IP: %s',
                          str(e),
                          stack_info=True)
            return 1

    if 'namespace' in args and args.namespace:
        vnic_utils.set_namespace(args.namespace)

    if 'start_sshd' in args and args.start_sshd:
        vnic_utils.set_sshd(args.start_sshd)

    if args.command == 'configure':
        vnic_utils.auto_config(args.sec_ip)
        _logger.info('Configured ')

    if args.command == 'unconfigure':
        vnic_utils.auto_deconfig(args.sec_ip)

    return 0