blob: f2bde32d7c69a7620fd14b838fbdd63dc3c5163b [file] [log] [blame]
Jan Benedikt7c0f6b12019-10-08 10:01:41 -04001#!/usr/bin/env bash
2
Bartek Grzybowski237a9222020-10-27 10:50:35 +01003# Set distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +01004distro_type="$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')"
5
Bartek Grzybowski237a9222020-10-27 10:50:35 +01006# Path to cloned offline-installer build directory with docker_entrypoint script
Jan Benedikt8fdbfe72019-10-15 06:07:46 -04007volume_offline_directory="$(readlink -f $(dirname ${0}))"
Jan Benedikt77459fe2020-02-10 13:46:52 +01008
Bartek Grzybowski237a9222020-10-27 10:50:35 +01009# Destination path for created repository
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040010volume_repo_directory="$(pwd)"
Jan Benedikt77459fe2020-02-10 13:46:52 +010011
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040012# Path inside container with cloned offline-installer build directory
13container_offline_volume="/mnt/offline/"
Jan Benedikt77459fe2020-02-10 13:46:52 +010014
Bartek Grzybowski237a9222020-10-27 10:50:35 +010015# Target repository path inside container
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040016container_repo_volume="/mnt/repo/"
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040017
Bartek Grzybowski237a9222020-10-27 10:50:35 +010018# Additional packages lists files path within container
Jan Benediktfdec88e2020-03-19 16:03:25 +010019container_list_volume="/mnt/additional-lists/"
20
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010021# Use cache by default
22drop_cache=false
23
Bartek Grzybowski237a9222020-10-27 10:50:35 +010024# Show script usage
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040025help () {
Jan Benediktfdec88e2020-03-19 16:03:25 +010026cat <<EOF
Bartek Grzybowski237a9222020-10-27 10:50:35 +010027Wrapper script running docker container for creating package repository
Jan Benediktfdec88e2020-03-19 16:03:25 +010028
Bartek Grzybowski237a9222020-10-27 10:50:35 +010029Repository type is set with --target-platform option and the default is to use host OS platform type
Jan Benediktfdec88e2020-03-19 16:03:25 +010030
Bartek Grzybowski237a9222020-10-27 10:50:35 +010031usage: create_repo.sh [OPTION]...
Jan Benediktfdec88e2020-03-19 16:03:25 +010032
Bartek Grzybowski237a9222020-10-27 10:50:35 +010033
34 -d | --destination-repository target path to store downloaded packages. Current directory by default
35 -c | --cloned-directory path to directory containing this and docker-entrypoint scripts (offline-installer/build directory)
36 Set it only when you want to use different script/datalists
37 -t | --target-platform target repository platform type (ubuntu/rhel/centos)
38 -a | --additional-list additional packages list; can be used multiple times for more additional lists
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010039 -n | --container-name-suffix add custom suffix to docker container name
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010040 -r | --drop-cache remove cached packages (use package cache by default)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010041 -h | --help show this help
42
43If build folder from offline repository is not specified current one will be used by default.
Jan Benediktfdec88e2020-03-19 16:03:25 +010044EOF
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040045}
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040046
Bartek Grzybowski237a9222020-10-27 10:50:35 +010047# Get distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +010048# Set Docker image name and version based on type of linux distribution
49# Set expected directory for RPM/DEB packages
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010050set_environment () {
Jan Benedikt77459fe2020-02-10 13:46:52 +010051 case "$1" in
52 ubuntu)
53 distro_type="ubuntu"
54 docker_image="ubuntu:18.04"
55 expected_dir="resources/pkg/deb"
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010056 container_name="${1}_repo${container_name_suffix}"
Jan Benedikt77459fe2020-02-10 13:46:52 +010057 ;;
58 centos|rhel)
59 distro_type="rhel"
60 docker_image="centos:centos7.6.1810"
61 expected_dir="resources/pkg/rpm"
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010062 container_name="${1}_repo${container_name_suffix}"
Jan Benedikt77459fe2020-02-10 13:46:52 +010063 ;;
64 *)
65 echo "Unknown type of linux distribution."
66 exit 1
67 ;;
68 esac
69}
70
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040071# Getting input parametters
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040072if [[ $# -eq 0 ]] ; then
73 help # show help
74 exit 0
75fi
Jan Benedikt77459fe2020-02-10 13:46:52 +010076
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040077while [[ $# -gt 0 ]]
78do
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040079 case "$1" in
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040080 -h|--help)
81 # Help parametter
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040082 help # show help
83 exit 0
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040084 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040085 -c|--cloned-directory)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010086 # Directory parameter
87 # Set path to offline-installer build directory
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040088 volume_offline_directory="$2"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010089 shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040090 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040091 -d|--destination-repository)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010092 # Repository directory parameter
93 # Set destination path for created repository
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040094 volume_repo_directory="$2"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +010095 shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040096 ;;
Jan Benediktfdec88e2020-03-19 16:03:25 +010097 -t|--target-platform)
Jan Benedikt77459fe2020-02-10 13:46:52 +010098 # Repository type (rpm/deb)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010099 # Set target platform for repository
Jan Benedikt77459fe2020-02-10 13:46:52 +0100100 target_input="$2"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100101 shift
Jan Benedikt77459fe2020-02-10 13:46:52 +0100102 ;;
Jan Benediktfdec88e2020-03-19 16:03:25 +0100103 -a|--additional-list)
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100104 # Array of additional packages lists
Jan Benediktfdec88e2020-03-19 16:03:25 +0100105 additional_lists+=("$2")
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100106 shift
Jan Benediktfdec88e2020-03-19 16:03:25 +0100107 ;;
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100108 -n|--container-name-suffix)
109 # Set custom container name suffix
110 container_name_suffix="_${2}"
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100111 shift
112 ;;
113 -r|--drop-cache)
114 # Set flag to clean cache
115 drop_cache=true
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100116 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400117 *)
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400118 # unknown option
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400119 help # show help
120 exit 1
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400121 ;;
122 esac
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100123 shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400124done
125
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100126# Check if user specified repository type
127# This setting has higher priority than distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +0100128if ! test -z "$target_input"
129then
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100130 set_environment "$target_input"
Jan Benedikt77459fe2020-02-10 13:46:52 +0100131else
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100132 set_environment "$distro_type"
Jan Benedikt77459fe2020-02-10 13:46:52 +0100133fi
134
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100135# Check if path contains expected components:
Jan Benedikt77459fe2020-02-10 13:46:52 +0100136# "resources/pkg/rpm" for Rhel/CentOS or
137# "resources/pkg/deb" for Ubuntu/Debian
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400138if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100139 # Create repo folder if it doesn't exist
Jan Benedikt77459fe2020-02-10 13:46:52 +0100140 case "$distro_type" in
141 ubuntu)
142 volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
143 ;;
144 rhel)
Bartek Grzybowskif6c503b2020-09-08 15:59:07 +0200145 volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm
Jan Benedikt77459fe2020-02-10 13:46:52 +0100146 ;;
147 esac
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400148 [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
149fi
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400150
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100151# Check if container is already running
152if [ ! $(docker ps -q -f name="^${container_name}$") ];
153then
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400154 # run repo container
155 # name of container $container_name
156 # docker entrypoint script from mounted volume
Jan Benediktfdec88e2020-03-19 16:03:25 +0100157 # with dynamic parameters
158 # mount additional packages lists to container
159 param_array=()
160 mounted_lists=()
161 param_array+=(--directory ${container_repo_volume})
162 param_array+=(--list ${container_offline_volume}data_lists/)
163 param_array+=(--packages-lists-path ${container_list_volume})
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100164 if ${drop_cache};
165 then
166 param_array+=(--drop-cache)
167 fi
Jan Benediktfdec88e2020-03-19 16:03:25 +0100168 [[ ! ${#additional_lists[@]} -eq 0 ]] && \
169 for array_list in "${additional_lists[@]}";
170 do
171 param_array+=(--additional-list "${array_list##*/}") && \
172 mounted_lists+=(-v ${array_list}:${container_list_volume}${array_list##*/})
173 done
174
Bartek Grzybowski7f8b8e72020-11-20 12:51:17 +0100175 docker run --name $container_name \
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400176 -v ${volume_offline_directory}:${container_offline_volume} \
177 -v ${volume_repo_directory}:${container_repo_volume} \
Jan Benediktfdec88e2020-03-19 16:03:25 +0100178 "${mounted_lists[@]}" \
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400179 --rm \
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400180 --entrypoint="${container_offline_volume}docker-entrypoint.sh" \
Bartek Grzybowski32f35f92020-10-28 14:45:53 +0100181 ${docker_image} \
Jan Benediktfdec88e2020-03-19 16:03:25 +0100182 "${param_array[@]}"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400183fi