From efa7c5ab7d863f9dd0d6d4092f2de537174ed65d Mon Sep 17 00:00:00 2001 From: Ron Stone Date: Fri, 4 Nov 2022 14:10:22 -0400 Subject: [PATCH] Download files Pull arbitrary files into the build. Use swapable configs to specify source and dest. Remove echo used for testing. Clean up comments Clean up usage output Add missing semicolon Standardize arg handling Add flag to skip branch lookup & dl from non-git URLs Delete commented/obsolete lines Troubleshoot zuul -1: Failing on "grep '^\[testenv:pdf-docs\]' src/opendev.org/starlingx/docs/tox.ini" - this change does not touch tox.ini - other reviews are passing with the same tox file - removing new subdir named 'configs' on hunch that this may be a reserved name? - Adjust linters to relax line lenght restrition on yaml files Update target location for alarms and logs. Delete temporary events dl used in testing Signed-off-by: Ron Stone Change-Id: I8ffff95688bf9a3a93e7e5a01bacbf132abe312d --- get-remote-files.sh | 138 ++++++++++++++++++++++++++++++++++++++++++++ templates/events.sh | 13 +++++ tox.ini | 4 +- 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100755 get-remote-files.sh create mode 100644 templates/events.sh diff --git a/get-remote-files.sh b/get-remote-files.sh new file mode 100755 index 000000000..83d109fed --- /dev/null +++ b/get-remote-files.sh @@ -0,0 +1,138 @@ +#!/usr/bin/env bash + +# Fetch arbitrary files from a remote repo for processing/ +# inclusion in local build. + +message () { echo -e "$@" 1>&2; } + +usage_error () { + message "\nUsage: $0 config-file -o [-f -b] + contains fetch and save locations for files + -o sets the output path to the save locations in or to STDOUT + **Note** Do not set to \"stdout\" if you are downloading a binary file. + -f optionally forces existing output files to be overwritten + -b skips branch lookup. Use this if downloading from a non-git URL\n" + exit 1 +} + +check_util_deps () { + for dep in $@ + do + if ! hash $dep 2>/dev/null; then + message >&2 "... $dep dependency not met. Please install." + exit 1 + fi + done +} + +check_file_deps () { + for filereq in $@ + do + if [ ! -f "${filereq}" ] && [ ! -L "${filereq}" ]; then message "${filereq} not found. Quiting."; exit 1; fi + done +} + +load_configs () { + CONTEXT_DIR="${BASH_SOURCE%/*}" + if [[ ! -d "$CONTEXT_DIR" ]]; then CONTEXT_DIR="$PWD"; fi + . "$CONTEXT_DIR/$config_file" + message "Loaded $CONTEXT_DIR/$config_file" +} + +get_remote () { + + if [[ $no_branch = "t" ]]; then message "Branch ignored"; return; fi + + local regex_br="^defaultbranch\=(.*)\s*$" + local _remote=$(grep defaultbranch $branch_file) + + if [[ "${_remote}" =~ $regex_br ]] + then + remote="${BASH_REMATCH[1]}/" + else + message "Can't find remote branch"; exit 1 + fi + message "Remote is: $remote" +} + + +fetch_files () { + for f in "${!remote_files[@]}"; do + + local _outfile + + case $out_method in + + "file") + _outfile="$common_target${remote_files[$f]}" + ;; + "stdout") + _outfile="-" + ;; + *) + message "Should never get here!"; exit 1; + ;; + + esac + + if [ -f $_outfile ] && [ ! "$force" = "t" ] && [ "$out_method" = "file" ]; then + message "$_outfile already exists, use \"-f\" to override. Quiting" + usage_error + exit 1 + fi + + wget -q -O $_outfile http://$remote_repo/$remote$f + + if [ $? -ne 0 ]; then + if [ ! -s $_outfile ]; then rm -f $_outfile; fi + message "Could not download ${remote_files[$f]}. Quiting" + exit 1 + fi + + done +} + +while getopts "c:o:fb" flag +do + case "${flag}" in + + c) + config_file=$OPTARG + ;; + + o) + case $OPTARG in + + file) out_method="file" ;; + stdout) out_method="stdout" ;; + *) usage_error ;; + + esac + ;; + + f) force="t" ;; + + b) no_branch="t" ;; + + *) message "Unknown arg \"$flag\""; usage_error ;; + + esac +done + +shift "$(( OPTIND - 1 ))" + +if [ -z "$config_file" ] || [ -z "$out_method" ] +then + message "ARGS CHECK FAILED" + usage_error +fi + +branch_file=".gitreview" + +## Run + +check_util_deps wget +check_file_deps $branch_file $config_file +declare -A remote_files && load_configs $config_file +get_remote +fetch_files diff --git a/templates/events.sh b/templates/events.sh new file mode 100644 index 000000000..e5b7956f5 --- /dev/null +++ b/templates/events.sh @@ -0,0 +1,13 @@ +# If set, will be prepended to output paths +common_target="doc/source/fault-mgmt/kubernetes/" + +# The repo to download from +remote_repo="opendev.org/starlingx/fault/raw/branch" + +# Associate remote files and local paths +# key is the path to target in remote repo, relative to repo root +# value is path and name to be saved, relative to CWD and common_target if set + +remote_files=( + ["fm-doc/fm_doc/events.yaml"]="test-events.yaml" + ) diff --git a/tox.ini b/tox.ini index 6a5b12ca8..5089cb33b 100644 --- a/tox.ini +++ b/tox.ini @@ -34,7 +34,9 @@ commands = bash -c "find {toxinidir} \ \( -name .tox -prune \) \ -o -type f -name '*.yaml' \ - -print0 | xargs -0 yamllint" + -print0 | xargs -0 yamllint -d '\{extends: relaxed, rules: \{line-length: \{max: 260\}\}\}'" + # -print0 | xargs -0 yamllint" + [testenv:venv] commands = {posargs}