-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_template.sh
173 lines (140 loc) · 4.32 KB
/
prepare_template.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
#!/bin/bash
# Thanks to https://github.com/vsoch/singularity-shiny
usage () {
echo "Steps:
----------------------------------------------------------------------
1. Use this script to prepare your shiny-server.conf (configuration)
/bin/bash prepare_template.sh
----------------------------------------------------------------------
2. If needed, you can provide the following arguments
Commands:
help: show help and exit
start: the generation of your config
Options:
--port: the port for the application (e.g., shiny default is 3737)
--user: the user for the run_as directive in the shiny configuration
--base: base folder with applications
--logs: temporary folder with write for logs (not required)
--disable-index: disable directory indexing
----------------------------------------------------------------------
3. Make sure Singularity is loaded, and run the container using
the commands shown by the template.
"
}
# Start the application
SHINY_START="no";
# Port for Flask
CHECK_PORT="notnull"
while [[ ! -z $CHECK_PORT ]]; do
SHINY_PORT=$(( ( RANDOM % 60000 ) + 1025 ))
CHECK_PORT=$(netstat -atn | grep $SHINY_PORT)
done
# Base for apps
SHINY_BASE=/srv/shiny-server;
# Log folder assumed to be bound to
SHINY_LOGS=$(mktemp -d /tmp/shiny-server.XXXXXX) && rmdir ${SHINY_LOGS};
# Disable indexing (on, default, is not disabled)
DISABLE_DIRINDEX="on";
# User to run_as, defaults to docker
SHINY_USER="${USER}"
if [ $# -eq 0 ]; then
usage
exit
fi
while true; do
case ${1:-} in
-h|--help|help)
usage
exit
;;
-s|--start|start)
SHINY_START="yes"
shift
;;
-p|--port|port)
shift
SHINY_PORT="${1:-}"
shift
;;
-b|--base|base)
shift
SHINY_BASE="${1:-}"
shift
;;
-u|--user)
shift
SHINY_USER="${1:-}"
shift
;;
-di|--disable-index|disable-index)
DISABLE_DIRINDEX="off"
shift
;;
-l|logs|--logs)
shift
SHINY_LOGS="${1:-}"
shift
;;
-*)
echo "Unknown option: ${1:-}"
exit 1
;;
*)
break
;;
esac
done
# Functions
function prepare_conf() {
SHINY_PORT=$1
SHINY_BASE=$2
SHINY_LOGS=$3
DISABLE_DIRINDEX=$4
SHINY_USER=$5
CONFIG="run_as ${SHINY_USER};
server {
listen ${SHINY_PORT};
# Define a location at the base URL
location / {
# Host the directory of Shiny Apps stored in this directory
site_dir ${SHINY_BASE};
# Log all Shiny output to files in this directory
log_dir ${SHINY_LOGS};
# When a user visits the base URL rather than a particular application,
# an index of the applications available in this directory will be shown.
directory_index ${DISABLE_DIRINDEX};
}
}"
echo "${CONFIG}";
}
# Are we starting the server?
if [ "${SHINY_START}" == "yes" ]; then
echo "Generating shiny configuration...";
echo "port: ${SHINY_PORT}";
echo "logs:" ${SHINY_LOGS};
echo "base: ${SHINY_BASE}";
echo "run_as: ${SHINY_USER}";
# Prepare the template
CONFIG=$(prepare_conf $SHINY_PORT $SHINY_BASE $SHINY_LOGS $DISABLE_DIRINDEX $SHINY_USER);
# Temporary directories, if don't exist
mkdir -p "${SHINY_LOGS}";
mkdir -p ${SHINY_LOGS}/logs;
mkdir -p ${SHINY_LOGS}/lib;
# Configuration file
echo "${CONFIG}" > "shiny-server.conf";
echo "Server logging will be in ${SHINY_LOGS}";
echo
echo "To run your server:
module load singularity
singularity run --bind $SHINY_LOGS/logs:/var/log/shiny \\
--bind $SHINY_LOGS/lib:/var/lib/shiny-server \\
--bind shiny-server.conf:/etc/shiny-server/shiny-server.conf shiny.simg
---------------------------------------------------------------------------
For custom applications, also add --bind $SHINY_BASE:/srv/shiny-server
To see your applications, open your browser to http://127.0.0.1:$SHINY_PORT or
open a ssh connection from your computer to your cluster.
"
exit
else
usage
fi