-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateConfiguration.groovy
More file actions
206 lines (188 loc) · 5.83 KB
/
UpdateConfiguration.groovy
File metadata and controls
206 lines (188 loc) · 5.83 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
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
/**
* Update the eSignature configuration stored in .env and
* localApplication.properties based on the system's hostname and the IP
* address. This will prefer the VPN IP address, if found.
*
* Exit codes:
* -1 configuration file could not be written
* -2 could not determine hostname
* -3 could not determine ip address
*/
class UpdateConfiguration {
/**
* Work starts here.
*/
void run() {
println ''
IpConfig ip = getIpAddess()
if (!ip.isVpn) {
println 'WARNING: It seems you are NOT on the VPN.'
println 'WARNING: If this is a first time setup, you may not'
println 'WARNING: be able to create the signature container.'
println ''
}
println "IP is ${ip.ip} (${ip.isVpn ? "VPN" : "NOT on the VPN"})"
String hostname = getHostname()
println "Hostname is ${hostname}"
makeFilesFromTemplates([
'__VPN_IP__': ip.ip,
'__LOCAL_HOSTNAME__': hostname,
'__IS_VPN__': "${ip.isVpn}",
])
}
/**
* Get the local hostname.
* If not obtainable, the application will exit with -2.
*
* @return the hostname as a String.
*/
String getHostname() {
String hostname = java.net.InetAddress.getLocalHost().getHostName()
if (!hostname) {
System.err.println('Could not determine hostname')
System.err.println('Exiting.')
System.exit(-2)
}
return hostname
}
/**
* Use `ipconfig` to find the IP address of the VPN (preferably)
* otherwise the non-VPN ip address.
* If not obtainable, the application will exit with -3.
*
* @return The IPv4 Address, preferably for the VPN
*/
IpConfig getIpAddess() {
List<Map> configs = parseIpConfig()
/**
* This code may not fully cover every scenario.
* If this isn't detecting your IP when you are
* and aren't on the VPN, we need to update this code.
*/
boolean isVpn = true
String localOrVpnIp = findIpAddress(configs,
'Connection-specific DNS Suffix', 'corp.peopleclick.com')
if (!localOrVpnIp) {
// Look for a non-VPN IP address
isVpn = false
localOrVpnIp = findIpAddress(configs,
'Connection-specific DNS Suffix', 'local')
}
if (!localOrVpnIp) {
System.err.println('Could not detect IP address. Check script logic.')
System.err.println('Exiting.')
System.exit(-3)
}
return new IpConfig(ip: localOrVpnIp, isVpn: isVpn)
}
class IpConfig {
String ip
boolean isVpn
}
/**
* Execute ipconfig and parse the data into a List of configs.
* Each config is a map.
*/
List<Map> parseIpConfig() {
String cmd = 'ipconfig'
List<Map> configs = []
Map currentConfig = null
// Parse output result of ipconfig into configs
cmd.execute().text.split('[\n\r]').
collect { String line ->
line.trim()
}.
each { String line ->
if (!line) { return }
else if (line.startsWith('Ethernet adapter ') || line.startsWith('Wireless LAN adapter')) {
currentConfig = ['name': line]
configs << currentConfig
}
else if (!currentConfig) { return }
else {
ConfigEntry configEntry = splitDataLine(line)
if (configEntry) {
currentConfig[configEntry.key] = configEntry.value
}
}
}
return configs
}
class ConfigEntry {
String key
String value
}
/**
* A key/value line from ipconfig (within an adapter stanza) looks like
* "IPv4 Address. . . . . . . . . . . : 192.168.1.153"
* Split this into a ConfigEntry with
* key: "IPv4 Address"
* value: "192.168.1.153"
* If this format is not found, return null
*
* @return a ConfigEntry or null
*/
ConfigEntry splitDataLine(String line) {
def result = (line =~ /^(.*?)( ?\.?)* :[ ]?(.*)$/).findAll()
if (result.size() == 1 &&
result[0].size() == 4 &&
result[0][3]) {
// We have both a key and a non-empty value
return new ConfigEntry(key: result[0][1], value: result[0][3])
}
else {
return null
}
}
/**
* Return the 'IPv4 Address' for a config with
* 'Connection-specific DNS Suffix' == dnsSuffixPriorty
* or null.
*
* @return the IPv4 address or null
*/
String findIpAddress(List<Map> configs, String searchField, String searchValue) {
Map foundConfig = configs.find { Map config ->
config[searchField] == searchValue && config.containsKey('IPv4 Address')
}
return foundConfig ? foundConfig.'IPv4 Address' : null
}
/**
* Map of template files to use to create
* usable files. Key is source template file,
* value is output file to be created.
*/
Map templates = [
(new File('localApplication.template')) : (new File('localApplication.properties')),
(new File('env.template')) : (new File('.env')),
]
/**
* Create usable files from the template files,
* substituting config values.
* If any of the source template files don't exist,
* the application will exit with -1.
*/
void makeFilesFromTemplates(Map substitutions) {
println ''
templates.each { File sourceFile, File destFile ->
if (!sourceFile.exists()) {
System.err.println('Source template file ${sourceFile} missing')
System.err.println('Exiting.')
System.exit(-1)
}
destFile.delete()
String output = sourceFile.text
substitutions.each { subKey, subValue ->
output = output.replaceAll(subKey, subValue)
}
destFile << output
println "Created file from template ${destFile}"
}
}
/**
* Script entry point.
*/
static void main(String[] args) {
new UpdateConfiguration().run()
}
}