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