|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Deploy to packagecloud repositories |
| 4 | +# ----------------------------------- |
| 5 | +# |
| 6 | +# `deploy_packagecloud.sh` is equivalent to |
| 7 | +# `deploy_packagecloud.sh staging`. |
| 8 | +# |
| 9 | +# `deploy_packagecloud.sh staging` requires the following |
| 10 | +# environment variables: |
| 11 | +# |
| 12 | +# - OS |
| 13 | +# - DIST |
| 14 | +# - DEPLOY_STAGING_PACKAGECLOUD_USER |
| 15 | +# - DEPLOY_STAGING_PACKAGECLOUD_TOKEN |
| 16 | +# |
| 17 | +# `deploy_packagecloud.sh production` requires the following |
| 18 | +# environment variables: |
| 19 | +# |
| 20 | +# - OS |
| 21 | +# - DIST |
| 22 | +# - DEPLOY_PRODUCTION_PACKAGECLOUD_USER |
| 23 | +# - DEPLOY_PRODUCTION_PACKAGECLOUD_TOKEN |
| 24 | +# |
| 25 | +# If one of those variables is not set or empty, then deployment |
| 26 | +# will be skipped. |
| 27 | + |
| 28 | +# Make shell strictier. |
| 29 | +# |
| 30 | +# - Exit with a failure on a first failed command. |
| 31 | +# - Exit with a failure on an attempt to use an unset variable. |
| 32 | +# - Print each executed commmand. |
| 33 | +# |
| 34 | +# Note: The script expects that Travis-CI will filter sensitive |
| 35 | +# information (such as a token): 'Display value in build log' |
| 36 | +# toogle should be OFF for to keep a value secure. |
| 37 | +set -eux |
| 38 | + |
| 39 | +configuration=${1:-staging} |
| 40 | + |
| 41 | +# Choose credentials. |
| 42 | +if [ ${configuration} = staging ]; then |
| 43 | + DEPLOY_PACKAGECLOUD_USER="${DEPLOY_STAGING_PACKAGECLOUD_USER:-}" |
| 44 | + DEPLOY_PACKAGECLOUD_TOKEN="${DEPLOY_STAGING_PACKAGECLOUD_TOKEN:-}" |
| 45 | +elif [ ${configuration} = production ]; then |
| 46 | + DEPLOY_PACKAGECLOUD_USER="${DEPLOY_PRODUCTION_PACKAGECLOUD_USER:-}" |
| 47 | + DEPLOY_PACKAGECLOUD_TOKEN="${DEPLOY_PRODUCTION_PACKAGECLOUD_TOKEN:-}" |
| 48 | +else |
| 49 | + echo "Unknown configuration: ${configuration}" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +# Skip deployment if some variables are not set or empty. |
| 54 | +if [ -z "${OS:-}" ] || [ -z "${DIST:-}" ] || \ |
| 55 | + [ -z "${DEPLOY_PACKAGECLOUD_USER}" ] || \ |
| 56 | + [ -z "${DEPLOY_PACKAGECLOUD_TOKEN}" ]; then |
| 57 | + echo "Skip deployment: some of necessary environment" |
| 58 | + echo "variables are not set or empty" |
| 59 | + exit 0 |
| 60 | +fi |
| 61 | + |
| 62 | +# Verify that packpack is cloned into the current directory. |
| 63 | +packagecloud_tool=./packpack/tools/packagecloud |
| 64 | +if [ ! -f "${packagecloud_tool}" ]; then |
| 65 | + echo "Could not find ${packagecloud_tool}" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +# Staging repository: keep older packages in case of a |
| 70 | +# version clash. |
| 71 | +# |
| 72 | +# It would be better to replace old ones, but there is no |
| 73 | +# such option in the packagecloud tool we use. It may be |
| 74 | +# important if we'll have some manual or automatic testing |
| 75 | +# upward a staging repository. But at least CI will not fail |
| 76 | +# because a package is already exists. |
| 77 | +push_args="" |
| 78 | +if [ "${configuration}" = staging ]; then |
| 79 | + push_args="${push_args} --ignore-duplicates" |
| 80 | +fi |
| 81 | + |
| 82 | +# Setup environment variables for the packagecloud tool. |
| 83 | +export PACKAGECLOUD_TOKEN="${DEPLOY_PACKAGECLOUD_TOKEN}" |
| 84 | + |
| 85 | +# FIXME: It would be good to upstream the repos_list command. |
| 86 | +(cd packpack/tools && |
| 87 | + patch -p1 -i ../../.travis/packagecloud-list-repos.patch) |
| 88 | + |
| 89 | +# Fetch list of repositories from packagecloud.io. |
| 90 | +# |
| 91 | +# Set default repositories if the attempt to fetch them fails. |
| 92 | +# |
| 93 | +# We have tarantool repositories on packagecloud.io up to |
| 94 | +# 2_4. The next ones present only in the S3 based storage. |
| 95 | +repositories="" |
| 96 | +for i in $(seq 1 5); do |
| 97 | + repositories="$(${packagecloud_tool} list_repos || true)" |
| 98 | + [ -n "${repositories}" ] && break |
| 99 | +done |
| 100 | +[ -z "${repositories}" ] && repositories="1_6 1_7 1_9 1_10 2x 2_2 2_3 2_4" |
| 101 | + |
| 102 | +for repo in ${repositories}; do |
| 103 | + # FIXME: Enable *.ddeb when packagecloud.io will support it. |
| 104 | + for file in build/*.rpm build/*.deb build/*.dsc; do |
| 105 | + extension=${file##*.} |
| 106 | + |
| 107 | + # Skip non-matched globs: say, build/*.rpm on Debian. |
| 108 | + basename="$(basename "${file}" ".${extension}")" |
| 109 | + [ "${basename}" = "*" ] && continue |
| 110 | + |
| 111 | + # Push all source files listed in .dsc file together with |
| 112 | + # the file. |
| 113 | + # |
| 114 | + # FIXME: It seems logical to move this logic to the |
| 115 | + # packagecloud tool we use. |
| 116 | + files="${file}" |
| 117 | + if [ "${extension}" = "dsc" ]; then |
| 118 | + parse_dsc_file='{ |
| 119 | + if ($0 == "Files:") { |
| 120 | + FILES_SECTION = 1; |
| 121 | + } else if (FILES_SECTION != 0) { |
| 122 | + print "build/"$3; |
| 123 | + } |
| 124 | + }' |
| 125 | + files="${files} $(awk "${parse_dsc_file}" "${file}")" |
| 126 | + fi |
| 127 | + |
| 128 | + user=${DEPLOY_PACKAGECLOUD_USER} |
| 129 | + |
| 130 | + # Retry failed attempts to upload a package. |
| 131 | + # |
| 132 | + # packagecloud.io sometimes replieds with 502 Bad Gateway |
| 133 | + # for attempts to push, so retrying is important here. |
| 134 | + # |
| 135 | + # FIXME: This way we don't differentiate network errors |
| 136 | + # and all other ones. It would be much better to retry |
| 137 | + # from inside the packagecloud tool (requests library |
| 138 | + # supports it). |
| 139 | + for i in $(seq 1 5); do |
| 140 | + # FIXME: The tool fetches distributions.json each |
| 141 | + # time. It can cache the data somewhere and reuse |
| 142 | + # during some time period until expiration. |
| 143 | + ${packagecloud_tool} push ${push_args} ${user}/${repo} \ |
| 144 | + ${extension} ${OS} ${DIST} ${files} && break |
| 145 | + done |
| 146 | + done |
| 147 | +done |
0 commit comments