summaryrefslogtreecommitdiff
path: root/scripts/gitlab_workhorse_component_helpers.sh
blob: ebd43a125b9990bc4d9077702c0d9c950e56e8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env bash

set -euo pipefail

export CURL_TOKEN_HEADER="${CURL_TOKEN_HEADER:-"JOB-TOKEN"}"
export GITLAB_WORKHORSE_BINARIES_LIST="gitlab-resize-image gitlab-zip-cat gitlab-zip-metadata gitlab-workhorse"
export GITLAB_WORKHORSE_PACKAGE_FILES_LIST="${GITLAB_WORKHORSE_BINARIES_LIST} WORKHORSE_TREE"
export GITLAB_WORKHORSE_TREE=${GITLAB_WORKHORSE_TREE:-$(git rev-parse HEAD:workhorse)}
export GITLAB_WORKHORSE_PACKAGE="workhorse-${GITLAB_WORKHORSE_TREE}.tar.gz"
export GITLAB_WORKHORSE_PACKAGE_URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${GITLAB_WORKHORSE_FOLDER}/${GITLAB_WORKHORSE_TREE}/${GITLAB_WORKHORSE_PACKAGE}"

function gitlab_workhorse_archive_doesnt_exist() {
  local package_url="${GITLAB_WORKHORSE_PACKAGE_URL}"

  status=$(curl -I --silent --retry 3 --output /dev/null -w "%{http_code}" "${package_url}")

  [[ "${status}" != "200" ]]
}

function create_gitlab_workhorse_package() {
  local archive_filename="${GITLAB_WORKHORSE_PACKAGE}"
  local folder_to_archive="${GITLAB_WORKHORSE_FOLDER}"
  local workhorse_folder_path="${TMP_TEST_GITLAB_WORKHORSE_PATH}"
  local tar_working_folder="${TMP_TEST_FOLDER}"

  echoinfo "Running 'tar -czvf ${archive_filename} -C ${tar_working_folder} ${folder_to_archive}'"
  tar -czvf ${archive_filename} -C ${tar_working_folder} ${folder_to_archive}
  du -h ${archive_filename}
}

function extract_gitlab_workhorse_package() {
  local tar_working_folder="${TMP_TEST_FOLDER}"
  mkdir -p "${tar_working_folder}"

  echoinfo "Extracting archive to ${tar_working_folder}"

  tar -xzv -C ${tar_working_folder} < /dev/stdin
}

function upload_gitlab_workhorse_package() {
  local archive_filename="${GITLAB_WORKHORSE_PACKAGE}"
  local package_url="${GITLAB_WORKHORSE_PACKAGE_URL}"
  local token_header="${CURL_TOKEN_HEADER}"
  local token="${CI_JOB_TOKEN}"

  echoinfo "Uploading ${archive_filename} to ${package_url} ..."
  curl --fail --silent --retry 3 --header "${token_header}: ${token}" --upload-file "${archive_filename}" "${package_url}"
}

function read_curl_gitlab_workhorse_package() {
  local package_url="${GITLAB_WORKHORSE_PACKAGE_URL}"
  local token_header="${CURL_TOKEN_HEADER}"
  local token="${CI_JOB_TOKEN}"

  echoinfo "Downloading from ${package_url} ..."

  curl --fail --silent --retry 3 --header "${token_header}: ${token}" "${package_url}"
}

function download_and_extract_gitlab_workhorse_package() {
  read_curl_gitlab_workhorse_package | extract_gitlab_workhorse_package
}

function select_gitlab_workhorse_essentials() {
  local tmp_path="${CI_PROJECT_DIR}/tmp/${GITLAB_WORKHORSE_FOLDER}"
  local original_gitlab_workhorse_path="${TMP_TEST_GITLAB_WORKHORSE_PATH}"

  mkdir -p ${tmp_path}
  cd ${original_gitlab_workhorse_path} && mv ${GITLAB_WORKHORSE_PACKAGE_FILES_LIST} ${tmp_path} && cd -
  rm -rf ${original_gitlab_workhorse_path}

  # Move the temp folder to its final destination
  mv ${tmp_path} ${TMP_TEST_FOLDER}
}