cronicle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,150 @@
1
+ require 'serverspec'
2
+ require 'net/ssh'
3
+ require 'tempfile'
4
+
5
+ require 'cronicle'
6
+
7
+ String.disable_colorization = true
8
+
9
+ def get_ssh_config(host)
10
+ Tempfile.open('', Dir.tmpdir) do |config|
11
+ config.write(`vagrant ssh-config #{host}`)
12
+ config.close
13
+ Net::SSH::Config.for(host, [config.path])
14
+ end
15
+ end
16
+
17
+ def specinfra_config_set_nil(key)
18
+ Specinfra.configuration.instance_variable_set("@#{key}", nil)
19
+ RSpec.configuration.send("#{key}=", nil)
20
+ end
21
+
22
+ CRON_DIRS = %w(
23
+ /var/spool/cron/crontabs
24
+ /var/spool/cron
25
+ )
26
+
27
+ def get_cron_dir
28
+ CRON_DIRS.find do |dir|
29
+ Specinfra.backend.run_command("test -d #{dir}").exit_status.zero?
30
+ end
31
+ end
32
+
33
+ def set_crontab(user, content)
34
+ cron_dir = get_cron_dir
35
+
36
+ Tempfile.open('', Dir.tmpdir) do |fp|
37
+ fp << content
38
+ fp.flush
39
+ Specinfra.backend.send_file(fp.path, [cron_dir, user].join('/'))
40
+ end
41
+ end
42
+
43
+ def get_crontabs
44
+ cron_dir = get_cron_dir
45
+ crontabs = Specinfra.backend.run_command("ls #{cron_dir}/*").stdout.strip.split(/\s+/)
46
+
47
+ Hash[*crontabs.map {|crontab|
48
+ content = Specinfra.backend.run_command("cat #{crontab}").stdout
49
+ [crontab, content]
50
+ }.flatten]
51
+ end
52
+
53
+ def get_file(path)
54
+ Specinfra.backend.run_command("cat #{path}").stdout
55
+ end
56
+
57
+ def get_uname
58
+ Specinfra.backend.run_command('uname -a').stdout.strip
59
+ end
60
+
61
+ set :backend, :ssh
62
+ set :sudo_password, 'cronicle'
63
+
64
+ TARGET_HOSTS = %w(amazon_linux ubuntu)
65
+
66
+ SSH_OPTIONS_BY_HOST = Hash[*TARGET_HOSTS.map {|host|
67
+ options = Tempfile.open('', Dir.tmpdir) do |config|
68
+ config.write(`vagrant ssh-config #{host}`)
69
+ config.flush
70
+ Net::SSH::Config.for(host, [config.path])
71
+ end
72
+
73
+ [host, options]
74
+ }.flatten]
75
+
76
+ def on(*hosts)
77
+ hosts.flatten.map(&:to_s).each do |host|
78
+ specinfra_config_set_nil(:ssh)
79
+ specinfra_config_set_nil(:scp)
80
+
81
+ Specinfra.configuration.host = host
82
+ ssh_options = SSH_OPTIONS_BY_HOST[host]
83
+ Specinfra.configuration.ssh_options = ssh_options
84
+
85
+ yield(ssh_options)
86
+ end
87
+ end
88
+
89
+ RSpec.configure do |config|
90
+ config.before(:each) do
91
+ on :ubuntu do
92
+ Specinfra.backend.run_command("apt-get -y install ruby")
93
+ end
94
+ end
95
+
96
+ config.before(:each) do
97
+ on TARGET_HOSTS do
98
+ cron_dir = get_cron_dir
99
+ Specinfra.backend.run_command("rm -f #{cron_dir}/*")
100
+ end
101
+ end
102
+ end
103
+
104
+ def cronicle(*args)
105
+ command = args.shift
106
+ options = args.last.kind_of?(Hash) ? args.pop : {}
107
+
108
+ tempfile(`vagrant ssh-config`) do |ssh_config|
109
+ SSHKit::Backend::Netssh.configure do |ssh|
110
+ ssh.ssh_options = {:config => ssh_config.path}
111
+ end
112
+
113
+ client = cronicle_client(options)
114
+
115
+ tempfile(yield) do |f|
116
+ args = [command, f.path, args].flatten
117
+ client.send(*args)
118
+ end
119
+ end
120
+ end
121
+
122
+ def cronicle_client(options = {})
123
+ options = {
124
+ :sudo_password => 'cronicle'
125
+ }.merge(options)
126
+
127
+ hosts = SSH_OPTIONS_BY_HOST.keys
128
+ host_list = Cronicle::HostList.new(hosts.join(','))
129
+
130
+ if ENV['DEBUG'] == '1'
131
+ options[:debug] = true
132
+ Cronicle::Logger.instance.set_debug(true)
133
+ else
134
+ options[:logger] ||= Logger.new('/dev/null')
135
+ end
136
+
137
+ Cronicle::Client.new(host_list, options)
138
+ end
139
+
140
+ def tempfile(content, options = {})
141
+ basename = "#{File.basename __FILE__}.#{$$}"
142
+ basename = [basename, options[:ext]] if options[:ext]
143
+
144
+ Tempfile.open(basename) do |f|
145
+ f.puts(content)
146
+ f.flush
147
+ f.rewind
148
+ yield(f)
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cronicle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sourcify
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.0.rc4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.0.rc4
27
+ - !ruby/object:Gem::Dependency
28
+ name: sshkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: parallel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: diffy
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: 3.0.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: 3.0.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: serverspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: It is a tool for execute script, and define cron on remote hosts.
154
+ email:
155
+ - sgwr_dts@yahoo.co.jp
156
+ executables:
157
+ - cronicle
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - .gitignore
162
+ - .rspec
163
+ - .travis.yml
164
+ - Gemfile
165
+ - LICENSE.txt
166
+ - README.md
167
+ - Rakefile
168
+ - Vagrantfile
169
+ - bin/cronicle
170
+ - cronicle.gemspec
171
+ - cronicle.us-east-1.pem.enc
172
+ - lib/cronicle.rb
173
+ - lib/cronicle/cli.rb
174
+ - lib/cronicle/client.rb
175
+ - lib/cronicle/driver.rb
176
+ - lib/cronicle/dsl.rb
177
+ - lib/cronicle/dsl/context.rb
178
+ - lib/cronicle/dsl/context/job.rb
179
+ - lib/cronicle/exporter.rb
180
+ - lib/cronicle/ext/hash_ext.rb
181
+ - lib/cronicle/ext/sshkit_ext.rb
182
+ - lib/cronicle/ext/string_ext.rb
183
+ - lib/cronicle/host_list.rb
184
+ - lib/cronicle/logger.rb
185
+ - lib/cronicle/utils.rb
186
+ - lib/cronicle/version.rb
187
+ - spec/cronicle_create_spec.rb
188
+ - spec/cronicle_delete_spec.rb
189
+ - spec/cronicle_exec_spec.rb
190
+ - spec/cronicle_update_spec.rb
191
+ - spec/host_list_spec.rb
192
+ - spec/spec_helper.rb
193
+ homepage: https://github.com/winebarrel/cronicle
194
+ licenses:
195
+ - MIT
196
+ metadata: {}
197
+ post_install_message:
198
+ rdoc_options: []
199
+ require_paths:
200
+ - lib
201
+ required_ruby_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ required_rubygems_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ requirements: []
212
+ rubyforge_project:
213
+ rubygems_version: 2.0.14
214
+ signing_key:
215
+ specification_version: 4
216
+ summary: It is a tool for execute script, and define cron on remote hosts.
217
+ test_files:
218
+ - spec/cronicle_create_spec.rb
219
+ - spec/cronicle_delete_spec.rb
220
+ - spec/cronicle_exec_spec.rb
221
+ - spec/cronicle_update_spec.rb
222
+ - spec/host_list_spec.rb
223
+ - spec/spec_helper.rb