Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Bartek Grzybowski | 27e6584 | 2020-10-29 15:31:12 +0100 | [diff] [blame] | 3 | set -eo pipefail |
| 4 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 5 | # Set distribution family |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 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 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 20 | # Target path for created repository |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 21 | OFFLINE_REPO_DIR="" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 22 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 23 | # Path to directory containing onap_rpm.list and onap_deb.list files |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 24 | PCKG_LIST_DIR="" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 25 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 26 | # Path to additional packages lists |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 27 | ADD_LIST_DIR="" |
| 28 | |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 29 | # Use cache by default |
| 30 | drop_cache=false |
| 31 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 32 | # Show help |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 33 | help () { |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 34 | cat <<EOF |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 35 | Docker entrypoint script for creating RPM/DEB repository based on container platform type |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 36 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 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 |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 43 | -r | --drop-cache remove cached packages (use package cache by default) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 44 | -h | --help show this help |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 45 | |
| 46 | Both paths have to be set with shared volume between |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 47 | container and the host. Default path in container is: /tmp/ |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 48 | Repository will be created at: /<path>/resources/pkg/rhel/ |
| 49 | RMP/DEB list is stored at: ./data_list/ |
| 50 | EOF |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 53 | # Getting input parameters |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 54 | if [[ $# -eq 0 ]] ; then |
| 55 | help # show help |
| 56 | exit 0 |
| 57 | fi |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 58 | while [[ $# -gt 0 ]] |
| 59 | do |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 60 | case "$1" in |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 61 | -h|--help) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 62 | # Help parameter |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 63 | help # show help |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 64 | exit |
| 65 | ;; |
| 66 | -d|--directory) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 67 | # Directory parameter |
| 68 | # Set target reposity path |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 69 | OFFLINE_REPO_DIR="$2" |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 70 | shift |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 71 | ;; |
| 72 | -l|--list) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 73 | # List parameter |
| 74 | # Set path containing onap_rpm.list or onap_deb.list file |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 75 | PCKG_LIST_DIR="$2" |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 76 | shift |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 77 | ;; |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 78 | -p|--packages-lists-path) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 79 | # Path parameter |
| 80 | # Set path for additional packages lists |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 81 | ADD_LIST_DIR="$2" |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 82 | shift |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 83 | ;; |
| 84 | -a|--additional-list) |
| 85 | # Array of additional packages lists |
| 86 | ADDITIONAL_LISTS+=("$2") |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 87 | shift |
| 88 | ;; |
| 89 | -r|--drop-cache) |
| 90 | # Set flag to clean cache |
| 91 | drop_cache=true |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 92 | ;; |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 93 | *) |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 94 | # unknown option |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 95 | help # show help |
| 96 | exit |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 97 | ;; |
| 98 | esac |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 99 | shift |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 100 | done |
| 101 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 102 | # Testing if directory parameter was used |
| 103 | # If not variable is set to /tmp/repo by default |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 104 | if test -z "$OFFLINE_REPO_DIR" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 105 | then |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 106 | OFFLINE_REPO_DIR="/tmp/repo/" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 107 | fi |
| 108 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 109 | # Testing if list parameter was used |
| 110 | # If not variable is set to default value /tmp/offline/data-list |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 111 | if test -z "$PCKG_LIST_DIR" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 112 | then |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 113 | PCKG_LIST_DIR="/tmp/offline/data_list" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 114 | fi |
| 115 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 116 | # Testing if additional packages list parameter was used |
| 117 | # If not variable is set to default value /tmp/additional-lists |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 118 | if test -z "$PCKG_LIST_DIR" |
| 119 | then |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 120 | PCKG_LIST_DIR="/tmp/additional-lists" |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 121 | fi |
| 122 | |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 123 | # Clean target repo dir if --drop-cache set |
| 124 | if ${drop_cache}; |
| 125 | then |
| 126 | rm -rf ${OFFLINE_REPO_DIR}/* |
| 127 | fi |
Bartek Grzybowski | 524ed12 | 2020-10-29 14:03:50 +0100 | [diff] [blame] | 128 | |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 129 | case "$distro_type" in |
| 130 | ubuntu) |
| 131 | # Change current working dir |
| 132 | pushd $OFFLINE_REPO_DIR |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 133 | |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 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 |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 139 | |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 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 |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 143 | |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 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 | |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 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 |
Bartek Grzybowski | ce6f9f1 | 2021-02-19 14:37:12 +0100 | [diff] [blame] | 167 | for i in $(cat ${list_file});do apt-get download $i -y; done |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 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 |
Bartek Grzybowski | ce6f9f1 | 2021-02-19 14:37:12 +0100 | [diff] [blame] | 172 | apt-get download $depends -y; |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 173 | done; |
| 174 | done |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 175 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 176 | # Download all packages with dependencies from all additional packages lists via apt-get to repository folder |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 177 | if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then |
| 178 | for list in ${ADDITIONAL_LISTS[@]} |
| 179 | do |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 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 |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 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 |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 205 | |
| 206 | # In repository folder create gz package with deb packages |
| 207 | dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz |
| 208 | ;; |
| 209 | |
| 210 | rhel) |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 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 |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 215 | |
| 216 | # Add official docker repository |
| 217 | yum-config-manager --add-repo=https://download.docker.com/linux/centos/7/x86_64/stable/ |
| 218 | |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 219 | # Create tmp file for package list |
| 220 | list_file=$(mktemp) |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 221 | |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 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 |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 236 | if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then |
| 237 | for list in ${ADDITIONAL_LISTS[@]} |
| 238 | do |
Bartek Grzybowski | 7f8b8e7 | 2020-11-20 12:51:17 +0100 | [diff] [blame] | 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 |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 255 | done |
| 256 | fi |
| 257 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame] | 258 | # Create repository |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 259 | createrepo $OFFLINE_REPO_DIR |
| 260 | ;; |
| 261 | |
| 262 | *) |
| 263 | echo "Unknown type of linux distribution." |
| 264 | exit 1 |
| 265 | ;; |
| 266 | esac |