-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.sh
More file actions
133 lines (117 loc) · 2.98 KB
/
compose.sh
File metadata and controls
133 lines (117 loc) · 2.98 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
#!/bin/bash
#
# title :configute.sh
# description :Compose Dockerfile from snippet(s)
# author :Blake
#----------------------------------------------------
# Setup variables
SNIPPET_PATH="./resources/snippet"
DOCKER_FILE="Dockerfile"
# Color definitions
COLOR_WHITE='\033[1;97m'
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_YELLOW='\033[0;33m'
COLOR_OFF='\033[0m'
# Snippet variables
SNIPPET_NAME=""
SNIPPET_VERSION=""
function failure {
echo -e "${COLOR_RED}[FATAL] ${1}${COLOR_OFF}"
exit 1
}
function success {
if [ "${#1}" -gt 1 ]; then
echo -e "${COLOR_GREEN}[SUCCESS] ${1}${COLOR_OFF}"
fi
exit 0
}
function warning {
echo -e "${COLOR_YELLOW}[WARNING] ${1}${COLOR_OFF}"
}
function get_snippet_info {
SNIPPET_NAME=`echo $1 | cut -d = -f 1`
SNIPPET_VERSION=`echo $1 | cut -s -d = -f 2`
}
function check_snippet_existed {
get_snippet_info $1
if [ ! -f "$SNIPPET_PATH/$SNIPPET_NAME" ]; then
failure "Snippet '$1' not found!"
fi
}
function write_snippet {
get_snippet_info $1
AP_NAME=`echo $SNIPPET_NAME | tr '[a-z]' '[A-Z]'`
echo "# Snippet: $SNIPPET_NAME" >> "$2"
# replace snippet version when append to Dockerfile
if [ ${#SNIPPET_VERSION} -ne 0 ]; then
grep -q "ENV ${AP_NAME}_VERSION" "$SNIPPET_PATH/$SNIPPET_NAME"
if [ $? -ne 0 ]; then
warning "Unable to replace ${SNIPPET_NAME} version, please make sure 'ENV ${AP_NAME}_VERSION x.y.z' existed is '$SNIPPET_PATH/$SNIPPET_NAME'."
fi
sed "s/ENV ${AP_NAME}_VERSION.*/ENV ${AP_NAME}_VERSION $SNIPPET_VERSION/g" "$SNIPPET_PATH/$SNIPPET_NAME" >> "$2"
else
cat "$SNIPPET_PATH/$SNIPPET_NAME" >> "$2"
fi
echo "" >> "$2"
}
function escape_regex {
result=`echo $1 | sed 's/\//\\\\\//g'`
echo "$result"
}
function get_available_snippets {
local -a ret=()
count=0
for var in "$SNIPPET_PATH"/*
do
escape_pattern=$(escape_regex $SNIPPET_PATH)
entry=`echo $var | sed "s/$escape_pattern\///g"`
# omit snippet name starts with "_"
if [[ "$entry" == _* ]]; then
continue
fi
ret[$count]=$entry
count=$(( $count + 1 ))
done
echo ${ret[@]}
}
function print_help {
local indent=" "
declare -a snippets=()
snippets=$(get_available_snippets)
echo -e "${COLOR_WHITE}NAME${COLOR_OFF}"
echo -e "${indent}$0 -- compose Dockerfile from snippet(s)\n"
echo -e "${COLOR_WHITE}AVAILABLE SNIPPETS${COLOR_OFF}"
for i in ${snippets[@]}; do
echo "${indent}$i"
done
echo ""
echo -e "${COLOR_WHITE}EXAMPLES${COLOR_OFF}"
echo "${indent}$0 --base"
echo "${indent}$0 gcc jdk"
echo "${indent}$0 pip jdk=7u79-b15"
echo ""
success
}
# handle arguments
if [ "$#" -lt 1 ]; then
print_help
elif [ "$1" == "--base" ]; then
# pop up other arguments
for var in "$@";
do
shift
done
fi
for var in "$@";
do
check_snippet_existed $var
done
# compose Dockerfile
cat $SNIPPET_PATH/_header > $DOCKER_FILE
for var in "$@";
do
write_snippet $var $DOCKER_FILE
done
cat $SNIPPET_PATH/_footer >> $DOCKER_FILE
success "Composed file: $DOCKER_FILE"