Skip to content

Commit 5eddec5

Browse files
[AC] Add new option to ac tool to refresh venv scripts (#2028)
* Function for enabling refresh commands * added refresh command in command list * Handled attached argument format * Handled separate arguments * PR-Enabler/1948/ac script * word correction * added command for specific env as comment * Updated Action FOR REFRESH COMMAND * Update refresh function * Updated action and argument parsing * Removed comments * lint resolved * lint resolved * lint resolved * lint issue fixed * Lint issue fixed * lint fixed * Fixed * Fixed few things * Added a couple of examples --------- Co-authored-by: Fernando Flores <[email protected]>
1 parent 20e744d commit 5eddec5

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

ac

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,84 @@ venv_stop(){
19091909
venv_pid=`ps -ef |grep $venv_tty | grep -v "grep" | grep "/bin/sh -i" | awk '{print $3}'`
19101910
kill -9 $venv_pid > /dev/null 2>&1
19111911
}
1912+
#->venv-refresh:
1913+
## Refresh managed virtual environments with the latest scripts
1914+
## Usage: ac --refresh-env [--all] [--name <venv-name>]
1915+
## Options:
1916+
## --all - Refresh all environments
1917+
## --name - Specific environment to refresh
1918+
## Example:
1919+
## $ ac --refresh-env # Refresh the active environment
1920+
## $ ac --refresh-env --all # Refresh all environments
1921+
## $ ac --refresh-env=all # Refresh all environments
1922+
## $ ac --refresh-env --name venv-2.18 # Refresh a specific environment (e.g., venv-2.18)
1923+
## $ ac --refresh-env=venv-2.18 # Refresh a specific environment (e.g., venv-2.18)
1924+
venv_refresh() {
1925+
local raw_arg="$1" # Value from --refresh-env=...
1926+
local all_flag="$2" # --all option value
1927+
local name_arg="$3" # --name option value
1928+
local mode
1929+
local env_name
1930+
local target_envs=()
1931+
local source_dir="${PWD}/scripts"
1932+
1933+
# Validate source files
1934+
local required_files=("profile.sh" "mounts.sh" "mounts.env")
1935+
for f in "${required_files[@]}"; do
1936+
[[ ! -f "${source_dir}/${f}" ]] && message_error "Missing source file: ${source_dir}/${f}" && exit_all
1937+
done
1938+
1939+
# Argument resolution priority: 1. = syntax 2. --all/--name flags
1940+
if [ -n "$raw_arg" ]; then
1941+
# Handle --refresh-env=value syntax
1942+
case "$raw_arg" in
1943+
all)
1944+
mode="all"
1945+
;;
1946+
venv-*)
1947+
mode="named"
1948+
env_name="$raw_arg"
1949+
;;
1950+
*)
1951+
message_error "Invalid refresh-env argument: $raw_arg"
1952+
exit_all
1953+
;;
1954+
esac
1955+
elif [ "$all_flag" = "true" ]; then
1956+
mode="all"
1957+
elif [ -n "$name_arg" ]; then
1958+
mode="named"
1959+
env_name="$name_arg"
1960+
else
1961+
mode="active"
1962+
fi
1963+
1964+
# Mode execution
1965+
case "$mode" in
1966+
all)
1967+
for env in "${VENV_HOME_MANAGED}"/venv-*; do
1968+
[[ -d "$env" ]] && target_envs+=("$env")
1969+
done
1970+
[[ ${#target_envs[@]} -eq 0 ]] && message_error "No managed environments found" && exit_all
1971+
;;
1972+
named)
1973+
[[ -z "$env_name" ]] && message_error "Environment name required" && exit_all
1974+
env=$(validate_venv "$env_name")
1975+
target_envs+=("$env")
1976+
;;
1977+
active)
1978+
ensure_managed_venv_exists "--refresh-env"
1979+
target_envs+=("$VENV")
1980+
;;
1981+
esac
19121982

1983+
# Perform file operations
1984+
for env in "${target_envs[@]}"; do
1985+
message "Refreshing: $(basename "$env")"
1986+
cp -v "${source_dir}/profile.sh" "${source_dir}/mounts.sh" "${source_dir}/mounts.env" "$env/"
1987+
chmod +x "$env/mounts.sh" "$env/profile.sh"
1988+
done
1989+
}
19131990
# ==============================================================================
19141991
# Main arg parsing
19151992
# ==============================================================================
@@ -2033,6 +2110,18 @@ while true; do
20332110
ensure_managed_venv_exists $1
20342111
option_submitted="--venv-stop"
20352112
;;
2113+
--refresh-env|--refresh-env=?*) # command
2114+
ensure_managed_venv_exists $1
2115+
option_submitted="--refresh-env"
2116+
2117+
# Capture raw argument value if using = syntax
2118+
if [[ "$1" == *=* ]]; then
2119+
refresh_arg="${1#*=}"
2120+
else
2121+
refresh_arg=""
2122+
fi
2123+
;;
2124+
20362125
# End of main command list.
20372126
# Start of command options.
20382127
# Used by:
@@ -2383,4 +2472,6 @@ elif [ "$option_submitted" ] && [ "$option_submitted" = "--venv-start" ] ; then
23832472
venv_start $name
23842473
elif [ "$option_submitted" ] && [ "$option_submitted" = "--venv-stop" ] ; then
23852474
venv_stop $name
2475+
elif [ "$option_submitted" = "--refresh-env" ]; then
2476+
venv_refresh "$refresh_arg" "$all" "$name"
23862477
fi
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trivial:
2+
- ac - Refreshed mount tables and scripts in existing environments without recreating venv.
3+
(https://github.com/ansible-collections/ibm_zos_core/pull/2028).

0 commit comments

Comments
 (0)