github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/build-support/functions/40-publish.sh (about) 1 function hashicorp_release { 2 # Arguments: 3 # $1 - Path to directory containing all of the release artifacts 4 # 5 # Returns: 6 # 0 - success 7 # * - failure 8 # 9 # Notes: 10 # Requires the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables 11 # to be set 12 13 status "Uploading files" 14 hc-releases upload "${1}" || return 1 15 16 status "Publishing the release" 17 hc-releases publish || return 1 18 19 return 0 20 } 21 22 function confirm_git_remote { 23 # Arguments: 24 # $1 - Path to git repo 25 # $2 - remote name 26 # 27 # Returns: 28 # 0 - success 29 # * - error 30 # 31 32 local remote="$2" 33 local url=$(git_remote_url "$1" "${remote}") 34 35 echo -e "\n\nConfigured Git Remote: ${remote}" 36 echo -e "Configured Git URL: ${url}\n" 37 38 local answer="" 39 40 while true 41 do 42 case "${answer}" in 43 [yY]* ) 44 status "Remote Accepted" 45 return 0 46 break 47 ;; 48 [nN]* ) 49 err "Remote Rejected" 50 return 1 51 break 52 ;; 53 * ) 54 read -p "Is this Git Remote correct to push ${CONSUL_PKG_NAME} to? [y/n]: " answer 55 ;; 56 esac 57 done 58 } 59 60 function confirm_git_push_changes { 61 # Arguments: 62 # $1 - Path to git repo 63 # 64 # Returns: 65 # 0 - success 66 # * - error 67 # 68 69 if ! test -d "$1" 70 then 71 err "ERROR: '$1' is not a directory. confirm_git_push_changes must be called with the path to a git repo as the first argument'" 72 return 1 73 fi 74 75 pushd "${1}" > /dev/null 76 77 78 declare -i ret=0 79 git_log_summary || ret=1 80 if test ${ret} -eq 0 81 then 82 # put a empty line between the git changes and the prompt 83 echo "" 84 85 local answer="" 86 87 while true 88 do 89 case "${answer}" in 90 [yY]* ) 91 status "Changes Accepted" 92 ret=0 93 break 94 ;; 95 [nN]* ) 96 err "Changes Rejected" 97 ret=1 98 break 99 ;; 100 ?) 101 # bindata_assetfs.go will make these meaningless 102 git_diff "$(pwd)" ":!agent/bindata_assetfs.go"|| ret 1 103 answer="" 104 ;; 105 * ) 106 read -p "Are these changes correct? [y/n] (or type ? to show the diff output): " answer 107 ;; 108 esac 109 done 110 fi 111 112 popd > /dev/null 113 return $ret 114 } 115 116 function extract_consul_local { 117 # Arguments: 118 # $1 - Path to the zipped binary to test 119 # $2 - Version to look for 120 # 121 # Returns: 122 # 0 - success 123 # * - error 124 125 local zfile="${1}/${CONSUL_PKG_NAME}_${2}_$(go env GOOS)_$(go env GOARCH).zip" 126 127 if ! test -f "${zfile}" 128 then 129 err "ERROR: File not found or is not a regular file: ${zfile}" 130 return 1 131 fi 132 133 local ret=0 134 local tfile="$(mktemp) -t "${CONSUL_PKG_NAME}_")" 135 136 unzip -p "${zfile}" "consul" > "${tfile}" 137 if test $? -eq 0 138 then 139 chmod +x "${tfile}" 140 echo "${tfile}" 141 return 0 142 else 143 err "ERROR: Failed to extract consul binary from the zip file" 144 return 1 145 fi 146 } 147 148 function confirm_consul_version { 149 # Arguments: 150 # $1 - consul exe to use 151 # 152 # Returns: 153 # 0 - success 154 # * - error 155 local consul_exe="$1" 156 157 if ! test -x "${consul_exe}" 158 then 159 err "ERROR: '${consul_exe} is not an executable" 160 return 1 161 fi 162 163 "${consul_exe}" version 164 165 # put a empty line between the version output and the prompt 166 echo "" 167 168 local answer="" 169 170 while true 171 do 172 case "${answer}" in 173 [yY]* ) 174 status "Version Accepted" 175 ret=0 176 break 177 ;; 178 [nN]* ) 179 err "Version Rejected" 180 ret=1 181 break 182 ;; 183 * ) 184 read -p "Is this Consul version correct? [y/n]: " answer 185 ;; 186 esac 187 done 188 } 189 190 function confirm_consul_info { 191 # Arguments: 192 # $1 - Path to a consul exe that can be run on this system 193 # 194 # Returns: 195 # 0 - success 196 # * - error 197 198 local consul_exe="$1" 199 local log_file="$(mktemp) -t "consul_log_")" 200 "${consul_exe}" agent -dev > "${log_file}" 2>&1 & 201 local consul_pid=$! 202 sleep 1 203 status "First 25 lines/1s of the agents output:" 204 head -n 25 "${log_file}" 205 206 echo "" 207 local ret=0 208 local answer="" 209 210 while true 211 do 212 case "${answer}" in 213 [yY]* ) 214 status "Consul Agent Output Accepted" 215 break 216 ;; 217 [nN]* ) 218 err "Consul Agent Output Rejected" 219 ret=1 220 break 221 ;; 222 * ) 223 read -p "Is this Consul Agent Output correct? [y/n]: " answer 224 ;; 225 esac 226 done 227 228 if test "${ret}" -eq 0 229 then 230 status "Consul Info Output" 231 "${consul_exe}" info 232 echo "" 233 local answer="" 234 235 while true 236 do 237 case "${answer}" in 238 [yY]* ) 239 status "Consul Info Output Accepted" 240 break 241 ;; 242 [nN]* ) 243 err "Consul Info Output Rejected" 244 return 1 245 break 246 ;; 247 * ) 248 read -p "Is this Consul Info Output correct? [y/n]: " answer 249 ;; 250 esac 251 done 252 fi 253 254 if test "${ret}" -eq 0 255 then 256 local tfile="$(mktemp) -t "${CONSUL_PKG_NAME}_")" 257 if ! curl -o "${tfile}" "http://localhost:8500/ui/" 258 then 259 err "ERROR: Failed to curl http://localhost:8500/ui/" 260 return 1 261 fi 262 263 local ui_vers=$(ui_version "${tfile}") 264 if test $? -ne 0 265 then 266 err "ERROR: Failed to determine the ui version from the index.html file" 267 return 1 268 fi 269 status "UI Version: ${ui_vers}" 270 local ui_logo_type=$(ui_logo_type "${tfile}") 271 if test $? -ne 0 272 then 273 err "ERROR: Failed to determine the ui logo/binary type from the index.html file" 274 return 1 275 fi 276 status "UI Logo: ${ui_logo_type}" 277 278 echo "" 279 local answer="" 280 281 while true 282 do 283 case "${answer}" in 284 [yY]* ) 285 status "Consul UI/Logo Version Accepted" 286 break 287 ;; 288 [nN]* ) 289 err "Consul UI/Logo Version Rejected" 290 return 1 291 break 292 ;; 293 * ) 294 read -p "Is this Consul UI/Logo Version correct? [y/n]: " answer 295 ;; 296 esac 297 done 298 fi 299 300 301 302 status "Requesting Consul to leave the cluster / shutdown" 303 "${consul_exe}" leave 304 wait ${consul_pid} > /dev/null 2>&1 305 306 return $? 307 } 308 309 function extract_consul { 310 extract_consul_local "$1" "$2" 311 } 312 313 function verify_release_build { 314 # Arguments: 315 # $1 - Path to top level Consul source 316 # $2 - expected version (optional - will parse if empty) 317 # 318 # Returns: 319 # 0 - success 320 # * - failure 321 322 if ! test -d "$1" 323 then 324 err "ERROR: '$1' is not a directory. publish_release must be called with the path to the top level source as the first argument'" 325 return 1 326 fi 327 328 local sdir="$1" 329 330 local vers="$(get_version ${sdir} true false)" 331 if test -n "$2" 332 then 333 vers="$2" 334 fi 335 336 if test -z "${vers}" 337 then 338 err "Please specify a version (couldn't parse one from the source)." 339 return 1 340 fi 341 342 status_stage "==> Verifying release files" 343 check_release "${sdir}/pkg/dist" "${vers}" true || return 1 344 345 status_stage "==> Extracting Consul version for local system" 346 local consul_exe=$(extract_consul "${sdir}/pkg/dist" "${vers}") || return 1 347 # make sure to remove the temp file 348 trap "rm '${consul_exe}'" EXIT 349 350 status_stage "==> Confirming Consul Version" 351 confirm_consul_version "${consul_exe}" || return 1 352 353 status_stage "==> Confirming Consul Agent Info" 354 confirm_consul_info "${consul_exe}" || return 1 355 } 356 357 function publish_release { 358 # Arguments: 359 # $1 - Path to top level Consul source that contains the built release 360 # $2 - boolean whether to publish to git upstream 361 # $3 - boolean whether to publish to releases.hashicorp.com 362 # 363 # Returns: 364 # 0 - success 365 # * - error 366 367 if ! test -d "$1" 368 then 369 err "ERROR: '$1' is not a directory. publish_release must be called with the path to the top level source as the first argument'" 370 return 1 371 fi 372 373 local sdir="$1" 374 local pub_git="$2" 375 local pub_hc_releases="$3" 376 377 if test -z "${pub_git}" 378 then 379 pub_git=1 380 fi 381 382 if test -z "${pub_hc_releases}" 383 then 384 pub_hc_releases=1 385 fi 386 387 local vers="$(get_version ${sdir} true false)" 388 if test $? -ne 0 389 then 390 err "Please specify a version (couldn't parse one from the source)." 391 return 1 392 fi 393 394 verify_release_build "$1" "${vers}" || return 1 395 396 status_stage "==> Confirming Git is clean" 397 is_git_clean "$1" true || return 1 398 399 status_stage "==> Confirming Git Changes" 400 confirm_git_push_changes "$1" || return 1 401 402 status_stage "==> Checking for blacklisted Git Remote" 403 local remote=$(find_git_remote "${sdir}") || return 1 404 git_remote_not_blacklisted "${sdir}" "${remote}" || return 1 405 406 status_stage "==> Confirming Git Remote" 407 confirm_git_remote "${sdir}" "${remote}" || return 1 408 409 if is_set "${pub_git}" 410 then 411 status_stage "==> Pushing to Git" 412 git_push_ref "$1" "" "${remote}" || return 1 413 git_push_ref "$1" "v${vers}" "${remote}" || return 1 414 fi 415 416 if is_set "${pub_hc_releases}" 417 then 418 status_stage "==> Publishing to releases.hashicorp.com" 419 hashicorp_release "${sdir}/pkg/dist" || return 1 420 fi 421 422 return 0 423 }