Merge "[GENERAL] Add Andreas Geissler as committer."
[oom/offline-installer.git] / build / docker-entrypoint.sh
1 #!/usr/bin/env bash
2
3 set -eo pipefail
4
5 # Set distribution family
6 distro_type=$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')
7 case "$distro_type" in
8         ubuntu)
9                 distro_type="ubuntu"
10         ;;
11         rhel|centos)
12                 distro_type="rhel"
13         ;;
14         *)
15                 echo "Unknown type of linux distribution."
16                 exit 1
17         ;;
18 esac
19
20 # Target path for created repository
21 OFFLINE_REPO_DIR=""
22
23 # Path to directory containing onap_rpm.list and onap_deb.list files
24 PCKG_LIST_DIR=""
25
26 # Path to additional packages lists
27 ADD_LIST_DIR=""
28
29 # Use cache by default
30 drop_cache=false
31
32 # Show help
33 help () {
34 cat <<EOF
35 Docker entrypoint script for creating RPM/DEB repository based on container platform type
36
37 usage: create-repo.sh [OPTION]...
38
39   -d | --directory              target repository path
40   -l | --list                   input rpm/deb list directory
41   -a | --additional-list        additional packages list; can be used multiple times
42   -p | --packages-lists-path    other additional packages lists
43   -r | --drop-cache             remove cached packages (use package cache by default)
44   -h | --help                   show this help
45
46 Both paths have to be set with shared volume between
47 container and the host. Default path in container is: /tmp/
48 Repository will be created at: /<path>/resources/pkg/rhel/
49 RMP/DEB list is stored at: ./data_list/
50 EOF
51 }
52
53 # Getting input parameters
54 if [[ $# -eq 0 ]] ; then
55     help # show help
56     exit 0
57 fi
58 while [[ $# -gt 0 ]]
59 do
60     case "$1" in
61         -h|--help)
62             # Help parameter
63             help # show help
64             exit
65             ;;
66         -d|--directory)
67             # Directory parameter
68             # Set target reposity path
69             OFFLINE_REPO_DIR="$2"
70             shift
71             ;;
72         -l|--list)
73             # List parameter
74             # Set path containing onap_rpm.list or onap_deb.list file
75             PCKG_LIST_DIR="$2"
76             shift
77             ;;
78         -p|--packages-lists-path)
79             # Path parameter
80             # Set path for additional packages lists
81             ADD_LIST_DIR="$2"
82             shift
83             ;;
84         -a|--additional-list)
85             # Array of additional packages lists
86             ADDITIONAL_LISTS+=("$2")
87             shift
88             ;;
89         -r|--drop-cache)
90             # Set flag to clean cache
91             drop_cache=true
92             ;;
93         *)
94             # unknown option
95             help # show help
96             exit
97             ;;
98     esac
99     shift
100 done
101
102 # Testing if directory parameter was used
103 # If not variable is set to /tmp/repo by default
104 if test -z "$OFFLINE_REPO_DIR"
105 then
106     OFFLINE_REPO_DIR="/tmp/repo/"
107 fi
108
109 # Testing if list parameter was used
110 # If not variable is set to default value /tmp/offline/data-list
111 if test -z "$PCKG_LIST_DIR"
112 then
113     PCKG_LIST_DIR="/tmp/offline/data_list"
114 fi
115
116 # Testing if additional packages list parameter was used
117 # If not variable is set to default value /tmp/additional-lists
118 if test -z "$PCKG_LIST_DIR"
119 then
120     PCKG_LIST_DIR="/tmp/additional-lists"
121 fi
122
123 # Clean target repo dir if --drop-cache set
124 if ${drop_cache};
125 then
126     rm -rf ${OFFLINE_REPO_DIR}/*
127 fi
128
129 case "$distro_type" in
130     ubuntu)
131         # Change current working dir
132         pushd $OFFLINE_REPO_DIR
133
134         # Install dpkg-deb package for create repository in folder
135         # Install software-properties-common to get add-apt-repository command
136         # Install apt-transport-https, ca-certificates, curl and gnupg-agent allowing apt to use a repository over HTTPS
137         apt-get update -y
138         apt-get install dpkg-dev apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
139
140         # Add Docker's official GPG key:
141         curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
142         apt-key fingerprint 0EBFCD88
143
144         # Add docker repository
145         add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
146
147         # Temp fix of known bug
148         # https://bugs.launchpad.net/ubuntu/+source/aptitude/+bug/1543280
149         chown _apt $OFFLINE_REPO_DIR
150
151         # Create tmp file for package list
152         list_file=$(mktemp)
153
154         # Enumerate packages that are already downloaded
155         for package in $(cat ${PCKG_LIST_DIR}/onap_deb.list);
156         do
157             # If package name contains explicit version info cut the version string off for further processing
158             p=$(echo $package |sed -r 's/=.*//')
159             # Add package to download list only if it's not already there
160             if [ $(ls ${p}_*.deb 2>/dev/null | wc -l) -eq 0 ];
161             then
162                 echo ${package} >> ${list_file}
163             fi
164         done
165
166         # Download all packages via apt-get to repository folder
167         for i in $(cat ${list_file});do apt-get download $i -y; done
168         for i in $(cat ${list_file});
169             do
170                 for depends in $(apt-cache depends $i | grep -E 'Depends' | grep -v 'Depends:.*>$' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
171                 do
172                     apt-get download $depends -y;
173                 done;
174             done
175
176         # Download all packages with dependencies from all additional packages lists via apt-get to repository folder
177         if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
178             for list in ${ADDITIONAL_LISTS[@]}
179             do
180
181                 # Create tmp file for package list
182                 list_file=$(mktemp)
183
184                 # Enumerate packages that are already downloaded
185                 for package in $(cat ${ADD_LIST_DIR}/${list});
186                 do
187                     # If package name contains explicit version info cut the version string off for further processing
188                     p=$(echo $package |sed -r 's/=.*//')
189                     # Add package to download list only if it's not already there
190                     if [ $(ls ${p}_*.deb 2>/dev/null | wc -l) -eq 0 ];
191                     then
192                         echo ${package} >> ${list_file}
193                     fi
194                 done
195
196                 for i in $(cat ${list_file});do apt-get download $i -y; done
197                 for i in $(cat ${list_file});
198                 do
199                     for depends in $(apt-cache depends $i | grep -E 'Depends' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
200                         do apt-get download $depends -y;
201                     done;
202                 done
203             done
204         fi
205
206         # In repository folder create gz package with deb packages
207         dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
208     ;;
209
210     rhel)
211         # Install createrepo package for create repository in folder,
212         # yum-utils due to yum-config-manager for adding docker repository
213         # and epel-release for additional packages (like jq etc.)
214         yum install createrepo yum-utils epel-release -y
215
216         # Add official docker repository
217         yum-config-manager --add-repo=https://download.docker.com/linux/centos/7/x86_64/stable/
218
219         # Create tmp file for package list
220         list_file=$(mktemp)
221
222         # Enumerate packages that are already downloaded
223         for package in $(cat ${PCKG_LIST_DIR}/onap_rpm.list);
224         do
225             # Add package to download list only if it's not already there
226             if [ ! -f ${OFFLINE_REPO_DIR}/${package}.rpm ];
227             then
228                 echo ${package} >> ${list_file}
229             fi
230         done
231
232         # Download all packages from onap_rpm.list via yumdownloader to repository folder
233         for i in $(cat ${list_file});do yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y; done
234
235         # Download all packages from all additional packages lists via yumdownloader to repository folder
236         if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
237             for list in ${ADDITIONAL_LISTS[@]}
238             do
239                 # Create tmp file for additional package list
240                 list_file=$(mktemp)
241                 # Enumerate packages that are already downloaded
242                 for package in $(cat ${ADD_LIST_DIR}/${list});
243                 do
244                     # Add package to download list only if it's not already there
245                     if [ ! -f ${OFFLINE_REPO_DIR}/${package}.rpm ];
246                     then
247                         echo ${package} >> ${list_file}
248                     fi
249                 done
250
251                 for i in $(cat ${list_file});
252                 do
253                     yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y
254                 done
255             done
256         fi
257
258         # Create repository
259         createrepo $OFFLINE_REPO_DIR
260     ;;
261
262     *)
263         echo "Unknown type of linux distribution."
264         exit 1
265     ;;
266 esac