forked from chunjie-sam-liu/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjrocker
executable file
·188 lines (164 loc) · 4.35 KB
/
jrocker
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
#!/usr/bin/env bash
param=$#
serv=$1
variable=$2
port=${variable:-8686}
arr[1]='up'
arr[2]='dn'
arr[3]='ss'
arr[4]='ex'
arr[5]='imgs'
arr[6]='rmi'
arr[7]='pull'
function usage {
if [ "$param" -gt 2 -o "${param}" -lt 1 ]
then
echo "Usage:"
echo " jrocker [up|dn|ss|ex|imgs|rmi] [port|image-id]"
echo "Details:"
echo " up -> startup a container for jrocker, default port 8686."
echo " dn -> shutdown a container or all container of jrocker."
echo " ex -> exec jrocker container."
echo " ss -> status of jrocker container."
echo " imgs -> list images of chunjiesamliu/jrocker"
echo " rmi -> remove image of chunjiesamliu/jrocker"
echo " pull -> pull latest image of chunjiesamliu/jrocker"
echo "For example:"
echo " jrocker up 9191"
exit 1
fi
if [[ ! "${arr[@]}" =~ "$serv" ]]
then
echo "Error:"
echo " Input must be one of 'up', 'dn', 'ss', 'ex', 'imgs' and 'rmi' to start jrocker"
echo "Example:"
echo " jrocker ss"
exit 1
fi
}
function cpf {
d=$1
for i in `find ${HOME} -mindepth 1 -prune -name '.*'`;
do
name=`basename ${i}`
[[ ${name} =~ '.rstudio' ]] && continue
[[ ${name} =~ '.vscode' ]] && continue
cmd="cp -rf ${i} ${d}/${name}"
eval ${cmd}
done
}
function lnf {
d=$1
for i in `find ${HOME} -mindepth 1 -prune -type f -name '.*'`;
do
name=`basename ${i}`
[[ ${name} =~ '.rstudio' ]] && continue
[[ ${name} =~ '.vscode' ]] && continue
cmd="ln ${i} ${d}/${name}"
eval ${cmd}
done
}
function up {
cs=(`docker ps|grep -o ${USER}-rstudio.*$`)
[[ ${cs[@]} =~ "${USER}-rstudio-${port}" ]] && echo "Error: ${USER}-rstudio-${port} exists." && exit 1
[[ ${port} -lt 8686 ]] && echo "Error: New port should be larger than 8686." && exit 1
jd=${HOME}/.${USER}-rstudio/${USER}-rstudio-${port}
[[ ! -d ${jd} ]] \
&& mkdir -p ${jd} \
&& lnf ${jd} \
&& ln -s /workspace/${USER} ${jd}/workspace \
&& ln -s /project/${USER} ${jd}/project \
&& ln -s /data/${USER} ${jd}/data
docker run -d -p ${port}:8787 \
-m 1000g \
-v ${jd}:${HOME}/ \
-v /workspace/${USER}/:${HOME}/workspace/ \
-v /project/${USER}/:${HOME}/project/ \
-v /data/${USER}/:${HOME}/data/ \
-v ${HOME}/R/:${HOME}/R/ \
-v ${HOME}/scripts/:${HOME}/scripts/ \
-v ${HOME}/tools/:${HOME}/tools/ \
-v ${HOME}/github/:${HOME}/github/ \
-v ${HOME}/tmp/:${HOME}/tmp/ \
-e USER=${USER} -e PASSWORD=🙃 \
-e USERID=$UID -e GROUPID=$GID -e ROOT=TRUE \
--name ${USER}-rstudio-${port} chunjiesamliu/jrocker:latest
echo "Start ${USER}-rstudio successfully with port ${port}."
}
function dn {
cs=(`docker ps|grep -o ${USER}-rstudio.*$`)
if [ ${param} -eq 2 ]
then
c="${USER}-rstudio-${port}"
if [[ ${cs[@]} =~ ${port} ]]
then
docker container stop ${c}
echo "Container ${c} is stopped"
docker container rm ${c}
echo "Container ${c} is removed"
else
echo "Error: Container ${c} not in the docker ps"
fi
else
echo "Notice: You are going to stop all ${USER}-rstudio container ${cs[@]}."
echo "Input (yes/no):"
read choice
case ${choice} in
yes)
echo "All jrocker container stopped."
for c in ${cs[@]};
do
docker container stop ${c}
echo "Container ${c} is stopped"
docker container rm ${c}
echo "Container ${c} is removed"
done
;;
no)
echo "No ${USER}-rstudio container stopped."
;;
*)
echo "No ${USER}-rstudio container stopped."
;;
esac
fi
}
function ss {
info=`docker ps|grep ${USER}-rstudio`
[[ -z ${info} ]] && echo "Notice: no ${USER}-rstudio container." || docker ps|grep ${USER}-rstudio
}
function ex {
cs=(`docker ps|grep -o ${USER}-rstudio.*$`)
c="${USER}-rstudio-${port}"
[[ ${param} -ne 2 ]] && echo "Error: Input port for container." && ss && exit 1
if [[ ${param} -eq 2 && ${cs[@]} =~ ${port} ]]
then
docker exec -it -u $UID:$GID -w ~ ${c} bash
else
echo "Notice: Input valid jrocker port."
docker ps|grep ${USER}-rstudio
fi
}
function imgs {
docker image ls chunjiesamliu/jrocker
}
function rmi {
if [ ${param} -eq 2 ]
then
docker image rm ${variable}
else
echo "Notice: Input valid jrocker image."
exit 1
fi
}
function pull {
docker pull chunjiesamliu/jrocker:latest
}
usage
[[ ${serv} = 'up' ]] && up || usage
[[ ${serv} = 'dn' ]] && dn || usage
[[ ${serv} = 'ss' ]] && ss || usage
[[ ${serv} = 'ex' ]] && ex || usage
[[ ${serv} = 'rmi' ]] && rmi || usage
[[ ${serv} = 'imgs' ]] && imgs || usage
[[ ${serv} = 'pull' ]] && pull || usage