balena-cloud-apps 1.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/.circleci/build.sh +20 -0
  2. package/.circleci/config.yml +40 -0
  3. package/.gitattributes +2 -0
  4. package/LICENSE +201 -0
  5. package/README.md +79 -0
  6. package/README.md~ +27 -0
  7. package/aarch64.env +4 -0
  8. package/armhf.env +4 -0
  9. package/common.env +2 -0
  10. package/docker-compose.aarch64 +0 -0
  11. package/docker-compose.yml +0 -0
  12. package/index.js +13 -0
  13. package/package-lock.json.moved +372 -0
  14. package/package.json +40 -0
  15. package/test/build/aarch64.env +4 -0
  16. package/test/build/armhf.env +4 -0
  17. package/test/build/common.env +2 -0
  18. package/test/build/deployment/images/dind-php7/Dockerfile.aarch64 +62 -0
  19. package/test/build/deployment/images/dind-php7/Dockerfile.armhf +62 -0
  20. package/test/build/deployment/images/dind-php7/Dockerfile.template +62 -0
  21. package/test/build/deployment/images/dind-php7/Dockerfile.x86_64 +62 -0
  22. package/test/build/deployment/images/dind-php7/README.md +2 -0
  23. package/test/build/docker-compose.aarch64 +7 -0
  24. package/test/build/docker-compose.armhf +7 -0
  25. package/test/build/docker-compose.x86_64 +7 -0
  26. package/test/build/docker-compose.yml +7 -0
  27. package/test/build/submodule/Dockerfile.aarch64 +11 -0
  28. package/test/build/submodule/Dockerfile.armhf +11 -0
  29. package/test/build/submodule/Dockerfile.template +11 -0
  30. package/test/build/submodule/Dockerfile.x86_64 +11 -0
  31. package/test/build/x86_64.env +4 -0
  32. package/test/build-test.sh +64 -0
  33. package/vendor/autoload.php +7 -0
  34. package/vendor/cni/auto_reboot.sh +98 -0
  35. package/vendor/cni/balena_deploy.sh +303 -0
  36. package/vendor/cni/docker_build.sh +63 -0
  37. package/vendor/cni/init_functions.sh +67 -0
  38. package/vendor/cni/post_install.sh +12 -0
  39. package/x86_64.env +4 -0
@@ -0,0 +1,7 @@
1
+ version: '2'
2
+ services:
3
+ testsvc:
4
+ docker:
5
+ context: submodule
6
+ dockerfile: Dockerfile.aarch64
7
+ image: betothreeprod/generic-aarch64
@@ -0,0 +1,7 @@
1
+ version: '2'
2
+ services:
3
+ testsvc:
4
+ docker:
5
+ context: submodule
6
+ dockerfile: Dockerfile.armhf
7
+ image: betothreeprod/raspberrypi3
@@ -0,0 +1,7 @@
1
+ version: '2'
2
+ services:
3
+ testsvc:
4
+ docker:
5
+ context: submodule
6
+ dockerfile: Dockerfile.x86_64
7
+ image: betothreeprod/intel-nuc
@@ -0,0 +1,7 @@
1
+ version: '2'
2
+ services:
3
+ testsvc:
4
+ docker:
5
+ context: submodule
6
+ dockerfile: Dockerfile.template
7
+ image: betothreeprod/%%BALENA_MACHINE_NAME%%
@@ -0,0 +1,11 @@
1
+ ARG BALENA_MACHINE_NAME
2
+ FROM balenalib/${BALENA_MACHINE_NAME:-generic-aarch64}:latest
3
+ ARG DKR_ARCH
4
+ ENV DKR_ARCH ${DKR_ARCH:-aarch64}
5
+ RUN ["cross-build-start"]
6
+
7
+ RUN printf "* %s VM, %s IMAGE" "$(arch)" "$DKR_ARCH"
8
+
9
+ RUN ["cross-build-end"]
10
+
11
+ CMD true
@@ -0,0 +1,11 @@
1
+ ARG BALENA_MACHINE_NAME
2
+ FROM balenalib/${BALENA_MACHINE_NAME:-raspberrypi3}:latest
3
+ ARG DKR_ARCH
4
+ ENV DKR_ARCH ${DKR_ARCH:-armhf}
5
+ RUN ["cross-build-start"]
6
+
7
+ RUN printf "* %s VM, %s IMAGE" "$(arch)" "$DKR_ARCH"
8
+
9
+ RUN ["cross-build-end"]
10
+
11
+ CMD true
@@ -0,0 +1,11 @@
1
+ ARG BALENA_MACHINE_NAME
2
+ FROM balenalib/${BALENA_MACHINE_NAME:-raspberrypi3}:latest
3
+ ARG DKR_ARCH
4
+ ENV DKR_ARCH ${DKR_ARCH:-armhf}
5
+ # RUN ["cross-build-start"]
6
+
7
+ RUN printf "* %s VM, %s IMAGE" "$(arch)" "$DKR_ARCH"
8
+
9
+ # RUN ["cross-build-end"]
10
+
11
+ CMD true
@@ -0,0 +1,11 @@
1
+ ARG BALENA_MACHINE_NAME
2
+ FROM balenalib/${BALENA_MACHINE_NAME:-intel-nuc}:latest
3
+ ARG DKR_ARCH
4
+ ENV DKR_ARCH ${DKR_ARCH:-x86_64}
5
+ # RUN ["cross-build-start"]
6
+
7
+ RUN printf "* %s VM, %s IMAGE" "$(arch)" "$DKR_ARCH"
8
+
9
+ # RUN ["cross-build-end"]
10
+
11
+ CMD true
@@ -0,0 +1,4 @@
1
+ DKR_ARCH=x86_64
2
+ BALENA_MACHINE_NAME=intel-nuc
3
+ IMG_TAG=latest
4
+ PRIMARY_HUB=betothreeprod\\/intel-nuc
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env bash
2
+ vendord="$(cd "$(dirname "${BASH_SOURCE[0]}")/../vendor/cni" && pwd)"
3
+ chkSet="x${DEBIAN_FRONTEND:-}"
4
+ DEBIAN_FRONTEND='noninteractive'
5
+ testd="$(cd "$(dirname "${BASH_SOURCE[0]}")/build" && pwd)"
6
+ # shellcheck disable=SC1090
7
+ . "$vendord/init_functions.sh" "$testd"
8
+ LOG=$(new_log "/tmp")
9
+ [ "$DEBUG" ] && LOG=$(new_log "/dev" "/stderr")
10
+ function test_deploy() {
11
+ # x86_64
12
+ args=("3" --nobuild --exit)
13
+ # shellcheck disable=SC1090
14
+ . "$vendord/balena_deploy.sh" "$testd" "${args[@]}" >> "$LOG"
15
+ grep -q "intel-nuc" < "$testd/submodule/Dockerfile.x86_64"
16
+ }
17
+ function test_deploy_2() {
18
+ # aarch64
19
+ args=("2" --nobuild --exit)
20
+ # shellcheck disable=SC1090
21
+ . "$vendord/balena_deploy.sh" "$testd" "${args[@]}" >> "$LOG"
22
+ grep -q "generic-aarch64" < "$testd/submodule/Dockerfile.aarch64"
23
+ }
24
+ function test_deploy_3() {
25
+ # armhf
26
+ args=("1" --nobuild --exit)
27
+ # shellcheck disable=SC1090
28
+ . "$vendord/balena_deploy.sh" "$testd" "${args[@]}" >> "$LOG"
29
+ grep -q "raspberrypi3" < "$testd/submodule/Dockerfile.armhf"
30
+ }
31
+ function test_docker_3() {
32
+ args=("${testd}/submodule" -m . "betothreeprod/raspberrypi3" "$DKR_ARCH")
33
+ # shellcheck disable=SC1090
34
+ . "$vendord/docker_build.sh" "${args[@]}" >> "$LOG"
35
+ docker image ls -q "${args[3]}*"
36
+ }
37
+ function test_docker() {
38
+ args=("${testd}/deployment/images/dind-php7" -m . "betothreeprod/dind-php7" "$DKR_ARCH")
39
+ # shellcheck disable=SC1090
40
+ . "$vendord/docker_build.sh" "${args[@]}" >> "$LOG"
41
+ docker image ls -q "${args[3]}*"
42
+ }
43
+ test_deploy
44
+ results=("$?")
45
+ test_docker
46
+ results+=("$?")
47
+ test_deploy_2
48
+ results+=("$?")
49
+ test_deploy_3
50
+ results+=("$?")
51
+ test_docker_3
52
+ results+=("$?")
53
+ [ "$chkSet" = 'x' ] && unset DEBIAN_FRONTEND || DEBIAN_FRONTEND=${chkSet:2}
54
+ check_log "$LOG"
55
+
56
+ for r in "${!results[@]}"; do
57
+ if [ "${results[$r]}" -gt 0 ]; then
58
+ cat "$LOG"
59
+ log_failure_msg "test n°$r FAIL"
60
+ exit "${results[$r]}"
61
+ else
62
+ log_success_msg "test n°$r PASS"
63
+ fi
64
+ done
@@ -0,0 +1,7 @@
1
+ <?php
2
+
3
+ // autoload.php @generated by Composer
4
+
5
+ require_once __DIR__ . '/composer/autoload_real.php';
6
+
7
+ return ComposerAutoloaderInit4862b1ef3ab5a2a3d33cfe6bf6fb3bda::getLoader();
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env bash
2
+
3
+ ### BEGIN INIT INFO
4
+ # Provides: Auto_Reboot
5
+ # Required-Start:
6
+ # Required-Stop:
7
+ # Should-Start:
8
+ # Default-Start: 2 3 4 5
9
+ # Default-Stop:
10
+ # Short-Description: Auto Reboot Service
11
+ # Description: Check if the system dpkg need a restart and send corresponding signal.
12
+ ### END INIT INFO
13
+
14
+ set -eu
15
+ if [ ! -f /etc/os-releases ] \
16
+ || [ "$(grep -q "ID=" < /etc/os-releases)" != "debian" ] \
17
+ && [ "$(grep -q "ID=" < /etc/os-releases)" != "ubuntu" ]; then
18
+ echo -e "This script only made for Debian and Ubuntu linux"
19
+ exit 3
20
+ fi
21
+
22
+ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
23
+ SVC_NAME=auto_reboot
24
+ DAEMON_CF="/etc/$SVC_NAME/$SVC_NAME.conf"
25
+ DAEMON_SVC="/usr/lib/systemd/system/$SVC_NAME.service.d/"
26
+ # shellcheck disable=SC1091
27
+ . /lib/lsb/init-functions
28
+
29
+ [ -f $DAEMON_CF ] || mkdir -p "$(dirname $DAEMON_CF)"
30
+ [ -f $DAEMON_CF ] || touch "$DAEMON_CF"
31
+ [ -d $DAEMON_SVC ] || mkdir -p "$DAEMON_SVC"
32
+
33
+ banner=("" "[services.d] $DAEMON_SVC" ""); log_daemon_msg "%s\n" "${banner[@]}"
34
+
35
+ DAEMON_CF="$DAEMON_CF $(find "$DAEMON_SVC" -name "*.conf")"
36
+ for cnf in $DAEMON_CF; do
37
+ # shellcheck disable=SC1090
38
+ . "$cnf"
39
+ done
40
+ [ -z "${scriptsd:-}" ] && scriptsd="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
41
+ usage=("" \
42
+ "Usage: $0 {start|stop|status|restart|install|uninstall}" \
43
+ "" \
44
+ "Definition: Silently reboot when the system claims:" \
45
+ " *** System restart required ***" \
46
+ "")
47
+ function install() {
48
+ cp -f "${scriptsd}/$SVC_NAME" "/etc/init.d/$SVC_NAME"
49
+ chmod +x "/etc/init.d/$SVC_NAME"
50
+ systemctl enable "$SVC_NAME"
51
+ }
52
+ function uninstall() {
53
+ systemctl disable "$SVC_NAME"
54
+ rm -f "/etc/init.d/$SVC_NAME" "$DAEMON_SVC"
55
+ }
56
+ function start() {
57
+ bash -c "while [ ! -f /run/reboot-required ]; do sleep 30; done; cat /run/reboot-required.dpkgs 2> /dev/null; reboot" &
58
+ echo $! > "/run/$SVC_NAME.pid"
59
+ }
60
+ function stop() {
61
+ kill "$(cat "/run/$SVC_NAME.pid")"
62
+ rm "/run/$SVC_NAME.pid"
63
+ }
64
+ function status() {
65
+ if [ -e "/run/$SVC_NAME.pid" ]; then
66
+ echo "$SVC_NAME is running, pid=$(cat "/run/$SVC_NAME.pid")"
67
+ else
68
+ echo "$SVC_NAME is NOT running"
69
+ exit 1
70
+ fi
71
+ }
72
+ case "$1" in
73
+ start)
74
+ start
75
+ ;;
76
+ stop)
77
+ stop
78
+ ;;
79
+ restart)
80
+ stop
81
+ start
82
+ ;;
83
+ status)
84
+ # code to check status of app comes here
85
+ # example: status program_name
86
+ status
87
+ ;;
88
+ install)
89
+ install
90
+ ;;
91
+ uninstall)
92
+ uninstall
93
+ ;;
94
+ *)
95
+ printf "%s\n" "${usage[@]}"
96
+ ;;
97
+ esac
98
+ exit 0
@@ -0,0 +1,303 @@
1
+ #!/usr/bin/env bash
2
+ set -u
3
+ [ "$#" -eq 0 ] && echo "usage $0 <project_root|\${BASH_SOURCE[0]}> <args>" && exit 0
4
+ [ -f "$1" ] && set -- "$(cd "$(dirname "$1")" && pwd)" "${@:2}"
5
+ project_root="$(cd "$1" && pwd)"; shift
6
+ banner=("" "[$0] BASH ${BASH_SOURCE[0]}" "$project_root" ""); printf "%s\n" "${banner[@]}"
7
+
8
+ # shellcheck source=init_functions.sh
9
+ . "$(command -v init_functions)" "${BASH_SOURCE[0]}"
10
+ [ "${DEBUG:-0}" != 0 ] && log_daemon_msg "passed args $*"
11
+
12
+ LOG=${LOG:-"$(new_log "." "$(basename "$project_root").log")"}
13
+ usage=("" \
14
+ "Usage ${BASH_SOURCE[0]} [1|2|3|<arch>] [1,--local|2,--balena|3,--nobuild|4,--docker|5,--push] [0,--exit]" \
15
+ " 1|arm32*|armv7l|armhf ARMv7 OS" \
16
+ " 2|arm64*|aarch64 ARMv8 OS" \
17
+ " 3|amd64|x86_64 All X86 64 bits OS (Mac or PC)" \
18
+ "" \
19
+ " 1,--local Pushing to local network Balena machine" \
20
+ " and continuous build, issue command" \
21
+ " balena login to authenticate." \
22
+ " 2,--balena Pushing to Balena Cloud Servers and" \
23
+ " continuous build, issue command" \
24
+ " balena login to authenticate." \
25
+ " 3,--nobuild Don't run any build process, format only" \
26
+ " the architecture templates. (prompt)" \
27
+ " 4,--docker Build a the docker images on localhost" \
28
+ " machine. Docker CE must be installed." \
29
+ " Balena Library enables ARM Cross-Build." \
30
+ " 5,--push Push latest changes to Github." \
31
+ " 6,--build-deps Deployment images dependencies build." \
32
+ " 0,--exit Quit script (non interactive)." \
33
+ "" \
34
+ "Deployment images Set BALENA_PROJECTS=(./dir_one ./dir_two ./dir_three)" \
35
+ " in common.env file." \
36
+ "Variable filters Set BALENA_PROJECTS_FLAGS=(VAR_ONE VAR_TWO)" \
37
+ " in common.env" \
38
+ "")
39
+ arch=${1:-''}
40
+ saved=("${@:2}")
41
+ while true; do
42
+ case $arch in
43
+ 1|arm32*|armv7l|armhf)
44
+ arch="armhf"
45
+ break;;
46
+ 2|arm64*|aarch64)
47
+ arch="aarch64"
48
+ break;;
49
+ 3|amd64|x86_64|i386)
50
+ arch="x86_64"
51
+ break;;
52
+ *)
53
+ printf "%s\n" "${usage[@]}"
54
+ if [ "${DEBIAN_FRONTEND:-}" = 'noninteractive' ]; then
55
+ arch=$(grep "DKR_ARCH" < "$project_root/.env"| cut -d= -f2)
56
+ else
57
+ read -rp "Set docker machine architecture ARM32, ARM64 bits or X86-64 (choose 1, 2 or 3) ? " arch
58
+ fi
59
+ ;;
60
+ esac
61
+ log_progress_msg "Architecture $arch was selected"
62
+ done
63
+ DKR_ARCH=${arch}
64
+ [ ! -f "$project_root/${DKR_ARCH}.env" ] && log_failure_msg "Missing arch file ${DKR_ARCH}.env" && exit 1
65
+ ln -vsf "$project_root/${DKR_ARCH}.env" "$project_root/.env" >> "$LOG"
66
+ # shellcheck disable=SC1090
67
+ . "$project_root/.env" && . "$project_root/common.env"
68
+ ### ADD ANY ENVIRONMENT VARIABLE TO BALENA_PROJECTS_FLAGS
69
+ flags=()
70
+ if [ -n "$BALENA_PROJECTS_FLAGS" ]; then
71
+ log_daemon_msg "Found ${#BALENA_PROJECTS_FLAGS[@]} flags set BALENA_PROJECTS_FLAGS" >> "$LOG"
72
+ flags=("${BALENA_PROJECTS_FLAGS[@]}")
73
+ fi
74
+ function setArch() {
75
+ while [ "$#" -gt 1 ]; do
76
+ cat /dev/null > "$1.sed"
77
+ sed=("s/%%BALENA_MACHINE_NAME%%/${BALENA_MACHINE_NAME}/g" \
78
+ "s/(Dockerfile\.)[^\.]*/\\1${DKR_ARCH}/g" \
79
+ "s/%%BALENA_ARCH%%/${DKR_ARCH}/g" \
80
+ "s/(DKR_ARCH[=:-]+)[^\$ }]+/\\1${DKR_ARCH}/g" )
81
+ printf "%s\n" "${sed[@]}" >> "$1.sed"
82
+ for flag in "${flags[@]}"; do
83
+ flag_val=$(eval "echo \${$flag}")
84
+ sed=("s#(${flag}[=:-]+)[^\$ }]+#\\1${flag_val}#g" \
85
+ "s#%%${flag}%%#${flag_val}#g" )
86
+ printf "%s\n" "${sed[@]}" >> "$1.sed"
87
+ done
88
+ sed -E -f "$1.sed" "$1" > "$2"
89
+ shift 2; done
90
+ }
91
+ ### ADD ANY SUBMODULE DOCKER IMAGE / SERVICE TO BALENA_PROJECTS
92
+ projects=(".")
93
+ if [ "${#BALENA_PROJECTS[@]}" -gt 0 ]; then
94
+ log_daemon_msg "Found ${#BALENA_PROJECTS[@]} projects set BALENA_PROJECTS" >> "$LOG"
95
+ projects=("${BALENA_PROJECTS[@]}")
96
+ fi
97
+ function deploy_deps() {
98
+ mapfile -t dock < <(find "${project_root}/deployment/images" -name "Dockerfile.${DKR_ARCH}")
99
+ for d in "${dock[@]}"; do
100
+ dir=$(dirname "$d")
101
+ docker_build "$dir" "." "$DOCKER_USER/$(basename "$dir")" "${DKR_ARCH}"
102
+ done
103
+ }
104
+ ### ADD HERE ### A MARKER STARTS... ### A MARKER ENDS
105
+ function setMARKERS(){
106
+ # shellcheck disable=SC2089
107
+ export MARK_BEGIN="RUN [^a-z]*cross-build-start[^a-z]*"
108
+ # shellcheck disable=SC2089
109
+ export MARK_END="RUN [^a-z]*cross-build-end[^a-z]*"
110
+ export ARM_BEGIN="### ARM BEGIN"
111
+ export ARM_END="### ARM END"
112
+ }
113
+ ### ------------------------------
114
+ # Disable blocks to Cross-Build ARM on x86_64 (-c) and ARM only (-a)
115
+ # Default: -a -c (Disable Cross-Build)
116
+ function comment() {
117
+ [ "$#" -eq 0 ] && log_failure_msg "missing file input" && exit 0
118
+ file=$1
119
+ [ "$#" -eq 1 ] && comment "$file" -a -c && return
120
+ cat /dev/null > "$file.sed"
121
+ while [ "$#" -gt 1 ]; do case $2 in
122
+ -a*|--arm)
123
+ echo "/${ARM_BEGIN}/,/${ARM_END}/s/^[# ]*(.*)/# \\1/g" >> "$file.sed"
124
+ ;;
125
+ -c*|--cross)
126
+ sed=("s/[# ]*(${MARK_BEGIN})/# \\1/g" \
127
+ "s/[# ]*(${MARK_END})/# \\1/g")
128
+ printf "%s\n" "${sed[@]}" >> "$file.sed"
129
+ ;;
130
+ esac; shift; done;
131
+ sed -i.x.old -E -f "$file.sed" "$file" >> "$LOG"
132
+ }
133
+ ### ------------------------------
134
+ # Enable blocks to Cross-Build ARM on x86_64 (-c) and ARM only (-a)
135
+ # Default: -a -c (Enable Cross-Build ARM only on x86_64)
136
+ function uncomment() {
137
+ [ "$#" -eq 0 ] && log_failure_msg "missing file input" && exit 0
138
+ file=$1
139
+ [ "$#" -eq 1 ] && uncomment "$file" -a -c && return
140
+ cat /dev/null > "$file.sed"
141
+ while [ "$#" -gt 1 ]; do case $2 in
142
+ -a*|--arm)
143
+ echo "/${ARM_BEGIN}/,/${ARM_END}/s/^(# )+(.*)/\\2/g" >> "$file.sed"
144
+ ;;
145
+ -c*|--cross)
146
+ sed=("s/(# )+(${MARK_BEGIN})/\\2/g" \
147
+ "s/(# )+(${MARK_END})/\\2/g")
148
+ printf "%s\n" "${sed[@]}" >> "$file.sed"
149
+ ;;
150
+ esac; shift; done;
151
+ sed -i.x.old -E -f "$file.sed" "$file"
152
+ }
153
+ setMARKERS
154
+ function cross_build_start() {
155
+ crossbuild=1
156
+ if [ "$#" -gt 0 ]; then case $1 in
157
+ -[d]*)
158
+ log_progress_msg "$MARK_END" >> "$LOG"
159
+ crossbuild=0
160
+ ;;
161
+ *)
162
+ log_failure_msg "Wrong usage: ${FUNCNAME[0]} $1" >&2
163
+ exit 3;;
164
+ esac;
165
+ else
166
+ log_progress_msg "$MARK_BEGIN" >> "$LOG"
167
+ fi
168
+ for d in "${projects[@]}"; do
169
+ ln -vsf "$project_root/${DKR_ARCH}.env" "$project_root/$d/.env" >> "$LOG"
170
+ [ "$(cd "$project_root/$d" && pwd)" != "$(pwd)" ] && ln -vsf "$project_root/common.env" "$project_root/$d/common.env" >> "$LOG"
171
+ setArch "$project_root/$d/Dockerfile.template" "$project_root/$d/Dockerfile.${DKR_ARCH}"
172
+ if [ "$crossbuild" = 0 ]; then
173
+ if [ "$arch" != "x86_64" ]; then
174
+ comment "$project_root/$d/Dockerfile.${DKR_ARCH}" -c
175
+ uncomment "$project_root/$d/Dockerfile.${DKR_ARCH}" -a
176
+ uncomment "$project_root/docker-compose.${DKR_ARCH}" -a
177
+ else
178
+ comment "$project_root/$d/Dockerfile.${DKR_ARCH}"
179
+ comment "$project_root/docker-compose.${DKR_ARCH}"
180
+ fi
181
+ else
182
+ if [ "$arch" != "x86_64" ]; then
183
+ uncomment "$project_root/docker-compose.${DKR_ARCH}"
184
+ uncomment "$project_root/$d/Dockerfile.${DKR_ARCH}"
185
+ else
186
+ comment "$project_root/docker-compose.${DKR_ARCH}"
187
+ comment "$project_root/$d/Dockerfile.${DKR_ARCH}"
188
+ fi
189
+ fi
190
+ [ "$(cd "$project_root/$d" && pwd)" != "$(pwd)" ] && cd "$project_root/$d" && git_commit "${DKR_ARCH} pushed ${d}"
191
+ cd "$project_root" || return
192
+ done
193
+ git_commit "${DKR_ARCH} pushed"
194
+ }
195
+ function git_commit() {
196
+ if ! git config user.email > /dev/null; then
197
+ githubuserid=${MAINTAINER:-'add-MAINTAINER-email-to-environment@github.com'}
198
+ git config --local user.email "$githubuserid"
199
+ git config --local user.name "$(echo "$githubuserid "| cut -d@ -f1)"
200
+ fi
201
+ git commit -a -m "${1:-"Add commit message"}" >> "$LOG" 2>&1 || true
202
+ }
203
+ function native_compose_file_set() {
204
+ if [ "$#" -gt 0 ]; then
205
+ case $1 in
206
+ -[d]*)
207
+ cp -vf "$project_root/docker-compose.yml.old" "$project_root/docker-compose.yml"
208
+ ;;
209
+ *)
210
+ cp -vf "$project_root/docker-compose.yml" "$project_root/docker-compose.yml.old"
211
+ setArch "$project_root/docker-compose.yml" "$project_root/docker-compose.${DKR_ARCH}"
212
+ cp -vf "$project_root/docker-compose.$1" "$project_root/docker-compose.yml"
213
+ ;;
214
+ esac
215
+ else
216
+ native_compose_file_set "${DKR_ARCH}"
217
+ fi
218
+ }
219
+ function balena_push() {
220
+ fleets=("$#")
221
+ i=0
222
+ [ "$#" -gt 0 ] && for a in "$@"; do fleets+=("$a"); done
223
+ for a in "${!fleets[@]}"; do
224
+ [ "$a" = 0 ] && continue
225
+ printf "[%s]: %s " "$a" "${fleets[$a]}"
226
+ done
227
+ log_daemon_msg "Found ${fleets[0]} fleets."
228
+ read -rp "Where do you want to push [1-${fleets[0]}] ? " i
229
+ log_daemon_msg "${fleets[$i]} was selected"
230
+ bash -c "balena push ${fleets[$i]}"
231
+ }
232
+ set -- "${saved[@]}"
233
+ while true; do
234
+ # SSH-ADD to Agent (PID)
235
+ eval "$(ssh-agent)"
236
+ ssh-add ~/.ssh/*id_rsa >> "$LOG" 2>&1 || true
237
+ next=${target:-${1:-}}
238
+ # store target
239
+ log_daemon_msg "$0 $arch $next" >> "$LOG"
240
+ unset target
241
+ case $next in
242
+ 1|--local)
243
+ slogger -st docker "Allow cross-build"
244
+ cross_build_start
245
+ native_compose_file_set
246
+ if command -v balena; then
247
+ # shellcheck disable=SC2046
248
+ balena_push $(balena scan | awk '/address:/{print $2}') || true
249
+ else
250
+ log_failure_msg "Please install Balena Cloud to run this script."
251
+ fi
252
+ native_compose_file_set -d
253
+ ;;
254
+ 4|--docker)
255
+ slogger -st docker "Allow cross-build"
256
+ cross_build_start
257
+ file=docker-compose.${DKR_ARCH}
258
+ if [ -f "$file" ]; then
259
+ bash -c "docker-compose -f $file --host ${DOCKER_HOST:-''} build" >> "$LOG"
260
+ else
261
+ bash -c "docker build -f Dockerfile.${DKR_ARCH} . && docker ps" >> "$LOG"
262
+ fi
263
+ ;;
264
+ 2|--balena)
265
+ slogger -st docker "Deny cross-build"
266
+ cross_build_start -d
267
+ native_compose_file_set
268
+ if command -v balena > /dev/null; then
269
+ # shellcheck disable=SC2046
270
+ balena_push $(balena fleets | awk '{if (NR>1) print $2}') || true
271
+ else
272
+ log_warning_msg "Balena Cloud not installed. Using git push."
273
+ git push -uf balena || true
274
+ fi
275
+ native_compose_file_set -d
276
+ ;;
277
+ 3|--nobuild)
278
+ slogger -st docker "Allow cross-build" >> "$LOG"
279
+ cross_build_start
280
+ ;;
281
+ 5|--push)
282
+ git push --recurse-submodules=on-demand
283
+ ;;
284
+ 6|--build-deps)
285
+ slogger -st docker "Allow cross-build" >> "$LOG"
286
+ cross_build_start
287
+ deploy_deps
288
+ ;;
289
+ 0|--exit)
290
+ log_daemon_msg "deploy's exiting..." >> "$LOG"
291
+ break;;
292
+ *)
293
+ if [ "${DEBIAN_FRONTEND:-}" = 'noninteractive' ]; then
294
+ # try last target
295
+ target=$(grep "$0 $arch" < "$LOG" | tail -1 | cut -d' ' -f3)
296
+ else
297
+ read -rp "What target docker's going to use \
298
+ (0:exit, 1:local balena, 2:balena, 3:don't build, 4:build, 5:push, 6:build dependencies) ?" target
299
+ fi
300
+ ;;
301
+ esac; shift
302
+ done
303
+ check_log "$LOG"
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env bash
2
+ set -u
3
+ [ "$#" -eq 0 ] && echo "usage $0 <work_dir> <args>" && exit 0
4
+ [ -f "$1" ] && set -- "$(cd "$(dirname "$1")" && pwd)" "${@:2}"
5
+ workd="$(cd "$1" && pwd)"; shift
6
+ banner=("" "[$0] BASH ${BASH_SOURCE[0]}" "$workd" ""); printf "%s\n" "${banner[@]}"
7
+ [ "${DEBUG:-0}" != 0 ] && printf "passed arg %s\n" "$*"
8
+ usage=("" \
9
+ "Usage: $0 [options] <work_sub_dir> <container_name> [DKR_ARCH]" \
10
+ " <-f, --force> <-e> <-m, --make-space>" \
11
+ "" \
12
+ " Base balenalib/raspberrypi3 images may be" \
13
+ " built from a Mac or PC without any specific" \
14
+ " cross-build backend, just set DKR_ARCH to ARM32" \
15
+ " or ARM64." \
16
+ "" \
17
+ " work_sub_dir: Path relative to $0" \
18
+ " container_name: Set to username/container to push to" \
19
+ " Docker.io, e.g. myself/cakephp2-image x86_64" \
20
+ " DKR_ARCH: 1|arm32*|armv7l|armhf ARMv7 OS" \
21
+ " 2|arm64*|aarch64 ARMv8 OS" \
22
+ " 3|amd64|x86_64 All X86 64 bits" \
23
+ " OS (Mac or PC)" \
24
+ " -f,--force: Set docker daemon restart flag on" \
25
+ " -e: Reset docker machine environment variables" \
26
+ " -m,--make-space: Remove exited containers to free some disk space" \
27
+ " TAG: Set as environment variable IMG_TAG" \
28
+ " File <DKR_ARCH>.env" \
29
+ "More about docker tag:" \
30
+ " docker tag <repository/image:tag> <new_repository/new_image:tag>" \
31
+ "")
32
+ [ "$#" -lt 3 ] && printf "%s\n" "${usage[@]}" && exit 0
33
+ while [[ "$#" -gt 0 ]]; do case $1 in
34
+ -[fF]*|--force)
35
+ docker-machine restart default;;
36
+ -[eE]*)
37
+ eval "$(docker-machine env)";;
38
+ -[mM]*|--make-space)
39
+ docker rm "$(docker ps -q -a -f 'status=exited')" 2> /dev/null \
40
+ || docker volume rm "$(docker volume ls -qf dangling=true)" 2> /dev/null || true
41
+ ;;
42
+ -[hH]*|--help)
43
+ printf "%s\n" "${usage[@]}"
44
+ exit 0;;
45
+ *)
46
+ DIR="$1"
47
+ NAME=$(echo "$2" | cut -d: -f1)
48
+ DKR_ARCH="$3"
49
+ shift 2
50
+ ;;
51
+ esac; shift; done
52
+ # shellcheck disable=SC1090
53
+ . "${workd}/.env" && . "${workd}/common.env"
54
+ docker build -f "$workd/$DIR/Dockerfile.${DKR_ARCH}" -t "$NAME:$IMG_TAG" "$workd/$DIR"
55
+ container=$(echo "$NAME" | cut -d/ -f2-)
56
+ docker run --rm -itd --name "$container" "$NAME:$IMG_TAG" &
57
+ sleep 2
58
+ container="$(docker ps -q -a -f "name=$container")"
59
+ [ "$container" ] && docker stop "$container"
60
+ docker login docker.io -u "${DOCKER_USER:-}" -p "${DOCKER_PASS:-}"
61
+ docker push "$NAME:$IMG_TAG"
62
+ docker tag "$NAME:$IMG_TAG" "$NAME:$IMG_TAG-${DKR_ARCH}"
63
+ docker push "$NAME:$IMG_TAG-${DKR_ARCH}"