Skip to content

Commit

Permalink
Merge pull request #212 from vespos/ami2
Browse files Browse the repository at this point in the history
ami2 script for lcls-1 hutches
  • Loading branch information
silkenelson authored Dec 14, 2024
2 parents 203187b + 4eaaf9d commit dc477fb
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,21 @@ usage: startami options<br/>
</td>
</tr>

<tr>>
<td>startami2</td>
<td>
usage: startami2 <command><br/>
<br/>
AMI2 commands for LCLS-I<br/>
<br/>
COMMANDS:<br/>
status: print the status of AMI processes<br/>
restart: restart all AMI processes<br/>
stop: stop all AMI processes<br/>
client: start a second AMI client, connecting to the existing manager
</td>
</tr>

<tr>
<td>stopami</td>
<td>
Expand Down
119 changes: 119 additions & 0 deletions scripts/startami2
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash

# ----------- Functions -----------
get_status(){
echo "STATUS:"
grep "ami2_" "$STATUS_FILE" | awk '{print $2"\t"$1"\t"$3}'
}


start_new_client(){
# Just start a client to connect to an existing manager
MGR_STATUS=$(grep ami2_manager "$STATUS_FILE" | awk '{print $3}')
rm "$STATUS_FILE"
if [[ $MGR_STATUS == RUNNING ]]; then
echo "Manager running on $AMI2_MGR_HOST."

echo "Starting AMI2 client on $(hostname)."


# These exports are needed (as they are in the cnf file, see ami2_env)
export PATH=/reg/g/pcds/dist/pds/$HUTCH/ami2-current/install-lcls1/bin:/reg/g/pcds/dist/pds/$HUTCH/ami2-current/install-lcls1/condadir/bin:/usr/bin:/usr/local/bin
export MYPYPATH=/reg/g/pcds/dist/pds/$HUTCH/ami2-current/install-lcls1/lib/python3.9/site-packages
export PYTHONPATH=/reg/g/pcds/dist/pds/$HUTCH/ami2-current/install-lcls1/lib/python3.9/site-packages
export SIT_ROOT=/cds/data/psdm
export SIT_PSDM_DATA=/cds/data/psdm
export SIT_DATA=/cds/sw/ds/ana/conda1/inst/envs/ana-4.0.62-py3/data:/cds/group/psdm/data/

GRAPH_NAME="graph_$(tr -dc A-Za-z0-9 </dev/urandom | head -c 4;)"
echo "Command: $(which ami-client) -H $AMI2_MGR_HOST -g $GRAPH_NAME"
ami-client -H "$AMI2_MGR_HOST" -g "$GRAPH_NAME"

else
echo "Manager not in running state. Can't start a client."
echo "Manager: $MGR_STATUS"
fi
}


restart_all(){
# Restart all the AMI2-related proc-servers
echo "Restarting all AMI2 processes"
for i in "${!AMI2_PROC[@]}"; do
if [[ ${AMI2_PROC[i]} =~ "monreqsrv" ]]; then
continue # skip monreq processes that might have ami2 in their name
fi
echo "${AMI2_PROC[i]}"
echo "${AMI2_HOST[i]}"
echo "${AMI2_STATUS[i]}"
$PROCMGR restart "$CNF" "${AMI2_PROC[i]}"
done
}

stop_all(){
# Stops all the AMI2-related proc-servers
echo "Stopping all AMI2 processes"
for i in "${!AMI2_PROC[@]}"; do
if [[ ${AMI2_PROC[i]} =~ "monreqsrv" ]]; then
continue # skip monreq processes that might have ami2 in their name
fi
echo "${AMI2_PROC[i]}"
echo "${AMI2_HOST[i]}"
echo "${AMI2_STATUS[i]}"
$PROCMGR stop "$CNF" "${AMI2_PROC[i]}"
done
}



# ----------- Main -----------
if [[ $# -lt 1 ]]; then
CMD="status"
echo "Default: CMD: $CMD"
else
CMD=$1
fi

if [[ $# -lt 2 ]]; then
HUTCH=${HOSTNAME:0:3}
HUTCHES=("xpp" "xcs" "mfx" "cxi" "mec")
if [[ ${HUTCHES[*]} =~ $HUTCH ]]; then
echo "Valid hutch found: $HUTCH"
else
echo "Invalid hutch: $HUTCH"
echo "Valid hutches are '${HUTCHES[*]}'. This is a LCLS-I only tool."
exit 1
fi
else
HUTCH=$2
fi

PROCMGR="/cds/group/pcds/dist/pds/$HUTCH/current/tools/procmgr/procmgr"
if [[ $HUTCH == "cxi" ]]; then
CNF="/reg/g/pcds/dist/pds/$HUTCH/scripts/cxi_0.cnf" # special CXI case...
else
CNF="/reg/g/pcds/dist/pds/$HUTCH/scripts/$HUTCH.cnf"
fi
STATUS_FILE="/tmp/ami2_procmgr_status_$USER"

$PROCMGR status "$CNF" > "$STATUS_FILE"

AMI2_MGR_HOST=$(grep ami2_manager "$STATUS_FILE" | awk '{print $1}')
# shellcheck disable=SC2207
AMI2_PROC=($(grep ami2_ "$STATUS_FILE" | awk '{print $2}'))
# shellcheck disable=SC2207
AMI2_HOST=($(grep ami2_ "$STATUS_FILE" | awk '{print $1}'))
# shellcheck disable=SC2207
AMI2_STATUS=($(grep ami2_ "$STATUS_FILE" | awk '{print $3}'))

if [[ $CMD == "status" ]]; then
get_status
elif [[ $CMD == "client" ]]; then
start_new_client
elif [[ $CMD == "restart" ]]; then
restart_all
elif [[ $CMD == "stop" ]]; then
stop_all
fi

rm "$STATUS_FILE"

0 comments on commit dc477fb

Please sign in to comment.