gluez 0.1.5 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gluez/formatter.rb +16 -8
- data/lib/gluez/impl/linux.rb +46 -68
- data/lib/gluez.rb +56 -14
- metadata +2 -2
data/lib/gluez/formatter.rb
CHANGED
@@ -2,22 +2,30 @@ module Gluez
|
|
2
2
|
class Formatter
|
3
3
|
def self.format(lines)
|
4
4
|
indent = 0
|
5
|
+
num_line = 0
|
5
6
|
|
6
7
|
prev_line = nil
|
7
8
|
lines.split("\n").collect do |line|
|
8
|
-
|
9
|
+
num_line += 1
|
10
|
+
begin
|
11
|
+
line = line.strip
|
9
12
|
|
10
|
-
|
13
|
+
indent -= 2 if ['}', 'fi', 'else'].include?(line)
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
l = (' ' * indent) + line
|
16
|
+
indent += 2 if [/^function /, /^if /, /^else/].any?{|e| line =~ e}
|
14
17
|
|
15
|
-
|
16
|
-
|
18
|
+
# indent -= 2 if line =~ /^data=/
|
19
|
+
indent -= 2 if line.index('cat >~/.gluez_transfer <<\DATA')
|
20
|
+
indent += 2 if line == '"' && prev_line == 'DATA'
|
17
21
|
|
18
|
-
|
22
|
+
prev_line = line
|
19
23
|
|
20
|
-
|
24
|
+
l
|
25
|
+
rescue Exception => e
|
26
|
+
puts "error on line: #{num_line}"
|
27
|
+
raise e
|
28
|
+
end
|
21
29
|
end.join("\n")
|
22
30
|
end
|
23
31
|
end
|
data/lib/gluez/impl/linux.rb
CHANGED
@@ -9,12 +9,10 @@ module Gluez
|
|
9
9
|
case task
|
10
10
|
when :gem
|
11
11
|
steps << {
|
12
|
-
:check => "
|
13
|
-
:code => "
|
12
|
+
:check => "\\$(gem list | awk '{print \\$1}' | grep ^#{name}$ | wc -l) -eq 1",
|
13
|
+
:code => "gem install #{name} --version #{opts[:version]} --user-install --no-rdoc --no-ri"
|
14
14
|
}
|
15
15
|
|
16
|
-
name = "#{name}@#{opts[:user]}"
|
17
|
-
|
18
16
|
when :file
|
19
17
|
steps << {
|
20
18
|
:check => "-f #{name}",
|
@@ -22,15 +20,10 @@ module Gluez
|
|
22
20
|
}
|
23
21
|
|
24
22
|
steps << {
|
25
|
-
:check => "
|
23
|
+
:check => %Q("\\$(stat -L --format=%a #{name})" = "#{opts[:chmod, "644"]}"),
|
26
24
|
:code => "chmod #{opts[:chmod, "644"]} #{name}"
|
27
25
|
}
|
28
26
|
|
29
|
-
steps << {
|
30
|
-
:check => "\"$(stat -L --format=%G:%U #{name})\" = \"#{opts[:chown, "root:root"]}\"",
|
31
|
-
:code => "chown --no-dereference #{opts[:chown, "root:root"]} #{name}"
|
32
|
-
}
|
33
|
-
|
34
27
|
when :dir
|
35
28
|
steps << {
|
36
29
|
:check => "-d #{name}",
|
@@ -38,18 +31,16 @@ module Gluez
|
|
38
31
|
}
|
39
32
|
|
40
33
|
steps << {
|
41
|
-
:check => "
|
34
|
+
:check => %Q("\\$(stat -L --format=%a #{name})" = "#{opts[:chmod, "755"]}"),
|
42
35
|
:code => "chmod #{opts[:chmod, "755"]} #{name}"
|
43
36
|
}
|
44
37
|
|
45
|
-
steps << {
|
46
|
-
:check => "\"$(stat -L --format=%G:%U #{name})\" = \"#{opts[:chown, "root:root"]}\"",
|
47
|
-
:code => "chown --no-dereference #{opts[:chown, "root:root"]} #{name}"
|
48
|
-
}
|
49
|
-
|
50
38
|
when :package
|
51
39
|
steps << {
|
52
|
-
:check =>
|
40
|
+
:check => [
|
41
|
+
"\\$(apt-cache policy #{name} | grep Installed | wc -l) -eq 1",
|
42
|
+
"\\$(apt-cache policy #{name} | grep Installed | grep '(none)' | wc -l) -eq 0"
|
43
|
+
],
|
53
44
|
:code => "apt-get install #{name} --yes"
|
54
45
|
}
|
55
46
|
|
@@ -68,7 +59,7 @@ DATA
|
|
68
59
|
CMD
|
69
60
|
|
70
61
|
steps << {
|
71
|
-
:check => "
|
62
|
+
:check => %Q("\\$(echo -n $data | base64 -i -d | md5sum - | awk '{print $1}')" = "\\$(crontab -u #{name} -l | md5sum - | awk '{print $1}')"),
|
72
63
|
:code => "echo -n $data | base64 -i -d | crontab -u #{name} -"
|
73
64
|
}
|
74
65
|
|
@@ -80,10 +71,10 @@ CMD
|
|
80
71
|
content = Gluez::Erb::Engine.parse(raw, opts[:vars, {}])
|
81
72
|
|
82
73
|
setup = <<-CMD
|
83
|
-
|
74
|
+
su -l #{opts[:user, 'root']} -c "cat >~/.gluez_transfer <<\\DATA
|
84
75
|
#{content.strip}
|
85
76
|
DATA
|
86
|
-
|
77
|
+
"
|
87
78
|
CMD
|
88
79
|
|
89
80
|
steps << {
|
@@ -92,18 +83,13 @@ CMD
|
|
92
83
|
}
|
93
84
|
|
94
85
|
steps << {
|
95
|
-
:check => "
|
86
|
+
:check => %Q("\\$(stat -L --format=%a #{name})" = "#{opts[:chmod, "644"]}"),
|
96
87
|
:code => "chmod #{opts[:chmod, "644"]} #{name}"
|
97
88
|
}
|
98
89
|
|
99
90
|
steps << {
|
100
|
-
:check => "
|
101
|
-
:code => "
|
102
|
-
}
|
103
|
-
|
104
|
-
steps << {
|
105
|
-
:check => "\"$(echo -n $data | base64 -i -d | md5sum - | awk '{print $1}')\" = \"$(md5sum #{name} | awk '{print $1}')\"",
|
106
|
-
:code => "echo -n ${data} | base64 -i -d > #{name}"
|
91
|
+
:check => %Q("\\$(cat ~/.gluez_transfer | base64 -i -d - | md5sum - | awk '{print \\$1}')" = "\\$(md5sum #{name} | awk '{print \\$1}')"),
|
92
|
+
:code => "chmod +w #{name} && cat ~/.gluez_transfer | base64 -i -d - > #{name} && chmod #{opts[:chmod, "644"]} #{name}"
|
107
93
|
}
|
108
94
|
|
109
95
|
when :group
|
@@ -122,14 +108,21 @@ CMD
|
|
122
108
|
setup = "service --status-all 1>/tmp/gluez.tmp 2>&1"
|
123
109
|
|
124
110
|
steps << {
|
125
|
-
:check =>
|
111
|
+
:check => [
|
112
|
+
%Q("\\$(grep #{name} /tmp/gluez.tmp | wc -l)" = "1"),
|
113
|
+
%Q("\\$(service #{name} status | grep -E 'is running|start/running' | wc -l)" = "1")
|
114
|
+
],
|
115
|
+
|
126
116
|
:code => "service #{name} start"
|
127
117
|
}
|
128
118
|
|
129
119
|
when :stop
|
130
120
|
setup = "service --status-all 1>/tmp/gluez.tmp 2>&1"
|
131
121
|
steps << {
|
132
|
-
:check =>
|
122
|
+
:check => [
|
123
|
+
%Q("\\$(grep #{name} /tmp/gluez.tmp | wc -l)" = "1"),
|
124
|
+
%Q("\\$(service #{name} status | grep -E 'is running|start/running' | wc -l)" = "0"),
|
125
|
+
],
|
133
126
|
:code => "service #{name} stop"
|
134
127
|
}
|
135
128
|
|
@@ -137,38 +130,32 @@ CMD
|
|
137
130
|
setup = "service --status-all 1>/tmp/gluez.tmp 2>&1"
|
138
131
|
|
139
132
|
steps << {
|
140
|
-
:check =>
|
141
|
-
:code =>
|
142
|
-
if [[ "$(grep #{name} /tmp/gluez.tmp | wc -l)" = "1" && "$(service #{name} status | grep -E 'is running|start/running' | wc -l)" = "1" ]]; then
|
143
|
-
service #{name} restart
|
144
|
-
else
|
145
|
-
service #{name} start
|
146
|
-
fi
|
147
|
-
CODE
|
133
|
+
:check => '1 -eq 0',
|
134
|
+
:code => "service #{name} restart"
|
148
135
|
}
|
149
136
|
|
150
137
|
when :bash
|
151
138
|
steps << {
|
152
139
|
:check => opts[:not_if],
|
153
|
-
:code =>
|
140
|
+
:code => opts[:code]
|
154
141
|
}
|
155
142
|
|
156
143
|
when :bash_once
|
157
144
|
home_dir = (opts[:user, 'root'] == 'root') ? "/root" : "/home/#{opts[:user, 'root']}"
|
158
145
|
steps << {
|
159
146
|
:check => "-f #{home_dir}/.gluez/#{name}",
|
160
|
-
:code =>
|
147
|
+
:code => opts[:code] + "\n" + "touch #{home_dir}/.gluez/#{name}"
|
161
148
|
}
|
162
149
|
|
163
150
|
when :bundler
|
164
151
|
steps << {
|
165
|
-
:check => "\"
|
166
|
-
:code => "
|
152
|
+
:check => "\"\\$(cd #{name} && bundle check > /dev/null && echo 'up2date')\" = \"up2date\"",
|
153
|
+
:code => "cd #{name} && bundle install"
|
167
154
|
}
|
168
155
|
|
169
156
|
when :umount
|
170
157
|
steps << {
|
171
|
-
:check => "
|
158
|
+
:check => "\\$(mount | grep \"on #{name} type\" | wc -l) -eq 0",
|
172
159
|
:code => "umount #{name}"
|
173
160
|
}
|
174
161
|
|
@@ -179,30 +166,28 @@ CMD
|
|
179
166
|
cmd = "#{cmd} #{opts[:device]} #{name}"
|
180
167
|
|
181
168
|
steps << {
|
182
|
-
:check => "
|
169
|
+
:check => "\\$(mount | grep \"on #{name} type\" | wc -l) -eq 1",
|
183
170
|
:code => cmd
|
184
171
|
}
|
185
172
|
|
186
173
|
when :link
|
187
174
|
steps << {
|
188
|
-
:check =>
|
175
|
+
:check => [
|
176
|
+
"-L #{name}",
|
177
|
+
"\\$(file #{name} | grep \"#{name.gsub('~', '\$HOME')}: symbolic link to \\\\\\`#{opts[:target].gsub('~', '\$HOME')}'\" | wc -l) -eq 1"
|
178
|
+
],
|
189
179
|
:code => "ln -f -s #{opts[:target]} #{name}"
|
190
180
|
}
|
191
181
|
|
192
|
-
steps << {
|
193
|
-
:check => "\"$(stat --format=%G:%U #{name})\" = \"#{opts[:chown]}\"",
|
194
|
-
:code => "chown --no-dereference #{opts[:chown]} #{name}"
|
195
|
-
}
|
196
|
-
|
197
182
|
when :enable
|
198
183
|
steps << {
|
199
|
-
:check => "
|
184
|
+
:check => "\\$(update-rc.d -n -f #{name} remove | grep '/etc/rc' | wc -l) -gt 0",
|
200
185
|
:code => "/usr/sbin/update-rc.d #{name} defaults"
|
201
186
|
}
|
202
187
|
|
203
188
|
when :disable
|
204
189
|
steps << {
|
205
|
-
:check => "
|
190
|
+
:check => "\\$(update-rc.d -n -f #{name} remove | grep '/etc/rc' | wc -l) -eq 0",
|
206
191
|
:code => "/usr/sbin/update-rc.d -f #{name} remove"
|
207
192
|
}
|
208
193
|
|
@@ -211,34 +196,27 @@ CMD
|
|
211
196
|
home_dir = (user != "root") ? "/home/#{opts[:user]}" : "/root"
|
212
197
|
|
213
198
|
steps << {
|
214
|
-
:check => "-d
|
215
|
-
:code => "mkdir
|
199
|
+
:check => "-d ~/tmp",
|
200
|
+
:code => "mkdir ~/tmp"
|
216
201
|
}
|
217
202
|
|
218
203
|
steps << {
|
219
204
|
:check => opts[:test],
|
220
205
|
:code => <<-CODE
|
221
|
-
[ -f
|
222
|
-
[ -d
|
223
|
-
|
224
|
-
wget --no-check-certificate -O #{home_dir}/tmp/#{opts[:filename]} #{name}
|
206
|
+
[ -f ~/tmp/#{opts[:filename]} ] && rm ~/tmp/#{opts[:filename]}
|
207
|
+
[ -d ~/tmp/#{opts[:folder]} ] && rm -rf ~/tmp/#{opts[:folder]}
|
225
208
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
chown -R #{user}:#{user} #{home_dir}/tmp/#{opts[:folder]}
|
231
|
-
|
232
|
-
su -l #{user} -c 'cd #{home_dir}/tmp/#{opts[:folder]} && #{opts[:steps].join(' && ')}'
|
233
|
-
|
234
|
-
chown -R #{user}:#{user} #{home_dir}/tmp/#{opts[:folder].split('/').first}
|
209
|
+
wget --no-check-certificate -O ~/tmp/#{opts[:filename]} #{name}
|
210
|
+
tar -C ~/tmp/ -x#{opts[:filename] =~ /\.tar\.bz2/ ? "j" : "z"}f ~/tmp/#{opts[:filename]}
|
211
|
+
cd ~/tmp/#{opts[:folder]} && #{opts[:steps].join(' && ')}
|
235
212
|
CODE
|
236
213
|
}
|
237
214
|
|
238
215
|
else
|
239
216
|
raise "unsupported task: #{task}, name: #{name}, opts: #{opts.inspect}"
|
240
217
|
end
|
241
|
-
|
218
|
+
|
219
|
+
name = "#{name}@#{opts[:user]}" if opts.key?(:user)
|
242
220
|
|
243
221
|
[name, setup, steps]
|
244
222
|
end
|
data/lib/gluez.rb
CHANGED
@@ -17,7 +17,7 @@ module Gluez
|
|
17
17
|
|
18
18
|
lines = []
|
19
19
|
lines << "#!/bin/bash"
|
20
|
-
lines << "set -e"
|
20
|
+
# lines << "set -e"
|
21
21
|
|
22
22
|
entries.each do |entry|
|
23
23
|
|
@@ -27,6 +27,18 @@ module Gluez
|
|
27
27
|
opts = OptionWrapper.new(opts)
|
28
28
|
|
29
29
|
name, setup, steps = Gluez::Impl::Linux.steps(cfg, task, name, opts)
|
30
|
+
|
31
|
+
steps.each do |step|
|
32
|
+
if step[:check].is_a?(Array)
|
33
|
+
step[:check].each_with_index do |check, idx|
|
34
|
+
step[:check][idx] = step[:check][idx].gsub("\"", "\\\"")
|
35
|
+
end
|
36
|
+
else
|
37
|
+
step[:check] = step[:check].gsub("\"", "\\\"")
|
38
|
+
end
|
39
|
+
step[:code] = step[:code].gsub("\"", "\\\"")
|
40
|
+
end
|
41
|
+
|
30
42
|
entry[1] = name
|
31
43
|
|
32
44
|
fun = function_name(task, name)
|
@@ -47,9 +59,9 @@ module Gluez
|
|
47
59
|
end
|
48
60
|
|
49
61
|
if cfg[:simulate]
|
50
|
-
generate_simulate(lines, fun, steps, notifies)
|
62
|
+
generate_simulate(lines, opts[:user, 'root'], fun, steps, notifies)
|
51
63
|
else
|
52
|
-
generate_execute(lines, fun, steps, notifies)
|
64
|
+
generate_execute(lines, opts[:user, 'root'], fun, steps, notifies)
|
53
65
|
end
|
54
66
|
|
55
67
|
lines << "}"
|
@@ -72,9 +84,11 @@ module Gluez
|
|
72
84
|
"#{task}_#{name}".gsub('/', '_').gsub(':', '_').gsub("@", "_").gsub("~", "_").gsub(".", "_")
|
73
85
|
end
|
74
86
|
|
75
|
-
def self.generate_simulate(lines, fun, steps, notifies)
|
87
|
+
def self.generate_simulate(lines, user, fun, steps, notifies)
|
76
88
|
|
77
|
-
lines <<
|
89
|
+
lines << code_check(steps, user, true)
|
90
|
+
|
91
|
+
lines << "if [[ $? -eq 0 ]]; then"
|
78
92
|
lines << " echo \"[ up2date ] #{fun}\""
|
79
93
|
lines << "else"
|
80
94
|
lines << " echo \"[ not up2date ] #{fun}\""
|
@@ -85,20 +99,45 @@ module Gluez
|
|
85
99
|
|
86
100
|
lines << "fi"
|
87
101
|
end
|
88
|
-
|
89
|
-
def self.
|
102
|
+
|
103
|
+
def self.code_check(steps, user, applied)
|
104
|
+
steps.collect do |step|
|
105
|
+
|
106
|
+
if step[:check].is_a?(Array)
|
107
|
+
|
108
|
+
step[:check].collect do |check|
|
109
|
+
"su -l #{user} -c \"test " + check + "\""
|
110
|
+
end.join(" && ")
|
111
|
+
|
112
|
+
else
|
113
|
+
"su -l #{user} -c \"test " + step[:check] + "\"" unless (applied && step[:check] == '1 -eq 0')
|
114
|
+
end
|
115
|
+
|
116
|
+
end.join(" && ")
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.generate_execute(lines, user, fun, steps, notifies)
|
120
|
+
|
121
|
+
lines << code_check(steps, user, false)
|
90
122
|
|
91
|
-
lines << "if [[
|
123
|
+
lines << "if [[ $? -eq 0 ]]; then"
|
92
124
|
lines << " echo \"[ up2date ] #{fun}\""
|
93
125
|
lines << "else"
|
94
126
|
|
95
127
|
if steps.length > 1
|
96
128
|
(0..steps.length-1).to_a.each do |limit|
|
97
|
-
|
129
|
+
|
130
|
+
lines << code_check(steps[0..limit], user, false)
|
131
|
+
|
132
|
+
lines << "if [[ $? -eq 0 ]]; then"
|
98
133
|
lines << " echo \"[ up2date ] #{fun}\""
|
99
134
|
lines << "else"
|
100
|
-
|
101
|
-
lines << "
|
135
|
+
|
136
|
+
lines << " su -l #{user} -c \"#{steps[limit][:code]}\""
|
137
|
+
|
138
|
+
lines << code_check(steps[0..limit], user, true)
|
139
|
+
|
140
|
+
lines << " if [[ $? -eq 0 ]]; then"
|
102
141
|
lines << " echo \"[ applied ] #{fun}\""
|
103
142
|
lines << " else"
|
104
143
|
lines << " echo \"[ not applied ] #{fun}\""
|
@@ -107,8 +146,11 @@ module Gluez
|
|
107
146
|
lines << "fi"
|
108
147
|
end
|
109
148
|
else
|
110
|
-
lines << " #{steps[0][:code]}"
|
111
|
-
|
149
|
+
lines << " su -l #{user} -c \"#{steps[0][:code]}\""
|
150
|
+
|
151
|
+
lines << code_check([steps[0]], user, true)
|
152
|
+
|
153
|
+
lines << " if [[ $? -eq 0 ]]; then"
|
112
154
|
lines << " echo \"[ applied ] #{fun}\""
|
113
155
|
lines << " else"
|
114
156
|
lines << " echo \"[ not applied ] #{fun}\""
|
@@ -122,5 +164,5 @@ module Gluez
|
|
122
164
|
|
123
165
|
lines << "fi"
|
124
166
|
end
|
125
|
-
|
167
|
+
|
126
168
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: gluez
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: "0.2"
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jan Zimmek
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05
|
13
|
+
date: 2011-06-05 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|