Skip to content

Commit

Permalink
Changes to enable run Single check (#885)
Browse files Browse the repository at this point in the history
Co-authored-by: Jayasimha Raghavan <[email protected]>
  • Loading branch information
jayasimha-raghavan-unskript and Jayasimha Raghavan authored Oct 3, 2023
1 parent 0b7fd9b commit 6b58bd1
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 32 deletions.
86 changes: 86 additions & 0 deletions unskript-ctl/bash_completion_unskript_ctl.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash


_unskript-client-completion() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"


# Find the absolute path of unskript-client.py
local unskript_client_script
unskript_client_script="$(which unskript-client.py)"

if [ -n "$unskript_client_script" ]; then
# Check if the script exists and save check names
/usr/bin/env python "$unskript_client_script" --save-check-names /tmp/checknames.txt
/usr/bin/env python "$unskript_client_script" -h > /tmp/allopts.txt
if [ -f "/tmp/checknames.txt" ]; then
opt2="$(cat /tmp/checknames.txt)"
fi
fi
# Define options with each option on a separate line using newline characters
opts="--list-runbooks --run-runbook --run-checks --display-failed-checks --list-checks --show-audit-trail --display-failed-logs --create-credentials --credential-list --start-debug --stop-debug"

# Completion logic
case "${prev}" in
-rr|--run-runbook)
# Provide completion suggestions for runbook filenames
COMPREPLY=( $(compgen -f -- "${cur}") )
return 0
;;

-rc|--run-checks)
case ${prev} in
-rc|--run-checks)
case ${cur} in
-f)
COMPREPLY=( ${opt2} )
;;
*)
COMPREPLY=( $(compgen -W "--all | --type | --failed | --check" -- "${cur}") )
;;
esac
return 0
;;
*)
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
;;
esac
return 0
;;

-lc|--list-checks)
# Provide completion suggestions for list-checks options
COMPREPLY=( $(compgen -W "--all | --type" -- "${cur}") )
return 0
;;

-sa|--show-audit-trail)
# Provide completion suggestions for show-audit-trail options
COMPREPLY=( $(compgen -W "--all | --type | --execution_id" -- "${cur}") )
return 0
;;

-dl|--display-failed-logs)
# Provide completion suggestions for display-failed-logs options
COMPREPLY=( $(compgen -W "--execution_id <EXECUTION_ID>" -- "${cur}") )
return 0
;;

-cc|--create-credentials)
# Provide completion suggestions for create-credentials options
COMPREPLY=( $(compgen -W "--type creds_file_path" -- "${cur}") )
return 0
;;

*) # Default: Provide completion suggestions for global options
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}" -o nospace) )
return 0
;;
esac
}

# Register the completion function for unskript-client.py
complete -F _unskript-client-completion unskript-ctl.sh
45 changes: 45 additions & 0 deletions unskript-ctl/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,51 @@ def get_checks_by_connector(connector_name: str, full_snippet: bool = False):
db.close()
return list_checks

def get_all_check_names():
"""get_all_check_names This function queries the snippets DB for
checks and returns the Names
:rtype: List containing names of all check
"""
try:
db = DB(CS_DB_PATH)
except Exception as e:
raise e
tm = transaction.TransactionManager()
connection = db.open(tm)
root = connection.root()
cs = root.get('unskript_cs')
list_check_names = []
if cs is None:
raise Exception("Code Snippets Are missing")
for s in cs:
d = s
if d.get('metadata').get('action_is_check') is False:
continue
list_check_names.append(d.get('metadata').get('action_entry_function'))


tm.commit()
del root
connection.close()
db.close()
return list_check_names

def get_check_by_name(name: str):
"""get_check_by_name This function utilizes the function get_check_by_connector
with `all` filter and finds out the check that matches the name and returns
the code snippet that matches the name.
:type name: str
:param name: Check name
:rtype: Returns the Check
"""
all_snippets = get_checks_by_connector('all', True)
snippet_list = [x for x in all_snippets if x.get('metadata').get('action_entry_function') == name]

return snippet_list

def get_creds_by_connector(connector_type: str):
"""get_creds_by_connector This function queries ZoDB and returns the
credential data in the form of a tuple of (cred_name, cred_id).
Expand Down
Loading

0 comments on commit 6b58bd1

Please sign in to comment.