-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_request.sh
191 lines (143 loc) · 4.83 KB
/
new_request.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
#!/bin/bash -l
CLIENT=$1 # Client identifier (e.g. username)
TICKET=$2 # Ticket number
DIR=$3 # Location where to create new request directory
# Help function with instructions on how to use this script.
Help()
{
# Display Help
echo "HELP"
echo "Description: Creates a directory hierarchy for a "
echo " new request"
echo
echo "Syntax: $(basename $0) CLIENT TICKET DIR"
echo "Arguments:"
echo " CLIENT A client identifier (e.g. username)."
echo " TICKET Request identifier (e.g. ticket number)."
echo " DIR Location to create directory hierarchy (if blank defaults to pwd)."
echo
}
# If the DIR argument is not provided
# then use the current working directory
# for the location of the new request
# environment creation.
if [[ -z "${DIR}" ]]; then
DIR=${PWD}
fi
# Check for valid arguments
if [ -z "$CLIENT" ]
then
printf "CLIENT not provided \n\n"
Help
exit 1
fi
if [ -z "$TICKET" ]
then
printf "TICKET not provided \n\n"
Help
exit 1
fi
# Assemble the new request environment directory
NEW_DIR=${DIR}/${CLIENT}/${TICKET}
# Check if the request directory already exists
if [ -d ${NEW_DIR} ]; then
printf "Directory already exists: \n ${NEW_DIR}\n"
Help
exit 1
fi
# Create new directory and structure for the new request.
mkdir -p ${NEW_DIR}
cd ${NEW_DIR}
mkdir -p data # Directory to store relevant data used
# by the client's scripts
mkdir -p env_setup # Directory to store scripts files associated
# with setting up the enviroment for
# the request at hand
mkdir -p scripts # Directory to store the client's scripts
mkdir -p output # Directory to store any outputs generated
# by the client's scripts.
# Create a gitignore file for known files we don't
# want to track using git.
cat > .gitignore << EOF
data/
.gitignore
output/
EOF
# Create a helper bash script for creating
# an isolated R environment, which can be sourced
# when needed.
cat > env_setup/r_env.sh << EOF
#!/bin/bash -l
# Load an R module and set the $R_LIBS_USER environment
# variable so that any R packages installed will
# be installed in the new request directory.
# Source this r_env.sh file before starting R so
# only R packages associated with this request environment
# are available during the R session
## ARGUMENTS ##
R_MODULE=\$1 # Define a specific module to load
## DEFAULTS ##
R_MODULE_DEFAULT=R # Define a default module to load if no
# module is provided as argument.
R_DEFAULT_DIR=\${R_MODULE_DEFAULT}/default # Define a default library location for default R module.
# Check if an R Module was specified as an argument.
if [ -z "\$R_MODULE" ]; then
# If R Module is not provided as an argument, use
# the default values.
module load \${R_MODULE_DEFAULT}
export R_LIBS_USER=${NEW_DIR}/env_setup/\${R_DEFAULT_DIR}
# Add the library directory to gitignore
echo env_setup/\${R_DEFAULT_DIR}/ >>${NEW_DIR}/.gitignore
else
# If R module is provided, load that module
#and define \$R_LIBS_USER
# based on this module name.
module load \${R_MODULE}
export R_LIBS_USER=${NEW_DIR}/env_setup/\${R_MODULE}
# Add the library directory to gitignore
echo env_setup/\${R_MODULE}/ >> ${NEW_DIR}/.gitignore
fi
EOF
# Create a helper bash script that can be sourced
# to create a python virtualenv environment.
cat > env_setup/py_virtenv.sh << EOF
#!/bin/bash -l
# Load a python module and then
# create a virtualenv with site packages
# included.
## ARGUMENTS ##
PY_MODULE=\$1 # Define a specific module to load
## DEFAULTS ##
PY_MODULE_DEFAULT=python3 # Define a default module to load if no
# module is provided as argument.
PY_DEFAULT_DIR=\${PY_MODULE_DEFAULT}/default # Define a virtualenv path name for the default Python module
# Check if a Python module was specified as an argument.
if [ -z "\$PY_MODULE" ]; then
# If a Python module is not provided as an argument, use
# the default values.
module load \${PY_MODULE_DEFAULT}
PY_VIRT_DIR=${NEW_DIR}/env_setup/\${PY_DEFAULT_DIR}
# Add the library directory to gitignore
echo env_setup/\${PY_DEFAULT_DIR}/ >> ${NEW_DIR}/.gitignore
else
# If a Python module is provided, load that module
# and define \$PY_VIRT_DIR based on the module name
module load \${PY_MODULE}
PY_VIRT_DIR=${NEW_DIR}/env_setup/\${PY_MODULE}
# Add the library directory to gitignore
echo env_setup/\${PY_MODULE}/ >> ${NEW_DIR}/.gitignore
fi
# Check if the virtual environment already exist at \$PY_VIRT_DIR
# if not, create it.
if [ -d \${PY_VIRT_DIR} ]; then
printf "Found virtualenv at:\n \${PY_VIRT_DIR}\n"
else
printf "Creating a virtualenv at:\n \${PY_VIRT_DIR}\n"
virtualenv --system-site-package \${PY_VIRT_DIR}
fi
# Activate the environment.
source \${PY_VIRT_DIR}/bin/activate
EOF
# Initialize the new request directory as a git repository
# to track changes as the code is modified.
git init