-
Notifications
You must be signed in to change notification settings - Fork 0
/
ak
executable file
·271 lines (224 loc) · 8.23 KB
/
ak
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env bash
# This command is a bit like a Makefile: if your are outside a Docker
# container it will send you inside a new Docker container and then it
# will give you shortcuts to main project commands.
IMAGE=${IMAGE:="akretion/voodoo"}
USE_ADMINER=${USE_ADMINER:=false}
NAME=${NAME:="voodoo-${PWD##*/}"}
PORT=${PORT:="8069"}
POLLING_PORT=${POLLING_PORT:="8072"}
ADMINER_PORT=${ADMINER_PORT:="8061"}
SERVER=${SERVER:="$2.akretion.com"}
POSTGRESQL_DB=${POSTGRESQL_DB:="db"}
POSTGRESQL_DATA=${POSTGRESQL_DATA:="/workspace/.db"}
WORKDIR=${WORKDIR:="/workspace"}
START_OPTIONS=${START_OPTIONS:="-ti"}
CONTAINER_FILE=${CONTAINER_DIR:="/.devstep"}
PIP_DIR=${PIP_DIR:="/.devstep/.pip_packages"}
SHARED_EGGS_DIR=${SHARED_EGGS_DIR:="/.devstep/addons/voodoo/host_eggs"}
IMAGE_EGGS_DIR=${IMAGE_EGGS_DIR:="/.devstep/addons/voodoo/eggs"}
SHARED_ODOO_DIR=${SHARED_ODOO_DIR:="/.devstep/addons/voodoo/odoo"}
DOCKER_USER=${DOCKER_USER:="developer"}
DOCKER_RM=${DOCKER_RM:="true"}
SSH_USER=${SSH_USER:="api"}
SSH_KEY=${SSH_KEY:=""}
BUILDOUT=${BUILDOUT:="buildout.dev.cfg"}
# *****************************************************************************
if [ ! -d "$CONTAINER_DIR" ]; then
# we are outside of the container,
# this is the bash equivalent of Vagrant with the Docker provider
if [[ -z $1 || $1 == "up" || $1 == "run" || $1 == "build" || $1 == "debug" || \
$1 == "console" && -z $2 || $1 == "psql" || $1 == "help" && -z $2 ]]; then
if [[ ! -z "$(git remote -v | grep "github.com/akretion/voodoo")" ]]; then
git remote remove origin # avoid push to wrong remote
fi
if [[ ! $(docker images | grep "$IMAGE") ]]; then
echo "Docker $IMAGE not found!"
echo "Warning! We will now download 1.6 GB (but only once)"
docker pull fgrehm/devstep:v0.2.0
docker pull akretion/voodoo
fi
VOLUMES="-v $PWD:$WORKDIR"
odoo_dir=$(sed -n 's/^odoo-directory *= *\([^ ]*.*\)/\1/p' < $BUILDOUT)
if [ ! -z "$odoo_dir" ]; then
eval odoo_dir=$odoo_dir # eval ~
odoo_dir=$(dirname "$odoo_dir/fake") # expands to absolute path
echo "assuming shared Odoo directory: $odoo_dir"
VOLUMES="$VOLUMES -v $odoo_dir:$SHARED_ODOO_DIR"
fi
eggs_dir=$(sed -n 's/^eggs-directory *= *\([^ ]*.*\)/\1/p' < $BUILDOUT)
if [ ! -z "$eggs_dir" ]; then
eval eggs_dir=$eggs_dir # eval ~
eggs_dir=$(dirname "$eggs_dir/fake") # expands to absolute path
echo "assuming shared eggs directory: $eggs_dir"
[ ! -d "$eggs_dir" ] && mkdir -p "$eggs_dir"
VOLUMES="$VOLUMES -v $eggs_dir:$SHARED_EGGS_DIR"
fi
echo "Entering Docker warp zone with shared volumes:"
echo "$VOLUMES"
[[ ! -z $SSH_KEY ]] && cp $SSH_KEY .id_rsa
containers=$(docker ps -a | grep " $NAME " | awk '{ print $1 }') || true
if [ ! -z $containers ]; then
echo "killing existing container(s)"
docker rm -v -f $containers 2>/dev/null || true
fi
nc -z 127.0.0.1 "$PORT"
while [ $? = 0 ]
do
echo "WARNING port $PORT seems busy, trying to offset ports"
PORT=$((PORT+10))
POLLING_PORT=$((POLLING_PORT+10))
ADMINER_PORT=$((ADMINER_PORT+10))
nc -z 127.0.0.1 "$PORT"
done
echo "Odoo will run at http://localhost:$PORT"
if [ "$USE_ADMINER" = true ]; then
echo "Adminer will run at http://localhost:$ADMINER_PORT/?pgsql=db&username=developer"
fi
PORTS="-p $PORT:8069 -p $POLLING_PORT:8072"
runodoo () {
docker run $START_OPTIONS -e POSTGRESQL_DB="$POSTGRESQL_DB" \
-e POSTGRESQL_DATA="$POSTGRESQL_DATA" \
-u $DOCKER_USER \
--rm=$DOCKER_RM --name $NAME $PORTS $VOLUMES -w "$WORKDIR" $IMAGE \
-- /bin/bash -c "'$WORKDIR/ak $@'"
}
runadminer () {
if [ "$USE_ADMINER" = true ]; then
sleep 1
docker run -d -p $ADMINER_PORT:80 --link $NAME:db sebastienbeau/adminer > .adminer.pid
fi
}
runadminer & runodoo
if [ "$USE_ADMINER" = true ]; then
echo "*** Killing Adminer"
adminerid=$(cat .adminer.pid)
id=$(docker kill $adminerid)
id=$(docker rm $adminerid)
rm .adminer.pid
fi
else
exec ssh "$SSH_USER@$SERVER" "$@"
fi
# *****************************************************************************
else # when we are inside of the container
[[ -f /workspace/.id_rsa ]] && sudo mkdir -p /.devstep/.ssh && sudo mv /workspace/.id_rsa /.devstep/.ssh/id_rsa
export PYTHONPATH=$PYTHONPATH:$PIP_DIR
if [ ! -d $PIP_DIR ]; then
# NOTE sadly we cannot persit .pip_packages inside /workspace
# because of this pip bug https://github.com/pypa/pip/issues/1489
[ ! $(cat requirements.txt | grep -v "^#" | grep "[a-z]" | wc -l) = "0" ] && \
echo "installing requirements.txt with pip" && \
pip install -r requirements.txt --target=$PIP_DIR
fi
if [ -d $SHARED_EGGS_DIR ]; then
eggs_dir="$SHARED_EGGS_DIR"
if [ ! $(ls "$SHARED_EGGS_DIR" | grep reportlab) ]; then
echo "initializing shared host eggs directory from Docker image"
sudo chown -R $DOCKER_USER $eggs_dir
cp "$IMAGE_EGGS_DIR"/. $eggs_dir -R
fi
else
eggs_dir="eggs"
fi
if [ ! -f bin/buildout ]; then
echo "Bootstrapping Buildout..."
cd /tmp
cp "$WORKDIR/buildout.cfg" .
sudo pip install --upgrade setuptools
sudo pip install --upgrade zc.buildout
wget https://raw.github.com/buildout/buildout/master/bootstrap/bootstrap.py
python bootstrap.py --allow-site-packages
cp -rf bin "$WORKDIR/bin"
cd "$WORKDIR"
fi
[ ! -d parts ] && mkdir parts
if [ -f "$SHARED_ODOO_DIR"/setup.py ]; then
[ ! -f parts/odoo/setup.py ] && ln -s "$SHARED_ODOO_DIR" parts/odoo
fi
case $1 in # Command dispatch ***********************************************
run)
shift
[ ! -f bin/start_openerp ] || [ ! -f parts/odoo/setup.py ] && \
export FROM_BASH=true && ak build
ak wait_postgres
python bin/start_openerp "$@"
;;
debug)
shift
ak wait_postgres
python bin/start_openerp "$@" "--debug"
;;
build)
shift
if [ ! -d parts/odoo ]; then
cat<<EOF
WARNING you didn't set any valid existing odoo repo in your
buildout.dev.cfg odoo-directory.
If you continue, the recipe will do a FULL CLONE of Odoo into this
directory, this clone will download ~400 Mo and can take a while...
Alternatively you can abort and close the container and set a valid
existing odoo-directory in buildout.dev.cfg if you already have some
with the right branch on your computer.
will now clone odoo...
EOF
fi
echo "building project with Anybox Buildout recipe..."
if [ -d /etc/service/postgresql ]; then
python bin/buildout "$@" "-c" "$BUILDOUT" "buildout:eggs-directory=$eggs_dir" \
"buildout:openerp-downloads-directory=/.devstep/addons/voodoo/downloads" \
"buildout:develop-eggs-directory=/.devstep/addons/voodoo/develop-eggs" \
"openerp:options.db_user=$DOCKER_USER" "openerp:options.db_name=db" "openerp:options.data_dir=.filestore"
else
python bin/buildout "$@" "-c" "$BUILDOUT" "buildout:eggs-directory=$eggs_dir"
fi
;;
up)
shift
export FROM_BASH=true
ak build
ak run "$@"
;;
console)
shift
ak wait_postgres
python bin/python_openerp "$@"
;;
psql)
shift
ak wait_postgres
if [ -z "$1" ]; then
psql db
else
psql "$@"
fi
;;
wait_postgres)
[ ! -d /etc/service/postgresql ] && exit
[ -f /.devstep/log/postgresql.log ] && r=$(grep "ready to accept connections" /.devstep/log/postgresql.log)
[ -z "$r" ] && echo "waiting for Postgresql embedded server to start..."
while [ -z "$r" ]
do
sleep 0.1
[ -f /.devstep/log/postgresql.log ] && r=$(grep "ready to accept connections" /.devstep/log/postgresql.log)
done
sleep 0.01
if [ -z "$(psql -l | grep db | grep $DOCKER_USER)" ]; then
sleep 0.3
psql -c "ALTER DATABASE db OWNER TO $DOCKER_USER;"
sleep 0.3
fi
exit
;;
help)
cat<<EOF
available commands are:
run, debug, build, up, console, psql, help
EOF
;;
*)
[ ! -z "$1" ] && exec ssh "$SSH_USER@$SERVER" "$@"
;;
esac
[ "$FROM_BASH" != true ] && export FROM_BASH=true && exec /bin/bash
fi