Skip to content

Commit a00ed1a

Browse files
bryanlattenBryan Latten
andauthored
CPU Arch: added helper script for simple string replacements (#66)
* CPU Arch: added helper script for simple string replacements * Archstring: remaining type from testing * Archstring: now with named parameters * README: updates * Archstring: cleanup * Archstring: extraneous a * Archstring: EOF newline * Goss: testing shorthand parameters * Archstring: detect and filter bad arguments * Goss: fixed missing EOL * Archstring: removed extraneous semicolon * Archstring: bracket consistency Co-authored-by: Bryan Latten <[email protected]>
1 parent b132b0e commit a00ed1a

File tree

10 files changed

+117
-25
lines changed

10 files changed

+117
-25
lines changed

Dockerfile-alpine

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM alpine:3.14 as base
44

55
# Ensure scripts are available for use in next command
66
COPY ./container/root/scripts/* /scripts/
7+
COPY ./container/root/usr/local/bin/* /usr/local/bin/
78

89
# - Symlink variant-specific scripts to default location
910
# - Add additional repositories to pull packages from

Dockerfile-centos-7

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM centos:7 as base
44

55
# Ensure scripts are available for use in next command
66
COPY ./container/root/scripts/* /scripts/
7+
COPY ./container/root/usr/local/bin/* /usr/local/bin/
78

89
# - Symlink variant-specific scripts to default location
910
# - Upgrade base security packages, then clean packaging leftover

Dockerfile-centos-8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM centos:8 as base
44

55
# Ensure scripts are available for use in next command
66
COPY ./container/root/scripts/* /scripts/
7+
COPY ./container/root/usr/local/bin/* /usr/local/bin/
78

89
# - Symlink variant-specific scripts to default location
910
# - Upgrade base security packages, then clean packaging leftover

Dockerfile-ubuntu-18.04

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM ubuntu:18.04 as base
44

55
# Ensure scripts are available for use in next command
66
COPY ./container/root/scripts/* /scripts/
7+
COPY ./container/root/usr/local/bin/* /usr/local/bin/
78

89
# - Symlink variant-specific scripts to default location
910
# - Upgrade base security packages, then clean packaging leftover

Dockerfile-ubuntu-20.04

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FROM ubuntu:20.04 as base
44

55
# Ensure scripts are available for use in next command
66
COPY ./container/root/scripts/* /scripts/
7+
COPY ./container/root/usr/local/bin/* /usr/local/bin/
78

89
# - Symlink variant-specific scripts to default location
910
# - Upgrade base security packages, then clean packaging leftover

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Provides base OS, security patches, and tools for quick and easy spinup.
1616
* Centos 7 builds available, tagged as `-centos-7`
1717
* Centos 8 builds available, tagged as `-centos-8`
1818

19-
2019
### Tools
2120

2221
* [S6](https://github.com/just-containers/s6-overlay) process supervisor is used for `only` for zombie reaping (as PID 1), boot coordination, and termination signal translation
@@ -57,27 +56,33 @@ S6_KILL_FINISH_MAXTIME | S6_KILL_FINISH_MAXTIME=55000 | The maximum time (in ms)
5756
S6_KILL_GRACETIME | S6_KILL_GRACETIME=500 | Wait time (in ms) for S6 finish scripts before sending kill signal. This value has a max of 65535 on Alpine variants.
5857

5958
* `with-contenv` tool, which is used to expose environment variables across scripts, has a limitation that it cannot read beyond 4k characters for environment variable values. To work around this issue, use the script `/scripts/with-bigcontenv` instead of `with-contenv`. You'll need to remove the `with-contenv` from the shebang line, and add `source /scripts/with-bigcontenv` in the next line after the shebang line.
59+
6060
### Startup/Runtime Modification
6161

6262
To inject changes just before runtime, shell scripts may be placed into the
6363
`/etc/cont-init.d` folder.
6464
As part of the process manager, these scripts are run in advance of the supervised processes. @see https://github.com/just-containers/s6-overlay#executing-initialization-andor-finalization-tasks
6565

66+
### Processor Architectures
67+
68+
All variants are tested on x64 and arm64.
69+
The convenience script `archstring` is provided to switch between strings based on the current machine.
70+
Usage:
71+
```archstring --x64 intel --arm64 arm```
72+
Which will return "intel" when on x64 and arm when on arm64. This is handy when package names or download paths need to be modified per architecture.
73+
6674
### Testing
6775

6876
- Container tests itself as part of build process using [goss](https://github.com/aelsabbahy/goss) validator. To add additional build-time tests, overwrite (or extend) the `./container/root/goss.base.yaml` file.
6977
- To initiate run-time validation, please execute `test.sh`. It uses [dgoss](https://github.com/aelsabbahy/goss/tree/master/extras/dgoss) validator. To add additional run-time tests, extend `./test.sh` and `./goss.yaml` file.
7078

71-
72-
7379
### Advanced Modification
7480

7581
More advanced changes can take effect using the `run.d` system. Similar to the `/etc/cont-init.d/` script system, any shell scripts (ending in .sh) in the `/run.d/` folder will be executed ahead of the S6 initialization.
7682

7783
- If a `run.d` script terminates with a non-zero exit code, container will stop, terminating with the script's exit code, unless...
7884
- If script terminates with exit code of $SIGNAL_BUILD_STOP (99), this will signal the container to stop cleanly. This can be used for a multi-stage build process
7985

80-
8186
### Shutdown Behavior
8287

8388
#### Sequence of events for a crashed supervised service:

container/root/goss.base.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,38 @@ command:
44
exit-status: 0
55
stderr: []
66
stdout: []
7+
# Validate binary installation and availability
8+
# Unfortunately cannot rely on any test architecture to prove its working
9+
archstring --x64 example --arm64 example:
10+
exit-status: 0
11+
stdout:
12+
- example
13+
archstring -i example -a example:
14+
exit-status: 0
15+
stdout:
16+
- example
17+
archstring --x64 example:
18+
exit-status: 1
19+
stdout:
20+
- /--arm64/
21+
archstring -i example:
22+
exit-status: 1
23+
stdout:
24+
- /--arm64/
25+
archstring --arm64 example:
26+
exit-status: 1
27+
stdout:
28+
- /--x64/
29+
archstring -a example:
30+
exit-status: 1
31+
stdout:
32+
- /--x64/
33+
# uneven number of parameters (shift overrun)
34+
archstring -a a -i i -x:
35+
exit-status: 1
36+
stdout:
37+
- /usage/
38+
archstring -a a -i i -x a:
39+
exit-status: 1
40+
stdout:
41+
- /usage/

container/root/scripts/install_goss.sh

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,12 @@
99
GOSS_VERSION=v0.3.16
1010

1111
# Locate manually and commit below from https://github.com/aelsabbahy/goss/releases/download/${GOSS_VERSION}/goss-linux-${ARCH}.sha256
12-
GOSS_SHA256=""
1312
# Determined automatically to correctly select binary
14-
ARCH=""
15-
16-
if [[ "$(uname -m)" = "x86_64" ]]; then
17-
echo "[goss install] Detected x86_64 architecture"
18-
ARCH="amd64"
19-
GOSS_SHA256=827e354b48f93bce933f5efcd1f00dc82569c42a179cf2d384b040d8a80bfbfb
20-
elif [[ "$(uname -m)" = "aarch64" ]]; then
21-
echo "[goss install] Detected ARM architecture"
22-
ARCH="arm"
23-
GOSS_SHA256=67c1e6185759a25bf9db334a9fe795a25708f2b04abe808a87d72edd6cd393fd
24-
fi;
13+
ARCH="$(archstring --arm64 arm --x64 amd64)"
14+
GOSS_SHA256="$(archstring \
15+
--x64 827e354b48f93bce933f5efcd1f00dc82569c42a179cf2d384b040d8a80bfbfb \
16+
--arm64 67c1e6185759a25bf9db334a9fe795a25708f2b04abe808a87d72edd6cd393fd \
17+
)"
2518

2619
curl -fL https://github.com/aelsabbahy/goss/releases/download/${GOSS_VERSION}/goss-linux-${ARCH} -o /usr/local/bin/goss
2720

container/root/scripts/install_s6.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77
# Requires curl, gpg (or gnupg on Alpine), and tar to be present
88

99
# Determined automatically to correctly select binary
10-
ARCH=""
11-
12-
if [[ "$(uname -m)" = "x86_64" ]]; then
13-
echo "[s6 install] Detected x86_64 architecture"
14-
ARCH="amd64"
15-
elif [[ "$(uname -m)" = "aarch64" ]]; then
16-
echo "[s6 install] Detected ARM architecture"
17-
ARCH="aarch64"
18-
fi;
10+
ARCH="$(archstring --x64 amd64 --arm64 aarch64)"
1911

2012
S6_NAME=s6-overlay-${ARCH}.tar.gz
2113
S6_VERSION=v2.2.0.3
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
# Script to detect processor architecture and output one of the two input parameters
4+
# Example: when a download path requires an arch-specific package name
5+
#
6+
# --x64 {value}: output if an x86_64 architecture is detected
7+
# --arm64 {value}: output if an arm64 or aarch64 architecture is detected
8+
function usage() {
9+
echo "usage: $(basename $0) --x64 value --arm64 value"
10+
exit 1
11+
}
12+
13+
if [[ "$#" -eq 0 ]]; then
14+
usage
15+
fi
16+
17+
while [[ "$#" -gt 0 ]]
18+
do
19+
case $1 in
20+
-i|--x64)
21+
declare x64="$2"
22+
;;
23+
-a|--arm64)
24+
declare arm64="$2"
25+
;;
26+
*)
27+
usage
28+
;;
29+
esac
30+
# shift 2, since we consumed the arch specifier and the value
31+
shift 2
32+
# assert there are positional parameters remaining
33+
if [[ "$?" -ne 0 ]]; then
34+
usage
35+
fi;
36+
done
37+
38+
if [[ -z "$x64" ]]; then
39+
echo "error: missing --x64 input"
40+
exit 1
41+
fi
42+
43+
if [[ -z "$arm64" ]]; then
44+
echo "error: missing --arm64 input"
45+
exit 1
46+
fi
47+
48+
DETECTED_ARCH="$(uname -m)"
49+
ARCH_X64='x86_64'
50+
ARCH_ARM64='aarch64'
51+
ARCH_ARM64_ALT='arm64'
52+
53+
if [[ $DETECTED_ARCH == $ARCH_X64 ]]; then
54+
echo $x64
55+
elif [[ $DETECTED_ARCH == $ARCH_ARM64 ]]; then
56+
echo $arm64
57+
elif [[ $DETECTED_ARCH == $ARCH_ARM64_ALT ]]; then
58+
echo $arm64
59+
else
60+
echo "failure: $DETECTED_ARCH not supported"
61+
exit 1
62+
fi;

0 commit comments

Comments
 (0)