+++ /dev/null
-#!/bin/bash
-
-check_result() {
- local endpoint="$1"
- local context="$2"
-
- local start
- local timelimit
-
- start=$(date +"%s")
- timelimit=$((5 * 60))
-
- while (( ( $(date +"%s") - start ) < timelimit )); do
- local msg
- local fmsg
- local fmsgtype
- local ctx
- local result
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- ipc_msg_dump "$msg"
-
- if ! fmsg=$(ipc_msg_get_data "$msg"); then
- log_warn "Can't get data from message. Dropping."
- continue
- fi
-
- log_highlight "data" <<< "$fmsg" | log_debug
-
- if ! fmsgtype=$(foundry_msg_get_type "$fmsg"); then
- log_warn "Can't determine message type. Dropping."
- continue
- fi
-
- if [[ "$fmsgtype" != "build" ]]; then
- log_warn "Unexpected message type. Dropping."
- continue
- fi
-
- if ! ctx=$(foundry_msg_build_get_context "$fmsg"); then
- log_warn "Dropping message without context"
- continue
- fi
-
- if [[ "$ctx" != "$context" ]]; then
- # These are not the builds you're looking for
- continue
- fi
-
- if ! result=$(foundry_msg_build_get_result "$fmsg"); then
- log_error "Could not get result from build message"
- return 1
- fi
-
- foundry_context_get_logs "$context" | log_highlight "Build logs"
- foundry_context_get_files "$context" | log_highlight "Build artifacts"
-
- return "$result"
- done
-
- return 1
-}
-
-build() {
- local repository="$1"
- local branch="$2"
-
- local endpoint
- local package
- local buildreq
- local context
-
- package="${repository##*/}"
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open IPC endpoint"
- return 1
- fi
-
- if ! ipc_endpoint_subscribe "$endpoint" "builds"; then
- log_error "Could not subscribe to builds"
- return 1
- fi
-
- if ! context=$(foundry_context_new "$package"); then
- log_error "Could not make a new foundry context"
- return 1
- fi
-
- if ! buildreq=$(foundry_msg_buildrequest_new "$context" \
- "$repository" \
- "$branch"); then
- log_error "Could not make buildrequest message"
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/buildbot" "$buildreq"; then
- log_error "Could not send buildrequest message"
- return 1
- fi
-
- if ! check_result "$endpoint" "$context"; then
- log_error "Build failed"
- return 1
- fi
-
- return 0
-}
-
-main() {
- local repository
- local branch
-
- opt_add_arg "r" "repository" "rv" "" "The repository to build"
- opt_add_arg "b" "branch" "rv" "" "The branch to build"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- repository=$(opt_get "repository")
- branch=$(opt_get "branch")
-
- if ! build "$repository" "$branch"; then
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "foundry/msg" "foundry/context"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-emit_signrequest() {
- local endpoint="$1"
- local context="$2"
- local artifacts=("${@:3}")
-
- local signrequest
- local dst
-
- dst="pub/signbot"
-
- if (( ${#artifacts[@]} == 0 )); then
- log_warn "Nothing to be signed for $context"
- return 1
- fi
-
- if ! signrequest=$(foundry_msg_signrequest_new "$context" "${artifacts[@]}"); then
- log_error "Could not make sign request for $context"
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "$dst" "$signrequest"; then
- log_error "Could not send signrequest to $dst"
- return 1
- fi
-
- return 0
-}
-
-emit_buildrequest() {
- local endpoint="$1"
- local context="$2"
- local repository="$3"
- local branch="$4"
-
- local buildreq
-
- if ! buildreq=$(foundry_msg_buildrequest_new "$context" \
- "$repository" \
- "$branch"); then
- log_error "Could not make build request"
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/buildbot" "$buildreq"; then
- return 1
- fi
-
- return 0
-}
-
-emit_testrequest() {
- local endpoint="$1"
- local commitmsg="$2"
-
- local repository
- local branch
- local commit
- local context
- local project
-
- local testrequest
-
- if ! repository=$(foundry_msg_commit_get_repository "$msg") ||
- ! branch=$(foundry_msg_commit_get_branch "$msg") ||
- ! commit=$(foundry_msg_commit_get_commit "$msg"); then
- return 1
- fi
-
- project="${repository##*/}"
-
- if ! context=$(foundry_context_new "$project"); then
- return 1
- fi
-
- log_debug "Created context $context for $project"
-
- if ! testrequest=$(foundry_msg_testrequest_new "$context" \
- "$repository" \
- "$branch" \
- "$commit"); then
- log_error "Could not make test request"
- return 1
- fi
-
- log_debug "Sending test request $endpoint -> pub/testbot"
-
- if ! ipc_endpoint_send "$endpoint" "pub/testbot" "$testrequest"; then
- return 1
- fi
-
- return 0
-}
-
-emit_distrequest() {
- local endpoint="$1"
- local signmsg="$2"
-
- local tid
- local artifacts
- local distrequest
-
- if ! tid=$(foundry_msg_sign_get_tid "$signmsg"); then
- return 1
- fi
-
- readarray -t artifacts < <(foundry_msg_sign_get_artifacts "$signmsg")
-
- if (( ${#artifacts[@]} == 0 )); then
- return 1
- fi
-
- if ! distrequest=$(foundry_msg_distrequest "$tid" "${artifacts[@]}"); then
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/distbot" "$distrequest"; then
- return 1
- fi
-
- return 0
-}
-
-emit_mergerequest() {
- local endpoint="$1"
- local context="$2"
- local repository="$3"
- local srcbranch="$4"
- local dstbranch="$5"
-
- local mergerequest
-
- if ! mergerequest=$(foundry_msg_mergerequest_new "$context" \
- "$repository" \
- "$srcbranch" \
- "$dstbranch"); then
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/mergebot" "$mergerequest"; then
- return 1
- fi
-
- return 0
-}
-
-_handle_build() {
- local endpoint="$1"
- local msg="$2"
-
- local context
- local artifacts
- local artifact
- local result
-
- artifacts=()
-
- if ! context=$(foundry_msg_build_get_context "$msg"); then
- log_warn "Dropping message without context"
- return 0
- fi
-
- if ! result=$(foundry_msg_build_get_result "$msg"); then
- log_warn "Dropping message without result"
- return 0
- fi
-
- if (( result != 0 )); then
- log_warn "Not emitting sign request for failed build $context"
- return 0
- fi
-
- while read -r artifact; do
- artifacts+=("$artifact")
- done < <(foundry_msg_build_get_artifacts "$msg")
-
- if ! emit_signrequest "$endpoint" "$context" "${artifacts[@]}"; then
- log_error "Could not emit sign request for $context"
- return 1
- fi
-
- log_info "Sign request for $context emitted."
-
- return 0
-}
-
-_handle_commit() {
- local endpoint="$1"
- local msg="$2"
-
- local repository
- local branch
-
- if ! repository=$(foundry_msg_commit_get_repository "$msg") ||
- ! branch=$(foundry_msg_commit_get_branch "$msg"); then
- return 1
- fi
-
- case "$branch" in
- "testing")
- log_debug "Commit on \"testing\" branch -> sending test request"
-
- if ! emit_testrequest "$endpoint" "$msg" \
- "$repository" "$branch"; then
- return 1
- fi
- ;;
-
- *)
- log_warn "Ignoring commit on $repository#$branch"
- return 1
- ;;
- esac
-
- return 0
-}
-
-_handle_test() {
- local endpoint="$1"
- local msg="$2"
-
- local context
- local repository
- local result
- local branch
-
- if ! context=$(foundry_msg_test_get_context "$msg"); then
- log_warn "Dropping test message without context"
- return 1
- fi
-
- if ! repository=$(foundry_msg_test_get_repository "$msg"); then
- log_warn "Dropping test message without repository"
- return 1
- fi
-
- if ! branch=$(foundry_msg_test_get_branch "$msg"); then
- log_warn "Dropping test message for \"$repository\" without branch"
- return 1
- fi
-
- if [[ "$branch" != "testing" ]]; then
- log_info "Ignoring test result for \"$repository\", branch \"$branch\""
- return 0
- fi
-
- if ! result=$(foundry_msg_test_get_result "$msg"); then
- log_warn "Dropping test message for \"$repository\" without result"
- return 1
- fi
-
- if (( result != 0 )); then
- log_info "Ignoring test for \"$repository\" with result \"$result\""
- return 0
- fi
-
- log_info "Sending merge request for \"$repository\""
- if ! emit_mergerequest "$endpoint" "$context" "$repository" \
- "testing" "stable"; then
- return 1
- fi
-
- return 0
-}
-
-_handle_sign() {
- local endpoint="$1"
- local msg="$2"
-
- if ! emit_distrequest "$endpoint" "$msg"; then
- return 1
- fi
-
- return 0
-}
-
-_handle_merge() {
- local endpoint="$1"
- local msg="$2"
-
- local context
- local repository
- local branch
- local accepted_branches
-
- accepted_branches=(
- "master"
- "stable"
- )
-
- if ! context=$(foundry_msg_merge_get_context "$msg"); then
- log_warn "Dropping merge message without context"
- return 0
- fi
-
- if ! repository=$(foundry_msg_merge_get_repository "$msg"); then
- log_warn "Dropping merge message without repository (context $context)"
- return 0
- fi
-
- if ! branch=$(foundry_msg_merge_get_destination_branch "$msg"); then
- log_warn "Dropping merge message for $context@$repository without destination branch"
- return 0
- fi
-
- if ! array_contains "$branch" "${accepted_branches[@]}"; then
- log_info "Ignoring merge message for $context@$repository#$branch"
- return 0
- fi
-
- if ! emit_buildrequest "$endpoint" "$context" "$repository" "$branch"; then
- log_error "Could not send build request for $repository#$branch"
- return 1
- fi
-
- return 0
-}
-
-_handle_notification() {
- local endpoint="$1"
- local msg="$2"
-
- local fmsg
- local type
- declare -A handlers
-
- handlers["build"]=_handle_build
- handlers["commit"]=_handle_commit
- handlers["test"]=_handle_test
- handlers["sign"]=_handle_sign
- handlers["merge"]=_handle_merge
-
- if ! fmsg=$(ipc_msg_get_data "$msg"); then
- log_warn "Dropping message without data"
- return 1
- fi
-
- if ! type=$(foundry_msg_get_type "$fmsg"); then
- log_warn "Dropping message without type"
- return 1
- fi
-
- log_debug "Received $type message"
-
- if ! array_contains "$type" "${!handlers[@]}"; then
- log_warn "Unexpected message type: $type"
- return 1
- fi
-
- log_debug "Message is handled by ${handlers[$type]}"
-
- if ! "${handlers[$type]}" "$endpoint" "$fmsg"; then
- return 1
- fi
-
- return 0
-}
-
-_route_messages() {
- local endpoint
-
- local topics
- local topic
-
- topics=("commits"
- "tests"
- "merges"
- "builds"
- "signs")
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open endpoint"
- return 1
- fi
-
- for topic in "${topics[@]}"; do
- if ! ipc_endpoint_subscribe "$endpoint" "$topic"; then
- log_error "Could not subscribe to $topic"
- return 1
- fi
- done
-
- while inst_running; do
- local msg
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- _handle_notification "$endpoint" "$msg"
- done
-
- return 0
-}
-
-main() {
- opt_add_arg "n" "name" "rv" "" "The name of this instance"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- if ! inst_singleton _route_messages; then
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- return 1
- fi
-
- if ! include "log" "opt" "ipc" "inst" "foundry/msg" "foundry/context"; then
- return 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-check_result() {
- local endpoint="$1"
- local context="$2"
-
- local start
- local timelimit
-
- start=$(date +"%s")
- timelimit=$((5 * 60))
-
- while (( ( $(date +"%s") - start ) < timelimit )); do
- local msg
- local fmsg
- local msgtype
- local ctx
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- if ! fmsg=$(ipc_msg_get_data "$msg"); then
- log_warn "Dropping invalid message (no data)"
- continue
- fi
-
- if ! msgtype=$(foundry_msg_get_type "$fmsg"); then
- log_warn "Dropping message with unexpected type \"$msgtype\""
- continue
- fi
-
- if ! ctx=$(foundry_msg_get_context "$fmsg"); then
- log_warn "Dropping message without context"
- continue
- fi
-
- if [[ "$ctx" != "$context" ]]; then
- # Unrelated
- continue
- fi
-
- foundry_context_get_logs "$context" | log_highlight "$context logs"
- return 0
- done
-
- return 1
-}
-
-prepare_context() {
- local packages=("$@")
-
- local name
- local context
- local package
-
- if (( ${#packages[@]} < 1 )); then
- log_error "No packages to distribute"
- return 1
- fi
-
- name="${packages[0]}"
- name="${name%%_*}"
-
- if ! context=$(foundry_context_new "$name"); then
- log_error "Could not create new context for $name"
- return 1
- fi
-
- for package in "${package[@]}"; do
- if ! foundry_context_add_file "$context" "$package"; then
- log_error "Could not add $package to context $context"
- return 1
- fi
- done
-
- echo "$context"
- return 0
-}
-
-send_dist_request() {
- local endpoint="$1"
- local context="$2"
-
- local distreq
-
- if ! distreq=$(foundry_msg_distrequest_new "$context"); then
- log_error "Could not make distrequest message"
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/distbot" "$distreq"; then
- log_error "Could not send distrequest message to pub/distbot"
- return 1
- fi
-
- return 0
-}
-
-test_distbot() {
- local packages=("$@")
-
- local context
- local package
- local endpoint
- local name
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open an IPC endpoint"
- return 1
- fi
-
- if ! ipc_endpoint_subscribe "$endpoint" "dists"; then
- log_error "Could not subscribe $endpoint to dists"
- return 1
- fi
-
- if ! context=$(prepare_context "$name" "${packages[@]}"); then
- log_error "Could not prepare context"
- return 1
- fi
-
- if ! send_dist_request "$endpoint" "$context"; then
- log_error "Could not send distrequest message"
- return 1
- fi
-
- if ! check_result "$endpoint" "$context"; then
- return 1
- fi
-
- return 0
-}
-
-_add_package() {
- local name="$1"
- local value="$2"
-
- if [[ "$name" != "package" ]]; then
- return 1
- fi
-
- packages+=("$value")
- return 0
-}
-
-main() {
- local packages
-
- packages=()
-
- opt_add_arg "p" "package" "rv" "" "A package to add to distribute" "" _add_package
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- if ! test_distbot "${packages[@]}"; then
- echo "Test failed"
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "foundry/msg" "foundry/context"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-check_result() {
- local endpoint="$1"
- local context="$2"
-
- local start
- local timelimit
-
- start=$(date +"%s")
- timelimit=$((5 * 60))
-
- while (( ( $(date +"%s") - start ) < timelimit )); do
- local msg
- local fmsg
- local ctx
- local result
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- if ! fmsg=$(ipc_msg_get_data "$msg"); then
- log_warn "Dropping invalid message (no data)"
- continue
- fi
-
- if ! ctx=$(foundry_msg_merge_get_context "$fmsg"); then
- log_warn "Dropping invalid message (no context)"
- continue
- fi
-
- if [[ "$ctx" != "$context" ]]; then
- # message is about a different merge request
- continue
- fi
-
- if ! result=$(foundry_msg_merge_get_status "$fmsg"); then
- log_warn "Could not get result from merge message"
- return 1
- fi
-
- foundry_context_get_logs "$context" | log_highlight "Merge logs"
- return "$result"
-
- done
-
- return 1
-}
-
-test_mergebot() {
- local repository="$1"
- local srcbranch="$2"
- local dstbranch="$3"
-
- local endpoint
- local mergereq
- local project
-
- project="${repository##*/}"
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open IPC endpoint"
- return 1
- fi
-
- if ! ipc_endpoint_subscribe "$endpoint" "merges"; then
- log_error "Could not subscribe to merges"
- return 1
- fi
-
- if ! context=$(foundry_context_new "$project"); then
- log_error "Could not make a new foundry context"
- return 1
- fi
-
- if ! mergereq=$(foundry_msg_mergerequest_new "$context" \
- "$repository" \
- "$srcbranch" \
- "$dstbranch"); then
- log_error "Could not make mergerequest message"
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/mergebot" "$mergereq"; then
- log_error "Could not send mergerequest"
- return 1
- fi
-
- if ! check_result "$endpoint" "$context"; then
- return 1
- fi
-
- return 0
-}
-
-main() {
- local repository
- local srcbranch
- local dstbranch
-
- opt_add_arg "r" "repository" "rv" "" "Merge branches in this repository"
- opt_add_arg "s" "source" "rv" "" "The branch to merge from"
- opt_add_arg "d" "destination" "rv" "" "The branch to merge into"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- repository=$(opt_get "repository")
- srcbranch=$(opt_get "source")
- dstbranch=$(opt_get "destination")
-
- if ! test_mergebot "$repository" "$srcbranch" "$dstbranch"; then
- echo "Could not merge $repository [$srcbranch -> $dstbranch]"
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "foundry/msg" "foundry/context"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-merge() {
- local repository="$1"
- local sbranch="$2"
- local dbranch="$3"
-
- local clone
- local msg
- local err
-
- if ! clone=$(mktemp -d); then
- msg="Could not make temporary directory"
- log_error "$msg"
- echo "$msg"
- return 1
- fi
-
- err=1
-
- if ! git_clone "$repository" "$clone"; then
- msg="Could not clone $repository into $clone"
- log_error "$msg"
- echo "$msg"
-
- elif ! git_merge "$clone" "$sbranch" "$dbranch"; then
- msg="Could not merge $sbranch into $dbranch in $repository"
- log_error "$msg"
- echo "$msg"
-
- elif ! git_push "$clone" "$dbranch"; then
- msg="Could not push branch \"$dbranch\" of $repository to origin"
- log_error "$msg"
- echo "$msg"
-
- else
- err=0
- fi
-
- if ! rm -rf "$clone"; then
- msg="Could not clean up temporary directory $clone"
- log_warn "$msg"
- echo "$msg"
- fi
-
- return "$err"
-}
-
-handle_merge_request() {
- local endpoint="$1"
- local topic="$2"
- local mmsg="$3"
-
- local context
- local repository
- local sbranch
- local dbranch
- local mergelog
- local result
- local merge_msg
-
- if ! context=$(foundry_msg_mergerequest_get_context "$mmsg") ||
- ! repository=$(foundry_msg_mergerequest_get_repository "$mmsg") ||
- ! sbranch=$(foundry_msg_mergerequest_get_source_branch "$mmsg") ||
- ! dbranch=$(foundry_msg_mergerequest_get_destination_branch "$mmsg"); then
- log_warn "Dropping malformed message"
- log_highlight "message" <<< "$mmsg" | log_debug
- return 1
- fi
-
- result=0
-
- if ! mergelog=$(merge "$repository" "$sbranch" "$dbranch"); then
- result=1
- fi
-
- if ! foundry_context_log "$context" "merge" <<< "$mergelog"; then
- log_error "Could not log to context $context"
- fi
-
- if ! merge_msg=$(foundry_msg_merge_new "$context" \
- "$repository" \
- "$sbranch" \
- "$dbranch" \
- "$result"); then
- log_error "Could not create merge message"
- return 1
- fi
-
- if ! ipc_endpoint_publish "$endpoint" "$topic" "$merge_msg"; then
- return 1
- fi
-
- return 0
-}
-
-handle_message() {
- local endpoint="$1"
- local topic="$2"
- local message="$3"
-
- local mmsg
- local msgtype
-
- if ! mmsg=$(ipc_msg_get_data "$message"); then
- log_warn "Dropping malformed message"
- ipc_msg_dump "$message" | log_warn
- return 1
- fi
-
- if ! msgtype=$(foundry_msg_get_type "$mmsg"); then
- log_warn "Dropping message without type"
- return 1
- fi
-
- if [[ "$msgtype" != "mergerequest" ]]; then
- log_warn "Dropping message with unexpected type $msgtype"
- return 1
- fi
-
- if ! handle_merge_request "$endpoint" "$topic" "$mmsg"; then
- return 1
- fi
-
- return 0
-}
-
-_mergebot_run() {
- local endpoint_name="$1"
- local topic="$2"
-
- local endpoint
-
- if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then
- log_error "Could not open endpoint $endpoint_name"
- return 1
- fi
-
- while inst_running; do
- local msg
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- handle_message "$endpoint" "$topic" "$msg"
- done
-
- return 0
-}
-
-main() {
- local endpoint
- local topic
-
- opt_add_arg "e" "endpoint" "v" "pub/mergebot" "The endpoint to receive messages on"
- opt_add_arg "t" "topic" "v" "merges" "The topic to publish merge messages on"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- endpoint=$(opt_get "endpoint")
- topic=$(opt_get "topic")
-
- if ! inst_start _mergebot_run "$endpoint" "$topic"; then
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "git" "inst" "ipc" "foundry/msg"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-publish_test_result() {
- local context="$1"
- local repository="$2"
- local result="$3"
- local branch="$4"
-
- local message
- local endpoint
- local err
-
- err=0
-
- if ! message=$(foundry_msg_test_new "$context" \
- "$repository" \
- "$branch" \
- "$result"); then
- log_error "Could not create test message"
- return 1
- fi
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open IPC endpoint"
- return 1
- fi
-
- if ! ipc_endpoint_publish "$endpoint" "tests" "$message"; then
- log_error "Could not publish test result"
- err=1
- fi
-
- if ! ipc_endpoint_close "$endpoint"; then
- log_error "Could not close IPC endpoint"
- fi
-
- return "$err"
-}
-
-main() {
- local context
- local repository
- local result
- local branch
-
- opt_add_arg "c" "context" "rv" "" "The context the test was performed in"
- opt_add_arg "r" "repository" "rv" "" "The repository that was tested"
- opt_add_arg "e" "result" "rv" 0 "The result of the test"
- opt_add_arg "b" "branch" "rv" "" "The branch that was tested"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- context=$(opt_get "context")
- repository=$(opt_get "repository")
- result=$(opt_get "result")
- branch=$(opt_get "branch")
-
- if ! publish_test_result "$context" "$repository" "$result" "$branch"; then
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "foundry/msg"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-check_result() {
- local endpoint="$1"
- local context="$2"
-
- local start
- local timelimit
-
- start=$(date +"%s")
- timelimit=$((5 * 60))
-
- while (( ( $(date +"%s") - start ) < timelimit )); do
- local msg
- local fmsg
- local ctx
- local artifact
- local ret
-
- ret=0
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- if ! fmsg=$(ipc_msg_get_data "$msg"); then
- log_warn "Dropping invalid message (not a foundry message)"
- continue
- fi
-
- if ! ctx=$(foundry_msg_sign_get_context "$fmsg"); then
- log_warn "Dropping invalid message (no context in message)"
- continue
- fi
-
- if [[ "$ctx" != "$context" ]]; then
- # Unrelated
- continue
- fi
-
- while read -r artifact; do
- if [[ "$artifact" != *".deb" ]]; then
- continue
- fi
-
- if ! dpkg-sig --verify "$artifact"; then
- log_error "Invalid signature on $artifact"
- ret=1
- fi
- done < <(foundry_context_get_files "$context")
-
- return "$ret"
- done
-
- return 1
-}
-
-test_signbot() {
- local project="$1"
- local artifacts=("${@:2}")
-
- local signreq
- local endpoint
- local context
- local artifact
-
- if ! context=$(foundry_context_new "$project"); then
- log_error "Could not create context for $project"
- return 1
- fi
-
- log_info "Created context $context"
-
- for artifact in "${artifacts[@]}"; do
- log_info "Adding $artifact to context $context"
- if ! foundry_context_add_file "$context" "build" "$artifact"; then
- log_error "Could not add $artifact to context $context"
- return 1
- fi
- done
-
- if ! signreq=$(foundry_msg_signrequest_new "$context"); then
- log_error "Could not make sign request"
- return 1
- fi
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open IPC endpoint"
- return 1
- fi
-
- if ! ipc_endpoint_subscribe "$endpoint" "signs"; then
- log_error "Could not subscribe to topic \"signs\""
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/signbot" "$signreq"; then
- log_error "Could not send sign request to pub/signbot"
- return 1
- fi
-
- if ! check_result "$endpoint" "$context"; then
- return 1
- fi
-
- return 0
-}
-
-_add_artifact() {
- local name="$1"
- local value="$2"
-
- if [[ "$name" == "artifact" ]]; then
- artifacts+=("$value")
- fi
-
- return 0
-}
-
-main() {
- local artifacts
- local project
-
- artifacts=()
-
- opt_add_arg "a" "artifact" "rv" "" "An artifact to be signed" "" _add_artifact
- opt_add_arg "p" "project" "rv" "" "Artifact project name"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- project=$(opt_get "project")
-
- log_info "Signing project $project"
- array_to_lines "${artifacts[@]}" | log_highlight "artifacts" | log_info
-
- if ! test_signbot "$project" "${artifacts[@]}"; then
- echo "Signing failed"
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "array" "foundry/msg" "foundry/context"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-check_result() {
- local endpoint="$1"
- local context="$2"
-
- local start
- local timelimit
-
- start=$(date +"%s")
- timelimit=$((5 * 60))
-
- while (( ( $(date +"%s") - start ) < timelimit )); do
- local msg
- local fmsg
- local ctx
- local result
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- if ! fmsg=$(ipc_msg_get_data "$msg"); then
- log_warn "Dropping invalid message (not a foundry message)"
- continue
- fi
-
- if ! ctx=$(foundry_msg_test_get_context "$fmsg"); then
- log_warn "Dropping invalid message (no context in message)"
- continue
- fi
-
- if [[ "$ctx" != "$context" ]]; then
- # Unrelated message
- continue
- fi
-
- if ! result=$(foundry_msg_test_get_result "$fmsg"); then
- log_warn "Could not get result from test message"
- return 1
- fi
-
- foundry_context_get_logs "$context" | log_highlight "Test logs"
-
- return "$result"
- done
-
- return 1
-}
-
-test_testbot() {
- local repository="$1"
- local branch="$2"
-
- local endpoint
- local testreq
- local context
- local package
-
- package="${repository##*/}"
-
- if ! endpoint=$(ipc_endpoint_open); then
- log_error "Could not open IPC endpoint"
- return 1
- fi
-
- if ! ipc_endpoint_subscribe "$endpoint" "tests"; then
- log_error "Could not subscribe to tests"
- return 1
- fi
-
- if ! context=$(foundry_context_new "$package"); then
- log_error "Could not make a new foundry context"
- return 1
- fi
-
- if ! testreq=$(foundry_msg_testrequest_new "$context" \
- "$repository" \
- "$branch"); then
- log_error "Could not make testrequest message"
- return 1
- fi
-
- if ! ipc_endpoint_send "$endpoint" "pub/testbot" "$testreq"; then
- log_error "Could not send testrequest"
- return 1
- fi
-
- if ! check_result "$endpoint" "$context"; then
- return 1
- fi
-
- return 0
-}
-
-main() {
- local repository
- local branch
-
- opt_add_arg "r" "repository" "rv" "" "The repository to be tested"
- opt_add_arg "b" "branch" "rv" "" "The branch to be tested"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- repository=$(opt_get "repository")
- branch=$(opt_get "branch")
-
- if ! test_testbot "$repository" "$branch"; then
- echo "Test failed"
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "foundry/msg" "foundry/context"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}
+++ /dev/null
-#!/bin/bash
-
-test_source_tree() {
- local repository="$1"
- local branch="$2"
-
- local destination
- local err
-
- if ! destination=$(mktemp -d); then
- echo "Could not create temporary directory"
- log_error "Could not create temporary directory"
- return 1
- fi
-
- if ! git clone "$repository" -b "$branch" --single-branch "$destination"; then
- local errmsg
-
- errmsg="Could not check out $repository#$branch to $destination"
- log_error "$errmsg"
- echo "$errmsg"
-
- if ! rm -rf "$destination"; then
- log_warn "Could not remove $destination"
- echo "Could not remove $destination"
- fi
-
- return 1
- fi
-
- err=0
-
- if ! ( cd "$destination" && make test ) 2>&1; then
- err=1
- fi
-
- if ! rm -rf "$destination"; then
- log_warn "Could not remove temporary directory $destination"
- echo "Could not remove temporary directory $destination"
- fi
-
- return "$err"
-}
-
-publish_result() {
- local endpoint="$1"
- local topic="$2"
- local context="$3"
- local repository="$4"
- local branch="$5"
- local result="$6"
-
- local testmsg
-
- if ! testmsg=$(foundry_msg_test_new "$context" \
- "$repository" \
- "$branch" \
- "$result"); then
- log_error "Could not make test message"
- return 1
- fi
-
- if ! ipc_endpoint_publish "$endpoint" "$topic" "$testmsg"; then
- log_error "Could not publish test message on $topic"
- return 1
- fi
-
- return 0
-}
-
-handle_test_request() {
- local endpoint="$1"
- local request="$2"
- local topic="$3"
-
- local context
- local repository
- local branch
- local result
- local testlog
-
- if ! context=$(foundry_msg_testrequest_get_context "$request"); then
- log_warn "Could not get context from message. Dropping."
- return 1
-
- elif ! repository=$(foundry_msg_testrequest_get_repository "$request"); then
- log_warn "Could not get repository from message. Dropping."
- return 1
-
- elif ! branch=$(foundry_msg_testrequest_get_branch "$request"); then
- log_warn "Could not get branch from message. Dropping."
- return 1
- fi
-
- result=0
- if ! testlog=$(mktemp --suffix="-test.log"); then
- log_error "Could not create logfile"
- return 1
- fi
-
- if ! test_source_tree "$repository" "$branch" &> "$testlog"; then
- result=1
- fi
-
- if ! foundry_context_add_log "$context" "test" "$testlog"; then
- log_error "Could not add logfile to context $context"
-
- if ! rm -f "$testlog"; then
- log_warn "Could not remove $testlog"
- fi
-
- return 1
- fi
-
- if ! publish_result "$endpoint" "$topic" "$context" \
- "$repository" "$branch" "$result"; then
- log_warn "Could not publish test result"
- return 1
- fi
-
- return 0
-}
-
-_testbot_run() {
- local endpoint_name="$1"
- local topic="$2"
-
- local endpoint
-
- if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then
- log_error "Could not listen on $endpoint_name"
- return 1
- fi
-
- while inst_running; do
- local msg
- local fmsg
- local ftype
-
- if ! msg=$(ipc_endpoint_recv "$endpoint" 5); then
- continue
- fi
-
- if ! fmsg=$(ipc_msg_get_data "$msg") ||
- ! ftype=$(foundry_msg_get_type "$fmsg") ||
- [[ "$ftype" != "testrequest" ]]; then
- log_warn "Dropping invalid message"
- continue
- fi
-
- handle_test_request "$endpoint" "$fmsg" "$topic"
- done
-
- return 0
-}
-
-main() {
- local endpoint_name
- local topic
-
- opt_add_arg "n" "name" "rv" "" \
- "The name of this instance"
- opt_add_arg "e" "endpoint" "v" "pub/testbot" \
- "The endpoint to use for IPC messaging"
- opt_add_arg "t" "topic" "v" "tests" \
- "The topic under which to publish notifications"
-
- if ! opt_parse "$@"; then
- return 1
- fi
-
- endpoint_name=$(opt_get "endpoint")
- topic=$(opt_get "topic")
-
- if ! inst_start _testbot_run "$endpoint_name" "$topic"; then
- return 1
- fi
-
- return 0
-}
-
-{
- if ! . toolbox.sh; then
- exit 1
- fi
-
- if ! include "log" "opt" "ipc" "inst" "foundry/msg" "foundry/context"; then
- exit 1
- fi
-
- main "$@"
- exit "$?"
-}