summaryrefslogtreecommitdiff
path: root/devgerrit/dgerrit.sh
blob: 9d480ecd1a20c2ea24f766279cb8d18f0eae2fb3 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#! /bin/sh
#===============================================================================
# Tizen gerrit+drupal+openldap 
# 01/17 2015
# 
# Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
# Author onstudy@samsung.com
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#===============================================================================

home="$( cd "$( dirname "$0" )" && pwd )"
. $home/config.conf

set -e
#-------------------------------------------------------------------------------
images=$IMAGES:$TAG
container=$CONTAINER

echo "IMAGES:"$images
echo "CONTAINER:"$container

if [ ! -z "$PORTS" ]
then
    for I in $PORTS; do
        service_port=$I
        if [ ! -z $(echo "$service_port" | grep ':') ]
        then
           if [ -z "$ports_cmd" ]
           then
              ports_cmd=$(echo "-p ${service_port} ")
           else
              ports_cmd=$(echo $ports_cmd"-p ${service_port} ")
           fi
        elif [ ! -z $(echo "$service_port" | grep '-') ]
        then
           d1=$(echo $service_port | cut -d '-' -f1)
           d2=$(echo $service_port | cut -d '-' -f2)
           instance=$((d2 - d1 + 1))
           #echo "instance:"$instance
           count=0
           while [ $count -lt $instance ]; do  
               expose_port=$((d1 + count))
               if [ -z "$ports_cmd" ]
               then
                  ports_cmd=$(echo "-p ${expose_port}:${expose_port} ")
               else
                  ports_cmd=$(echo $ports_cmd"-p ${expose_port}:${expose_port} ")
               fi
               count=`expr $count + 1`
           done
        else
           if [ -z "$ports_cmd" ]
           then
              ports_cmd=$(echo "-p ${service_port}:${service_port} ")
           else
              ports_cmd=$(echo $ports_cmd"-p ${service_port}:${service_port} ")
           fi
        fi
    done
fi
echo "PORT:"$ports_cmd

if [ ! -z "$ENVLIST" ]
then
    envlist_cmd=$(echo "--env-file $ENVLIST")
fi
echo "ENV LIST:"$envlist_cmd

if [ ! -z "$VOLUMES" ]
then
    for vol in $VOLUMES; do
        if [ -z "$volumes_cmd" ]
        then
           volumes_cmd=$(echo "-v /etc/localtime:/etc/localtime:ro -v $vol ")
        else
           volumes_cmd=$(echo $volumes_cmd "-v $vol ")
        fi
    done
fi
echo "VOLUMES:"$volumes_cmd
if [ ! -z "$HOSTNAME" ]
then
    hostname_cmd=$(echo "-h $HOSTNAME")
fi
echo "HOST NAME:"$hostname_cmd

if [ "$PRIVILEGED" = 'yes' ]
then
    privileged_cmd=$(echo "--privileged")
fi
echo "PRIVILEGED:"$privileged_cmd

if [ "$AUTOSTART" = 'always' ]
then
   autostart_cmd=$(echo "--restart=always")
elif [ "$AUTOSTART" = 'on-failure^' ]
then
   autostart_cmd=$(echo "--restart=on-failure:10")
fi
echo "AUTOSTART:"$autostart_cmd

if [ ! -z "$LINK_IMAGES" ]
then
    for link in $LINK_IMAGES; do
        if [ -z "$links_cmd" ]
        then
           links_cmd=$(echo "--link $link ")
        else
           links_cmd=$(echo $links_cmd "--link $link ")
        fi
    done
fi
echo "LINKS:"$links_cmd

if [ ! -z "$VOLUMES_IMAGES" ]
then
    for volumes_images in $VOLUMES_IMAGES; do
        if [ -z "$volumes_images_cmd" ]
        then
           volumes_images_cmd=$(echo "--volumes-from $volumes_images ")
        else
           volumes_images_cmd=$(echo $volumes_images_cmd "--volumes-from $volumes_images ")
        fi
    done
fi
echo "VOLUMES-FROM:"$volumes_images_cmd

if [ ! -z "$ADD_HOSTS" ]
then
    for hostserver in $ADD_HOSTS; do
        if [ -z "$add_hosts_cmd" ]
        then
           add_hosts_cmd=$(echo "--add-host=$hostserver ")
        else
           add_hosts_cmd=$(echo $add_hosts_cmd "--add-host=$hostserver ")
        fi
    done
fi
echo "ADD_HOST:"$add_hosts_cmd

if [ ! -z "$CAP_ADD" ]
then
    for capadd in $CAP_ADD; do
        if [ -z "$capadds_cmd" ]
        then
           capadds_cmd=$(echo "--cap-add=$capadd ")
        else
           capadds_cmd=$(echo $capadds_cmd "--cap-add=$capadd ")
        fi
    done
fi
echo "CAP_ADD:"$capadds_cmd

#-------------------------------------------------------------------------------
install_docker(){
    echo "Install lxc-docker-1.4.1"

    if [ $(lsb_release -a | sed -n '/Description/p' | grep -c "Ubuntu 14.04") -eq 1 ];
    then 
        echo "Ubuntu 14.04"
        if [ $(dpkg-query -W -f='${Status}' lxc-docker-1.4.1 2>/dev/null | grep -c "ok installed") -eq 0 ];
        then
            echo "sudo get -qO- https://get.docker.io/gpg" | sh || true
            echo "sudo apt-key add -" | sh || true    
            echo "sudo sh -c "'"echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"' | sh || true
            echo "sudo apt-get update" | sh || true                                             
            echo "sudo apt-get install -y lxc-docker-1.4.1" | sh || true
        else
            echo $(docker --version)
        fi                                                                          
    elif [ $(lsb_release -a | sed -n '/Description/p' | grep -c "Ubuntu 12.04") -eq 1 ];
    then
        echo "Ubuntu 12.04"
        if [ $(dpkg-query -W -f='${Status}' lxc-docker-1.4.1 2>/dev/null | grep -c "ok installed") -eq 0 ];
        then
            echo "sudo apt-get update" | sh || true
            echo "sudo apt-get install -y linux-image-generic-lts-raring linux-headers-generic-lts-raring" | sh || true
            echo "sudo apt-get install --install-recommends linux-generic-lts-raring xserver-xorg-lts-raring libgl1-mesa-glx-lts-raring" | sh || true
            echo "sudo apt-get update" | sh || true
            echo "sudo apt-get install -y apt-transport-https" | sh || true
            
            echo "sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9" | sh || true         
            echo "sudo sh -c "'"echo deb https://get.docker.com/ubuntu docker main\ > /etc/apt/sources.list.d/docker.list"' | sh || true
            echo "sudo apt-get update" | sh || true                                             
            echo "sudo apt-get install -y lxc-docker-1.4.1" | sh || true
        else
            echo $(docker --version)
        fi
    else
        echo "It does not install lxc-docker-1.4.1"
    fi

}
#-------------------------------------------------------------------------------
attach(){
    echo "Attaching docker containers:"
    docker exec -it $container script /dev/null -c /bin/bash || true
}
#-------------------------------------------------------------------------------
status(){
    echo "Status docker containers:"
    docker ps | grep "$container" || true
}
#-------------------------------------------------------------------------------
build(){
    echo "Build docker images:"$imags
    #### Add proxy on Dockerfile ####
    if [ ! $(env | grep -c "proxy") -eq 0 ];
    then
        PROXYS=$(env | grep "proxy" )
        echo "Enabled Proxy\n"$PROXYS
        for proxy in $PROXYS; do
           name=$(echo $proxy | sed 's/\=/ /g' | awk '{print $1}')
           ip=$(echo $proxy | sed 's/\=/ /g' | awk '{print $2}' | sed 's/,/\\,/g')
           sed -i 's,^#ENV '"$name"'.*,ENV '"$name $ip"',' Dockerfile
        done
    fi

    docker build --rm -t $images .
}
#-------------------------------------------------------------------------------
rebuild(){
    echo "Rebuild docker images:"
    docker build --no-cache --rm -t $images .
}
#-------------------------------------------------------------------------------
stop(){
    echo "Stopping docker containers:"
    docker stop $container || true
    docker ps | grep "$container" || true
}
#-------------------------------------------------------------------------------
start(){
    echo "Starting ${container}:"
    echo "    docker run -d --name $container \
                  $hostname_cmd        \ 
                  $privileged_cmd      \
                  $links_cmd           \
                  $ports_cmd           \
                  $envlist_cmd         \
                  $volumes_cmd         \
                  $volumes_images_cmd  \
                  $add_hosts_cmd       \
                  $capadds_cmd         \
                  $images
         "
    if [ ! -z "$links_cmd" ]
    then
        for link in $LINK_IMAGES; do
            link_sh=$(echo "$link" | cut -d ':' -f1)
            if [ -z "$(docker ps | grep "$link_sh"  | awk '{print $NF}')" ]
            then
               echo "Please you must be run the" "$link_sh"" before the" "$container"
               exit
            fi
        done
    fi
    for run_container in $(docker ps -a | grep "$container" | awk '{print $NF}'); do
    	if [ "$run_container" =  "$container" ];
    	then
    		docker start $container
		docker ps | grep "$container" || true
		exit
    	fi
    done
    docker run -d --name $container    \
                  $hostname_cmd        \
                  $privileged_cmd      \
                  $links_cmd           \
                  $ports_cmd           \
                  $envlist_cmd         \
                  $volumes_cmd         \
                  $volumes_images_cmd  \
                  $add_hosts_cmd       \
                  $capadds_cmd         \
                  $images

    docker ps | grep "$container" || true
}

#-------------------------------------------------------------------------------
kill(){
    echo "Killing docker containers:"
    docker kill $container || true
}
#-------------------------------------------------------------------------------
rm(){
    echo "Removing stopped containers:"
    docker rm $container || true
}
#-------------------------------------------------------------------------------
rmi(){
    echo "Removing all untagged images:"
    docker images | grep "^<none>" | awk '{print "docker rmi "$3}' | sh
}
#-------------------------------------------------------------------------------
logs(){
    echo "Show logs of a container"
    docker logs $container || true
}
#-------------------------------------------------------------------------------
cp(){
    echo "Copy files/folders from a container's filesystem"
    echo "docker cp $container:$1 $2 || true"
    docker cp $container:$1 $2 || true
}
#-------------------------------------------------------------------------------
pull(){
    echo "Pull an image or a repository from a Docker registry server"
    docker pull $images || true
}
#-------------------------------------------------------------------------------
push(){
    echo "Push an image or a repository to a Docker registry server"
    docker push $images || true
}
#-------------------------------------------------------------------------------
inspect(){
    echo "Return low-level information on a containe"
    docker inspect $container || true
}
#-------------------------------------------------------------------------------
top(){
    echo "Lookup the running processes of a container"
    docker top $container || true
}
#-------------------------------------------------------------------------------
save(){
    echo "Save an image to a tar archive"
    docker save -o $container"-"$TAG"-docker-image.tar.gz" $images || true
    echo $(pwd | awk -F '/' '{print $NF}')
    tar cvfzp ../$container"-"$TAG"-docker-script.tar.gz" ../$(pwd | awk -F '/' '{print $NF}')
    echo "Save $container-$TAG"
}
#-------------------------------------------------------------------------------
load(){
    echo "Load an image from a tar archive"
    docker load -i $container"-"$TAG"-docker-image.tar.gz" || true
    echo "Load $container-$TAG"
}
#-------------------------------------------------------------------------------
help(){
    echo "help"
    usage
}
#-------------------------------------------------------------------------------
usage (){
    echo "USAGE: $0" COMMAND 

    echo -e "\nCommands:"
    echo "    install   Install lxc-docker-1.4.1"
    echo "    start     Start a stopped container"
    echo "    attach    Attach to a running container"
    echo "    build     Build an image from a Dockerfile"
    echo "    stop      Stop a running container"
    echo "    status    Status a running container" 
    echo "    rm        Remove this containers"
    echo "    rmi       Remove all  <none> images"
    echo "    restart   stop, start a container"
    echo "    rebuild   Rebuild with no-cache options"
    echo "    kill      Kill a running container"
    echo "    logs      Fetch the logs of a container"
    echo "    cp        Copy files/folders from a container's filesystem to the host path"
    echo "    pull      Pull an image or a repository from a Docker registry server"
    echo "    push      Push an image or a repository to a Docker registry server"
    echo "    inspect   Return low-level information on a containe"
    echo "    top       Lookup the running processes of a container"
    echo "    save      Save an image to a tar archive"
    echo "    load      Load an image from a tar archive"
    echo "    help      help"
    exit 1
}
#-------------------------------------------------------------------------------
if [ $# -eq 0 ]
then
    usage
fi

#-------------------------------------------------------------------------------
case "$1" in
    install)
        install_docker
        ;;
    start)
        start
        ;;
    attach)
        attach
        ;;
    status)
        status
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    build)
        build
        ;;
    rebuild)
        rebuild
        ;;
    kill)
        kill
        rm
        ;;
    rm)
        rm
        ;;
    rmi)
        rmi
        ;;
    logs)
        logs
        ;;
    cp)
	cp $2 $3
        ;;
    pull)
        pull
        ;;
    push)
        push
        ;;
    inspect)
        inspect
        ;;
    top)
        top
        ;;
    save)
        save
        ;;
    load)
        load
        ;;
    help)
        help
        ;;
    *)
        usage
        ;;
esac
#===============================================================================