cronicle 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf5e59d994c9053a7e7f684735bf31e36e0894bd
4
- data.tar.gz: 23bbf254357c458e7381f510366a39c6bc52297f
3
+ metadata.gz: 55c08587227f16557d6a593632f4be80211529a6
4
+ data.tar.gz: fc493ac6139ef47144b822f450c87180b9282d28
5
5
  SHA512:
6
- metadata.gz: 9da051ad79732fa58805cdff44209add4ece98e4f019c5869c123239a7e6e3a64c97367998fc0f289e69c72db745f61fbb50301044736a64957c00adc7321855
7
- data.tar.gz: 8e5e860a68fd60386e402422f54c1d2fe290d891f5c413aa56a33a02439566342653827d6d383d8e832d20c6e4cd8cb5ccfbe46e67a86ac2c0b12f14fa28173d
6
+ metadata.gz: fc99682523adf3153934dd24bf7da7b9318a8e9ce5e139d7c55aaacfe022d55b6642368999452583809cd5a1e3cc23fdba72bcc3e2c9c0ce07d70933568eb074
7
+ data.tar.gz: ca24dc26a7a5ca099bd7b3360f13230cae62c3e8087cd0172910bc1aebad2d083e0badc69f4aedfbc586c54f266f6c45d12b1c4da85d6e34d6b4f990ffb3fe36
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
  ```
27
27
  Commands:
28
28
  cronicle apply # Apply cron jobs to remote hosts
29
+ cronicle cleanup # Clean up cron jobs on remote hosts
29
30
  cronicle exec JOB_NAME # Execute a job on remote hosts
30
31
  cronicle help [COMMAND] # Describe available commands or one specific command
31
32
 
@@ -146,7 +147,7 @@ server2
146
147
  ```javascript
147
148
  {
148
149
  "servers": {
149
- "server1": ["web", "app"]
150
+ "server1": ["web", "app"],
150
151
  "server2": ["db"]
151
152
  }
152
153
  }
data/lib/cronicle/cli.rb CHANGED
@@ -45,6 +45,14 @@ class Cronicle::CLI < Thor
45
45
  end
46
46
  end
47
47
 
48
+ desc 'cleanup', 'Clean up cron jobs on remote hosts'
49
+ def cleanup
50
+ with_logging do
51
+ set_ssh_options
52
+ client.cleanup
53
+ end
54
+ end
55
+
48
56
  private
49
57
 
50
58
  def with_logging
@@ -15,6 +15,10 @@ class Cronicle::Client
15
15
  walk(file)
16
16
  end
17
17
 
18
+ def cleanup
19
+ walk(nil, :cleanup => true)
20
+ end
21
+
18
22
  def exec(file, name)
19
23
  name = name.to_s
20
24
  jobs = load_file(file)
@@ -39,9 +43,18 @@ class Cronicle::Client
39
43
 
40
44
  private
41
45
 
42
- def walk(file)
43
- jobs = load_file(file)
44
- jobs_by_host = select_host(jobs)
46
+ def walk(file, opts = {})
47
+ if opts[:cleanup]
48
+ jobs_by_host = {}
49
+
50
+ @host_list.all.each do |host|
51
+ jobs_by_host[host] = {}
52
+ end
53
+ else
54
+ jobs = load_file(file)
55
+ jobs_by_host = select_host(jobs)
56
+ end
57
+
45
58
  exported = export_cron(jobs_by_host.keys)
46
59
  walk_hosts(jobs_by_host, exported)
47
60
  end
@@ -1,3 +1,3 @@
1
1
  module Cronicle
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -0,0 +1,358 @@
1
+ describe 'Cronicle::Client#cleanup' do
2
+ before do
3
+ on TARGET_HOSTS do |ssh_options|
4
+ user = ssh_options[:user]
5
+
6
+ set_crontab user, <<-CRON.unindent
7
+ FOO=bar
8
+ ZOO=baz
9
+ 1 1 1 1 1 echo #{user} > /dev/null
10
+ CRON
11
+
12
+ set_crontab :root, <<-CRON.unindent
13
+ FOO=bar
14
+ ZOO=baz
15
+ 1 1 1 1 1 echo root > /dev/null
16
+ CRON
17
+ end
18
+
19
+ cronicle(:apply) { <<-RUBY.unindent }
20
+ on servers: /.*/ do
21
+ job :foo, user: :root, schedule: '1 2 * * *' do
22
+ puts `uname`
23
+ puts `whoami`
24
+ end
25
+ end
26
+
27
+ on servers: /.*/ do
28
+ job :bar, user: :root, schedule: :@hourly, content: <<-SH.unindent
29
+ #!/bin/sh
30
+ echo hello
31
+ SH
32
+ end
33
+
34
+ on servers: /amazon_linux/ do
35
+ job :foo, user: 'ec2-user', schedule: '1 * * * *' do
36
+ puts 100
37
+ end
38
+ end
39
+
40
+ on servers: /ubuntu/ do
41
+ job :foo, user: :ubuntu, schedule: :@daily do
42
+ puts 200
43
+ end
44
+ end
45
+ RUBY
46
+ end
47
+
48
+ context 'when cron is cleaned up' do
49
+ let(:jobfile) do
50
+ <<-RUBY.unindent
51
+ on servers: /.*/ do
52
+ job :foo, user: :root, schedule: '1 2 * * *', bundle: 'ruby-mysql' do
53
+ require 'mysql'
54
+ p Mysql
55
+ end
56
+ end
57
+ RUBY
58
+ end
59
+
60
+ let(:amzn_crontab_orig) do
61
+ {
62
+ "/var/spool/cron/ec2-user" =>
63
+ "FOO=bar
64
+ ZOO=baz
65
+ 1 1 1 1 1 echo ec2-user > /dev/null
66
+ ",
67
+ "/var/spool/cron/root" =>
68
+ "FOO=bar
69
+ ZOO=baz
70
+ 1 1 1 1 1 echo root > /dev/null
71
+ 1 2 * * *\tcd /var/lib/cronicle/run/root/foo && /usr/local/bin/bundle exec /var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
72
+ "
73
+ }
74
+ end
75
+
76
+ let(:amzn_gemfile_orig) do
77
+ {
78
+ "/var/lib/cronicle/run/root/foo/Gemfile" =>
79
+ "source 'https://rubygems.org'
80
+ gem 'ruby-mysql'
81
+ "
82
+ }
83
+ end
84
+
85
+ let(:ubuntu_crontab_orig) do
86
+ {
87
+ "/var/spool/cron/crontabs/root" =>
88
+ "FOO=bar
89
+ ZOO=baz
90
+ 1 1 1 1 1 echo root > /dev/null
91
+ 1 2 * * *\tcd /var/lib/cronicle/run/root/foo && /usr/local/bin/bundle exec /var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
92
+ ",
93
+ "/var/spool/cron/crontabs/ubuntu" =>
94
+ "FOO=bar
95
+ ZOO=baz
96
+ 1 1 1 1 1 echo ubuntu > /dev/null
97
+ "
98
+ }
99
+ end
100
+
101
+ let(:ubuntu_gemfile_orig) do
102
+ {
103
+ "/var/lib/cronicle/run/root/foo/Gemfile" =>
104
+ "source 'https://rubygems.org'
105
+ gem 'ruby-mysql'
106
+ "
107
+ }
108
+ end
109
+
110
+ let(:amzn_crontab) do
111
+ {
112
+ "/var/spool/cron/ec2-user" =>
113
+ "FOO=bar
114
+ ZOO=baz
115
+ 1 1 1 1 1 echo ec2-user > /dev/null
116
+ ",
117
+ "/var/spool/cron/root" =>
118
+ "FOO=bar
119
+ ZOO=baz
120
+ 1 1 1 1 1 echo root > /dev/null
121
+ "
122
+ }
123
+ end
124
+
125
+ let(:amzn_gemfile) do
126
+ {
127
+ }
128
+ end
129
+
130
+ let(:ubuntu_crontab) do
131
+ {
132
+ "/var/spool/cron/crontabs/root" =>
133
+ "FOO=bar
134
+ ZOO=baz
135
+ 1 1 1 1 1 echo root > /dev/null
136
+ ",
137
+ "/var/spool/cron/crontabs/ubuntu" =>
138
+ "FOO=bar
139
+ ZOO=baz
140
+ 1 1 1 1 1 echo ubuntu > /dev/null
141
+ "
142
+ }
143
+ end
144
+
145
+ let(:ubuntu_gemfile) do
146
+ {
147
+ }
148
+ end
149
+
150
+ before do
151
+ cronicle(:apply) { jobfile }
152
+ end
153
+
154
+ it do
155
+ on :amazon_linux do
156
+ expect(get_uname).to match /amzn/
157
+ expect(get_crontabs).to eq amzn_crontab_orig
158
+
159
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.unindent
160
+ #!/usr/bin/env ruby
161
+ require 'mysql'
162
+ p Mysql
163
+ EOS
164
+
165
+ expect(get_gemfiles).to eq amzn_gemfile_orig
166
+ end
167
+
168
+ on :ubuntu do
169
+ expect(get_uname).to match /Ubuntu/
170
+ expect(get_crontabs).to eq ubuntu_crontab_orig
171
+
172
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.unindent
173
+ #!/usr/bin/env ruby
174
+ require 'mysql'
175
+ p Mysql
176
+ EOS
177
+
178
+ expect(get_gemfiles).to eq ubuntu_gemfile_orig
179
+ end
180
+
181
+ cronicle(:cleanup)
182
+
183
+ on :amazon_linux do
184
+ expect(get_uname).to match /amzn/
185
+ expect(get_crontabs).to eq amzn_crontab
186
+ expect(get_gemfiles).to eq amzn_gemfile
187
+ end
188
+
189
+ on :ubuntu do
190
+ expect(get_uname).to match /Ubuntu/
191
+ expect(get_crontabs).to eq ubuntu_crontab
192
+ expect(get_gemfiles).to eq ubuntu_gemfile
193
+ end
194
+ end
195
+ end
196
+
197
+ context 'when cron is cleaned up (dry-run)' do
198
+ let(:amzn_crontab_orig) do
199
+ {
200
+ "/var/spool/cron/ec2-user" =>
201
+ "FOO=bar
202
+ ZOO=baz
203
+ 1 1 1 1 1 echo ec2-user > /dev/null
204
+ 1 * * * *\t/var/lib/cronicle/libexec/ec2-user/foo 2>&1 | logger -t cronicle/ec2-user/foo
205
+ ",
206
+ "/var/spool/cron/root" =>
207
+ "FOO=bar
208
+ ZOO=baz
209
+ 1 1 1 1 1 echo root > /dev/null
210
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
211
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
212
+ "
213
+ }
214
+ end
215
+
216
+ let(:ubuntu_crontab_orig) do
217
+ {
218
+ "/var/spool/cron/crontabs/root" =>
219
+ "FOO=bar
220
+ ZOO=baz
221
+ 1 1 1 1 1 echo root > /dev/null
222
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
223
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
224
+ ",
225
+ "/var/spool/cron/crontabs/ubuntu" =>
226
+ "FOO=bar
227
+ ZOO=baz
228
+ 1 1 1 1 1 echo ubuntu > /dev/null
229
+ @daily\t/var/lib/cronicle/libexec/ubuntu/foo 2>&1 | logger -t cronicle/ubuntu/foo
230
+ "
231
+ }
232
+ end
233
+
234
+ let(:amzn_crontab) do
235
+ {
236
+ "/var/spool/cron/ec2-user" =>
237
+ "FOO=bar
238
+ ZOO=baz
239
+ 1 1 1 1 1 echo ec2-user > /dev/null
240
+ 2 * * * *\t/var/lib/cronicle/libexec/ec2-user/foo 2>&1 | logger -t cronicle/ec2-user/foo
241
+ ",
242
+ "/var/spool/cron/root" =>
243
+ "FOO=bar
244
+ ZOO=baz
245
+ 1 1 1 1 1 echo root > /dev/null
246
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
247
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
248
+ "
249
+ }
250
+ end
251
+
252
+ let(:ubuntu_crontab) do
253
+ {
254
+ "/var/spool/cron/crontabs/root" =>
255
+ "FOO=bar
256
+ ZOO=baz
257
+ 1 1 1 1 1 echo root > /dev/null
258
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
259
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
260
+ ",
261
+ "/var/spool/cron/crontabs/ubuntu" =>
262
+ "FOO=bar
263
+ ZOO=baz
264
+ 1 1 1 1 1 echo ubuntu > /dev/null
265
+ @daily\t/var/lib/cronicle/libexec/ubuntu/foo2 2>&1 | logger -t cronicle/ubuntu/foo2
266
+ "
267
+ }
268
+ end
269
+
270
+ it do
271
+ on :amazon_linux do
272
+ expect(get_uname).to match /amzn/
273
+ expect(get_crontabs).to eq amzn_crontab_orig
274
+
275
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.unindent
276
+ #!/usr/bin/env ruby
277
+ puts `uname`
278
+ puts `whoami`
279
+ EOS
280
+
281
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.unindent
282
+ #!/bin/sh
283
+ echo hello
284
+ EOS
285
+
286
+ expect(get_file('/var/lib/cronicle/libexec/ec2-user/foo')).to eq <<-EOS.unindent
287
+ #!/usr/bin/env ruby
288
+ puts 100
289
+ EOS
290
+ end
291
+
292
+ on :ubuntu do
293
+ expect(get_uname).to match /Ubuntu/
294
+ expect(get_crontabs).to eq ubuntu_crontab_orig
295
+
296
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.unindent
297
+ #!/usr/bin/env ruby
298
+ puts `uname`
299
+ puts `whoami`
300
+ EOS
301
+
302
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.unindent
303
+ #!/bin/sh
304
+ echo hello
305
+ EOS
306
+
307
+ expect(get_file('/var/lib/cronicle/libexec/ubuntu/foo')).to eq <<-EOS.unindent
308
+ #!/usr/bin/env ruby
309
+ puts 200
310
+ EOS
311
+ end
312
+
313
+ cronicle(:cleanup, dry_run: true)
314
+
315
+ on :amazon_linux do
316
+ expect(get_uname).to match /amzn/
317
+ expect(get_crontabs).to eq amzn_crontab_orig
318
+
319
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.unindent
320
+ #!/usr/bin/env ruby
321
+ puts `uname`
322
+ puts `whoami`
323
+ EOS
324
+
325
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.unindent
326
+ #!/bin/sh
327
+ echo hello
328
+ EOS
329
+
330
+ expect(get_file('/var/lib/cronicle/libexec/ec2-user/foo')).to eq <<-EOS.unindent
331
+ #!/usr/bin/env ruby
332
+ puts 100
333
+ EOS
334
+ end
335
+
336
+ on :ubuntu do
337
+ expect(get_uname).to match /Ubuntu/
338
+ expect(get_crontabs).to eq ubuntu_crontab_orig
339
+
340
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.unindent
341
+ #!/usr/bin/env ruby
342
+ puts `uname`
343
+ puts `whoami`
344
+ EOS
345
+
346
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.unindent
347
+ #!/bin/sh
348
+ echo hello
349
+ EOS
350
+
351
+ expect(get_file('/var/lib/cronicle/libexec/ubuntu/foo')).to eq <<-EOS.unindent
352
+ #!/usr/bin/env ruby
353
+ puts 200
354
+ EOS
355
+ end
356
+ end
357
+ end
358
+ end
@@ -1,4 +1,4 @@
1
- describe 'Cronicle::Client#apply (update)' do
1
+ describe 'Cronicle::Client#apply (delete)' do
2
2
  before do
3
3
  on TARGET_HOSTS do |ssh_options|
4
4
  user = ssh_options[:user]
@@ -279,17 +279,12 @@ ZOO=baz
279
279
  "FOO=bar
280
280
  ZOO=baz
281
281
  1 1 1 1 1 echo root > /dev/null
282
- 1 2 * * *\tcd /var/lib/cronicle/run/root/foo && /usr/local/bin/bundle exec /var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
283
282
  "
284
283
  }
285
284
  end
286
285
 
287
286
  let(:amzn_gemfile) do
288
287
  {
289
- "/var/lib/cronicle/run/root/foo/Gemfile" =>
290
- "source 'https://rubygems.org'
291
- gem 'ruby-mysql'
292
- "
293
288
  }
294
289
  end
295
290
 
@@ -299,7 +294,6 @@ gem 'ruby-mysql'
299
294
  "FOO=bar
300
295
  ZOO=baz
301
296
  1 1 1 1 1 echo root > /dev/null
302
- 1 2 * * *\tcd /var/lib/cronicle/run/root/foo && /usr/local/bin/bundle exec /var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
303
297
  ",
304
298
  "/var/spool/cron/crontabs/ubuntu" =>
305
299
  "FOO=bar
@@ -311,10 +305,6 @@ ZOO=baz
311
305
 
312
306
  let(:ubuntu_gemfile) do
313
307
  {
314
- "/var/lib/cronicle/run/root/foo/Gemfile" =>
315
- "source 'https://rubygems.org'
316
- gem 'ruby-mysql'
317
- "
318
308
  }
319
309
  end
320
310
 
@@ -349,7 +339,10 @@ gem 'ruby-mysql'
349
339
  expect(get_gemfiles).to eq ubuntu_gemfile_orig
350
340
  end
351
341
 
352
- cronicle(:apply) { '' }
342
+ cronicle(:apply) { <<-EOS }
343
+ on servers: /.*/ do
344
+ end
345
+ EOS
353
346
 
354
347
  on :amazon_linux do
355
348
  expect(get_uname).to match /amzn/
data/spec/spec_helper.rb CHANGED
@@ -126,9 +126,15 @@ def cronicle(*args)
126
126
  end
127
127
 
128
128
  client = cronicle_client(options)
129
-
130
- tempfile(yield) do |f|
131
- args = [command, f.path, args].flatten
129
+ content = block_given? ? yield : nil
130
+
131
+ if content
132
+ tempfile(yield) do |f|
133
+ args = [command, f.path, args].flatten
134
+ client.send(*args)
135
+ end
136
+ else
137
+ args = [command, args].flatten
132
138
  client.send(*args)
133
139
  end
134
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cronicle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sourcify
@@ -226,6 +226,7 @@ files:
226
226
  - lib/cronicle/logger.rb
227
227
  - lib/cronicle/utils.rb
228
228
  - lib/cronicle/version.rb
229
+ - spec/cronicle_cleanup_spec.rb
229
230
  - spec/cronicle_create_spec.rb
230
231
  - spec/cronicle_delete_spec.rb
231
232
  - spec/cronicle_exec_spec.rb
@@ -257,6 +258,7 @@ signing_key:
257
258
  specification_version: 4
258
259
  summary: It is a tool for execute script, and define cron on remote hosts.
259
260
  test_files:
261
+ - spec/cronicle_cleanup_spec.rb
260
262
  - spec/cronicle_create_spec.rb
261
263
  - spec/cronicle_delete_spec.rb
262
264
  - spec/cronicle_exec_spec.rb