blob: e70d69b227da2a38d62cac4198cd5e6f3491302d [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 Grzybowski237a9222020-10-27 10:50:35 +010021# Show script usage
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040022help () {
Jan Benediktfdec88e2020-03-19 16:03:25 +010023cat <<EOF
Bartek Grzybowski237a9222020-10-27 10:50:35 +010024Wrapper script running docker container for creating package repository
Jan Benediktfdec88e2020-03-19 16:03:25 +010025
Bartek Grzybowski237a9222020-10-27 10:50:35 +010026Repository type is set with --target-platform option and the default is to use host OS platform type
Jan Benediktfdec88e2020-03-19 16:03:25 +010027
Bartek Grzybowski237a9222020-10-27 10:50:35 +010028usage: create_repo.sh [OPTION]...
Jan Benediktfdec88e2020-03-19 16:03:25 +010029
Bartek Grzybowski237a9222020-10-27 10:50:35 +010030
31 -d | --destination-repository target path to store downloaded packages. Current directory by default
32 -c | --cloned-directory path to directory containing this and docker-entrypoint scripts (offline-installer/build directory)
33 Set it only when you want to use different script/datalists
34 -t | --target-platform target repository platform type (ubuntu/rhel/centos)
35 -a | --additional-list additional packages list; can be used multiple times for more additional lists
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010036 -n | --container-name-suffix add custom suffix to docker container name
Bartek Grzybowski237a9222020-10-27 10:50:35 +010037 -h | --help show this help
38
39If build folder from offline repository is not specified current one will be used by default.
Jan Benediktfdec88e2020-03-19 16:03:25 +010040EOF
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040041}
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040042
Bartek Grzybowski237a9222020-10-27 10:50:35 +010043# Get distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +010044# Set Docker image name and version based on type of linux distribution
45# Set expected directory for RPM/DEB packages
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010046set_environment () {
Jan Benedikt77459fe2020-02-10 13:46:52 +010047 case "$1" in
48 ubuntu)
49 distro_type="ubuntu"
50 docker_image="ubuntu:18.04"
51 expected_dir="resources/pkg/deb"
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010052 container_name="${1}_repo${container_name_suffix}"
Jan Benedikt77459fe2020-02-10 13:46:52 +010053 ;;
54 centos|rhel)
55 distro_type="rhel"
56 docker_image="centos:centos7.6.1810"
57 expected_dir="resources/pkg/rpm"
Bartek Grzybowskicddc5542020-10-27 13:30:06 +010058 container_name="${1}_repo${container_name_suffix}"
Jan Benedikt77459fe2020-02-10 13:46:52 +010059 ;;
60 *)
61 echo "Unknown type of linux distribution."
62 exit 1
63 ;;
64 esac
65}
66
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040067# Getting input parametters
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040068if [[ $# -eq 0 ]] ; then
69 help # show help
70 exit 0
71fi
Jan Benedikt77459fe2020-02-10 13:46:52 +010072
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040073while [[ $# -gt 0 ]]
74do
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040075 case "$1" in
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040076 -h|--help)
77 # Help parametter
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040078 help # show help
79 exit 0
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040080 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040081 -c|--cloned-directory)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010082 # Directory parameter
83 # Set path to offline-installer build directory
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040084 volume_offline_directory="$2"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040085 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040086 -d|--destination-repository)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010087 # Repository directory parameter
88 # Set destination path for created repository
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040089 volume_repo_directory="$2"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040090 ;;
Jan Benediktfdec88e2020-03-19 16:03:25 +010091 -t|--target-platform)
Jan Benedikt77459fe2020-02-10 13:46:52 +010092 # Repository type (rpm/deb)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010093 # Set target platform for repository
Jan Benedikt77459fe2020-02-10 13:46:52 +010094 target_input="$2"
95 ;;
Jan Benediktfdec88e2020-03-19 16:03:25 +010096 -a|--additional-list)
Bartek Grzybowski237a9222020-10-27 10:50:35 +010097 # Array of additional packages lists
Jan Benediktfdec88e2020-03-19 16:03:25 +010098 additional_lists+=("$2")
99 ;;
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100100 -n|--container-name-suffix)
101 # Set custom container name suffix
102 container_name_suffix="_${2}"
103 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400104 *)
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400105 # unknown option
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400106 help # show help
107 exit 1
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400108 ;;
109 esac
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400110 shift;shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400111done
112
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100113# Check if user specified repository type
114# This setting has higher priority than distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +0100115if ! test -z "$target_input"
116then
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100117 set_environment "$target_input"
Jan Benedikt77459fe2020-02-10 13:46:52 +0100118else
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100119 set_environment "$distro_type"
Jan Benedikt77459fe2020-02-10 13:46:52 +0100120fi
121
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100122# Check if path contains expected components:
Jan Benedikt77459fe2020-02-10 13:46:52 +0100123# "resources/pkg/rpm" for Rhel/CentOS or
124# "resources/pkg/deb" for Ubuntu/Debian
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400125if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100126 # Create repo folder if it doesn't exist
Jan Benedikt77459fe2020-02-10 13:46:52 +0100127 case "$distro_type" in
128 ubuntu)
129 volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
130 ;;
131 rhel)
Bartek Grzybowskif6c503b2020-09-08 15:59:07 +0200132 volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm
Jan Benedikt77459fe2020-02-10 13:46:52 +0100133 ;;
134 esac
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400135 [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
136fi
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400137
Bartek Grzybowskicddc5542020-10-27 13:30:06 +0100138# Check if container is already running
139if [ ! $(docker ps -q -f name="^${container_name}$") ];
140then
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400141 # run repo container
142 # name of container $container_name
143 # docker entrypoint script from mounted volume
Jan Benediktfdec88e2020-03-19 16:03:25 +0100144 # with dynamic parameters
145 # mount additional packages lists to container
146 param_array=()
147 mounted_lists=()
148 param_array+=(--directory ${container_repo_volume})
149 param_array+=(--list ${container_offline_volume}data_lists/)
150 param_array+=(--packages-lists-path ${container_list_volume})
151 [[ ! ${#additional_lists[@]} -eq 0 ]] && \
152 for array_list in "${additional_lists[@]}";
153 do
154 param_array+=(--additional-list "${array_list##*/}") && \
155 mounted_lists+=(-v ${array_list}:${container_list_volume}${array_list##*/})
156 done
157
Bartek Grzybowski32f35f92020-10-28 14:45:53 +0100158 docker run --name $container_name \
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400159 -v ${volume_offline_directory}:${container_offline_volume} \
160 -v ${volume_repo_directory}:${container_repo_volume} \
Jan Benediktfdec88e2020-03-19 16:03:25 +0100161 "${mounted_lists[@]}" \
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400162 --rm \
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400163 --entrypoint="${container_offline_volume}docker-entrypoint.sh" \
Bartek Grzybowski32f35f92020-10-28 14:45:53 +0100164 ${docker_image} \
Jan Benediktfdec88e2020-03-19 16:03:25 +0100165 "${param_array[@]}"
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400166fi