Skip to content

Commit

Permalink
Add wait_for_exists, add cronjob restype, fix stdout/stderr processing (
Browse files Browse the repository at this point in the history
  • Loading branch information
bsquizz committed Oct 22, 2020
1 parent 650d149 commit 79b7455
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions ocdeployer/utils.py
Expand Up @@ -21,6 +21,8 @@
# Resource types and their cli shortcuts
# Mostly listed here: https://docs.openshift.com/online/cli_reference/basic_cli_operations.html
SHORTCUTS = {
"configmap": "cm",
"cronjob": "cj",
"build": None,
"buildconfig": "bc",
"daemonset": "ds",
Expand All @@ -42,7 +44,6 @@
"statefulset": "sts",
"persistentvolume": "pv",
"persistentvolumeclaim": "pvc",
"configmap": "cm",
"replicaset": "rs",
"route": None,
}
Expand Down Expand Up @@ -252,9 +253,21 @@ def _out_line_handler(line, _, process):
except ErrorReturnCode as err:
# Sometimes stdout/stderr is empty in the exception even though we appended
# data in the callback. Perhaps buffers are not being flushed ... so just
# set the out lines/err lines we captured on the Exception before re-raising it
# set the out lines/err lines we captured on the Exception before re-raising it by
# re-init'ing the err and causing it to rebuild its message template.
#
# see https://github.com/amoffat/sh/blob/master/sh.py#L381
err.__init__(
full_cmd=err.full_cmd,
stdout="\n".join(out_lines).encode(),
stderr="\n".join(err_lines).encode(),
truncate=err.truncate,
)

# Make these plain strings for easier exception handling
err.stdout = "\n".join(out_lines)
err.stderr = "\n".join(err_lines)

last_err = err

# Ignore warnings that are printed to stderr in our error analysis
Expand Down Expand Up @@ -514,6 +527,17 @@ def _ready():
)


def wait_for_exists(restype, name, timeout=300):
restype = parse_restype(restype)
key = "{}/{}".format(SHORTCUTS.get(restype) or restype, name)
log.info("[%s] waiting up to %dsec for resource to exist", key, timeout)

def _exists():
return get_json(restype, name) is not {}

wait_for(_exists, timeout=timeout, delay=5, message="wait for '{}' to exist").format(key)


def wait_for_ready(restype, name, timeout=300, exit_on_err=False, _result_dict=None):
"""
Wait {timeout} for resource to be complete/ready/active.
Expand Down

0 comments on commit 79b7455

Please sign in to comment.