angry_mob_common_targets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/LICENSE +21 -0
  2. data/README.md +38 -0
  3. data/lib/common_mob.rb +9 -0
  4. data/lib/common_mob/digest.rb +43 -0
  5. data/lib/common_mob/erb.rb +72 -0
  6. data/lib/common_mob/file.rb +55 -0
  7. data/lib/common_mob/patch.rb +51 -0
  8. data/lib/common_mob/resource_locator.rb +9 -0
  9. data/lib/common_mob/shell.rb +323 -0
  10. data/lib/common_mob/template.rb +23 -0
  11. data/lib/common_mob/version.rb +3 -0
  12. data/targets/crontab_patch.rb +37 -0
  13. data/targets/extract.rb +40 -0
  14. data/targets/fetch.rb +40 -0
  15. data/targets/files.rb +244 -0
  16. data/targets/git.rb +84 -0
  17. data/targets/group.rb +33 -0
  18. data/targets/packages.rb +94 -0
  19. data/targets/ruby.rb +13 -0
  20. data/targets/services.rb +184 -0
  21. data/targets/shell.rb +43 -0
  22. data/targets/user.rb +108 -0
  23. data/vendor/mustache/CONTRIBUTORS +9 -0
  24. data/vendor/mustache/HISTORY.md +135 -0
  25. data/vendor/mustache/LICENSE +20 -0
  26. data/vendor/mustache/README.md +405 -0
  27. data/vendor/mustache/Rakefile +103 -0
  28. data/vendor/mustache/benchmarks/complex.erb +15 -0
  29. data/vendor/mustache/benchmarks/complex.haml +12 -0
  30. data/vendor/mustache/benchmarks/helper.rb +20 -0
  31. data/vendor/mustache/benchmarks/simple.erb +5 -0
  32. data/vendor/mustache/benchmarks/speed.rb +78 -0
  33. data/vendor/mustache/bin/mustache +90 -0
  34. data/vendor/mustache/contrib/mustache-mode.el +278 -0
  35. data/vendor/mustache/contrib/mustache.vim +69 -0
  36. data/vendor/mustache/examples/hash.rb +16 -0
  37. data/vendor/mustache/examples/hash.yml +5 -0
  38. data/vendor/mustache/examples/projects.mustache +26 -0
  39. data/vendor/mustache/examples/projects.yml +28 -0
  40. data/vendor/mustache/examples/self.mustache +4 -0
  41. data/vendor/mustache/examples/self.yml +3 -0
  42. data/vendor/mustache/examples/simple.mustache +10 -0
  43. data/vendor/mustache/examples/simple.rb +24 -0
  44. data/vendor/mustache/lib/mustache.rb +358 -0
  45. data/vendor/mustache/lib/mustache/context.rb +108 -0
  46. data/vendor/mustache/lib/mustache/generator.rb +160 -0
  47. data/vendor/mustache/lib/mustache/parser.rb +230 -0
  48. data/vendor/mustache/lib/mustache/sinatra.rb +180 -0
  49. data/vendor/mustache/lib/mustache/template.rb +59 -0
  50. data/vendor/mustache/lib/mustache/version.rb +3 -0
  51. data/vendor/mustache/lib/rack/bug/panels/mustache_panel.rb +81 -0
  52. data/vendor/mustache/lib/rack/bug/panels/mustache_panel/mustache_extension.rb +27 -0
  53. data/vendor/mustache/lib/rack/bug/panels/mustache_panel/view.mustache +46 -0
  54. data/vendor/mustache/man/mustache.1 +180 -0
  55. data/vendor/mustache/man/mustache.1.html +204 -0
  56. data/vendor/mustache/man/mustache.1.ron +127 -0
  57. data/vendor/mustache/man/mustache.5 +576 -0
  58. data/vendor/mustache/man/mustache.5.html +415 -0
  59. data/vendor/mustache/man/mustache.5.ron +324 -0
  60. data/vendor/mustache/mustache.gemspec +32 -0
  61. data/vendor/mustache/test/autoloading_test.rb +52 -0
  62. data/vendor/mustache/test/fixtures/comments.mustache +1 -0
  63. data/vendor/mustache/test/fixtures/comments.rb +14 -0
  64. data/vendor/mustache/test/fixtures/complex_view.mustache +17 -0
  65. data/vendor/mustache/test/fixtures/complex_view.rb +34 -0
  66. data/vendor/mustache/test/fixtures/crazy_recursive.mustache +9 -0
  67. data/vendor/mustache/test/fixtures/crazy_recursive.rb +31 -0
  68. data/vendor/mustache/test/fixtures/delimiters.mustache +8 -0
  69. data/vendor/mustache/test/fixtures/delimiters.rb +23 -0
  70. data/vendor/mustache/test/fixtures/double_section.mustache +7 -0
  71. data/vendor/mustache/test/fixtures/double_section.rb +14 -0
  72. data/vendor/mustache/test/fixtures/escaped.mustache +1 -0
  73. data/vendor/mustache/test/fixtures/escaped.rb +14 -0
  74. data/vendor/mustache/test/fixtures/inner_partial.mustache +1 -0
  75. data/vendor/mustache/test/fixtures/inner_partial.txt +1 -0
  76. data/vendor/mustache/test/fixtures/inverted_section.mustache +7 -0
  77. data/vendor/mustache/test/fixtures/inverted_section.rb +14 -0
  78. data/vendor/mustache/test/fixtures/lambda.mustache +7 -0
  79. data/vendor/mustache/test/fixtures/lambda.rb +31 -0
  80. data/vendor/mustache/test/fixtures/namespaced.mustache +1 -0
  81. data/vendor/mustache/test/fixtures/namespaced.rb +25 -0
  82. data/vendor/mustache/test/fixtures/nested_objects.mustache +17 -0
  83. data/vendor/mustache/test/fixtures/nested_objects.rb +35 -0
  84. data/vendor/mustache/test/fixtures/node.mustache +8 -0
  85. data/vendor/mustache/test/fixtures/partial_with_module.mustache +3 -0
  86. data/vendor/mustache/test/fixtures/partial_with_module.rb +37 -0
  87. data/vendor/mustache/test/fixtures/passenger.conf +5 -0
  88. data/vendor/mustache/test/fixtures/passenger.rb +27 -0
  89. data/vendor/mustache/test/fixtures/recursive.mustache +4 -0
  90. data/vendor/mustache/test/fixtures/recursive.rb +14 -0
  91. data/vendor/mustache/test/fixtures/simple.mustache +5 -0
  92. data/vendor/mustache/test/fixtures/simple.rb +26 -0
  93. data/vendor/mustache/test/fixtures/template_partial.mustache +2 -0
  94. data/vendor/mustache/test/fixtures/template_partial.rb +18 -0
  95. data/vendor/mustache/test/fixtures/template_partial.txt +4 -0
  96. data/vendor/mustache/test/fixtures/unescaped.mustache +1 -0
  97. data/vendor/mustache/test/fixtures/unescaped.rb +14 -0
  98. data/vendor/mustache/test/fixtures/utf8.mustache +3 -0
  99. data/vendor/mustache/test/fixtures/utf8_partial.mustache +1 -0
  100. data/vendor/mustache/test/helper.rb +7 -0
  101. data/vendor/mustache/test/mustache_test.rb +536 -0
  102. data/vendor/mustache/test/parser_test.rb +54 -0
  103. data/vendor/mustache/test/partial_test.rb +168 -0
  104. metadata +167 -0
@@ -0,0 +1,23 @@
1
+ require 'common_mob/erb'
2
+ require 'mustache'
3
+
4
+ module CommonMob
5
+ module Mustache
6
+ def render_mustache(src,variables)
7
+ ::Mustache.render(src.read, variables)
8
+ end
9
+ end
10
+
11
+ module Template
12
+ include CommonMob::Mustache
13
+ include CommonMob::Erb
14
+
15
+ def render_template(src,variables)
16
+ if src.to_s[/\.mustache$/]
17
+ render_mustache(src,variables)
18
+ else
19
+ render_erb(src,variables)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module CommonMob
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'common_mob'
2
+
3
+ class Crontab < AngryMob::Target
4
+ include CommonMob::ShellHelper
5
+ include CommonMob::PatchHelper
6
+
7
+ default_action
8
+ def patch
9
+ if before_state[:changed]
10
+ sh("crontab -u #{user} -", :input => new_crontab).run
11
+ end
12
+ end
13
+
14
+ def validate!
15
+ problem!("please specify a patch key as the default_object (crontab('key-something-unique'))") if default_object.blank?
16
+ problem!("please specify a crontab (:crontab => '* * * * * /bin/echo yay')" ) unless args.crontab?
17
+ end
18
+
19
+ def state
20
+ {
21
+ :changed => (new_crontab != crontab)
22
+ }
23
+ end
24
+
25
+ protected
26
+ def new_crontab
27
+ @new_crontab ||= patch_string(crontab, args.crontab, :key => default_object)
28
+ end
29
+
30
+ def crontab
31
+ sh("crontab -l -u #{user}").to_s
32
+ end
33
+
34
+ def user
35
+ args.user || 'root'
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ require 'common_mob'
2
+
3
+ class Tarball < AngryMob::Target
4
+ include CommonMob::ShellHelper
5
+
6
+ default_action
7
+ def extract
8
+ cmd = "tar #{compression_opt}xf #{default_object}"
9
+
10
+ if !dest.blank?
11
+ sh("#{cmd} --strip 1 -C #{dest}").run
12
+ else
13
+ sh("#{cmd}").run
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def compression_opt
20
+ ext = default_object.to_s[/\.([^\.]+)$/,1].downcase
21
+ log "ext=#{ext}"
22
+ case ext
23
+ when 'gz'
24
+ 'z'
25
+ when 'bz2'
26
+ 'j'
27
+ end
28
+ end
29
+
30
+ def dest
31
+ args.dest || args.extract_to || args.to
32
+ end
33
+
34
+ # no really good way to tell if we need to untar again...
35
+ def state
36
+ {
37
+ :rand => rand
38
+ }
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'common_mob'
2
+
3
+ class Fetch < AngryMob::Target
4
+ include CommonMob::ShellHelper
5
+ include CommonMob::DigestHelper
6
+
7
+ default_action
8
+ def fetch
9
+ log "expected_sha=#{expected_sha}"
10
+
11
+ return if before_state[:exists] && (expected_sha && before_state[:sha] == expected_sha)
12
+
13
+ sh("curl #{args.src} -L -o #{default_object}".tapp, :cwd => args.cwd).run
14
+
15
+ if expected_sha && state[:sha] != expected_sha
16
+ raise "downloaded file's sha didn't match expected sha #{expected_sha} != #{state[:sha]}"
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def expected_sha
23
+ [ args.sha, args.sha256, args.sha512 ].find {|s| !s.blank?}
24
+ end
25
+
26
+ def state
27
+ new_sha = if !args.sha.blank?
28
+ sha(default_object)
29
+ elsif !args.sha256.blank?
30
+ sha256(default_object)
31
+ elsif !args.sha512.blank?
32
+ sha512(default_object)
33
+ end
34
+
35
+ {
36
+ :exists => exist?,
37
+ :sha => new_sha
38
+ }
39
+ end
40
+ end
@@ -0,0 +1,244 @@
1
+ require 'common_mob'
2
+ require 'fileutils'
3
+
4
+ module FileHelpers
5
+ include CommonMob::DigestHelper
6
+ include CommonMob::ShellHelper
7
+ include CommonMob::PatchHelper
8
+ include CommonMob::FileHelper
9
+ include CommonMob::Template
10
+
11
+ def set_file_ownership
12
+ args.owner ||= args.user
13
+ args.group ||= args.user
14
+
15
+ set_file_attrs(default_object, args.owner, args.group, args.mode)
16
+ end
17
+ end
18
+
19
+
20
+ class DirTarget < AngryMob::Target
21
+ nickname 'dir'
22
+
23
+ include FileHelpers
24
+ default_action
25
+ def create
26
+ begin
27
+ mkpath unless exist?
28
+ set_file_ownership
29
+ rescue Errno::EEXIST
30
+ # *ulp*
31
+ end
32
+ end
33
+
34
+ def delete
35
+ FileUtils.rm_rf(default_object) if exist?
36
+ end
37
+
38
+ protected
39
+
40
+ def state
41
+ {
42
+ :exists => exist?
43
+ }
44
+ end
45
+
46
+ def default_object
47
+ Pathname(super)
48
+ end
49
+
50
+ def validate!
51
+ end
52
+ end
53
+
54
+ class FileTarget < AngryMob::Target
55
+ nickname 'file'
56
+ include FileHelpers
57
+
58
+ default_action
59
+ def create
60
+ if args.src
61
+ copy_resource resource(args.src)
62
+ elsif args.string
63
+ create_string
64
+ end
65
+
66
+ set_file_ownership
67
+ end
68
+
69
+ def delete
70
+ if exist?
71
+ log "deleting #{default_object}"
72
+ unlink
73
+ end
74
+ end
75
+
76
+ protected
77
+
78
+ def default_object
79
+ Pathname(super)
80
+ end
81
+
82
+ def state
83
+ {
84
+ :sha512 => sha512(default_object)
85
+ }
86
+ end
87
+
88
+ def src
89
+ resource(args.src)
90
+ end
91
+
92
+ def validate!
93
+ if !args.actions.include?('delete')
94
+ if args.src?
95
+ problem!(":src #{src} doesn't exist" ) unless src.exist?
96
+ elsif !args.src? && !args.string?
97
+ problem!("please specify one of :src or :string")
98
+ end
99
+ end
100
+ end
101
+
102
+
103
+ def copy_resource(src)
104
+ if sha512(src) != before_state[:sha512]
105
+ log "input string different from file contents, overwriting"
106
+ src.cp_to(default_object)
107
+ end
108
+ end
109
+
110
+ def create_string
111
+ if sha512(args.string) != before_state[:sha512]
112
+ log "input string different from file contents, overwriting"
113
+ default_object.open('w') {|f| f << args.string}
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ # symlink("from/path", :to => "to/path")
120
+ #
121
+ class Symlink < AngryMob::Target
122
+ include FileHelpers
123
+
124
+ default_action
125
+ def create
126
+ if before_state[:points_to] != to
127
+ log "linking #{from} -> #{to}"
128
+ sh("ln -nfs #{to} #{from}").run
129
+ end
130
+
131
+ ensure_points_to_correct_file!
132
+ end
133
+
134
+ def delete
135
+ end
136
+
137
+ protected
138
+
139
+ def ensure_points_to_correct_file!
140
+ raise "symlink doesn't point #{from} -> #{to} after action" unless state[:points_to] == to
141
+ end
142
+
143
+ def from
144
+ Pathname(default_object).expand_path
145
+ end
146
+ def to
147
+ Pathname(args.to).expand_path.realpath
148
+ end
149
+
150
+ def state
151
+ points_to = begin
152
+ from.readlink
153
+ rescue Errno::ENOENT,Errno::EINVAL
154
+ nil
155
+ end
156
+ {
157
+ :points_to => points_to
158
+ }
159
+ end
160
+
161
+ def validate!
162
+ problem!("file we're linking to doesn't exist") unless to.exist? # is this a real problem?
163
+ end
164
+ end
165
+
166
+
167
+ class Template < AngryMob::Target
168
+ include FileHelpers
169
+
170
+ default_action
171
+ def create
172
+ new_content = render_template(src,variables)
173
+
174
+ if sha512(new_content) != before_state[:sha512]
175
+ log "template has changed, overwriting"
176
+
177
+ backup_file(default_object)
178
+
179
+ default_object.open('w') {|f| f << new_content}
180
+ end
181
+
182
+ set_file_ownership
183
+ end
184
+
185
+ protected
186
+
187
+ def default_object
188
+ Pathname(super)
189
+ end
190
+
191
+ def src
192
+ resource(args.src)
193
+ end
194
+
195
+ def variables
196
+ (args.variables || args.vars || {}).update(:node => node)
197
+ end
198
+
199
+ def state
200
+ {
201
+ :sha512 => sha512(default_object)
202
+ }
203
+ end
204
+
205
+ def validate!
206
+ super
207
+ problem!(":src #{src} doesn't exist" ) unless src.exist?
208
+ problem!(":src #{src} isn't a file" ) unless src.file?
209
+ problem!(":src #{src} isn't readable") unless src.readable?
210
+ end
211
+ end
212
+
213
+ class Patch < AngryMob::Target
214
+ include FileHelpers
215
+
216
+ default_action
217
+ def patch
218
+ log "patchhing"
219
+
220
+ patched = patch_file(default_object)
221
+
222
+ if before_state[:sha512] != sha512(patched)
223
+ log "patch has changed, overwriting"
224
+
225
+ backup_file(default_object)
226
+ default_object.open('w') {|f| f << patched}
227
+ end
228
+ end
229
+
230
+ def unpatch
231
+ end
232
+
233
+ protected
234
+
235
+ def default_object
236
+ Pathname(super)
237
+ end
238
+
239
+ def state
240
+ {
241
+ :sha512 => sha512(default_object)
242
+ }
243
+ end
244
+ end
@@ -0,0 +1,84 @@
1
+ require 'common_mob'
2
+
3
+ class Git < AngryMob::Target
4
+ include CommonMob::ShellHelper
5
+
6
+ default_action
7
+ def sync
8
+ if is_git?
9
+ update
10
+ else
11
+ create
12
+ end
13
+ end
14
+
15
+ def update
16
+ log "updating"
17
+
18
+ set_repo
19
+
20
+ git("fetch").run
21
+ git("reset --hard #{ref}").run
22
+ end
23
+
24
+ def create
25
+ log "cloning"
26
+ git("clone -o #{remote} #{args.repo} #{default_object}", :cwd => default_object.parent).run
27
+ git("reset --hard #{ref}").run
28
+ end
29
+
30
+ protected
31
+
32
+ def changed
33
+ unless FalseClass === args.enable_submodules
34
+ git("submodule update --init").ok?
35
+ end
36
+ end
37
+
38
+ def is_git?
39
+ exist? && git('rev-parse --is-inside-work-tree').to_s == "true"
40
+ rescue CommonMob::ShellError
41
+ end
42
+
43
+ def git(*cmd)
44
+ cmd.options[:cwd] ||= default_object
45
+ cmd.options[:as] = args.as if args.as?
46
+ cmd[0] = "git #{cmd.first}"
47
+ sh(*cmd)
48
+ end
49
+
50
+ def set_repo
51
+ git("config remote.#{remote}.url #{args.repo}").run
52
+ # TODO write merge spec
53
+ # git("config branch.master.remote origin").run
54
+ end
55
+
56
+ def default_object
57
+ args.default_object.pathname
58
+ end
59
+
60
+ def remote
61
+ 'origin'
62
+ end
63
+
64
+ def ref
65
+ ref = args.ref || args.branch || 'master'
66
+ "#{remote}/#{ref}"
67
+ end
68
+
69
+ def revision
70
+ exist? && git("rev-parse HEAD").to_s
71
+ rescue CommonMob::ShellError
72
+ end
73
+
74
+ def state
75
+ {
76
+ :revision => revision
77
+ }
78
+ end
79
+
80
+ def validate!
81
+ problem!("no git repo defined") unless args.repo?
82
+ problem!("no git path defined") unless args.default_object?
83
+ end
84
+ end