Run docker container in foreground
[oom/offline-installer.git] / build / create_repo.sh
1 #!/usr/bin/env bash
2
3 # Set distribution type
4 distro_type="$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')"
5
6 # Path to cloned offline-installer build directory with docker_entrypoint script
7 volume_offline_directory="$(readlink -f $(dirname ${0}))"
8
9 # Destination path for created repository
10 volume_repo_directory="$(pwd)"
11
12 # Path inside container with cloned offline-installer build directory
13 container_offline_volume="/mnt/offline/"
14
15 # Target repository path inside container
16 container_repo_volume="/mnt/repo/"
17
18 # Additional packages lists files path within container
19 container_list_volume="/mnt/additional-lists/"
20
21 # Show script usage
22 help () {
23 cat <<EOF
24 Wrapper script running docker container for creating package repository
25
26 Repository type is set with --target-platform option and the default is to use host OS platform type
27
28 usage: create_repo.sh [OPTION]...
29
30
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   -n | --container-name-suffix     add custom suffix to docker container name
37   -h | --help                      show this help
38
39 If build folder from offline repository is not specified current one will be used by default.
40 EOF
41 }
42
43 # Get distribution type
44 # Set Docker image name and version based on type of linux distribution
45 # Set expected directory for RPM/DEB packages
46 set_environment () {
47     case "$1" in
48     ubuntu)
49         distro_type="ubuntu"
50         docker_image="ubuntu:18.04"
51         expected_dir="resources/pkg/deb"
52         container_name="${1}_repo${container_name_suffix}"
53     ;;
54     centos|rhel)
55         distro_type="rhel"
56         docker_image="centos:centos7.6.1810"
57         expected_dir="resources/pkg/rpm"
58         container_name="${1}_repo${container_name_suffix}"
59     ;;
60     *)
61         echo "Unknown type of linux distribution."
62         exit 1
63     ;;
64     esac
65 }
66
67 # Getting input parametters
68 if [[ $# -eq 0 ]] ; then
69     help # show help
70     exit 0
71 fi
72
73 while [[ $# -gt 0 ]]
74 do
75     case "$1" in
76         -h|--help)
77             # Help parametter
78             help # show help
79             exit 0
80             ;;
81         -c|--cloned-directory)
82             # Directory parameter
83             # Set path to offline-installer build directory
84             volume_offline_directory="$2"
85             ;;
86         -d|--destination-repository)
87             # Repository directory parameter
88             # Set destination path for created repository
89             volume_repo_directory="$2"
90             ;;
91         -t|--target-platform)
92             # Repository type (rpm/deb)
93             # Set target platform for repository
94             target_input="$2"
95             ;;
96         -a|--additional-list)
97             # Array of additional packages lists
98             additional_lists+=("$2")
99             ;;
100         -n|--container-name-suffix)
101             # Set custom container name suffix
102             container_name_suffix="_${2}"
103             ;;
104         *)
105             # unknown option
106             help # show help
107             exit 1
108             ;;
109     esac
110     shift;shift
111 done
112
113 # Check if user specified repository type
114 # This setting has higher priority than distribution type
115 if ! test -z "$target_input"
116 then
117     set_environment "$target_input"
118 else
119     set_environment "$distro_type"
120 fi
121
122 # Check if path contains expected components:
123 # "resources/pkg/rpm" for Rhel/CentOS or
124 # "resources/pkg/deb" for Ubuntu/Debian
125 if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
126     # Create repo folder if it doesn't exist
127     case "$distro_type" in
128         ubuntu)
129             volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
130         ;;
131         rhel)
132             volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm
133         ;;
134     esac
135     [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
136 fi
137
138 # Check if container is already running
139 if [ ! $(docker ps -q -f name="^${container_name}$") ];
140 then
141     # run repo container
142     # name of container $container_name
143     # docker entrypoint script from mounted volume
144     # 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
158     docker run --name $container_name \
159                -v ${volume_offline_directory}:${container_offline_volume} \
160                -v ${volume_repo_directory}:${container_repo_volume} \
161                "${mounted_lists[@]}" \
162                --rm \
163                --entrypoint="${container_offline_volume}docker-entrypoint.sh" \
164                     ${docker_image} \
165                     "${param_array[@]}"
166 fi