@@ -9,10 +9,12 @@ init_script_logging "stop"
99
1010usage (){
1111 cat << USG
12- Usage: stop.sh [--hard] [-h|--help]
12+ Usage: stop.sh [--components <csv>] [-- hard] [-h|--help]
1313
1414Stop the /dev/push stack (dev or prod auto-detected).
1515
16+ --components <csv>
17+ Comma-separated list of services to stop (${VALID_COMPONENTS// |/ , } )
1618 --hard Force stop all containers labeled for the devpush project
1719 -h, --help Show this help
1820USG
@@ -57,8 +59,21 @@ force_stop_all() {
5759
5860# Parse CLI flags
5961hard_mode=0
62+ comps=" "
6063while [[ $# -gt 0 ]]; do
6164 case " $1 " in
65+ --components)
66+ comps=" $2 "
67+ IFS=' ,' read -ra _sp_secs <<< " $comps"
68+ for comp in " ${_sp_secs[@]} " ; do
69+ comp=" ${comp// / } "
70+ [[ -z " $comp " ]] && continue
71+ if ! validate_component " $comp " ; then
72+ exit 1
73+ fi
74+ done
75+ shift 2
76+ ;;
6277 --hard) hard_mode=1; shift ;;
6378 -h|--help) usage ;;
6479 * ) err " Unknown option: $1 " ; usage ;;
@@ -70,21 +85,65 @@ cd "$APP_DIR" || { err "App dir not found: $APP_DIR"; exit 1; }
7085docker info > /dev/null 2>&1 || { err " Docker not accessible. Run with sudo or add your user to the docker group." ; exit 1; }
7186
7287if (( hard_mode== 0 )) ; then
73- if ! is_stack_running; then
74- printf ' \n'
75- printf " ${DIM} No running services to stop.${NC} \n"
76- exit 0
88+ if [[ -z " $comps " ]]; then
89+ if ! is_stack_running; then
90+ printf ' \n'
91+ printf " ${DIM} No running services to stop.${NC} \n"
92+ exit 0
93+ fi
7794 fi
7895fi
7996
97+ service_running () {
98+ local service=" $1 "
99+ docker ps \
100+ --filter " label=com.docker.compose.project=devpush" \
101+ --filter " label=com.docker.compose.service=${service} " \
102+ -q \
103+ | grep -q .
104+ }
105+
106+ selected_services=()
107+ if [[ -n " $comps " ]]; then
108+ IFS=' ,' read -ra _selected_services <<< " $comps"
109+ for service in " ${_selected_services[@]} " ; do
110+ service=" ${service// / } "
111+ [[ -n " $service " ]] && selected_services+=(" $service " )
112+ done
113+ fi
114+
80115if (( hard_mode== 1 )) ; then
81- if ! force_stop_all; then
116+ if (( ${# selected_services[@]} > 0 )) ; then
117+ selected_ids=()
118+ for service in " ${selected_services[@]} " ; do
119+ while IFS= read -r cid; do
120+ [[ -n " $cid " ]] && selected_ids+=(" $cid " )
121+ done < <(
122+ docker ps \
123+ --filter " label=com.docker.compose.project=devpush" \
124+ --filter " label=com.docker.compose.service=${service} " \
125+ -q 2> /dev/null || true
126+ )
127+ done
128+ printf ' \n'
129+ if (( ${# selected_ids[@]} > 0 )) ; then
130+ run_cmd " Stopping selected containers (${# selected_ids[@]} found)" docker stop " ${selected_ids[@]} "
131+ else
132+ printf " Stopping selected containers (0 found) ${YEL} ⊘${NC} \n"
133+ fi
134+ elif ! force_stop_all; then
82135 exit 1
83136 fi
84137else
85138 set_compose_base
86139 printf ' \n'
87- if ! run_cmd --try " Stopping stack..." " ${COMPOSE_BASE[@]} " stop; then
140+ if (( ${# selected_services[@]} > 0 )) ; then
141+ if ! run_cmd --try " Stopping selected services..." " ${COMPOSE_BASE[@]} " stop " ${selected_services[@]} " ; then
142+ printf ' \n'
143+ err " Graceful stop failed for selected services."
144+ exit 1
145+ fi
146+ elif ! run_cmd --try " Stopping stack..." " ${COMPOSE_BASE[@]} " stop; then
88147 printf ' \n'
89148 err " Graceful stop failed; force-stopping containers."
90149 if ! force_stop_all; then
@@ -99,7 +158,21 @@ if ! docker ps --filter "label=com.docker.compose.project=devpush" >/dev/null 2>
99158 err " Unable to verify whether containers are stopped (docker ps failed)."
100159 exit 1
101160fi
102- if is_stack_running; then
161+ if (( ${# selected_services[@]} > 0 )) ; then
162+ running_selected=0
163+ for service in " ${selected_services[@]} " ; do
164+ if service_running " $service " ; then
165+ running_selected=1
166+ break
167+ fi
168+ done
169+ if (( running_selected== 1 )) ; then
170+ printf ' \n'
171+ err " Some selected services are still running."
172+ err " Try: scripts/stop.sh --components ${comps} --hard (or run with sudo)."
173+ exit 1
174+ fi
175+ elif is_stack_running; then
103176 printf ' \n'
104177 err " Stack still has running containers."
105178 err " Try: scripts/stop.sh --hard (or run with sudo)."
0 commit comments