Merge "Adding Ubuntu support in Ansible - docker role"
[oom/offline-installer.git] / build / create_repo.sh
1 #!/usr/bin/env bash
2
3 # Set type of distribution
4 distro_type="$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')"
5
6 # Path to folder with cloned offline-installer build directory with docker_entrypoint script
7 volume_offline_directory="$(readlink -f $(dirname ${0}))"
8
9 # Path for directory where repository will be created
10 volume_repo_directory="$(pwd)"
11
12 # Path inside container with cloned offline-installer build directory
13 container_offline_volume="/mnt/offline/"
14
15 # Path inside container where will be created repository
16 container_repo_volume="/mnt/repo/"
17
18 # Path inside container where will be stored additional packages lists
19 container_list_volume="/mnt/additional-lists/"
20
21 # Show help for using this script
22 help () {
23 cat <<EOF
24 Script for run docker container creating DEB or RPM repository
25
26 Type of repository is created based on user input or if input is empty type of host OS
27
28 usage: create_repo.sh [-d|--destination-repository output directory] [-c|--cloned-directory input directory] 
29                       [-t|--target-platform centos target platform for repository]
30                       [-a|----additional-lists path to additional package list]
31 -h --help: Show this help
32 -d --destination-repository: set path where will be stored RPM packages. Default value is current directory
33 -c --cloned-directory: set path where is stored this script and docker-entrypoint script (offline-installer/build directory). Fill it just when you want to use different script/datalists
34 -t --target-platform: set target platform for repository (ubuntu/rhel/centos) 
35 -a --additional-list: add additional packages list
36                       can be used multiple times for more additional lists
37
38 If build folder from offline repository is not specified will be used default path of current folder.
39 EOF
40 }
41
42 # Get type of distribution
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 parametter
83             # Sets path where is cloned offline-installer build directory
84             volume_offline_directory="$2"
85             ;;
86         -d|--destination-repository)
87             # Repository direcotry parametter
88             # Sets path where will be repository created
89             volume_repo_directory="$2"
90             ;;
91         -t|--target-platform)
92             # Repository type (rpm/deb)
93             # Sets target platform for repository
94             target_input="$2"
95             ;;
96         -a|--additional-list)
97             # Array with more packages lists
98             # Add more packages lists to download
99             additional_lists+=("$2")
100             ;;
101         *)
102             # unknown option
103             help # show help
104             exit 1
105             ;;
106     esac
107     shift;shift
108 done
109
110 # Check if user specified type of repository
111 # This settings have higher priority, then type of distribution
112 if ! test -z "$target_input"
113 then
114     set_enviroment "$target_input"
115 else
116     set_enviroment "$distro_type"
117 fi
118
119 # Check if path contains expected path:
120 # "resources/pkg/rpm" for Rhel/CentOS or
121 # "resources/pkg/deb" for Ubuntu/Debian
122 if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then
123     # Create repo folder if it not exists
124     case "$distro_type" in
125         ubuntu)
126             volume_repo_directory="$volume_repo_directory"/resources/pkg/deb
127         ;;
128         rhel)
129             volume_repo_directory="$volume_repo_directory"/resources/pkg/rhel
130         ;;
131     esac
132     [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory
133 fi
134
135 #Check if container "centos-repo" is running
136 if [ ! "$(docker ps -q -f name=$container_name)" ]; then
137     if [ "$(docker ps -aq -f status=exited -f name=$container_name)" ]; then
138         # cleanup
139         docker rm $container_name
140     fi
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 -d \
159                --name $container_name \
160                -v ${volume_offline_directory}:${container_offline_volume} \
161                -v ${volume_repo_directory}:${container_repo_volume} \
162                "${mounted_lists[@]}" \
163                --rm \
164                --entrypoint="${container_offline_volume}docker-entrypoint.sh" \
165                     -it ${docker_image} \
166                     "${param_array[@]}"
167     docker logs $(docker ps --filter "name=${container_name}" --format '{{.ID}}' -a) -f
168 fi