blob: 8fce53cc9bdf1e44c7c7fa5e354f926c4980c4b1 [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
36 -h | --help show this help
37
38If build folder from offline repository is not specified current one will be used by default.
Jan Benediktfdec88e2020-03-19 16:03:25 +010039EOF
Jan Benedikt8fdbfe72019-10-15 06:07:46 -040040}
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040041
Bartek Grzybowski237a9222020-10-27 10:50:35 +010042# Get distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +010043# Set Docker image name and version based on type of linux distribution
44# Set expected directory for RPM/DEB packages
45set_enviroment () {
46 case "$1" in
47 ubuntu)
48 distro_type="ubuntu"
49 docker_image="ubuntu:18.04"
50 expected_dir="resources/pkg/deb"
51 container_name=$1"_repo"
52 ;;
53 centos|rhel)
54 distro_type="rhel"
55 docker_image="centos:centos7.6.1810"
56 expected_dir="resources/pkg/rpm"
57 container_name=$1"_repo"
58 ;;
59 *)
60 echo "Unknown type of linux distribution."
61 exit 1
62 ;;
63 esac
64}
65
Jan Benedikt7c0f6b12019-10-08 10:01:41 -040066# Getting input parametters
67POSITIONAL=()
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 ;;
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400100 *)
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400101 # unknown option
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400102 help # show help
103 exit 1
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400104 ;;
105 esac
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400106 shift;shift
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400107done
108
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100109# Check if user specified repository type
110# This setting has higher priority than distribution type
Jan Benedikt77459fe2020-02-10 13:46:52 +0100111if ! test -z "$target_input"
112then
113 set_enviroment "$target_input"
114else
115 set_enviroment "$distro_type"
116fi
117
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100118# Check if path contains expected components:
Jan Benedikt77459fe2020-02-10 13:46:52 +0100119# "resources/pkg/rpm" for Rhel/CentOS or
120# "resources/pkg/deb" for Ubuntu/Debian
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400121if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
Bartek Grzybowski237a9222020-10-27 10:50:35 +0100122 # Create repo folder if it doesn't exist
Jan Benedikt77459fe2020-02-10 13:46:52 +0100123 case "$distro_type" in
124 ubuntu)
125 volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
126 ;;
127 rhel)
Bartek Grzybowskif6c503b2020-09-08 15:59:07 +0200128 volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm
Jan Benedikt77459fe2020-02-10 13:46:52 +0100129 ;;
130 esac
Jan Benedikt8fdbfe72019-10-15 06:07:46 -0400131 [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
132fi
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400133
134#Check if container "centos-repo" is running
135if [ ! "$(docker ps -q -f name=$container_name)" ]; then
136 if [ "$(docker ps -aq -f status=exited -f name=$container_name)" ]; then
137 # cleanup
138 docker rm $container_name
139 fi
140 # run repo container
141 # name of container $container_name
142 # docker entrypoint script from mounted volume
Jan Benediktfdec88e2020-03-19 16:03:25 +0100143 # with dynamic parameters
144 # mount additional packages lists to container
145 param_array=()
146 mounted_lists=()
147 param_array+=(--directory ${container_repo_volume})
148 param_array+=(--list ${container_offline_volume}data_lists/)
149 param_array+=(--packages-lists-path ${container_list_volume})
150 [[ ! ${#additional_lists[@]} -eq 0 ]] && \
151 for array_list in "${additional_lists[@]}";
152 do
153 param_array+=(--additional-list "${array_list##*/}") && \
154 mounted_lists+=(-v ${array_list}:${container_list_volume}${array_list##*/})
155 done
156
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400157 docker run -d \
158 --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" \
Jan Benedikt77459fe2020-02-10 13:46:52 +0100164 -it ${docker_image} \
Jan Benediktfdec88e2020-03-19 16:03:25 +0100165 "${param_array[@]}"
Jan Benedikt77459fe2020-02-10 13:46:52 +0100166 docker logs $(docker ps --filter "name=${container_name}" --format '{{.ID}}' -a) -f
Jan Benedikt7c0f6b12019-10-08 10:01:41 -0400167fi