Files
tf_provider/devops/03_build_and_upload_provider.sh
T

194 lines
6.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="${ROOT_DIR:-$(cd "${SCRIPT_DIR}/.." && pwd)}"
PROVIDER_MAIN="${ROOT_DIR}/universal_rebuild/main.go"
BUILD_SCRIPT="${ROOT_DIR}/devops/build-provider.sh"
PROFILE_DIR="${PROFILE_DIR:-}"
if [[ "${1:-}" == "--profile" ]]; then
PROFILE_DIR="${2:-}"
shift 2
fi
if [[ -n "$PROFILE_DIR" ]]; then
if [[ ! -d "$PROFILE_DIR" ]]; then
echo "Error: profile directory not found: $PROFILE_DIR" >&2
exit 2
fi
PROFILE_ENV_FILE="${PROFILE_DIR}/profile.env"
if [[ -f "$PROFILE_ENV_FILE" ]]; then
set -a
# shellcheck disable=SC1090
source "$PROFILE_ENV_FILE"
set +a
fi
fi
if [[ -z "$PROFILE_DIR" ]]; then
echo "Error: --profile <path> is required. Strict mode stores generated files per stand only." >&2
exit 2
fi
resolve_root_path() {
local path_value="$1"
if [[ -z "$path_value" ]]; then
echo ""
return
fi
if [[ "$path_value" = /* ]]; then
echo "$path_value"
return
fi
echo "${ROOT_DIR}/${path_value}"
}
S3CFG_REGISTRY="${S3CFG_REGISTRY:-${ROOT_DIR}/secrets/.s3cfg_registry}"
TIMEOUTS_SOURCE="${TIMEOUTS_SOURCE:-${ROOT_DIR}/devops/config/operation_timeouts.json}"
PROFILE_RESOURCES_YAML_DIR="${PROFILE_DIR}/generated/resources_yaml"
PROFILE_GENERATED_GO_DIR="${PROFILE_DIR}/generated/go"
PROFILE_BUILD_DIR="${PROFILE_DIR}/generated/provider_build"
S3CFG_REGISTRY="$(resolve_root_path "$S3CFG_REGISTRY")"
TIMEOUTS_SOURCE="$(resolve_root_path "$TIMEOUTS_SOURCE")"
PROFILE_RESOURCES_YAML_DIR="$(resolve_root_path "$PROFILE_RESOURCES_YAML_DIR")"
PROFILE_GENERATED_GO_DIR="$(resolve_root_path "$PROFILE_GENERATED_GO_DIR")"
PROFILE_BUILD_DIR="$(resolve_root_path "$PROFILE_BUILD_DIR")"
if [[ -f "${PROFILE_DIR}/operation_timeouts.json" ]]; then
TIMEOUTS_SOURCE="${PROFILE_DIR}/operation_timeouts.json"
fi
load_s3cfg_registry() {
local cfg="$1"
if [[ ! -f "$cfg" ]]; then
echo "Error: S3 config not found: $cfg" >&2
exit 2
fi
local access_key secret_key host_base use_https endpoint
access_key=$(awk -F '=' '/^\s*access_key\s*=/ {gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit}' "$cfg")
secret_key=$(awk -F '=' '/^\s*secret_key\s*=/ {gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit}' "$cfg")
host_base=$(awk -F '=' '/^\s*host_base\s*=/ {gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit}' "$cfg")
use_https=$(awk -F '=' '/^\s*use_https\s*=/ {gsub(/^[ \t]+|[ \t]+$/, "", $2); print tolower($2); exit}' "$cfg")
if [[ -z "${S3_ACCESS_KEY:-}" && -n "$access_key" ]]; then
export S3_ACCESS_KEY="$access_key"
fi
if [[ -z "${S3_SECRET_KEY:-}" && -n "$secret_key" ]]; then
export S3_SECRET_KEY="$secret_key"
fi
if [[ -z "${S3_ENDPOINT:-}" && -n "$host_base" ]]; then
if [[ "$host_base" == http* ]]; then
endpoint="$host_base"
else
if [[ "$use_https" == "false" || "$use_https" == "0" || "$use_https" == "no" ]]; then
endpoint="http://${host_base}"
else
endpoint="https://${host_base}"
fi
fi
export S3_ENDPOINT="$endpoint"
fi
if [[ -z "${S3_ENDPOINT:-}" || -z "${S3_ACCESS_KEY:-}" || -z "${S3_SECRET_KEY:-}" ]]; then
echo "Error: S3 credentials are not set (S3_ENDPOINT/S3_ACCESS_KEY/S3_SECRET_KEY)" >&2
exit 2
fi
}
VERSION="${1:-}"
if [[ -z "$VERSION" ]]; then
VERSION="${PROVIDER_VERSION:-${RELEASE_VERSION:-}}"
fi
if [[ -z "$VERSION" ]]; then
VERSION=$(grep -E 'version string' "$PROVIDER_MAIN" | sed -E 's/.*"([0-9.]+)".*/\1/')
fi
if [[ -z "$VERSION" ]]; then
echo "Error: unable to detect version from $PROVIDER_MAIN" >&2
exit 2
fi
load_s3cfg_registry "$S3CFG_REGISTRY"
if [[ ! -f "$TIMEOUTS_SOURCE" ]]; then
echo "Error: operation timeouts config not found: $TIMEOUTS_SOURCE" >&2
exit 2
fi
if [[ ! -d "$PROFILE_RESOURCES_YAML_DIR" ]]; then
echo "Error: profile resources yaml directory not found: $PROFILE_RESOURCES_YAML_DIR" >&2
exit 2
fi
if ! compgen -G "${PROFILE_RESOURCES_YAML_DIR}/*.yaml" > /dev/null; then
echo "Error: no yaml specs found in profile: $PROFILE_RESOURCES_YAML_DIR" >&2
exit 2
fi
if [[ ! -d "$PROFILE_GENERATED_GO_DIR" ]]; then
echo "Error: profile generated go directory not found: $PROFILE_GENERATED_GO_DIR" >&2
exit 2
fi
if ! compgen -G "${PROFILE_GENERATED_GO_DIR}/*.go" > /dev/null; then
echo "Error: no generated go files found in profile: $PROFILE_GENERATED_GO_DIR" >&2
exit 2
fi
validate_registry_completeness() {
local resources_gen_dir="$1"
local registry_file="${resources_gen_dir}/registry.go"
if [[ ! -f "$registry_file" ]]; then
echo "Error: registry.go not found in resources_gen: $resources_gen_dir" >&2
exit 2
fi
local tmp_expected tmp_registered tmp_missing
tmp_expected="$(mktemp)"
tmp_registered="$(mktemp)"
tmp_missing="$(mktemp)"
find "$resources_gen_dir" -maxdepth 1 -type f -name '*_resource.go' -print0 \
| xargs -0 grep -hE 'func[[:space:]]+New[[:alnum:]_]+Resource[[:space:]]*\(' \
| sed -E 's/.*(New[[:alnum:]_]+Resource).*/\1/' \
| sort -u > "$tmp_expected"
grep -hEo 'New[[:alnum:]_]+Resource' "$registry_file" | sort -u > "$tmp_registered"
comm -23 "$tmp_expected" "$tmp_registered" > "$tmp_missing"
if [[ -s "$tmp_missing" ]]; then
echo "Error: registry.go is missing resource constructors:" >&2
sed 's/^/ - /' "$tmp_missing" >&2
rm -f "$tmp_expected" "$tmp_registered" "$tmp_missing"
exit 2
fi
rm -f "$tmp_expected" "$tmp_registered" "$tmp_missing"
}
TMP_PROVIDER_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_PROVIDER_DIR"' EXIT
cp -a "${ROOT_DIR}/universal_rebuild/." "$TMP_PROVIDER_DIR/"
mkdir -p "$TMP_PROVIDER_DIR/resources_yaml"
find "$TMP_PROVIDER_DIR/resources_yaml" -maxdepth 1 -type f -name '*.yaml' -delete
cp -a "${PROFILE_RESOURCES_YAML_DIR}"/*.yaml "$TMP_PROVIDER_DIR/resources_yaml/"
cp "$TIMEOUTS_SOURCE" "$TMP_PROVIDER_DIR/internal/provider/operation_timeouts.json"
mkdir -p "$TMP_PROVIDER_DIR/internal/resources_gen"
find "$TMP_PROVIDER_DIR/internal/resources_gen" -maxdepth 1 -type f -name '*.go' -delete
cp -R "${PROFILE_GENERATED_GO_DIR}"/*.go "$TMP_PROVIDER_DIR/internal/resources_gen/"
validate_registry_completeness "$TMP_PROVIDER_DIR/internal/resources_gen"
mkdir -p "$PROFILE_BUILD_DIR"
if [[ -n "${GPG_PRIVATE_KEY_PATH:-}" ]]; then
export GPG_KEY_FILE="$(resolve_root_path "$GPG_PRIVATE_KEY_PATH")"
fi
echo "Building provider version ${VERSION}..."
PROVIDER_DIR="$TMP_PROVIDER_DIR" BUILD_DIR="$PROFILE_BUILD_DIR" "$BUILD_SCRIPT" "$VERSION"