-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcenv
executable file
·212 lines (184 loc) · 6.87 KB
/
dcenv
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
# Wrapper for invoking docker-compose
# Builds configuration environment and required files accoring to the directory structure:
# ├── config
# │ ├── all
# │ │ ├── copy-source <--- source is copied verbatim
# │ │ │ └── etc
# │ │ │ └── nginx
# │ │ │ ├── nginx.conf
# │ │ ├── environment.conf
# │ │ └── gen-source <--- source is subsituted with enviroment, first from Root, then from All, then from Configuration Environment
# │ │ └── etc
# │ │ └── nginx
# │ │ └── sites-enabled
# │ │ ├── codermerlin.com.conf
# │ │ ├── permissions.conf <--- applies ownership and permissions changes to files, relative to the $configuration/target directory
# │ │ Line format: <relativePathname> <ownerUser> <ownerGroup> <permssions>
# │ │ Example: ./etc/nginx/nginx.conf:root:root:644
# │ └── prd
# │ ├── docker-compose.yml
# │ ├── environment.conf
# │ └── target <---- $targetEnvironmentPath for configuration 'prd'
# │ └── etc
# │ └── nginx
# │ ├── nginx.conf
# │ └── sites-enabled
# │ └── codermerlin.com.conf
#
#
# 1. Loads environment
# a. Root (root/docker)
# b. RootConfig (root/docker/$configuration)
# c. Global (config/all)
# d. Configuration-specific (config/$configuration)
# 2. Copies files
# a. Files under config/all/copy-source are copied as-is
# b. Files under config/all/gen-source are environment-substituted
# NB: If substitution is not desired, replace the "$" with "${DOLLAR}"
# 3. Loads .yml files
# a. Loads docker-compose.yml from the current directory
# b. Loads docker-compose.yml from the configuration directory
# 4. Docker-compose may reference the target directory
# with the environment variable $targetEnvironmentPath
# 5. Sets the --project-name to directoryName-configuration
#
# To debug environment, set environment: DC_DEBUG=1"
#
set -eu
# Ensure running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Gather arguments
configuration=$1
shift
# Set parameters
rootEnvironmentPath=/root/docker
globalEnvironmentPath=./config/all
permissionsPathname=$globalEnvironmentPath/permissions.conf
globalEnvironmentCopySourcePath=$globalEnvironmentPath/copy-source
globalEnvironmentGenSourcePath=$globalEnvironmentPath/gen-source
configEnvironmentPath=./config/$configuration
targetEnvironmentPath=$configEnvironmentPath/target
runtimeEnvironmentPath=$configEnvironmentPath/runtime
rootEnvironmentPathname=$rootEnvironmentPath/environment.conf
rootEnvironmentConfigPathname=$rootEnvironmentPath/environment-$configuration.conf
globalEnvironmentPathname=$globalEnvironmentPath/environment.conf
configEnvironmentPathname=$configEnvironmentPath/environment.conf
projectName="$(basename $(pwd))-$configuration"
# Convert to lowercase
projectName=${projectName,,}
composePathname="docker-compose.yml"
overrideComposePathname="$configEnvironmentPath/$composePathname"
# Export relevant variables
# The DOLLAR entry allows us to escape values which we don't want to interpolate
export DOLLAR='$'
export configuration
export configEnvironmentPath
export targetEnvironmentPath
export runtimeEnvironmentPath
export projectName
# Validate presence of compose files
if [ ! -f "$composePathname" ]
then
echo "Unable to find compose file '$composePathname'"
exit 1
fi
if [ ! -f "$overrideComposePathname" ]
then
echo "Unable to find compose file '$overrideComposePathname'"
exit 1
fi
# Validate presence of environment files
if [ ! -f "$rootEnvironmentPathname" ]
then
echo "Unable to find environment '$rootEnvironmentPathname'"
exit 1
fi
if [ ! -f "$rootEnvironmentConfigPathname" ]
then
echo "Unable to find environment '$rootEnvironmentConfigPathname'"
exit 1
fi
if [ ! -f "$globalEnvironmentPathname" ]
then
echo "Unable to find environment '$globalEnvironmentPathname'"
exit 1
fi
if [ ! -f "$configEnvironmentPathname" ]
then
echo "Unable to find environment '$configEnvironmentPathname'"
exit 1
fi
# Validate presence of permission file
if [ ! -f "$permissionsPathname" ]
then
echo "Unable to find permissions '$permissionsPathname'"
exit 1
fi
# Load environment (silently)
export $(envsubst < "$rootEnvironmentPathname" | grep -v '^#') > /dev/null
export $(envsubst < "$rootEnvironmentConfigPathname" | grep -v '^#') > /dev/null
export $(envsubst < "$globalEnvironmentPathname" | grep -v '^#') > /dev/null
export $(envsubst < "$configEnvironmentPathname" | grep -v '^#') > /dev/null
# Remove the target configuration directory to eliminate stale files
rm -rf "$targetEnvironmentPath"
# Copy to the corresponding configuration directory files from the CopySource path as-is
for source in $(find "$globalEnvironmentCopySourcePath" -mindepth 1 -type f -print | grep -v "~$")
do
# The target directory
target="$targetEnvironmentPath${source##$globalEnvironmentCopySourcePath}"
targetDir=$(dirname "$target")
if [ ${DC_DEBUG:-0} = "1" ]
then
echo "$source -> $target"
fi
mkdir -p "$targetDir"
cp "$source" "$target"
done;
# Substitute enviroment variable in any files located in the global gen source environment
# and copy to the corresponding configuration directory
for source in $(find "$globalEnvironmentGenSourcePath" -mindepth 1 -type f -print | grep -v "~$")
do
# The target directory
target="$targetEnvironmentPath${source##$globalEnvironmentGenSourcePath}"
targetDir=$(dirname "$target")
if [ ${DC_DEBUG:-0} = "1" ]
then
echo "$source -> $target (with substitution)"
fi
mkdir -p "$targetDir"
envsubst < "$source" > "$target"
done;
# Apply ownership and permissions changes
while IFS=':' read -ra line
do
if [ ! ${#line[@]} -eq 4 ]
then
echo "Incorrect permssions syntax in line: $line"
exit 1
fi
relativePathname=${line[0]}
ownerUser=${line[1]}
ownerGroup=${line[2]}
permissions=${line[3]}
fullPathname="$targetEnvironmentPath/$relativePathname"
if [ ${DC_DEBUG:-0} = "1" ]
then
echo "Setting ownership of '$fullPathname' to '$ownerUser':'$ownerGroup'"
echo "Setting permissions of '$fullPathname' to '$permissions'"
fi
chown "$ownerUser":"$ownerGroup" "$fullPathname"
chmod "$permissions" "$fullPathname"
done < $permissionsPathname
# Execute the specified command line
commandLine="$@"
# Dump debug information if specified
if [ ${DC_DEBUG:-0} = "1" ]
then
printenv
echo "Executing: $commandLine"
fi
eval "$commandLine"