-
Notifications
You must be signed in to change notification settings - Fork 0
/
octopython.sh
executable file
·200 lines (200 loc) · 5.56 KB
/
octopython.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
REPOBASE=/tmp/$USER/Octopython
WORKERS=0
CORES=0
MEMORY=0
DISK=0
NAME=""
LINK=""
JUPYTER="no"
PORT=8080
DESTROY="no"
CONDALIBS=""
BRANCH=""
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
CMDPID=""
function help {
echo "Usage: octopython.sh"
echo "The minimum arguments required are to specify the link to the github repo you wish to instantiate"
echo "Specifying the number of workers also requires to input a manager name"
echo "Command line options:"
echo " -l|--link: specify the link to the github repo"
echo " -b|--branch: specifiy the branch of a github repo\n"
echo " -w|--workers: specify the number of workers desired"
echo " -wc|--worker-cores: specify the cores of each worker"
echo " -wm|--worker-memory: specify the memory of each worker"
echo " -wd|--worker-disk: specify the disk of each worker"
echo " -n|--name: specify the name of the workqueue\n"
echo " -d|--destroy: if this argument is set, the program destroys the local git repo and conda enviroment"
echo " -c|--condalibs: specify additional conda libs (place in quotes)"
echo " -j|--jupyter: if this argument is set, the program initializes a jupyter notebook of the provided repo"
echo " -p|--port: specify jupyter port"
exit 1
}
# parse command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-w|--workers)
WORKERS="$2"
shift
shift
;;
-wc|--worker-cores)
CORES="$2"
shift
shift
;;
-wm|--worker-memory)
MEMORY="$2"
shift
shift
;;
-wd|--worker-disk)
DISK="$2"
shift
shift
;;
-n|--name)
NAME="$2"
shift
shift
;;
-l|--link)
LINK="$2"
shift
shift
;;
-b|--branch)
BRANCH="$2"
shift
shift
;;
-p|--port)
PORT="$2"
shift
shift
;;
-d|--destroy)
DESTROY="yes"
shift
;;
-c|--condalibs)
CONDALIBS="$2"
shift
shift
;;
-j|jupyter)
JUPYTER="yes"
shift
;;
-h|--help)
help
;;
*)
echo "Error: Unknown option"
echo "Use -h or --help to display help"
help
;;
esac
done
# check if the user has conda installed. If the user does not have conda, then ask them if they wish to install it
source ~/miniconda3/etc/profile.d/conda.sh
if [[ ! `command -v conda` == "conda" ]] ; then
echo "****Error: Conda not detected"
while true; do
read -p "Do you wish to install conda?" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Conda not installed: exiting"; exit 3;;
* ) echo "Please answer yes or no";;
esac
done
echo "****Downloading and installing conda"
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh > /tmp/conda-install.sh
bash /tmp/conda-install.sh
source ~/miniconda3/etc/profile.d/conda.sh
fi
# exit if the user does not provide an input github repo
if [[ "$LINK" == "" ]]; then
echo "Error: must specify a github repo to pull from"
exit 2
fi
# if the user asks for workers but does not specify a manager name, then quit
if [ "$NAME" == "" ] && [ ! "$WORKERS" == 0 ]; then
echo "Error: Please enter a manager name when requesting workers"
exit 4
fi
# if the user specifies properites of the workers but does not give them a name or specify how many, exit
if [ ! "$CORES" == 0 ] || [ ! "$MEMORY" == 0 ] || [ ! "$DISK" == 0 ]; then
if [ "$NAME" == "" ]; then
echo "Error: Please enter a manager name when requesting workers"
exit 5
fi
if [ "$WORKERS" == 0 ]; then
echo "Error: Please specify the number of workers desired"
exit 5
fi
fi
# seperate out the name of the github repo to be used as the name of the local directory
GITPATH=(${LINK///// })
FOLDERNAME=(${GITPATH[1]//./ })
echo "****Cloning git repository to local directory $REPOBASE/$FOLDERNAME"
if [[ "$BRANCH" == "" ]]; then
git clone $LINK $REPOBASE/$FOLDERNAME
else
git clone -b $BRANCH $LINK $REPOBASE/$FOLDERNAME
fi
cd $REPOBASE/$FOLDERNAME
FILE=$REPOBASE/$FOLDERNAME/environment.yml
# check if there is a local environment.yml file. If there is, then we can use that to determine the conda libraries required
# if such a file does not exist, then we check if the user provided libraries as a command line input
if [ ! -f "$FILE" ] && [ "$CONDALIBS" == "" ]; then
echo "Error: git repo does not have environment.yml or you did not specify the conda libraries required"
exit 3
fi
echo "****Creating local conda environment"
if [ "$CONDALIBS" == "" ];
then
conda env create --file environment.yml --prefix ./env
else
conda create --prefix ./env
fi
conda activate ./env
if [ ! "$CONDALIBS" == "" ]; then
echo "****Installing additional conda libraries"
conda install -y -c $CONDALIBS
fi
# special case for installing topEFT
SETUP=$REPOBASE/$FOLDERNAME/setup.py
if [ -f "$SETUP" ] ; then
pip install -e .
fi
if [[ "$NAME" != "" ]]; then
echo "Submitting $WORKERS workers to manager $NAME"
if [ ! "$CORES" == 0 ] || [ ! "$MEMORY" == 0 ] || [ ! "$DISK" == 0 ]; then
python $SCRIPTPATH/octopus_factory.py $NAME $WORKERS $CORES $MEMORY $DISK & CMDPID=$!
else
python $SCRIPTPATH/octopus_factory.py $NAME $WORKERS 1 1000 1000 & CMDPID=$!
fi
fi
if [ "$JUPYTER" == "yes" ]; then
echo "Initializing jupyter notebook on port $PORT"
jupyter notebook --no-browser --port=$PORT
else
echo "Github repo has been initalized at $REPOBASE with local conda environment ./env"
echo "You are now in a local copy of your git repo with a conda environment activated"
echo "Type 'conda activate ./env' to activate the conda environment, type exit to quit out of this program"
cd $REPOBASE/$FOLDERNAME
bash
echo "Program exiting: deleting local files"
fi
conda deactivate
if [ "$DESTROY" == "yes" ]; then
cd $REPOBASE
rm -frd $FOLDERNAME
fi
if [ ! "$CMDPID" == "" ]; then
kill -s 9 $CMDPID
fi