Merge "[GENERAL] Add Andreas Geissler as committer."
[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 # Use cache by default
22 drop_cache=false
23
24 # Show script usage
25 help () {
26 cat <<EOF
27 Wrapper script running docker container for creating package repository
28
29 Repository type is set with --target-platform option and the default is to use host OS platform type
30
31 usage: create_repo.sh [OPTION]...
32
33
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
39   -n | --container-name-suffix     add custom suffix to docker container name
40   -r | --drop-cache                remove cached packages (use package cache by default)
41   -h | --help                      show this help
42
43 If build folder from offline repository is not specified current one will be used by default.
44 EOF
45 }
46
47 # Get distribution type
48 # Set Docker image name and version based on type of linux distribution
49 # Set expected directory for RPM/DEB packages
50 set_environment () {
51     case "$1" in
52     ubuntu)
53         distro_type="ubuntu"
54         docker_image="ubuntu:18.04"
55         expected_dir="resources/pkg/deb"
56         container_name="${1}_repo${container_name_suffix}"
57     ;;
58     centos|rhel)
59         distro_type="rhel"
60         docker_image="centos:centos7.9.2009"
61         expected_dir="resources/pkg/rpm"
62         container_name="${1}_repo${container_name_suffix}"
63     ;;
64     *)
65         echo "Unknown type of linux distribution."
66         exit 1
67     ;;
68     esac
69 }
70
71 # Getting input parametters
72 if [[ $# -eq 0 ]] ; then
73     help # show help
74     exit 0
75 fi
76
77 while [[ $# -gt 0 ]]
78 do
79     case "$1" in
80         -h|--help)
81             # Help parametter
82             help # show help
83             exit 0
84             ;;
85         -c|--cloned-directory)
86             # Directory parameter
87             # Set path to offline-installer build directory
88             volume_offline_directory="$2"
89             shift
90             ;;
91         -d|--destination-repository)
92             # Repository directory parameter
93             # Set destination path for created repository
94             volume_repo_directory="$2"
95             shift
96             ;;
97         -t|--target-platform)
98             # Repository type (rpm/deb)
99             # Set target platform for repository
100             target_input="$2"
101             shift
102             ;;
103         -a|--additional-list)
104             # Array of additional packages lists
105             additional_lists+=("$2")
106             shift
107             ;;
108         -n|--container-name-suffix)
109             # Set custom container name suffix
110             container_name_suffix="_${2}"
111             shift
112             ;;
113         -r|--drop-cache)
114             # Set flag to clean cache
115             drop_cache=true
116             ;;
117         *)
118             # unknown option
119             help # show help
120             exit 1
121             ;;
122     esac
123     shift
124 done
125
126 # Check if user specified repository type
127 # This setting has higher priority than distribution type
128 if ! test -z "$target_input"
129 then
130     set_environment "$target_input"
131 else
132     set_environment "$distro_type"
133 fi
134
135 # Check if path contains expected components:
136 # "resources/pkg/rpm" for Rhel/CentOS or
137 # "resources/pkg/deb" for Ubuntu/Debian
138 if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
139     # Create repo folder if it doesn't exist
140     case "$distro_type" in
141         ubuntu)
142             volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
143         ;;
144         rhel)
145             volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm
146         ;;
147     esac
148     [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
149 fi
150
151 # Check if container is already running
152 if [ ! $(docker ps -q -f name="^${container_name}$") ];
153 then
154     # run repo container
155     # name of container $container_name
156     # docker entrypoint script from mounted volume
157     # 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})
164     if ${drop_cache};
165     then
166         param_array+=(--drop-cache)
167     fi
168     [[ ! ${#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
175         docker run --name $container_name \
176                -v ${volume_offline_directory}:${container_offline_volume} \
177                -v ${volume_repo_directory}:${container_repo_volume} \
178                "${mounted_lists[@]}" \
179                --rm \
180                --entrypoint="${container_offline_volume}docker-entrypoint.sh" \
181                     ${docker_image} \
182                     "${param_array[@]}"
183 fi