-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun-docker
More file actions
executable file
·151 lines (132 loc) · 5.11 KB
/
run-docker
File metadata and controls
executable file
·151 lines (132 loc) · 5.11 KB
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
#!/usr/bin/env bash
# =============================================================================
# Run Docker Locally Script
# =============================================================================
# This script runs the Testlum Docker container with the specified configuration.
#
# Usage: ./run-docker <image-name> <config> <path>
#
# Arguments:
# 1) Docker image name - The name of the Testlum Docker image
# 2) Config file - Configuration file: -c=<file>.xml or --config=<file>.xml
# 3) Path to resources - Path to test resources: -p=<path> or --path=<path>
#
# Example:
# ./run-docker testlum:1.0.2 -c=config.xml -p=/user/folder/resources
# =============================================================================
# Validation patterns
cfgPattern='^(-c=|--config=)[a-zA-Z0-9._:*#-]+(.xml)$'
pathPattern='^(-p=|--path=)(./|/|(./[a-zA-Z0-9_-]+)|(/[a-zA-Z0-9_-]+))+$'
# Input arguments
dockerImageName=$1
configFileName=$2
pathToFiles=$3
# -----------------------------------------------------------------------------
# Validates that all required arguments are provided
# -----------------------------------------------------------------------------
function validate_arguments() {
local has_errors=false
# Check if Docker image name is provided
if [[ -z "$dockerImageName" ]]; then
echo "Error: Docker image name is required (argument 1)"
has_errors=true
fi
# Check if config file matches the expected pattern
if [[ -z "$configFileName" ]]; then
echo "Error: Configuration file is required (argument 2)"
has_errors=true
elif [[ ! $configFileName =~ $cfgPattern ]]; then
echo "Error: Invalid configuration file format: '$configFileName'"
echo " Expected format: -c=<filename>.xml or --config=<filename>.xml"
has_errors=true
fi
# Check if path matches the expected pattern
if [[ -z "$pathToFiles" ]]; then
echo "Error: Path to test resources is required (argument 3)"
has_errors=true
elif [[ ! $pathToFiles =~ $pathPattern ]]; then
echo "Error: Invalid path format: '$pathToFiles'"
echo " Expected format: -p=<path> or --path=<path>"
has_errors=true
fi
if [[ "$has_errors" == true ]]; then
echo ""
print_usage
exit 1
fi
}
# -----------------------------------------------------------------------------
# Validates that Docker is installed and running
# -----------------------------------------------------------------------------
function validate_docker() {
if ! command -v docker &> /dev/null; then
echo "Error: Docker is not installed or not in PATH"
exit 1
fi
if ! docker info &> /dev/null; then
echo "Error: Docker daemon is not running"
exit 1
fi
}
# -----------------------------------------------------------------------------
# Validates that the specified path exists
# -----------------------------------------------------------------------------
function validate_path_exists() {
local path
path=$(get_path)
if [[ ! -d "$path" ]]; then
echo "Error: Path does not exist: '$path'"
exit 1
fi
}
# -----------------------------------------------------------------------------
# Main entry point
# -----------------------------------------------------------------------------
function main() {
validate_docker
validate_arguments
validate_path_exists
run_docker_locally
}
# -----------------------------------------------------------------------------
# Runs the Docker container with the specified configuration
# -----------------------------------------------------------------------------
function run_docker_locally() {
local path
path=$(get_path)
echo "Starting Testlum container..."
echo " Image: $dockerImageName"
echo " Config: $configFileName"
echo " Path: $path"
echo ""
docker run --rm --network=host \
--mount type=bind,source="$path",target="$path" \
"$dockerImageName" "$configFileName" "$pathToFiles"
}
# -----------------------------------------------------------------------------
# Extracts the path value from the path argument (removes -p= or --path= prefix)
# -----------------------------------------------------------------------------
function get_path() {
echo "$pathToFiles" | perl -pe 's/^(-p=|--path=)//'
}
# -----------------------------------------------------------------------------
# Prints usage information
# -----------------------------------------------------------------------------
function print_usage() {
echo "Usage: ./run-docker <image-name> <config> <path>"
echo ""
echo "Arguments:"
echo " 1) Docker image name"
echo " The name of the Testlum Docker image"
echo ""
echo " 2) Configuration file"
echo " Format: -c=<filename>.xml or --config=<filename>.xml"
echo " Allowed characters: [a-zA-Z0-9._:*#-]"
echo ""
echo " 3) Path to test resources"
echo " Format: -p=<path> or --path=<path>"
echo ""
echo "Example:"
echo " ./run-docker testlum:1.0.2 -c=config.xml -p=/user/folder/resources"
}
main