File: build_test_container.sh

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (144 lines) | stat: -rwxr-xr-x 4,457 bytes parent folder | download | duplicates (2)
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
#!/bin/bash

# ARG_OPTIONAL_SINGLE([flavor],[],[Which distribution to base the container upon],[fedora])
# ARG_OPTIONAL_SINGLE([public-key],[i],[Which identity file to use to SSH login to the container (looks for first .pub file under $HOME/.ssh by default)])
# ARG_OPTIONAL_REPEATED([package],[p],[Which additional package(s) to add to the test image.])
# ARG_OPTIONAL_SINGLE([name],[n],[Name of the test image],[ssg_test_suite])
# DEFINE_SCRIPT_DIR([])
# ARG_TYPE_GROUP_SET([flavors],[FLAVOR],[flavor],[fedora,centos,rhel,rhel8])
# ARG_HELP([Build a podman image to be used with the SSG test suite])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info


die()
{
	local _ret="${2:-1}"
	test "${_PRINT_HELP:-no}" = yes && print_help >&2
	echo "$1" >&2
	exit "${_ret}"
}

# validators

flavors()
{
	local _allowed=("fedora" "centos" "rhel" "rhel8") _seeking="$1"
	for element in "${_allowed[@]}"
	do
		test "$element" = "$_seeking" && echo "$element" && return 0
	done
	die "Value '$_seeking' (of argument '$2') doesn't match the list of allowed values: 'fedora', 'centos', 'rhel' and 'rhel8'" 4
}


begins_with_short_option()
{
	local first_option all_short_options='ipnh'
	first_option="${1:0:1}"
	test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}

# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_flavor="fedora"
_arg_public_key=
_arg_package=()
_arg_name="ssg_test_suite"


print_help()
{
	printf '%s\n' "Build a podman image to be used with the SSG test suite"
	printf 'Usage: %s [--flavor <FLAVOR>] [-i|--public-key <arg>] [-p|--package <arg>] [-n|--name <arg>] [-h|--help]\n' "$0"
	printf '\t%s\n' "--flavor: Which distribution to base the container upon. Can be one of: 'fedora', 'centos', 'rhel' and 'rhel8' (default: 'fedora')"
	printf '\t%s\n' "-i, --public-key: Which identity file to use to SSH login to the container (looks for first .pub file under $HOME/.ssh by default) (no default)"
	printf '\t%s\n' "-p, --package: Which additional package(s) to add to the test image. (empty by default)"
	printf '\t%s\n' "-n, --name: Name of the test image (default: 'ssg_test_suite')"
	printf '\t%s\n' "-h, --help: Prints help"
}


parse_commandline()
{
	while test $# -gt 0
	do
		_key="$1"
		case "$_key" in
			--flavor)
				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
				_arg_flavor="$(flavors "$2" "flavor")" || exit 1
				shift
				;;
			--flavor=*)
				_arg_flavor="$(flavors "${_key##--flavor=}" "flavor")" || exit 1
				;;
			-i|--public-key)
				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
				_arg_public_key="$2"
				shift
				;;
			--public-key=*)
				_arg_public_key="${_key##--public-key=}"
				;;
			-i*)
				_arg_public_key="${_key##-i}"
				;;
			-p|--package)
				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
				_arg_package+=("$2")
				shift
				;;
			--package=*)
				_arg_package+=("${_key##--package=}")
				;;
			-p*)
				_arg_package+=("${_key##-p}")
				;;
			-n|--name)
				test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
				_arg_name="$2"
				shift
				;;
			--name=*)
				_arg_name="${_key##--name=}"
				;;
			-n*)
				_arg_name="${_key##-n}"
				;;
			-h|--help)
				print_help
				exit 0
				;;
			-h*)
				print_help
				exit 0
				;;
			*)
				_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
				;;
		esac
		shift
	done
}

parse_commandline "$@"

# OTHER STUFF GENERATED BY Argbash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || { echo "Couldn't determine the script's running directory, which probably matters, bailing out" >&2; exit 2; }
# Validation of values


### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash

public_key=$_arg_public_key
test -n "$public_key" || public_key=$(ls "$HOME"/.ssh/*.pub | head -n 1)
test -f "$public_key" || die "Couldn't find your public key, specify its location as the script's argument."

# build without context
podman build --build-arg ADDITIONAL_PACKAGES="${_arg_package[*]}" --build-arg CLIENT_PUBLIC_KEY="$(cat "$public_key")" -t "${_arg_name}" - < "$script_dir/../Dockerfiles/test_suite-$_arg_flavor"

# ] <-- needed because of Argbash