nova-manage and libvirt_type flag

Asked by Armando Migliaccio

Is there any reason why the flag libvirt_type is declared on nova-manage?

flags.DECLARE('libvirt_type', 'nova.virt.libvirt.connection')

Can't we do without? I see no reason we couldn't just use FLAGS.libvirt_type and rely on the flag file definition.

Thanks,
Armando

Question information

Language:
English Edit question
Status:
Solved
For:
OpenStack Compute (nova) Edit question
Assignee:
No assignee Edit question
Solved by:
Mark McLoughlin
Solved:
Last query:
Last reply:
Revision history for this message
Mark McLoughlin (markmc) said :
#1

bin/nova-manage uses libvirt_type here:

        if (FLAGS.connection_type != 'libvirt' or
           (FLAGS.connection_type == 'libvirt' and
            FLAGS.libvirt_type not in ['kvm', 'qemu'])):
            msg = _('Only KVM and QEmu are supported for now. Sorry!')
            raise exception.Error(msg)

in order for --libvirt_type to be parsed from the command line or the contents of the flagfile, the option has to be registered before it is referenced in the code above

the option is registered in nova/virt/libvirt/connection.py:

      cfg.StrOpt('libvirt_type',

  FLAGS = flags.FLAGS
  FLAGS.add_options(libvirt_opts)

i.e. you need to import nova.virt.libvirt.connection in order for the option to be registered

Rather than import the module into the namespace, we use flags.DECLARE() to do this - all it does is import the module and check that the option has been registered:

  def DECLARE(name, module_string, flag_values=FLAGS):
      if module_string not in sys.modules:
          __import__(module_string, globals(), locals())
      if name not in flag_values:
          raise UnrecognizedFlag('%s not defined by %s' % (name, module_string))

We don't need to declare options like connection_type because that is registered when we import the nova.flags module

Revision history for this message
Armando Migliaccio (armando-migliaccio) said :
#2

Hi Mark,

thanks for the exhaustive reply! I was asking because I believe that nova-manage shouldn't be loading the entire libvirt module, just for retrieving a simply flag. In principle, the tool should be agnostic to underlying implementation details (e.g. hypervisors, networking etc), but I can see why you need it in this case.

Besides, if I don't have libvirt installed on my box (e.g. because I use xenserver/xpc or vmware) and I see these all sorts of logging traces about libvirt:

No handlers could be found for logger "nova.virt.libvirt.firewall"
 WARNING Libvirt module could not be loaded. NWFilterFirewall will not work correctly.
...

I wonder if this should be avoided.

Revision history for this message
Best Mark McLoughlin (markmc) said :
#3

"nova-manage prints libvirt related warnings if libvirt isn't installed" is certainly a valid bug IMHO

This would work:

       def is_libvirt_kvm_or_qemu():
           if FLAGS.connection_type != 'libvirt':
               return False

           flags.DECLARE('libvirt_type', 'nova.virt.libvirt.connection')

           return FLAGS.libvirt_type in ['kvm', 'qemu']

        if not is_libvirt_kvm_or_qermu():
            msg = _('Only KVM and QEmu are supported for now. Sorry!')
            raise exception.Error(msg)

Revision history for this message
Armando Migliaccio (armando-migliaccio) said :
#4

Thanks Mark McLoughlin, that solved my question.