xref: /aosp_15_r20/kernel/tests/tools/fetch_artifact.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/env bash
2*2f2c4c7aSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0
3*2f2c4c7aSAndroid Build Coastguard Worker
4*2f2c4c7aSAndroid Build Coastguard Worker# fetch_artifact .sh is a handy tool dedicated to download artifacts from ci.
5*2f2c4c7aSAndroid Build Coastguard Worker# The fetch_artifact binary is needed for this script. Please see more info at:
6*2f2c4c7aSAndroid Build Coastguard Worker#    go/fetch_artifact,
7*2f2c4c7aSAndroid Build Coastguard Worker#    or
8*2f2c4c7aSAndroid Build Coastguard Worker#    https://android.googlesource.com/tools/fetch_artifact/
9*2f2c4c7aSAndroid Build Coastguard Worker# Will use x20 binary: /google/data/ro/projects/android/fetch_artifact by default.
10*2f2c4c7aSAndroid Build Coastguard Worker# Can install fetch_artifact locally with:
11*2f2c4c7aSAndroid Build Coastguard Worker# sudo glinux-add-repo android stable && \
12*2f2c4c7aSAndroid Build Coastguard Worker# sudo apt update && \
13*2f2c4c7aSAndroid Build Coastguard Worker# sudo apt install android-fetch-artifact#
14*2f2c4c7aSAndroid Build Coastguard Worker#
15*2f2c4c7aSAndroid Build Coastguard WorkerDEFAULT_FETCH_ARTIFACT=/google/data/ro/projects/android/fetch_artifact
16*2f2c4c7aSAndroid Build Coastguard WorkerBOLD="$(tput bold)"
17*2f2c4c7aSAndroid Build Coastguard WorkerEND="$(tput sgr0)"
18*2f2c4c7aSAndroid Build Coastguard WorkerGREEN="$(tput setaf 2)"
19*2f2c4c7aSAndroid Build Coastguard WorkerRED="$(tput setaf 198)"
20*2f2c4c7aSAndroid Build Coastguard WorkerYELLOW="$(tput setaf 3)"
21*2f2c4c7aSAndroid Build Coastguard WorkerBLUE="$(tput setaf 34)"
22*2f2c4c7aSAndroid Build Coastguard Worker
23*2f2c4c7aSAndroid Build Coastguard Workerfunction print_info() {
24*2f2c4c7aSAndroid Build Coastguard Worker    echo "[$MY_NAME]: ${GREEN}$1${END}"
25*2f2c4c7aSAndroid Build Coastguard Worker}
26*2f2c4c7aSAndroid Build Coastguard Worker
27*2f2c4c7aSAndroid Build Coastguard Workerfunction print_warn() {
28*2f2c4c7aSAndroid Build Coastguard Worker    echo "[$MY_NAME]: ${YELLOW}$1${END}"
29*2f2c4c7aSAndroid Build Coastguard Worker}
30*2f2c4c7aSAndroid Build Coastguard Worker
31*2f2c4c7aSAndroid Build Coastguard Workerfunction print_error() {
32*2f2c4c7aSAndroid Build Coastguard Worker    echo -e "[$MY_NAME]: ${RED}$1${END}"
33*2f2c4c7aSAndroid Build Coastguard Worker    exit 1
34*2f2c4c7aSAndroid Build Coastguard Worker}
35*2f2c4c7aSAndroid Build Coastguard Worker
36*2f2c4c7aSAndroid Build Coastguard Workerfunction binary_checker() {
37*2f2c4c7aSAndroid Build Coastguard Worker    if which fetch_artifact &> /dev/null; then
38*2f2c4c7aSAndroid Build Coastguard Worker        FETCH_CMD="fetch_artifact"
39*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -z "${FETCH_ARTIFACT}" ] && [ -f "${FETCH_ARTIFACT}" ]; then
40*2f2c4c7aSAndroid Build Coastguard Worker        FETCH_CMD="${FETCH_ARTIFACT}"
41*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "$DEFAULT_FETCH_ARTIFACT" ]; then
42*2f2c4c7aSAndroid Build Coastguard Worker        FETCH_CMD="$DEFAULT_FETCH_ARTIFACT"
43*2f2c4c7aSAndroid Build Coastguard Worker    else
44*2f2c4c7aSAndroid Build Coastguard Worker        print_error "\n${RED} fetch_artifact is not found${END}"
45*2f2c4c7aSAndroid Build Coastguard Worker        echo -e "\n${RED} Please see go/fetch_artifact${END} or
46*2f2c4c7aSAndroid Build Coastguard Worker        https://android.googlesource.com/tools/fetch_artifact/+/refs/heads/main"
47*2f2c4c7aSAndroid Build Coastguard Worker        exit 1
48*2f2c4c7aSAndroid Build Coastguard Worker    fi
49*2f2c4c7aSAndroid Build Coastguard Worker}
50*2f2c4c7aSAndroid Build Coastguard Worker
51*2f2c4c7aSAndroid Build Coastguard Worker
52*2f2c4c7aSAndroid Build Coastguard Workerbinary_checker
53*2f2c4c7aSAndroid Build Coastguard Worker
54*2f2c4c7aSAndroid Build Coastguard Workerfetch_cli="$FETCH_CMD"
55*2f2c4c7aSAndroid Build Coastguard Worker
56*2f2c4c7aSAndroid Build Coastguard WorkerBUILD_INFO=
57*2f2c4c7aSAndroid Build Coastguard WorkerBUILD_FORMAT="ab://<branch>/<build_target>/<build_id>/<file_name>"
58*2f2c4c7aSAndroid Build Coastguard WorkerEXTRA_OPTIONS=
59*2f2c4c7aSAndroid Build Coastguard Worker
60*2f2c4c7aSAndroid Build Coastguard WorkerMY_NAME="${0##*/}"
61*2f2c4c7aSAndroid Build Coastguard Worker
62*2f2c4c7aSAndroid Build Coastguard Workerfor i in "$@"; do
63*2f2c4c7aSAndroid Build Coastguard Worker    case $i in
64*2f2c4c7aSAndroid Build Coastguard Worker        "ab://"*)
65*2f2c4c7aSAndroid Build Coastguard Worker        BUILD_INFO=$i
66*2f2c4c7aSAndroid Build Coastguard Worker        ;;
67*2f2c4c7aSAndroid Build Coastguard Worker        *)
68*2f2c4c7aSAndroid Build Coastguard Worker        EXTRA_OPTIONS+=" $i"
69*2f2c4c7aSAndroid Build Coastguard Worker        ;;
70*2f2c4c7aSAndroid Build Coastguard Worker    esac
71*2f2c4c7aSAndroid Build Coastguard Workerdone
72*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$BUILD_INFO" ]; then
73*2f2c4c7aSAndroid Build Coastguard Worker    print_error "$0 didn't come with the expected $BUILD_FORMAT"
74*2f2c4c7aSAndroid Build Coastguard Workerfi
75*2f2c4c7aSAndroid Build Coastguard Worker
76*2f2c4c7aSAndroid Build Coastguard WorkerIFS='/' read -ra array <<< "$BUILD_INFO"
77*2f2c4c7aSAndroid Build Coastguard Workerif [ ${#array[@]} -lt 6 ]; then
78*2f2c4c7aSAndroid Build Coastguard Worker    print_error "Invalid build format: $BUILD_INFO. Needs to be: $BUILD_FORMAT"
79*2f2c4c7aSAndroid Build Coastguard Workerelif [ ${#array[@]} -gt 7 ]; then
80*2f2c4c7aSAndroid Build Coastguard Worker    print_error "Invalid TEST_DIR format: $BUILD_INFO. Needs to be: $BUILD_FORMAT"
81*2f2c4c7aSAndroid Build Coastguard Workerelse
82*2f2c4c7aSAndroid Build Coastguard Worker    fetch_cli+=" --branch ${array[2]}"
83*2f2c4c7aSAndroid Build Coastguard Worker    fetch_cli+=" --target ${array[3]}"
84*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "${array[4]}" != latest* ]]; then
85*2f2c4c7aSAndroid Build Coastguard Worker        fetch_cli+=" --bid ${array[4]}"
86*2f2c4c7aSAndroid Build Coastguard Worker    else
87*2f2c4c7aSAndroid Build Coastguard Worker        fetch_cli+=" --latest"
88*2f2c4c7aSAndroid Build Coastguard Worker    fi
89*2f2c4c7aSAndroid Build Coastguard Worker    fetch_cli+="$EXTRA_OPTIONS"
90*2f2c4c7aSAndroid Build Coastguard Worker    fetch_cli+=" '${array[5]}'"
91*2f2c4c7aSAndroid Build Coastguard Workerfi
92*2f2c4c7aSAndroid Build Coastguard Worker
93*2f2c4c7aSAndroid Build Coastguard Workerprint_info "Run: $fetch_cli"
94*2f2c4c7aSAndroid Build Coastguard Workereval "$fetch_cli"
95