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 | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 21 | # Show script usage |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 22 | help () { |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 23 | cat <<EOF |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 24 | Wrapper script running docker container for creating package repository |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 25 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 26 | 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] | 27 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 28 | usage: create_repo.sh [OPTION]... |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 29 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 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. |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 39 | EOF |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 40 | } |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 41 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 42 | # Get distribution type |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 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 | |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 66 | # Getting input parametters |
| 67 | POSITIONAL=() |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 68 | if [[ $# -eq 0 ]] ; then |
| 69 | help # show help |
| 70 | exit 0 |
| 71 | fi |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 72 | |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 73 | while [[ $# -gt 0 ]] |
| 74 | do |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 75 | case "$1" in |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 76 | -h|--help) |
| 77 | # Help parametter |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 78 | help # show help |
| 79 | exit 0 |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 80 | ;; |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 81 | -c|--cloned-directory) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 82 | # Directory parameter |
| 83 | # Set path to offline-installer build directory |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 84 | volume_offline_directory="$2" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 85 | ;; |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 86 | -d|--destination-repository) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 87 | # Repository directory parameter |
| 88 | # Set destination path for created repository |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 89 | volume_repo_directory="$2" |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 90 | ;; |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 91 | -t|--target-platform) |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 92 | # Repository type (rpm/deb) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 93 | # Set target platform for repository |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 94 | target_input="$2" |
| 95 | ;; |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 96 | -a|--additional-list) |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 97 | # Array of additional packages lists |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 98 | additional_lists+=("$2") |
| 99 | ;; |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 100 | *) |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 101 | # unknown option |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 102 | help # show help |
| 103 | exit 1 |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 104 | ;; |
| 105 | esac |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 106 | shift;shift |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 107 | done |
| 108 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 109 | # Check if user specified repository type |
| 110 | # This setting has higher priority than distribution type |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 111 | if ! test -z "$target_input" |
| 112 | then |
| 113 | set_enviroment "$target_input" |
| 114 | else |
| 115 | set_enviroment "$distro_type" |
| 116 | fi |
| 117 | |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 118 | # Check if path contains expected components: |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 119 | # "resources/pkg/rpm" for Rhel/CentOS or |
| 120 | # "resources/pkg/deb" for Ubuntu/Debian |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 121 | if ! [[ "/$volume_repo_directory/" = *"/$expected_dir/"* ]]; then |
Bartek Grzybowski | 237a922 | 2020-10-27 10:50:35 +0100 | [diff] [blame^] | 122 | # Create repo folder if it doesn't exist |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 123 | case "$distro_type" in |
| 124 | ubuntu) |
| 125 | volume_repo_directory="$volume_repo_directory"/resources/pkg/deb |
| 126 | ;; |
| 127 | rhel) |
Bartek Grzybowski | f6c503b | 2020-09-08 15:59:07 +0200 | [diff] [blame] | 128 | volume_repo_directory="$volume_repo_directory"/resources/pkg/rpm |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 129 | ;; |
| 130 | esac |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 131 | [ ! -d "$volume_repo_directory" ] && mkdir -p $volume_repo_directory |
| 132 | fi |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 133 | |
| 134 | #Check if container "centos-repo" is running |
| 135 | if [ ! "$(docker ps -q -f name=$container_name)" ]; then |
| 136 | if [ "$(docker ps -aq -f status=exited -f name=$container_name)" ]; then |
| 137 | # cleanup |
| 138 | docker rm $container_name |
| 139 | fi |
| 140 | # run repo container |
| 141 | # name of container $container_name |
| 142 | # docker entrypoint script from mounted volume |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 143 | # with dynamic parameters |
| 144 | # mount additional packages lists to container |
| 145 | param_array=() |
| 146 | mounted_lists=() |
| 147 | param_array+=(--directory ${container_repo_volume}) |
| 148 | param_array+=(--list ${container_offline_volume}data_lists/) |
| 149 | param_array+=(--packages-lists-path ${container_list_volume}) |
| 150 | [[ ! ${#additional_lists[@]} -eq 0 ]] && \ |
| 151 | for array_list in "${additional_lists[@]}"; |
| 152 | do |
| 153 | param_array+=(--additional-list "${array_list##*/}") && \ |
| 154 | mounted_lists+=(-v ${array_list}:${container_list_volume}${array_list##*/}) |
| 155 | done |
| 156 | |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 157 | docker run -d \ |
| 158 | --name $container_name \ |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 159 | -v ${volume_offline_directory}:${container_offline_volume} \ |
| 160 | -v ${volume_repo_directory}:${container_repo_volume} \ |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 161 | "${mounted_lists[@]}" \ |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 162 | --rm \ |
Jan Benedikt | 8fdbfe7 | 2019-10-15 06:07:46 -0400 | [diff] [blame] | 163 | --entrypoint="${container_offline_volume}docker-entrypoint.sh" \ |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 164 | -it ${docker_image} \ |
Jan Benedikt | fdec88e | 2020-03-19 16:03:25 +0100 | [diff] [blame] | 165 | "${param_array[@]}" |
Jan Benedikt | 77459fe | 2020-02-10 13:46:52 +0100 | [diff] [blame] | 166 | docker logs $(docker ps --filter "name=${container_name}" --format '{{.ID}}' -a) -f |
Jan Benedikt | 7c0f6b1 | 2019-10-08 10:01:41 -0400 | [diff] [blame] | 167 | fi |