Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handle_exception(task_id, exception, traceback):
node_name, bundle_name, item_id = task_id.split(":", 2)
io.progress_advance()
if isinstance(exception, NotImplementedError):
io.stdout(_("{x} {node} {bundle} {item} (does not support verify)").format(
bundle=bold(bundle_name),
item=item_id,
node=bold(node_name),
x=cyan("?"),
))
else:
# Unlike with `bw apply`, it is OK for `bw verify` to encounter
# exceptions when getting an item's status. `bw verify` doesn't
# care about dependencies and therefore cannot know that looking
# up a database user requires the database to be installed in
# the first place.
io.debug("exception while verifying {}:".format(task_id))
io.debug(traceback)
io.debug(repr(exception))
io.stdout(_("{x} {node} {bundle} {item} (unable to get status, check --debug for details)").format(
bundle=bold(bundle_name),
item=item_id,
def _test(self):
with io.job(_("{node} {bundle} {item}").format(
bundle=bold(self.bundle.name),
item=self.id,
node=bold(self.node.name),
)):
if self._faults_missing_for_attributes:
self._raise_for_faults()
return self.test()
def format_item_result(result, node, bundle, item, interactive=False, details=None):
if details is True:
details_text = "({})".format(_("create"))
elif details is False:
details_text = "({})".format(_("remove"))
elif details is None:
details_text = ""
elif result == Item.STATUS_SKIPPED:
details_text = "({})".format(Item.SKIP_REASON_DESC[details])
else:
details_text = "({})".format(", ".join(sorted(details)))
if result == Item.STATUS_FAILED:
return "{x} {node} {bundle} {item} {status} {details}".format(
bundle=bold(bundle),
details=details_text,
item=item,
node=bold(node),
status=red(_("failed")),
x=bold(red("✘")),
)
elif result == Item.STATUS_ACTION_SUCCEEDED:
return "{x} {node} {bundle} {item} {status}".format(
bundle=bold(bundle),
item=item,
node=bold(node),
status=green(_("succeeded")),
x=bold(green("✓")),
)
elif result == Item.STATUS_SKIPPED:
return "{x} {node} {bundle} {item} {status} {details}".format(
else:
details_text = ", ".join(sorted(item_status.display_keys_to_fix))
io.stderr("{x} {node} {bundle} {item} ({details})".format(
bundle=bold(bundle_name),
details=details_text,
item=item_id,
node=bold(node_name),
x=red("✘"),
))
return False
else:
if show_all:
io.stdout("{x} {node} {bundle} {item}".format(
bundle=bold(bundle_name),
item=item_id,
node=bold(node_name),
x=green("✓"),
))
return True
)
elif result == Item.STATUS_ACTION_SUCCEEDED:
return "{x} {node} {bundle} {item} {status}".format(
bundle=bold(bundle),
item=item,
node=bold(node),
status=green(_("succeeded")),
x=bold(green("✓")),
)
elif result == Item.STATUS_SKIPPED:
return "{x} {node} {bundle} {item} {status} {details}".format(
bundle=bold(bundle),
details=details_text,
item=item,
node=bold(node),
x=bold(yellow("»")),
status=yellow(_("skipped")),
)
elif result == Item.STATUS_FIXED:
return "{x} {node} {bundle} {item} {status} {details}".format(
bundle=bold(bundle),
details=details_text,
item=item,
node=bold(node),
x=bold(green("✓")),
status=green(_("fixed")),
)
def stats_summary(results, totals, total_duration):
rows = [[
bold(_("node")),
_("items"),
_("OK"),
green(_("fixed")),
yellow(_("skipped")),
red(_("failed")),
_("time"),
], ROW_SEPARATOR]
for result in results:
rows.append([
result.node_name,
str(result.total),
str(result.correct),
green_unless_zero(result.fixed),
yellow_unless_zero(result.skipped),
red_unless_zero(result.failed),
try:
progress = (self.progress / float(self.progress_total))
elapsed = datetime.utcnow() - self.progress_start
remaining = elapsed / progress - elapsed
except ZeroDivisionError:
pass
else:
if table:
table.append(ROW_SEPARATOR)
table.extend([
[bold(_("Progress")), "{:.1f}%".format(progress * 100)],
ROW_SEPARATOR,
[bold(_("Elapsed")), format_duration(elapsed)],
ROW_SEPARATOR,
[
bold(_("Remaining")),
_("{} (estimate based on progress)").format(format_duration(remaining))
],
])
output = blue("i") + "\n"
if table:
for line in render_table(table):
output += ("{x} {line}\n".format(x=blue("i"), line=line))
else:
output += _("{x} No progress info available at this time.\n").format(x=blue("i"))
io.stderr(output + blue("i"))
def run(self):
if self.attributes['data_stdin'] is not None:
data_stdin = self.attributes['data_stdin']
# Allow users to use either a string/unicode object or raw
# bytes -- or Faults.
if isinstance(data_stdin, Fault):
data_stdin = data_stdin.value
if type(data_stdin) is not bytes:
data_stdin = data_stdin.encode('UTF-8')
else:
data_stdin = None
with io.job(_("{node} {bundle} {item}").format(
bundle=bold(self.bundle.name),
item=self.id,
node=bold(self.node.name),
)):
result = self.bundle.node.run(
self.attributes['command'],
data_stdin=data_stdin,
may_fail=True,
)
if self.attributes['expected_return_code'] is not None and \
not result.return_code == self.attributes['expected_return_code']:
raise ActionFailure(_("wrong return code: {}").format(result.return_code))
if self.attributes['expected_stderr'] is not None and \
result.stderr_text != self.attributes['expected_stderr']:
raise ActionFailure(_("wrong stderr"))
if self.attributes['expected_stdout'] is not None and \
for health, node_name, items, good, bad, unknown, duration in node_ranking:
rows.append([
node_name,
str(items),
green_unless_zero(good),
red_unless_zero(bad),
cyan_unless_zero(unknown),
"{0:.1f}%".format(health),
format_duration(duration),
])
if len(node_ranking) > 1:
rows.append(ROW_SEPARATOR)
rows.append([
bold(_("total ({} nodes)").format(len(node_stats.keys()))),
str(totals['items']),
green_unless_zero(totals['good']),
red_unless_zero(totals['bad']),
cyan_unless_zero(totals['unknown']),
"{0:.1f}%".format(totals['health']),
format_duration(total_duration),
])
alignments = {
1: 'right',
2: 'right',
3: 'right',
4: 'right',
5: 'right',
6: 'right',
7: 'right',
handle_result=handle_result,
pool_id="lock_show",
workers=args['node_workers'],
)
worker_pool.run()
if errors:
error_summary(errors)
return
rows = [[
bold(_("node")),
bold(_("ID")),
bold(_("created")),
bold(_("expires")),
bold(_("user")),
bold(_("items")),
bold(_("comment")),
], ROW_SEPARATOR]
for node_name, locks in sorted(locks_on_node.items()):
if locks:
first_lock = True
for lock in locks:
lock['formatted_date'] = format_timestamp(lock['date'])
lock['formatted_expiry'] = format_timestamp(lock['expiry'])
first_item = True
for item in lock['items']:
rows.append([
node_name if first_item and first_lock else "",
lock['id'] if first_item else "",
lock['formatted_date'] if first_item else "",