-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlbcserb
executable file
·141 lines (116 loc) · 3.27 KB
/
lbcserb
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
#!/usr/bin/env ruby
require 'erb'
if ARGV.length < 5
puts "Need at least 5 arguments. Also, this is only for lbcs scripts. How are you even seeing this?"
exit 1
end
maindir=ARGV.shift
lbcsdir=ARGV.shift
container=ARGV.shift
templatefile=ARGV.shift
outputfile=ARGV.shift
type=ARGV.shift
if type == 'addon'
addon=ARGV.shift
end
if type != "container" && type != "addon"
puts "Unknown type #{type}"
exit 1
end
if ! File.directory?(maindir)
puts "maindir arg '#{maindir}' doesn't look like a directory"
exit 1
end
if ! File.directory?(lbcsdir)
puts "lbcsdir arg '#{lbcsdir}' doesn't look like a directory"
exit 1
end
containerdir=File.join(maindir, 'containers', container)
if ! File.directory?(containerdir)
puts "containerdir arg '#{containerdir}' doesn't look like a directory"
exit 1
end
if type == 'addon'
addondir=File.join(containerdir, 'addons', addon)
if ! File.directory?(addondir)
puts "addondir arg '#{addondir}' doesn't look like a directory"
exit 1
end
end
if ! File.exist?(templatefile)
puts "templatefile arg '#{templatefile}' doesn't look like a file"
exit 1
end
vals={}
vals['maindir']=maindir
vals['lbcsdir']=lbcsdir
vals['containerdir']=containerdir
vals['container']=container
vals['container_name']=container
vals['user']=%x{id -un}.strip
if type == 'addon'
vals['addondir']=addondir
vals['addon']=addon
vals['addon_name']=addon
end
# All remaining args are "foo=bar" stuff to add to the
# erb variable list
for arg in ARGV[0..] do
stuff = arg.split('=', 2)
vals[stuff[0]] = stuff[1]
end
files = [
File.join(maindir, 'config'),
File.join(maindir, 'secrets'),
File.join(lbcsdir, 'config'),
File.join(lbcsdir, 'secrets'),
File.join(containerdir, 'config'),
File.join(containerdir, 'secrets'),
]
if type == 'addon'
files << File.join(addondir, 'config')
files << File.join(addondir, 'secrets')
end
def getvals(line)
stuff = line.split('=', 2)
if line =~ %r{^[a-z_].*="}
return stuff[0], stuff[1].sub( %r{^"(.*)"$}, '\1' )
elsif line =~ %r{^[a-z_].*='}
return stuff[0], stuff[1].sub( %r{^'(.*)'$}, '\1' )
else
return stuff[0], stuff[1]
end
end
# Get a list of the addons
if type == 'container'
addons_arr=[]
Dir[File.join(containerdir, 'addons', '*')].each do |addon|
line = File.read(File.join(addon, 'config')).lines.grep(%r{^name=}).first
stuff = getvals(line)
addons_arr << stuff[1]
end
vals['addons'] = addons_arr.join(' ')
end
# Now get all the vals from all the config and secrets files
for varfile in files do
if File.exist?(varfile)
for line in File.read(varfile).lines do
line = line.gsub(%r{#.*},'').strip
# Ruby local variables must start with a lower case letter
# or underscore. Also we don't care about lines with no =
if line =~ %r{^[a-z_].*=}
stuff = getvals(line)
vals[stuff[0]] = stuff[1]
end
end
end
end
templatetext = File.read(templatefile)
newtext = ERB.new(templatetext, trim_mode: '-').result_with_hash(vals)
oldtext=''
if File.exist?(outputfile)
oldtext = File.read(outputfile)
end
if oldtext != newtext
File.open(outputfile, 'w') { |file| file.write(newtext) }
end