Skip to content

Commit

Permalink
Fail gracefully if the blivet module can not be imported.
Browse files Browse the repository at this point in the history
XXX the error messages can be be misleading, as we first try to import
blivet3 and then blivet, so one will see the error from the blivet import
even when then one from blivet3 wold be more relevant (on RHEL 7).
  • Loading branch information
pcahyna committed Aug 5, 2019
1 parent 0630bfd commit cbc451c
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions library/blivet.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
'''

import logging
import traceback

BLIVET_PACKAGE = None

try:
from blivet3 import Blivet
Expand All @@ -88,20 +91,25 @@
from blivet3.util import set_up_logging
BLIVET_PACKAGE = 'blivet3'
except ImportError:
from blivet import Blivet
from blivet.callbacks import callbacks
from blivet.flags import flags as blivet_flags
from blivet.formats import get_format
from blivet.partitioning import do_partitioning
from blivet.size import Size
from blivet.util import set_up_logging
BLIVET_PACKAGE = 'blivet'

from ansible.module_utils.basic import AnsibleModule

blivet_flags.debug = True
set_up_logging()
log = logging.getLogger(BLIVET_PACKAGE + ".ansible")
try:
from blivet import Blivet
from blivet.callbacks import callbacks
from blivet.flags import flags as blivet_flags
from blivet.formats import get_format
from blivet.partitioning import do_partitioning
from blivet.size import Size
from blivet.util import set_up_logging
BLIVET_PACKAGE = 'blivet'
except ImportError:
LIB_IMP_ERR = traceback.format_exc()

from ansible.module_utils.basic import AnsibleModule, missing_required_lib

if BLIVET_PACKAGE:
blivet_flags.debug = True
set_up_logging()
log = logging.getLogger(BLIVET_PACKAGE + ".ansible")

use_partitions = None # create partitions on pool backing device disks?
disklabel_type = None # user-specified disklabel type

Expand Down Expand Up @@ -636,6 +644,9 @@ def run_module():

module = AnsibleModule(argument_spec=module_args,
supports_check_mode=True)
if not BLIVET_PACKAGE:
module.fail_json(msg=missing_required_lib("blivet"),
exception=LIB_IMP_ERR)

if not module.params['pools'] and not module.params['volumes']:
module.exit_json(**result)
Expand Down

0 comments on commit cbc451c

Please sign in to comment.