2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
21 echo "Usage: ${0} [options] /path/to/download/file.tar.gz download/fragment/eg/project/subdir/some-artifact-version.tar.gz"
23 echo " --force for a redownload even if /path/to/download/file.tar.gz exists."
24 echo " --working-dir /path/to/use Path for writing tempfiles. must exist."
25 echo " defaults to making a directory via mktemp that we clean."
26 echo " --keys url://to/project/KEYS where to get KEYS. needed to check signature on download."
30 # if no args specified, show usage
37 declare done_if_cached
="true"
39 declare cleanup
="true"
44 --force) shift; done_if_cached
="false";;
45 --working-dir) shift; working_dir
=$1; cleanup
="false"; shift;;
46 --keys) shift; keys
=$1; shift;;
49 *) break;; # terminate while loop
53 # should still have required args
61 if [ -f "${target}" ] && [ "true" = "${done_if_cached}" ]; then
62 echo "Reusing existing download of '${artifact}'."
66 if [ -z "${working_dir}" ]; then
67 if ! working_dir
="$(mktemp -d -t hbase-download-apache-artifact)" ; then
68 echo "Failed to create temporary working directory. Please specify via --working-dir" >&2
73 working_dir
="$(cd "$
(dirname "${working_dir}")"; pwd)/$(basename "${working_dir}")"
74 if [ ! -d "${working_dir}" ]; then
75 echo "passed working directory '${working_dir}' must already exist." >&2
81 if [ -n "${keys}" ]; then
82 echo "Stopping gpg agent daemon"
83 gpgconf
--homedir "${working_dir}/.gpg" --kill gpg-agent
84 echo "Stopped gpg agent daemon"
87 if [ "true" = "${cleanup}" ]; then
88 echo "cleaning up temp space."
89 rm -rf "${working_dir}"
92 trap cleanup EXIT SIGQUIT
94 echo "New download of '${artifact}'"
96 # N.B. this comes first so that if gpg falls over we skip the expensive download.
97 if [ -n "${keys}" ]; then
98 if [ ! -d "${working_dir}/.gpg" ]; then
99 rm -rf "${working_dir}/.gpg"
100 mkdir
-p "${working_dir}/.gpg"
101 chmod -R 700 "${working_dir}/.gpg"
104 echo "installing project KEYS"
105 curl
-L --fail -o "${working_dir}/KEYS" "${keys}"
106 if ! gpg
--homedir "${working_dir}/.gpg" --import "${working_dir}/KEYS" ; then
107 echo "ERROR importing the keys via gpg failed. If the output above mentions this error:" >&2
108 echo " gpg: can't connect to the agent: File name too long" >&2
109 # we mean to give them the command to run, not to run it.
110 #shellcheck disable=SC2016
111 echo 'then you prolly need to create /var/run/user/$(id -u)' >&2
112 echo "see this thread on gnupg-users: https://s.apache.org/uI7x" >&2
116 echo "downloading signature"
117 curl
-L --fail -o "${working_dir}/artifact.asc" "https://archive.apache.org/dist/${artifact}.asc"
120 echo "downloading artifact"
121 if ! curl
--dump-header "${working_dir}/artifact_download_headers.txt" -L --fail -o "${working_dir}/artifact" "https://www.apache.org/dyn/closer.lua?filename=${artifact}&action=download" ; then
122 echo "Artifact wasn't in mirror system. falling back to archive.a.o."
123 curl
--dump-header "${working_dir}/artifact_fallback_headers.txt" -L --fail -o "${working_dir}/artifact" "http://archive.apache.org/dist/${artifact}"
126 if [ -n "${keys}" ]; then
127 echo "verifying artifact signature"
128 gpg
--homedir "${working_dir}/.gpg" --verify "${working_dir}/artifact.asc"
129 echo "signature good."
132 echo "moving artifact into place at '${target}'"
133 # ensure we're on the same filesystem
134 mv "${working_dir}/artifact" "${target}.copying"
135 # attempt atomic move
136 mv "${target}.copying" "${target}"