-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4app
executable file
·188 lines (168 loc) · 5.04 KB
/
p4app
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env bash
# Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
P4APP_IMAGE=${P4APP_IMAGE:-dingdamu/p4app-ddos:nwhhd}
P4APP_CONTAINER_ARGS=${P4APP_CONTAINER_ARGS:-""}
myrealpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
P4APP_LOGDIR=$(myrealpath "${P4APP_LOGDIR:-/tmp/p4app_logs}")
function get_abs_filename() {
# Convert a possibly-relative path to an absolute path.
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}
function normalize_path() {
# Remove any trailing slash from the provided path.
echo ${1%/}
}
function build-command {
if [ -z $2 ]; then
OUTPUT_FILE="out.json"
else
OUTPUT_FILE="$2"
fi
run-command $1 --build-only "${@:3}"
rc=$?
[ $rc -eq 0 ] && cp "$P4APP_LOGDIR/program.json" $OUTPUT_FILE
exit $rc
}
function run-p4app {
APP_TO_RUN=/tmp/app.tar.gz
P4APP_NAME='hh'
docker run --privileged --interactive -i --tty --rm \
--name "$P4APP_NAME" \
-v $1:$APP_TO_RUN \
-e DISPLAY=$DISPLAY \
-v "$P4APP_LOGDIR":/tmp/p4app_logs \
$P4APP_CONTAINER_ARGS \
$P4APP_IMAGE $APP_TO_RUN "${@:2}"
}
function run-command {
# Run the .p4app package provided by the user.
if [ -d "$1" ]; then
# The user passed the package as a directory. Tar it up and pass it to the
# container.
PACKAGE_DIR=$(normalize_path "$1")
APP_FILE=$(mktemp /tmp/p4app.tar.gz.XXXXXX)
tar -czf "$APP_FILE" -C "$PACKAGE_DIR" .
run-p4app "$APP_FILE" "${@:2}"
rc=$?
rm "$APP_FILE"
elif [ -f "$1" ]; then
# The user passed the package as a file. We'll assume it's already a .tar.gz
# archive; just pass it to the container as-is.
APP_FILE=$(get_abs_filename "$1")
run-p4app "$APP_FILE" "${@:2}"
rc=$?
else
echo "Couldn't read p4app package: $1"
exit 1
fi
return $rc
}
function pack-command {
# Compress the provided .p4app package.
if [ -d "$1" ]; then
# The user passed the package as a directory. Tar it up.
PACKAGE_DIR=$(normalize_path "$1")
APP_FILE=$(mktemp /tmp/p4app.tar.gz.XXXXXX)
tar -czf "$APP_FILE" -C "$PACKAGE_DIR" . &&
rm -rf "$PACKAGE_DIR"
mv "$APP_FILE" "$PACKAGE_DIR"
elif [ -f "$1" ]; then
# The user passed the package as a file. We'll assume it's already a .tar.gz
# archive and leave it alone so that this command is idempotent.
echo "Package is already packed: $1"
else
echo "Couldn't read p4app package: $1"
exit 1
fi
}
function unpack-command {
# Decompress the provided .p4app package.
if [ -d "$1" ]; then
# The user passed the package as a directory. We'll leave it alone so that
# this command is idempotent.
echo "Package is already unpacked: $1"
elif [ -f "$1" ]; then
# The user passed the package as a file. Extract it.
APP_FILE=$(mktemp /tmp/p4app.tar.gz.XXXXXX)
mv "$1" "$APP_FILE" &&
mkdir -p "$1" &&
tar -xzf "$APP_FILE" -C "$1" &&
rm "$APP_FILE"
else
echo "Couldn't read p4app package: $1"
exit 1
fi
}
function update-command {
docker pull $P4APP_IMAGE
}
function exec-command {
#container_id=${P4APP_NAME:-$(docker ps | grep -m1 p4app | awk '{print $1}')}
container_id='hh'
if [ -z "$container_id" ]; then
(>&2 echo "Couldn't find any p4app currently running.")
exit 1
fi
if [ -t 1 ]; then
# Called from a terminal, be interactive
docker exec -it $container_id ${@:1}
else
# Inside a pipe, don't allocate a ptty
docker exec $container_id ${@:1}
fi
}
function usage-command {
echo "Usage:"
echo " p4app run <program.p4app>"
echo " Run a p4app."
echo " p4app run <program.p4app> <target>"
echo " Run a p4app, specifying a target."
echo " p4app build <program.p4app> <out.json>"
echo " Build a p4app, specifying the output JSON filename."
echo " p4app pack <program.p4app>"
echo " Compress a p4app directory into a single file, in-place."
echo " p4app unpack <program.p4app>"
echo " Expand a p4app file into a directory, in-place."
echo " p4app update"
echo " Update the toolchain to the newest version."
echo " p4app exec <command>"
echo " Execute a command on the most recent p4app instance."
exit 1
}
case "$1" in
"run")
run-command "${@:2}"
;;
"build")
build-command "${@:2}"
;;
"pack")
pack-command "${@:2}"
;;
"unpack")
unpack-command "${@:2}"
;;
"update")
update-command "${@:2}"
;;
"exec")
exec-command "${@:2}"
;;
*)
usage-command
;;
esac