aboutsummaryrefslogtreecommitdiff
path: root/brep/submit/submit.bash.in
blob: d6f025843b5e0eb0ea98f63b9d8b8a15dbaccccc (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
109
110
111
112
113
114
# file      : brep/submit/submit.bash.in
# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file

# @@ Should we make this module besides defining utility functions to also
#    parse and remove from args the common arguments (directory)?
#

if [ "$brep_submit" ]; then
  return 0
else
  brep_submit=true
fi

@import libbutl/manifest-serializer@

# Diagnostics.
#
self="$(basename $0)"

# Result reference (assigned later).
#
reference=

# Normally the brep module's log record looks like this:
#
# [Mon Jul 23 17:48:46.945079 2018] [brep:error] [pid 123:tid 456] [brep::submit::init]: error description
#
# We will use the (almost) same format for our diagnostics (redirected to the
# Apache's error_log) so it can easily be attributed to the brep module.
#
function info () # <severity> <text>
{
  local severity="$1"
  shift

  # Note: %N is Linux-specific.
  #
  local ts
  if ! ts="$(date +"%a %b %d %H:%M:%S.%6N %Y")"; then
    ts=
  fi

  echo "[$ts] [brep:$severity] [ref $reference] [$self]: $*" 1>&2;
}

function error () { info "error" "$*"; exit 1; }
function trace () { if [ "$verbose" ]; then info "info" "$*"; fi }

# Submission data directory.
#
# @@ Doesn't comply with implementation assuming that directory comes before
#    submit-handler-arguments. Can we change the doc?
#
dir="${1%/}" # @@ Call it directory?
shift

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

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

reference="$(basename $dir)"

# Serialize one manifest name/value pair.
#
function serialize () # <name> <value>
{
  printf "%s:%s\0" "$1" "$2" >&"$butl_manifest_serializer_ifd"
}

# Serialize the submission result manifest to stdout.
#
function result_manifest () # <status> <message> [<reference>]
{
  local sts="$1"
  local msg="$2"
  local ref="$3"

  trace "serializing result manifest"
  butl_manifest_serializer_start

  serialize ""        "1"    # Start of manifest.
  serialize "status"  "$sts"
  serialize "message" "$msg"

  if [ -n "$ref" ]; then
    serialize "reference" "$ref"
  fi

  butl_manifest_serializer_finish
}

# Verify the archive is a valid bpkg package and extract its manifest file.
#
function extract_package_manifest () # <archive> <manifest>
{
  local arc="$1"
  local man="$2"

  # Should we remove the submission directory with an invalid package?
  # Probably it's better to leave it for potential investigation. Note that we
  # can always grep for such directories based on the result.manifest file
  # they contain.
  #
  if ! bpkg pkg-verify --manifest "$arc" >"$man" 2>/dev/null; then
    trace "$arc is not a valid package"
    result_manifest 400 "archive is not a valid package (run bpkg pkg-verify for details)"
    exit 0
  fi
}