aboutsummaryrefslogtreecommitdiff
path: root/brep/handler/submit/submit-dir.in
blob: 1f677e458265c4fe9bd53e83f916f35bdf69b563 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash

# file      : brep/handler/submit/submit-dir.in
# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file

# Simple package submission handler with directory storage.
#
# Validate the package archive located in the specified submission directory
# extracting and parsing the package manifest (saved as package.manifest in
# the submission directory). Keep the submission directory unless simulating.
# Write the submission result manifest to stdout.
#
usage="usage: $0 <dir>"

verbose= #true

trap "{ exit 1; }" ERR
set -o errtrace # Trap ERR in functions.

@import brep/handler/handler@
@import brep/handler/submit/submit@

if [ "$#" != 1 ]; then
  error "$usage"
fi

# Submission data directory (last and the only argument).
#
data_dir="${!#/}"

if [ -z "$data_dir" ]; then
  error "$usage"
fi

if [ ! -d "$data_dir" ]; then
  error "'$data_dir' does not exist or is not a directory"
fi

reference="$(basename "$data_dir")"

# Parse the submission request manifest and obtain the archive path as well
# as the simulate value.
#
manifest_parser_start "$data_dir/request.manifest"

archive=
simulate=

while IFS=: read -ru "$manifest_parser_ofd" -d '' n v; do
  case "$n" in
    archive)  archive="$v"  ;;
    simulate) simulate="$v" ;;
  esac
done

manifest_parser_finish

if [ -z "$archive" ]; then
  error "archive manifest value expected"
fi

if [ -n "$simulate" -a "$simulate" != "success" ]; then
  exit_with_manifest 400 "unrecognized simulation outcome '$simulate'"
fi

m="$data_dir/package.manifest"
extract_package_manifest "$data_dir/$archive" "$m"

# Parse the package manifest and obtain the package name, version, and
# project.
#
manifest_parser_start "$m"

name=
version=
project=

while IFS=: read -ru "$manifest_parser_ofd" -d '' n v; do
  case "$n" in
    name)    name="$v"    ;;
    version) version="$v" ;;
    project) project="$v" ;;
  esac
done

manifest_parser_finish

if [ -z "$name" ]; then
  error "name manifest value expected"
fi

if [ -z "$version" ]; then
  error "version manifest value expected"
fi

if [ -z "$project" ]; then
  project="$name"
fi

if [ -n "$simulate" ]; then
  run rm -r "$data_dir"
  trace "package submission is simulated: $name/$version"
else
  trace "package submission is queued: $name/$version"
fi

exit_with_manifest 200 "package submission is queued: $name/$version"