gluez 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,34 +8,50 @@ module Gluez
8
8
 
9
9
  case task
10
10
  when :gem
11
- steps << "$(su -l #{opts[:user]} -c \"gem list | awk '{print \\$1}' | grep ^#{name}$ | wc -l\") -eq 1"
12
- steps << "su -l #{opts[:user]} -c \"gem install #{name} --version #{opts[:version]} --user-install --no-rdoc --no-ri\""
11
+ steps << {
12
+ :check => "$(su -l #{opts[:user]} -c \"gem list | awk '{print \\$1}' | grep ^#{name}$ | wc -l\") -eq 1",
13
+ :code => "su -l #{opts[:user]} -c \"gem install #{name} --version #{opts[:version]} --user-install --no-rdoc --no-ri\""
14
+ }
13
15
 
14
16
  name = "#{name}@#{opts[:user]}"
15
17
 
16
18
  when :file
17
- steps << "-f #{name}"
18
- steps << "touch #{name}"
19
-
20
- steps << "\"$(stat -L --format=%a #{name})\" = \"#{opts[:chmod, "644"]}\""
21
- steps << "chmod #{opts[:chmod, "644"]} #{name}"
22
-
23
- steps << "\"$(stat -L --format=%G:%U #{name})\" = \"#{opts[:chown, "root:root"]}\""
24
- steps << "chown --no-dereference #{opts[:chown, "root:root"]} #{name}"
19
+ steps << {
20
+ :check => "-f #{name}",
21
+ :code => "touch #{name}"
22
+ }
23
+
24
+ steps << {
25
+ :check => "\"$(stat -L --format=%a #{name})\" = \"#{opts[:chmod, "644"]}\"",
26
+ :code => "chmod #{opts[:chmod, "644"]} #{name}"
27
+ }
28
+
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
+ }
25
33
 
26
34
  when :dir
27
- steps << "-d #{name}"
28
- steps << "mkdir #{name}"
35
+ steps << {
36
+ :check => "-d #{name}",
37
+ :code => "mkdir #{name}"
38
+ }
29
39
 
30
- steps << "\"$(stat -L --format=%a #{name})\" = \"#{opts[:chmod, "755"]}\""
31
- steps << "chmod #{opts[:chmod, "755"]} #{name}"
40
+ steps << {
41
+ :check => "\"$(stat -L --format=%a #{name})\" = \"#{opts[:chmod, "755"]}\"",
42
+ :code => "chmod #{opts[:chmod, "755"]} #{name}"
43
+ }
32
44
 
33
- steps << "\"$(stat -L --format=%G:%U #{name})\" = \"#{opts[:chown, "root:root"]}\""
34
- steps << "chown --no-dereference #{opts[:chown, "root:root"]} #{name}"
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
+ }
35
49
 
36
50
  when :package
37
- steps << "$(apt-cache policy #{name} | grep Installed | wc -l) -eq 1 && $(apt-cache policy #{name} | grep Installed | grep '(none)' | wc -l) -eq 0"
38
- steps << "apt-get install #{name} --yes"
51
+ steps << {
52
+ :check => "$(apt-cache policy #{name} | grep Installed | wc -l) -eq 1 && $(apt-cache policy #{name} | grep Installed | grep '(none)' | wc -l) -eq 0",
53
+ :code => "apt-get install #{name} --yes"
54
+ }
39
55
 
40
56
  when :transfer
41
57
  raw = File.read(cfg[:file_dir] + "/files/" + opts[:source])
@@ -49,90 +65,124 @@ DATA
49
65
  )
50
66
  CMD
51
67
 
52
- steps << "-f #{name}"
53
- steps << "touch #{name}"
68
+ steps << {
69
+ :check => "-f #{name}",
70
+ :code => "touch #{name}"
71
+ }
54
72
 
55
- steps << "\"$(stat -L --format=%a #{name})\" = \"#{opts[:chmod, "644"]}\""
56
- steps << "chmod #{opts[:chmod, "644"]} #{name}"
73
+ steps << {
74
+ :check => "\"$(stat -L --format=%a #{name})\" = \"#{opts[:chmod, "644"]}\"",
75
+ :code => "chmod #{opts[:chmod, "644"]} #{name}"
76
+ }
57
77
 
58
- steps << "\"$(stat -L --format=%G:%U #{name})\" = \"#{opts[:chown, "root:root"]}\""
59
- steps << "chown --no-dereference #{opts[:chown, "root:root"]} #{name}"
78
+ steps << {
79
+ :check => "\"$(stat -L --format=%G:%U #{name})\" = \"#{opts[:chown, "root:root"]}\"",
80
+ :code => "chown --no-dereference #{opts[:chown, "root:root"]} #{name}"
81
+ }
60
82
 
61
- steps << "\"$(echo -n $data | base64 -i -d | md5sum - | awk '{print $1}')\" = \"$(md5sum #{name} | awk '{print $1}')\""
62
- steps << "echo -n ${data} | base64 -i -d > #{name}"
83
+ steps << {
84
+ :check => "\"$(echo -n $data | base64 -i -d | md5sum - | awk '{print $1}')\" = \"$(md5sum #{name} | awk '{print $1}')\"",
85
+ :code => "echo -n ${data} | base64 -i -d > #{name}"
86
+ }
63
87
 
64
88
  when :group
65
- steps << "$(cat /etc/group | grep ^#{name}: | wc -l) -eq 1"
66
- steps << "groupadd --gid #{opts[:gid]} #{name}"
89
+ steps << {
90
+ :check => "$(cat /etc/group | grep ^#{name}: | wc -l) -eq 1",
91
+ :code => "groupadd --gid #{opts[:gid]} #{name}"
92
+ }
67
93
 
68
94
  when :user
69
- steps << "$(cat /etc/passwd | grep ^#{name}: | wc -l) -eq 1"
70
- steps << "useradd --create-home --uid #{opts[:uid]} --gid #{opts[:gid]} --shell #{opts[:shell, "/bin/bash"]} #{name}"
95
+ steps << {
96
+ :check => "$(cat /etc/passwd | grep ^#{name}: | wc -l) -eq 1",
97
+ :code => "useradd --create-home --uid #{opts[:uid]} --gid #{opts[:gid]} --shell #{opts[:shell, "/bin/bash"]} #{name}"
98
+ }
71
99
 
72
100
  when :start
73
101
  setup = "service --status-all 1>/tmp/gluez.tmp 2>&1"
74
- steps << "\"$(grep #{name} /tmp/gluez.tmp | wc -l)\" = \"1\" && \"$(service #{name} status | grep -E 'is running|start/running' | wc -l)\" = \"1\""
75
- steps << "service #{name} start"
102
+
103
+ steps << {
104
+ :check => "\"$(grep #{name} /tmp/gluez.tmp | wc -l)\" = \"1\" && \"$(service #{name} status | grep -E 'is running|start/running' | wc -l)\" = \"1\"",
105
+ :code => "service #{name} start"
106
+ }
76
107
 
77
108
  when :stop
78
109
  setup = "service --status-all 1>/tmp/gluez.tmp 2>&1"
79
- steps << "\"$(grep #{name} /tmp/gluez.tmp | wc -l)\" = \"1\" && \"$(service #{name} status | grep -E 'is running|start/running' | wc -l)\" = \"0\""
80
- steps << "service #{name} stop"
110
+ steps << {
111
+ :check => "\"$(grep #{name} /tmp/gluez.tmp | wc -l)\" = \"1\" && \"$(service #{name} status | grep -E 'is running|start/running' | wc -l)\" = \"0\"",
112
+ :code => "service #{name} stop"
113
+ }
81
114
 
82
115
  when :restart
83
116
  setup = "service --status-all 1>/tmp/gluez.tmp 2>&1"
84
- steps << "1 -eq 0"
85
- steps << <<-CODE
86
- if [[ "$(grep #{name} /tmp/gluez.tmp | wc -l)" = "1" && "$(service #{name} status | grep -E 'is running|start/running' | wc -l)" = "1" ]]; then
87
- service #{name} restart
88
- else
89
- service #{name} start
90
- fi
91
- CODE
117
+
118
+ steps << {
119
+ :check => :false,
120
+ :code => <<-CODE
121
+ if [[ "$(grep #{name} /tmp/gluez.tmp | wc -l)" = "1" && "$(service #{name} status | grep -E 'is running|start/running' | wc -l)" = "1" ]]; then
122
+ service #{name} restart
123
+ else
124
+ service #{name} start
125
+ fi
126
+ CODE
127
+ }
92
128
 
93
129
  when :bash
94
- steps << opts[:not_if]
95
- steps << "su -l #{opts[:user, 'root']} -c '#{opts[:code]}'"
130
+ steps << {
131
+ :check => opts[:not_if],
132
+ :code => "su -l #{opts[:user, 'root']} -c '#{opts[:code]}'"
133
+ }
96
134
 
97
135
  when :link
98
- steps << "-L #{name} && $(file #{name} | grep \"#{name}: symbolic link to \\`#{opts[:target]}'\" | wc -l) -eq 1"
99
- steps << "ln -f -s #{opts[:target]} #{name}"
136
+ steps << {
137
+ :check => "-L #{name} && $(file #{name} | grep \"#{name}: symbolic link to \\`#{opts[:target]}'\" | wc -l) -eq 1",
138
+ :code => "ln -f -s #{opts[:target]} #{name}"
139
+ }
100
140
 
101
- steps << "\"$(stat --format=%G:%U #{name})\" = \"#{opts[:chown]}\""
102
- steps << "chown --no-dereference #{opts[:chown]} #{name}"
141
+ steps << {
142
+ :check => "\"$(stat --format=%G:%U #{name})\" = \"#{opts[:chown]}\"",
143
+ :code => "chown --no-dereference #{opts[:chown]} #{name}"
144
+ }
103
145
 
104
146
  when :enable
105
- steps << "$(update-rc.d -n -f #{name} remove | grep '/etc/rc' | wc -l) -gt 0"
106
- steps << "/usr/sbin/update-rc.d #{name} defaults"
147
+ steps << {
148
+ :check => "$(update-rc.d -n -f #{name} remove | grep '/etc/rc' | wc -l) -gt 0",
149
+ :code => "/usr/sbin/update-rc.d #{name} defaults"
150
+ }
107
151
 
108
152
  when :disable
109
- steps << "$(update-rc.d -n -f #{name} remove | grep '/etc/rc' | wc -l) -eq 0"
110
- steps << "/usr/sbin/update-rc.d -f #{name} remove"
153
+ steps << {
154
+ :check => "$(update-rc.d -n -f #{name} remove | grep '/etc/rc' | wc -l) -eq 0",
155
+ :code => "/usr/sbin/update-rc.d -f #{name} remove"
156
+ }
111
157
 
112
158
  when :source
113
159
  user = opts[:user, "root"]
114
160
  home_dir = (user != "root") ? "/home/#{opts[:user]}" : "/root"
115
161
 
116
- steps << "-d #{home_dir}/tmp"
117
- steps << "mkdir #{home_dir}/tmp && chown #{user}:#{user} #{home_dir}/tmp"
162
+ steps << {
163
+ :check => "-d #{home_dir}/tmp",
164
+ :code => "mkdir #{home_dir}/tmp && chown #{user}:#{user} #{home_dir}/tmp"
165
+ }
118
166
 
119
- steps << "-f #{opts[:binary]}"
120
- steps << <<-CODE
121
- [ -f #{home_dir}/tmp/#{opts[:filename]} ] && rm #{home_dir}/tmp/#{opts[:filename]}
122
- [ -d #{home_dir}/tmp/#{opts[:folder]} ] && rm -rf #{home_dir}/tmp/#{opts[:folder]}
167
+ steps << {
168
+ :check => "-f #{opts[:binary]}",
169
+ :code => <<-CODE
170
+ [ -f #{home_dir}/tmp/#{opts[:filename]} ] && rm #{home_dir}/tmp/#{opts[:filename]}
171
+ [ -d #{home_dir}/tmp/#{opts[:folder]} ] && rm -rf #{home_dir}/tmp/#{opts[:folder]}
123
172
 
124
- wget --no-check-certificate -O #{home_dir}/tmp/#{opts[:filename]} #{name}
173
+ wget --no-check-certificate -O #{home_dir}/tmp/#{opts[:filename]} #{name}
125
174
 
126
- chown #{user}:#{user} #{home_dir}/tmp/#{opts[:filename]}
175
+ chown #{user}:#{user} #{home_dir}/tmp/#{opts[:filename]}
127
176
 
128
- tar -C #{home_dir}/tmp/ -x#{opts[:filename] =~ /\.tar\.bz2/ ? "j" : "z"}f #{home_dir}/tmp/#{opts[:filename]}
177
+ tar -C #{home_dir}/tmp/ -x#{opts[:filename] =~ /\.tar\.bz2/ ? "j" : "z"}f #{home_dir}/tmp/#{opts[:filename]}
129
178
 
130
- chown -R #{user}:#{user} #{home_dir}/tmp/#{opts[:folder]}
179
+ chown -R #{user}:#{user} #{home_dir}/tmp/#{opts[:folder]}
131
180
 
132
- su -l #{user} -c 'cd #{home_dir}/tmp/#{opts[:folder]} && #{opts[:steps].join(' && ')}'
181
+ su -l #{user} -c 'cd #{home_dir}/tmp/#{opts[:folder]} && #{opts[:steps].join(' && ')}'
133
182
 
134
- chown -R #{user}:#{user} #{home_dir}/tmp/#{opts[:folder].split('/').first}
135
- CODE
183
+ chown -R #{user}:#{user} #{home_dir}/tmp/#{opts[:folder].split('/').first}
184
+ CODE
185
+ }
136
186
 
137
187
  else
138
188
  raise "unsupported task: #{task}, name: #{name}, opts: #{opts.inspect}"
@@ -1,34 +1,34 @@
1
- module Gluez
2
- module Impl
3
- class Mac
4
- def self.steps(cfg, task, name, opts)
5
-
6
- setup = nil
7
- steps = []
8
-
9
- case task
10
-
11
- when :group
12
- steps << "$(/usr/bin/dscl . -list Groups | grep ^#{name}$ | wc -l) -eq 1"
13
- steps << "$(/usr/bin/dscl . -create /Groups/#{name} PrimaryGroupID #{opts[:gid]})"
14
-
15
- when :user
16
- steps << "$(/usr/bin/dscl . -list Users | grep ^#{name}$ | wc -l) -eq 1"
17
- steps << "$(/usr/bin/dscl . -create /Users/#{name} UniqueID #{opts[:uid]} PrimaryGroupID #{opts[:gid]} UserShell #{opts[:shell, "/bin/bash"]} NFSHomeDir /Users/#{name})"
18
-
19
- steps << "-d /Users/#{name}"
20
- steps << "mkdir /Users/#{name}"
21
-
22
- steps << "\"$(stat -L -f %Sg:%Su /Users/#{name})\" = \"#{name}:#{name}\""
23
- steps << "chown #{name}:#{name} /Users/#{name}"
24
-
25
- else
26
- raise "unsupported task: #{task}, name: #{name}, opts: #{opts.inspect}"
27
- end
28
-
29
-
30
- [name, setup, steps]
31
- end
32
- end
33
- end
34
- end
1
+ # module Gluez
2
+ # module Impl
3
+ # class Mac
4
+ # def self.steps(cfg, task, name, opts)
5
+ #
6
+ # setup = nil
7
+ # steps = []
8
+ #
9
+ # case task
10
+ #
11
+ # when :group
12
+ # steps << "$(/usr/bin/dscl . -list Groups | grep ^#{name}$ | wc -l) -eq 1"
13
+ # steps << "$(/usr/bin/dscl . -create /Groups/#{name} PrimaryGroupID #{opts[:gid]})"
14
+ #
15
+ # when :user
16
+ # steps << "$(/usr/bin/dscl . -list Users | grep ^#{name}$ | wc -l) -eq 1"
17
+ # steps << "$(/usr/bin/dscl . -create /Users/#{name} UniqueID #{opts[:uid]} PrimaryGroupID #{opts[:gid]} UserShell #{opts[:shell, "/bin/bash"]} NFSHomeDir /Users/#{name})"
18
+ #
19
+ # steps << "-d /Users/#{name}"
20
+ # steps << "mkdir /Users/#{name}"
21
+ #
22
+ # steps << "\"$(stat -L -f %Sg:%Su /Users/#{name})\" = \"#{name}:#{name}\""
23
+ # steps << "chown #{name}:#{name} /Users/#{name}"
24
+ #
25
+ # else
26
+ # raise "unsupported task: #{task}, name: #{name}, opts: #{opts.inspect}"
27
+ # end
28
+ #
29
+ #
30
+ # [name, setup, steps]
31
+ # end
32
+ # end
33
+ # end
34
+ # end
data/lib/gluez.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'json'
2
-
3
1
  require 'gluez/ext/string'
4
2
 
5
3
  require 'gluez/option_wrapper'
@@ -7,19 +5,18 @@ require 'gluez/formatter'
7
5
  require 'gluez/erb/engine'
8
6
 
9
7
  require 'gluez/impl/linux'
10
- # require 'gluez/impl/mac'
11
8
 
12
9
  module Gluez
13
10
 
14
11
  def self.run(entries, cfg={})
15
12
 
16
13
  cfg = {
17
- :file_dir => "."
14
+ :file_dir => ".",
15
+ :simulate => false
18
16
  }.merge(cfg)
19
17
 
20
18
  lines = []
21
19
  lines << "#!/bin/bash"
22
- # lines << "set -x"
23
20
  lines << "set -e"
24
21
 
25
22
  entries.each do |entry|
@@ -48,8 +45,12 @@ module Gluez
48
45
  entries << [notify_task, notify_name, {:embedded => true}]
49
46
  end
50
47
  end
51
-
52
- add_steps(true, lines, [], [fun, steps], notifies)
48
+
49
+ if cfg[:simulate]
50
+ generate_simulate(lines, fun, steps, notifies)
51
+ else
52
+ generate_execute(lines, fun, steps, notifies)
53
+ end
53
54
 
54
55
  lines << "}"
55
56
 
@@ -61,7 +62,6 @@ module Gluez
61
62
 
62
63
  lines << function_name(task, name) unless opts[:embedded]
63
64
  end
64
-
65
65
 
66
66
  Formatter.format(lines.join("\n"))
67
67
  end
@@ -72,58 +72,55 @@ module Gluez
72
72
  "#{task}_#{name}".gsub('/', '_').gsub(':', '_').gsub("@", "_").gsub("~", "_").gsub(".", "_")
73
73
  end
74
74
 
75
- def self.add_steps(first, lines, completed, steps_wrapper, notifies)
75
+ def self.generate_simulate(lines, fun, steps, notifies)
76
+
77
+ lines << "if [[ #{steps.collect{|s| s[:check] == :false ? '1 -eq 0' : s[:check]}.join(' && ')} ]]; then"
78
+ lines << " echo \"[ up2date ] #{fun}\""
79
+ lines << "else"
80
+ lines << " echo \"[ not up2date ] #{fun}\""
76
81
 
77
- fun, steps = steps_wrapper
82
+ notifies.each do |notify|
83
+ lines << notify
84
+ end
78
85
 
79
- if first
80
- if steps.length == 2
81
- add_steps(false, lines, [], [fun, steps], notifies)
82
- else
83
- checks = []
84
- steps.each_with_index do |step, idx|
85
- checks << step if idx % 2 == 0
86
- end
86
+ lines << "fi"
87
+ end
88
+
89
+ def self.generate_execute(lines, fun, steps, notifies)
90
+
91
+ lines << "if [[ #{steps.collect{|s| s[:check] == :false ? '1 -eq 0' : s[:check]}.join(' && ')} ]]; then"
92
+ lines << " echo \"[ up2date ] #{fun}\""
93
+ lines << "else"
87
94
 
88
- lines << "if [[ #{checks.join(' && ')} ]]; then"
89
- lines << " echo \"[ #{fun} ] up2date\""
95
+ if steps.length > 1
96
+ (0..steps.length-1).to_a.each do |limit|
97
+ lines << "if [[ #{steps[0..limit].collect{|s| s[:check] == :false ? '1 -eq 0' : s[:check]}.join(' && ')} ]]; then"
98
+ lines << " echo \"[ up2date ] #{fun}\""
90
99
  lines << "else"
91
- lines << " echo \"[ #{fun} ] NOT up2date\""
92
- add_steps(false, lines, [], [fun, steps], notifies)
100
+ lines << " #{steps[limit][:code]}"
101
+ lines << " if [[ #{steps[0..limit].collect{|s| s[:check] == :false ? '1 -eq 0' : s[:check]}.join(' && ')} ]]; then"
102
+ lines << " echo \"[ applied ] #{fun}\""
103
+ lines << " else"
104
+ lines << " echo \"[ not applied ] #{fun}\""
105
+ lines << " exit 1"
106
+ lines << " fi"
93
107
  lines << "fi"
94
108
  end
95
-
96
109
  else
97
- check, code = steps.slice!(0,2)
98
-
99
- if check == "1 -eq 0"
100
- completed << "1 -eq 1"
101
- else
102
- completed << check
103
- end
104
-
105
- lines << "if [[ #{check} ]]; then"
106
- lines << " echo \"[ #{fun} ] up2date\""
107
- lines << "else"
108
- lines << " #{code}"
109
- lines << " if [[ #{completed.join(' && ')} ]]; then"
110
- lines << " echo \"[ #{fun} ] applied\""
110
+ lines << " #{steps[0][:code]}"
111
+ lines << " if [[ #{steps[0][:check] == :false ? '1 -eq 1' : steps[0][:check]} ]]; then"
112
+ lines << " echo \"[ applied ] #{fun}\""
111
113
  lines << " else"
114
+ lines << " echo \"[ not applied ] #{fun}\""
112
115
  lines << " exit 1"
113
116
  lines << " fi"
114
-
115
- lines << "fi"
116
-
117
- if steps.empty?
118
- notifies.each do |notify|
119
- lines << notify
120
- end
121
- else
122
- add_steps(false, lines, completed, [fun, steps], notifies)
123
- end
124
117
  end
125
118
 
119
+ notifies.each do |notify|
120
+ lines << notify
121
+ end
126
122
 
123
+ lines << "fi"
127
124
  end
128
125
 
129
126
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gluez
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.1"
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jan Zimmek
@@ -31,7 +31,6 @@ files:
31
31
  - lib/gluez/impl/mac.rb
32
32
  - lib/gluez/option_wrapper.rb
33
33
  - lib/gluez.rb
34
- - bin/gluez
35
34
  has_rdoc: true
36
35
  homepage:
37
36
  licenses: []
data/bin/gluez DELETED
@@ -1,3 +0,0 @@
1
- #!/bin/bash
2
-
3
- # /usr/bin/env ruby "require gluez; Gluez::run"