-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhelpers.sh
148 lines (115 loc) · 3.54 KB
/
helpers.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
#
# Helper Functions shared between forward tool scripts
#
# Configuration
#
function set_forward_script() {
FOUND="no"
echo "== Finding Script =="
declare -a FORWARD_SCRIPTS=("sbatches/${RESOURCE}/$SBATCH"
"sbatches/$SBATCH"
"${RESOURCE}/$SBATCH"
"$SBATCH");
for FORWARD_SCRIPT in "${FORWARD_SCRIPTS[@]}"
do
echo "Looking for ${FORWARD_SCRIPT}";
if [ -f "${FORWARD_SCRIPT}" ]
then
FOUND="${FORWARD_SCRIPT}"
echo "Script ${FORWARD_SCRIPT}";
break
fi
done
echo
if [ "${FOUND}" == "no" ]
then
echo "sbatch script not found!!";
echo "Make sure \$RESOURCE is defined" ;
echo "and that your sbatch script exists in the sbatches folder.";
exit
fi
}
#
# Job Manager
#
function check_previous_submit() {
echo "== Checking for previous notebook =="
PREVIOUS=`ssh ${RESOURCE} squeue --name=$NAME --user=$FORWARD_USERNAME -o "%R" -h`
if [ -z "$PREVIOUS" -a "${PREVIOUS+xxx}" = "xxx" ];
then
echo "No existing ${NAME} jobs found, continuing..."
else
echo "Found existing job for ${NAME}, ${PREVIOUS}."
echo "Please end.sh before using start.sh, or use resume.sh to resume."
exit 1
fi
}
function set_partition() {
if [ "${PARTITION}" == "gpu" ];
then
echo "== Requesting GPU =="
PARTITION="${PARTITION} --gres gpu:1"
fi
}
function get_machine() {
TIMEOUT=${TIMEOUT-1}
ATTEMPT=0
echo
echo "== Waiting for job to start, using exponential backoff =="
MACHINE=""
ALLOCATED="no"
while [[ $ALLOCATED == "no" ]]
do
# nodelist
MACHINE=`ssh ${RESOURCE} squeue --name=$NAME --user=$FORWARD_USERNAME -o "%N" -h`
if [[ "$MACHINE" != "" ]]
then
echo "Attempt ${ATTEMPT}: resources allocated to ${MACHINE}!.." 1>&2
ALLOCATED="yes"
break
fi
echo "Attempt ${ATTEMPT}: not ready yet... retrying in $TIMEOUT.." 1>&2
sleep $TIMEOUT
ATTEMPT=$(( ATTEMPT + 1 ))
TIMEOUT=$(( TIMEOUT * 2 ))
done
echo $MACHINE
MACHINE="`ssh ${RESOURCE} squeue --name=$NAME --user=$FORWARD_USERNAME -o "%R" -h`"
echo $MACHINE
# If we didn't get a node...
if [[ "$MACHINE" != "$MACHINEPREFIX"* ]]
then
echo "Tried ${ATTEMPTS} attempts!" 1>&2
exit 1
fi
}
#
# Instructions
#
function instruction_get_logs() {
echo
echo "== View logs in separate terminal =="
echo "ssh ${RESOURCE} cat $RESOURCE_HOME/forward-util/${SBATCH_NAME}.out"
echo "ssh ${RESOURCE} cat $RESOURCE_HOME/forward-util/${SBATCH_NAME}.err"
}
function print_logs() {
ssh ${RESOURCE} cat $RESOURCE_HOME/forward-util/${SBATCH_NAME}.out
ssh ${RESOURCE} cat $RESOURCE_HOME/forward-util/${SBATCH_NAME}.err
}
#
# Port Forwarding
#
function setup_port_forwarding() {
echo
echo "== Setting up port forwarding =="
sleep 5
if $ISOLATEDCOMPUTENODE
then
echo "ssh -L $PORT:localhost:$PORT ${RESOURCE} ssh -L $PORT:localhost:$PORT -N $MACHINE &"
ssh -L $PORT:localhost:$PORT ${RESOURCE} ssh -L $PORT:localhost:$PORT -N "$MACHINE" &
else
echo "ssh $DOMAINNAME -l $FORWARD_USERNAME -K -L $PORT:$MACHINE:$PORT -N &"
ssh "$DOMAINNAME" -l $FORWARD_USERNAME -K -L $PORT:$MACHINE:$PORT -N &
fi
}