-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.sh
executable file
·76 lines (70 loc) · 2.52 KB
/
execute.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
# This script executes a script on a set of containers based on
# orchestration.conf. It accepts 2 or 5 arguments:
# CONTAINER_BASE - base name of the containers
# USER_BASE - base name of the users to create
# INDEX - Index at which to begin numbering containers and users
# ORCH_FILE - path to orchestration.conf
# SCRIPT - Path to a script to run
# If ORCHESTRATION_MODE is set to 1 in lxd.conf, then only the latter two args
# must be provided. The called script cannot use variables defined in this
# script with the exception of $user, which has the username
# PREAMBLE ---------------------------------------------------------------------
# the configuration file specifies the global parameters
root_dir=$(dirname "$0")
source $root_dir/lxd.conf
# input validation
if [[ "$#" != 6 && "$#" != 3 ]]; then
echo "Syntax: script CONTAINER_BASE USERNAME INDEX ORCH_FILE SCRIPT MODE"
echo "Syntax: script ORCH_FILE SCRIPT MODE (for ORCHESTRATION_MODE = 1)"
echo "e.g. orchestrate.sh mpi eskandarin 0 orchestrate.conf test.sh 0"
echo "MODE: 0 - script accepts no args and executes within each container"
echo "MODE: 1 - script accepts agent, container, username, key as args from orchestration.conf"
exit 1
fi
if [[ "$#" == 5 ]]; then
if [[ $ORCHESTRATION_MODE != 0 ]]; then
echo "Error in number of arguments and value of orchestration mode in execute.sh"
exit 1
fi
container_base=$1
user_base=$2
orch_index=$3
orch_file=$4
user_script=$5
mode=$6
else
if [[ $ORCHESTRATION_MODE != 1 ]]; then
echo "Error in number of arguments and value of orchestration mode in orchestrate.sh"
exit 1
fi
orch_file=$1
user_script=$2
mode=$3
fi
# MAIN -------------------------------------------------------------------------
readarray -t agents < "$orch_file"
if [[ $mode == 0 ]]; then
readarray -t script < "$user_script"
fi
index=$orch_index
for row in "${agents[@]}"; do
row_array=(${row})
agent=${row_array[0]}
if [[ $ORCHESTRATION_MODE == 0 ]]; then
container=${container_base}${index}
user=${user_base}${index}
else
container=${row_array[1]}
user=${row_array[2]}
user_key=${row_array[3]}
fi
if [[ $mode == 0 ]]; then
for comm in "${script[@]}"; do
comm="${comm//\$user/$user}" # replace '$user' with the value of $user
lxc exec $agent:$container -- $comm
done
else
$user_script $agent $container $user $user_key
fi
done