18870: Need to declare NODES as array
[arvados.git] / docker / migrate-docker19 / migrate.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: Apache-2.0
5
6 # This script is called by arv-migrate-docker19 to perform the actual migration
7 # of a single image.  This works by running Docker-in-Docker (dnd.sh) to
8 # download the image using Docker 1.9 and then upgrading to Docker 1.13 and
9 # uploading the converted image.
10
11 # When using bash in pid 1 and using "trap on EXIT"
12 # it will sometimes go into an 100% CPU infinite loop.
13 #
14 # Using workaround from here:
15 #
16 # https://github.com/docker/docker/issues/4854
17 if [ "$$" = 1 ]; then
18   $0 "$@"
19   exit $?
20 fi
21
22 # -x           show script
23 # -e           exit on error
24 # -o pipefail  use exit code from 1st failure in pipeline, not last
25 set -x -e -o pipefail
26
27 image_tar_keepref=$1
28 image_id=$2
29 image_repo=$3
30 image_tag=$4
31 project_uuid=$5
32 graph_driver=$6
33
34 if [[ "$image_repo" = "<none>" ]] ; then
35   image_repo=none
36   image_tag=latest
37 fi
38
39 # Print free space in /var/lib/docker
40 function freespace() {
41     df -B1 /var/lib/docker | tail -n1 | sed 's/  */ /g' | cut -d' ' -f4
42 }
43
44 # Run docker-in-docker script and then wait for it to come up
45 function start_docker {
46     /root/dnd.sh $graph_driver &
47     for i in $(seq 1 10) ; do
48         if docker version >/dev/null 2>/dev/null ; then
49             return
50         fi
51         sleep 1
52     done
53     false
54 }
55
56 # Kill docker from pid then wait for it to be down
57 function kill_docker {
58     if test -f /var/run/docker.pid ; then
59         kill $(cat /var/run/docker.pid)
60     fi
61     for i in $(seq 1 10) ; do
62         if ! docker version >/dev/null 2>/dev/null ; then
63             return
64         fi
65         sleep 1
66     done
67     false
68 }
69
70 # Ensure that we clean up docker graph and/or lingering cache files on exit
71 function cleanup {
72     kill_docker
73     rm -rf /var/lib/docker/*
74     rm -rf /root/.cache/arvados/docker/*
75     echo "Available space after cleanup is $(freespace)"
76 }
77
78 trap cleanup EXIT
79
80 start_docker
81
82 echo "Initial available space is $(freespace)"
83
84 arv-get $image_tar_keepref | docker load
85
86
87 docker tag $image_id $image_repo:$image_tag
88
89 docker images -a
90
91 kill_docker
92
93 echo "Available space after image load is $(freespace)"
94
95 cd /root/pkgs
96 dpkg -i libltdl7_2.4.2-1.11+b1_amd64.deb docker-engine_1.13.1-0~debian-jessie_amd64.deb
97
98 echo "Available space after image upgrade is $(freespace)"
99
100 start_docker
101
102 docker images -a
103
104 if [[ "$image_repo" = "none" ]] ; then
105   image_repo=$(docker images -a --no-trunc | sed 's/  */ /g' | grep ^none | cut -d' ' -f3)
106   image_tag=""
107 fi
108
109 UUID=$(arv-keepdocker --force-image-format --project-uuid=$project_uuid $image_repo $image_tag)
110
111 echo "Available space after arv-keepdocker is $(freespace)"
112
113 echo "Migrated uuid is $UUID"