Drop docker container cleanup statement
[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   -h | --help                      show this help
37
38 If build folder from offline repository is not specified current one will be used by default.
39 EOF
40 }
41
42 # Get distribution type
43 # Set Docker image name and version based on type of linux distribution
44 # Set expected directory for RPM/DEB packages
45 set_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
66 # Getting input parametters
67 POSITIONAL=()
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         *)
101             # unknown option
102             help # show help
103             exit 1
104             ;;
105     esac
106     shift;shift
107 done
108
109 # Check if user specified repository type
110 # This setting has higher priority than distribution type
111 if ! test -z "$target_input"
112 then
113     set_enviroment "$target_input"
114 else
115     set_enviroment "$distro_type"
116 fi
117
118 # Check if path contains expected components:
119 # "resources/pkg/rpm" for Rhel/CentOS or
120 # "resources/pkg/deb" for Ubuntu/Debian
121 if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
122     # Create repo folder if it doesn't exist
123     case "$distro_type" in
124         ubuntu)
125             volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
126         ;;
127         rhel)
128             volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm
129         ;;
130     esac
131     [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
132 fi
133
134 #Check if container "centos-repo" is running
135 if [ ! "$(docker ps -q -f name=$container_name)" ]; then
136     # run repo container
137     # name of container $container_name
138     # docker entrypoint script from mounted volume
139     # with dynamic parameters
140     # mount additional packages lists to container
141     param_array=()
142     mounted_lists=()
143     param_array+=(--directory ${container_repo_volume})
144     param_array+=(--list ${container_offline_volume}data_lists/)
145     param_array+=(--packages-lists-path ${container_list_volume})
146     [[ ! ${#additional_lists[@]} -eq 0 ]] && \
147         for array_list in "${additional_lists[@]}";
148         do
149             param_array+=(--additional-list "${array_list##*/}") && \
150             mounted_lists+=(-v ${array_list}:${container_list_volume}${array_list##*/})
151         done
152
153     docker run -d \
154                --name $container_name \
155                -v ${volume_offline_directory}:${container_offline_volume} \
156                -v ${volume_repo_directory}:${container_repo_volume} \
157                "${mounted_lists[@]}" \
158                --rm \
159                --entrypoint="${container_offline_volume}docker-entrypoint.sh" \
160                     -it ${docker_image} \
161                     "${param_array[@]}"
162     docker logs $(docker ps --filter "name=${container_name}" --format '{{.ID}}' -a) -f
163 fi