From: Matthias Kruk Date: Sun, 18 Jul 2021 00:13:54 +0000 (+0900) Subject: include/context: Add module to keep track of build/test contexts X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=57ac132a39011cf8429695da4b8c91edc9944e83;p=foundry include/context: Add module to keep track of build/test contexts This commit adds the context module, which implements a set of functions to track the state of a transaction across the buildsystem. This module will be used to store the logs and artifacts associated with a context in a central location, identified by the context identifier which is stored in several of the IPC messages. --- diff --git a/include/context.sh b/include/context.sh new file mode 100644 index 0000000..d542530 --- /dev/null +++ b/include/context.sh @@ -0,0 +1,161 @@ +#!/bin/bash + +__init() { + if ! include "log" "array"; then + return 1 + fi + + declare -gxr __foundry_context_root="/var/lib/foundry/contexts" + + return 0 +} + +_foundry_context_new_id() { + local context_name="$1" + + local timestamp + local number + + if ! timestamp=$(date +"%Y%m%d-%H%M"); then + return 1 + fi + + if ! number=$(printf "%04d" "$((RANDOM % 10000))"); then + return 1 + fi + + echo "$context_name-$timestamp-$number" + return 0 +} + +foundry_context_new() { + local context_name="$1" + + local tid + local context_path + + if ! tid=$(_foundry_context_new_id "$context_name"); then + return 1 + fi + + context_path="$__foundry_context_root/$tid" + + if ! mkdir -p "$context_path/files" \ + "$context_path/logs"; then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_context_remove() { + local context="$1" + + local context_path + + context_path="$__foundry_context_root/$context" + + if [[ -z "$context" ]]; then + return 1 + fi + + if ! rm -r "$context_path"; then + return 1 + fi + + return 0 +} + +foundry_context_add_file() { + local context="$1" + local filetype="$2" + local file="$3" + + local context_path + local file_path + + context_path="$__foundry_context_root/$context" + file_path="$context_path/$filetype" + + if ! mkdir -p "$file_path"; then + return 1 + fi + + if ! cp "$file" "$file_path/."; then + return 1 + fi + + return 0 +} + +foundry_context_get_files() { + local context="$1" + local file_type="$2" + + local file_dir + local files + local file + + # file_type may be omitted to get all files + file_dir="$__foundry_context_root/$context/$file_type" + files=() + + while read -r file; do + local absolute + + if ! absolute=$(realpath "$file"); then + continue + fi + + files+=("$file") + done < <(find "$file_dir" -type f) + + array_to_lines "${files[@]}" + return 0 +} + +foundry_context_add_log() { + local context="$1" + local logtype="$2" + local log="$3" + + local logdir + + logdir="$__foundry_context_root/$context/logs/$logtype" + + if ! mkdir -p "$logdir"; then + return 1 + fi + + if ! cp "$log" "$logdir/."; then + return 1 + fi + + return 0 +} + +foundry_context_get_logs() { + local context="$1" + local logtype="$2" + + local logdir + local logs + local log + + logdir="$__foundry_context_root/$context/$logtype" + logs=() + + while read -r log; do + local absolute + + if ! absolute=$(realpath "$log"); then + continue + fi + + logs+=("$absolute") + done < <(find "$logdir" -type f) + + array_to_lines "${logs[@]}" + return 0 +}