blob: d6f526d3bdd99ddfacb115ef1a4ef5105b255b42 [file] [log] [blame]
Jan Benedikt7c0f6b12019-10-08 10:01:41 -04001#!/usr/bin/env bash
2
Bartek Grzybowski27e65842020-10-29 15:31:12 +01003set -eo pipefail
4
Bartek Grzybowski237a9222020-10-27 10:50:35 +01005# Set distribution family
Jan Benedikt77459fe2020-02-10 13:46:52 +01006distro_type=$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')
7case "$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 ;;
18esac
19
Bartek Grzybowski237a9222020-10-27 10:50:35 +010020# Target path for created repository
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040021OFFLINE_REPO_DIR=""
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040022
Bartek Grzybowski237a9222020-10-27 10:50:35 +010023# Path to directory containing onap_rpm.list and onap_deb.list files
Jan Benedikt77459fe2020-02-10 13:46:52 +010024PCKG_LIST_DIR=""
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040025
Bartek Grzybowski237a9222020-10-27 10:50:35 +010026# Path to additional packages lists
Jan Benediktfdec88e2020-03-19 16:03:25 +010027ADD_LIST_DIR=""
28
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010029# Use cache by default
30drop_cache=false
31
Bartek Grzybowski237a9222020-10-27 10:50:35 +010032# Show help
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040033help () {
Jan Benediktfdec88e2020-03-19 16:03:25 +010034cat <<EOF
Bartek Grzybowski237a9222020-10-27 10:50:35 +010035Docker entrypoint script for creating RPM/DEB repository based on container platform type
Jan Benediktfdec88e2020-03-19 16:03:25 +010036
Bartek Grzybowski237a9222020-10-27 10:50:35 +010037usage: 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
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010043 -r | --drop-cache remove cached packages (use package cache by default)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010044 -h | --help show this help
Jan Benediktfdec88e2020-03-19 16:03:25 +010045
46Both paths have to be set with shared volume between
Bartek Grzybowski237a9222020-10-27 10:50:35 +010047container and the host. Default path in container is: /tmp/
Jan Benediktfdec88e2020-03-19 16:03:25 +010048Repository will be created at: /<path>/resources/pkg/rhel/
49RMP/DEB list is stored at: ./data_list/
50EOF
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040051}
52
Bartek Grzybowski237a9222020-10-27 10:50:35 +010053# Getting input parameters
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040054if [[ $# -eq 0 ]] ; then
55 help # show help
56 exit 0
57fi
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040058while [[ $# -gt 0 ]]
59do
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040060 case "$1" in
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040061 -h|--help)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010062 # Help parameter
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040063 help # show help
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040064 exit
65 ;;
66 -d|--directory)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010067 # Directory parameter
68 # Set target reposity path
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040069 OFFLINE_REPO_DIR="$2"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010070 shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040071 ;;
72 -l|--list)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010073 # List parameter
74 # Set path containing onap_rpm.list or onap_deb.list file
Jan Benedikt77459fe2020-02-10 13:46:52 +010075 PCKG_LIST_DIR="$2"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010076 shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040077 ;;
Jan Benediktfdec88e2020-03-19 16:03:25 +010078 -p|--packages-lists-path)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010079 # Path parameter
80 # Set path for additional packages lists
Jan Benediktfdec88e2020-03-19 16:03:25 +010081 ADD_LIST_DIR="$2"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010082 shift
Jan Benediktfdec88e2020-03-19 16:03:25 +010083 ;;
84 -a|--additional-list)
85 # Array of additional packages lists
86 ADDITIONAL_LISTS+=("$2")
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010087 shift
88 ;;
89 -r|--drop-cache)
90 # Set flag to clean cache
91 drop_cache=true
Jan Benediktfdec88e2020-03-19 16:03:25 +010092 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040093 *)
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040094 # unknown option
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040095 help # show help
96 exit
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040097 ;;
98 esac
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010099 shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400100done
101
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100102# Testing if directory parameter was used
103# If not variable is set to /tmp/repo by default
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400104if test -z "$OFFLINE_REPO_DIR"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400105then
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400106 OFFLINE_REPO_DIR="/tmp/repo/"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400107fi
108
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100109# Testing if list parameter was used
110# If not variable is set to default value /tmp/offline/data-list
Jan Benedikt77459fe2020-02-10 13:46:52 +0100111if test -z "$PCKG_LIST_DIR"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400112then
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100113 PCKG_LIST_DIR="/tmp/offline/data_list"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400114fi
115
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100116# Testing if additional packages list parameter was used
117# If not variable is set to default value /tmp/additional-lists
Jan Benediktfdec88e2020-03-19 16:03:25 +0100118if test -z "$PCKG_LIST_DIR"
119then
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100120 PCKG_LIST_DIR="/tmp/additional-lists"
Jan Benediktfdec88e2020-03-19 16:03:25 +0100121fi
122
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100123# Clean target repo dir if --drop-cache set
124if ${drop_cache};
125then
126 rm -rf ${OFFLINE_REPO_DIR}/*
127fi
Bartek Grzybowski524ed122020-10-29 14:03:50 +0100128
Jan Benedikt77459fe2020-02-10 13:46:52 +0100129case "$distro_type" in
130 ubuntu)
131 # Change current working dir
132 pushd $OFFLINE_REPO_DIR
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400133
Jan Benedikt77459fe2020-02-10 13:46:52 +0100134 # 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
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400139
Jan Benedikt77459fe2020-02-10 13:46:52 +0100140 # Add Docker's official GPG key:
141 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
142 apt-key fingerprint 0EBFCD88
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400143
Jan Benedikt77459fe2020-02-10 13:46:52 +0100144 # 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
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100151 # 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
Bartek Grzybowskice6f9f12021-02-19 14:37:12 +0100167 for i in $(cat ${list_file});do apt-get download $i -y; done
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100168 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
Bartek Grzybowskice6f9f12021-02-19 14:37:12 +0100172 apt-get download $depends -y;
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100173 done;
174 done
Jan Benediktfdec88e2020-03-19 16:03:25 +0100175
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100176 # Download all packages with dependencies from all additional packages lists via apt-get to repository folder
Jan Benediktfdec88e2020-03-19 16:03:25 +0100177 if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
178 for list in ${ADDITIONAL_LISTS[@]}
179 do
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100180
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
Jan Benediktfdec88e2020-03-19 16:03:25 +0100199 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
Jan Benedikt77459fe2020-02-10 13:46:52 +0100205
206 # In repository folder create gz package with deb packages
207 dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
208 ;;
209
210 rhel)
Jan Benediktfdec88e2020-03-19 16:03:25 +0100211 # 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
Jan Benedikt77459fe2020-02-10 13:46:52 +0100215
216 # Add official docker repository
217 yum-config-manager --add-repo=https://download.docker.com/linux/centos/7/x86_64/stable/
218
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100219 # Create tmp file for package list
220 list_file=$(mktemp)
Jan Benedikt77459fe2020-02-10 13:46:52 +0100221
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100222 # 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
Jan Benediktfdec88e2020-03-19 16:03:25 +0100236 if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
237 for list in ${ADDITIONAL_LISTS[@]}
238 do
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100239 # 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
Jan Benediktfdec88e2020-03-19 16:03:25 +0100255 done
256 fi
257
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100258 # Create repository
Jan Benedikt77459fe2020-02-10 13:46:52 +0100259 createrepo $OFFLINE_REPO_DIR
260 ;;
261
262 *)
263 echo "Unknown type of linux distribution."
264 exit 1
265 ;;
266esac