backup_jenkins 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,34 +8,6 @@ module BackupJenkins
8
8
  @config = config
9
9
  end
10
10
 
11
- def backup_directory
12
- @backup_directory ||= "#{config.backup["dir_base"]}/#{config.base_file_name}_#{timestamp}"
13
- end
14
-
15
- def create_dir_and_copy(file_names)
16
- file_names.each do |file_name|
17
- create_dir_and_copy_impl(file_name)
18
- end
19
- end
20
-
21
- def list_local_files
22
- format_backup_file_data(backup_files)
23
- end
24
-
25
- def create_dir_and_copy_impl(file_name)
26
- raise "file '#{file_name}' does not exist" unless FileTest.file?(file_name)
27
-
28
- new_file_name = new_file_path(file_name)
29
- new_file_dir = File.dirname(new_file_name)
30
-
31
- FileUtils.mkdir_p(new_file_dir, :verbose => config.verbose)
32
- FileUtils.cp(file_name, new_file_name, :verbose => config.verbose)
33
- end
34
-
35
- def new_file_path(file_name)
36
- "#{backup_directory}/#{file_name.gsub(%r{#{config.jenkins["home"]}}, "")}".gsub(%r{//}, '/')
37
- end
38
-
39
11
  def do_backup
40
12
  raise "Backup directory already exists! (#{backup_directory})" if FileTest.directory?(backup_directory)
41
13
 
@@ -48,15 +20,12 @@ module BackupJenkins
48
20
  clean_up
49
21
  end
50
22
 
51
- def remove_old_backups
52
- files_to_remove.each do |file|
53
- FileUtils.rm(file)
54
- end
23
+ def tarball_filename
24
+ "#{backup_directory}.tar.bz2"
55
25
  end
56
26
 
57
- def files_to_remove
58
- glob_of_backup_files -
59
- glob_of_backup_files.last(config.backup["backups_to_keep"]["local"])
27
+ def list_local_files
28
+ format_backup_file_data(backup_files)
60
29
  end
61
30
 
62
31
  def clean_up
@@ -65,8 +34,19 @@ module BackupJenkins
65
34
 
66
35
  puts "Removing #{tarball_filename}"
67
36
  FileUtils.rm_rf(tarball_filename)
68
- rescue Errno::ENOENT => e
69
- puts e
37
+ rescue Errno::ENOENT
38
+ end
39
+
40
+ private
41
+
42
+ attr_reader :config
43
+
44
+ def backup_directory
45
+ @backup_directory ||= "#{config.backup["dir_base"]}/#{config.base_file_name}_#{timestamp}"
46
+ end
47
+
48
+ def timestamp
49
+ Time.now.strftime("%Y%m%d_%H%M")
70
50
  end
71
51
 
72
52
  def copy_files
@@ -75,6 +55,26 @@ module BackupJenkins
75
55
  create_dir_and_copy(jobs_files)
76
56
  end
77
57
 
58
+ def create_dir_and_copy(file_names)
59
+ file_names.each do |file_name|
60
+ create_dir_and_copy_impl(file_name)
61
+ end
62
+ end
63
+
64
+ def create_dir_and_copy_impl(file_name)
65
+ raise "file '#{file_name}' does not exist" unless FileTest.file?(file_name)
66
+
67
+ new_file_name = new_file_path(file_name)
68
+ new_file_dir = File.dirname(new_file_name)
69
+
70
+ FileUtils.mkdir_p(new_file_dir, :verbose => config.verbose)
71
+ FileUtils.cp(file_name, new_file_name, :verbose => config.verbose)
72
+ end
73
+
74
+ def new_file_path(file_name)
75
+ "#{backup_directory}/#{file_name.gsub(%r{#{config.jenkins["home"]}}, "")}".gsub(%r{//}, '/')
76
+ end
77
+
78
78
  def plugin_files
79
79
  Dir["#{config.jenkins["home"]}/plugins/*.jpi"] +
80
80
  Dir["#{config.jenkins["home"]}/plugins/*.jpi.pinned"] +
@@ -90,31 +90,31 @@ module BackupJenkins
90
90
  end
91
91
 
92
92
  def create_tarball
93
+ puts "Creating #{tarball_filename}..." if config.verbose
93
94
  Dir.chdir(backup_directory)
94
95
  %x{tar #{tar_options} #{tarball_filename} .}
95
96
  raise "Error creating tarball" unless FileTest.file?(tarball_filename)
96
97
  end
97
98
 
98
- def remove_temporary_files
99
- FileUtils.rm_rf(backup_directory, :verbose => config.verbose)
100
- end
101
-
102
- def tarball_filename
103
- "#{backup_directory}.tar.bz2"
104
- end
105
-
106
99
  def tar_options
107
100
  %w(j c f).tap do |options|
108
101
  options.unshift('v') if config.verbose
109
102
  end.join('')
110
103
  end
111
104
 
112
- private
105
+ def remove_old_backups
106
+ files_to_remove.each do |file|
107
+ FileUtils.rm(file)
108
+ end
109
+ end
113
110
 
114
- attr_reader :config
111
+ def files_to_remove
112
+ glob_of_backup_files -
113
+ glob_of_backup_files.last(config.backup["backups_to_keep"]["local"])
114
+ end
115
115
 
116
- def timestamp
117
- Time.now.strftime("%Y%m%d_%H%M")
116
+ def glob_of_backup_files
117
+ Dir["#{config.backup["dir_base"]}/#{config.base_file_name}_*tar.bz2"]
118
118
  end
119
119
 
120
120
  def backup_files
@@ -126,8 +126,8 @@ module BackupJenkins
126
126
  end
127
127
  end
128
128
 
129
- def glob_of_backup_files
130
- Dir["#{config.backup["dir_base"]}/#{config.base_file_name}_*tar.bz2"]
129
+ def remove_temporary_files
130
+ FileUtils.rm_rf(backup_directory, :verbose => config.verbose)
131
131
  end
132
132
  end
133
133
  end
@@ -9,13 +9,6 @@ module BackupJenkins
9
9
  end
10
10
  end
11
11
 
12
- def check_config
13
- if !config.valid?
14
- STDERR.puts "Config file is incorrect."
15
- show_help
16
- end
17
- end
18
-
19
12
  def parse_options
20
13
  options.each do |opt, arg|
21
14
  case opt
@@ -45,6 +38,23 @@ module BackupJenkins
45
38
  show_help
46
39
  end
47
40
 
41
+ def check_config
42
+ if !config.valid?
43
+ STDERR.puts "Config file is incorrect."
44
+ show_help
45
+ end
46
+ end
47
+
48
+ def run
49
+ do_backup
50
+ upload_file unless @only_local
51
+ rescue Interrupt
52
+ clean_up_backup
53
+ exit 0
54
+ end
55
+
56
+ private
57
+
48
58
  def options_possible
49
59
  [
50
60
  [
@@ -65,15 +75,6 @@ module BackupJenkins
65
75
  ]
66
76
  end
67
77
 
68
- def run
69
- do_backup
70
- upload_file unless @only_local
71
- rescue Interrupt
72
- clean_up_backup
73
- end
74
-
75
- private
76
-
77
78
  def do_backup
78
79
  backup.do_backup
79
80
  end
@@ -10,23 +10,10 @@ module BackupJenkins
10
10
  !verbose.nil? && jenkins_valid? && aws_valid? && backup_valid?
11
11
  end
12
12
 
13
- def method_missing(meth, *args, &block)
14
- return config[meth.to_s] if config.has_key?(meth.to_s)
15
- super
16
- end
17
-
18
- def respond_to?(meth)
19
- config.has_key?(meth.to_s) || super
20
- end
21
-
22
13
  def base_file_name
23
14
  "#{backup["file_name_base"]}_#{hostname}"
24
15
  end
25
16
 
26
- def hostname
27
- %x{hostname -s}.chomp
28
- end
29
-
30
17
  def override(options = {})
31
18
  @config.merge!(options)
32
19
  end
@@ -35,6 +22,10 @@ module BackupJenkins
35
22
 
36
23
  attr_reader :config
37
24
 
25
+ def default_config_file_path
26
+ "#{ENV['HOME']}/.config/backup_jenkins/config.yml"
27
+ end
28
+
38
29
  def config_file(config_file_path)
39
30
  YAML.load_file(config_file_path)
40
31
  rescue Errno::ENOENT
@@ -44,10 +35,6 @@ module BackupJenkins
44
35
  exit 1
45
36
  end
46
37
 
47
- def default_config_file_path
48
- "#{ENV['HOME']}/.config/backup_jenkins/config.yml"
49
- end
50
-
51
38
  def config_file_example
52
39
  File.read(config_file_example_path)
53
40
  end
@@ -70,5 +57,18 @@ module BackupJenkins
70
57
  backup["backups_to_keep"]["remote"] &&
71
58
  backup["backups_to_keep"]["local"]
72
59
  end
60
+
61
+ def hostname
62
+ %x{hostname -s}.chomp
63
+ end
64
+
65
+ def method_missing(meth, *args, &block)
66
+ return config[meth.to_s] if config.has_key?(meth.to_s)
67
+ super
68
+ end
69
+
70
+ def respond_to?(meth)
71
+ config.has_key?(meth.to_s) || super
72
+ end
73
73
  end
74
74
  end
@@ -1,32 +1,64 @@
1
1
  module BackupJenkins
2
2
  module Formatter
3
3
 
4
- # Assumes that filenames are ordered already
4
+ # Assumes that filenames are ordered already.
5
+ #
5
6
  # Requires a structure like:
6
7
  # [
7
8
  # { :key => "jenkins_berman_20121107_1721.tar.bz2", :content_length => 88762813 },
8
9
  # { :key => "jenkins_berman_20121107_1745.tar.bz2", :content_length => 88762572 }
9
10
  # ]
10
11
  def format_backup_file_data(file_hashes)
11
- by_host = {}
12
- file_hashes.each do |file_hash|
13
- (date, host, key) = extract_data_from_filename(file_hash[:key])
14
- by_host[host] ||= []
15
- by_host[host].push([date, key, file_hash[:content_length] / 2.0**20])
12
+ by_host = build_structure_by_host(file_hashes)
13
+ by_host_to_formatted_output(by_host)
14
+ end
15
+
16
+ private
17
+
18
+ BYTES_IN_MBYTE = 2.0 ** 20
19
+
20
+ class EntryFormatter
21
+ def initialize(entry)
22
+ @entry = entry
16
23
  end
17
24
 
18
- "".tap do |output|
19
- by_host.keys.sort.each do |host|
20
- data = by_host[host]
21
- output << host.capitalize << ":" << $/
22
- data.each do |datum|
23
- output << sprintf(" - %s key: %s (%0.2f MB)#{$/}", *datum)
24
- end
25
- output << $/
25
+ def to_s
26
+ sprintf(" - %s key: %s (%0.2f MB)#{$/}", *entry)
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :entry
32
+ end
33
+
34
+ class DataFormatter
35
+ def initialize(data)
36
+ @entries = data.map{ |entry| EntryFormatter.new(entry) }
37
+ end
38
+
39
+ def to_s
40
+ entries.map(&:to_s).join
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :entries
46
+ end
47
+
48
+ def build_structure_by_host(file_hashes)
49
+ {}.tap do |by_host|
50
+ file_hashes.each do |file_hash|
51
+ (date, host, key) = extract_data_from_filename(file_hash[:key])
52
+ by_host[host] ||= []
53
+ by_host[host].push([date, key, bytes_to_mb(file_hash[:content_length])])
26
54
  end
27
55
  end
28
56
  end
29
57
 
58
+ def bytes_to_mb(bytes)
59
+ bytes / BYTES_IN_MBYTE
60
+ end
61
+
30
62
  def extract_data_from_filename(filename)
31
63
  sans_base = filename.gsub(/#{config.backup["file_name_base"]}_/, '').gsub(/\.tar\.bz2/, '')
32
64
  (hostname, date, time) = sans_base.split("_")
@@ -34,5 +66,17 @@ module BackupJenkins
34
66
  [formatted_date, hostname, filename]
35
67
  end
36
68
 
69
+ def by_host_to_formatted_output(by_host)
70
+ "".tap do |output|
71
+ by_host.keys.sort.each do |host|
72
+ data = by_host[host]
73
+ output << build_host_entry(host, data)
74
+ end
75
+ end
76
+ end
77
+
78
+ def build_host_entry(host, data)
79
+ host.capitalize << ":" << $/ << DataFormatter.new(data).to_s << $/
80
+ end
37
81
  end
38
82
  end
@@ -1,3 +1,3 @@
1
1
  module BackupJenkins
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -38,7 +38,8 @@ describe BackupJenkins::Backup do
38
38
  config.should_receive(:base_file_name).and_return("filename")
39
39
  subject.should_receive(:timestamp).and_return("timestamp")
40
40
 
41
- subject.backup_directory.should == "/path/to/some/dir_base/filename_timestamp"
41
+ subject.send(:backup_directory).should ==
42
+ "/path/to/some/dir_base/filename_timestamp"
42
43
  end
43
44
 
44
45
  it "should get data only once" do
@@ -46,9 +47,9 @@ describe BackupJenkins::Backup do
46
47
  config.should_receive(:base_file_name).once.and_return("filename")
47
48
  subject.should_receive(:timestamp).once.and_return("timestamp")
48
49
 
49
- subject.backup_directory
50
- subject.backup_directory
51
- subject.backup_directory
50
+ subject.send(:backup_directory)
51
+ subject.send(:backup_directory)
52
+ subject.send(:backup_directory)
52
53
  end
53
54
  end
54
55
 
@@ -61,7 +62,7 @@ describe BackupJenkins::Backup do
61
62
  subject.stub(:create_dir_and_copy)
62
63
  end
63
64
 
64
- after { subject.copy_files }
65
+ after { subject.send(:copy_files) }
65
66
 
66
67
  it { subject.should_receive(:create_dir_and_copy).with("plugin_files") }
67
68
  it { subject.should_receive(:create_dir_and_copy).with("user_content_files") }
@@ -74,7 +75,7 @@ describe BackupJenkins::Backup do
74
75
  subject.should_receive(:create_dir_and_copy_impl).with("b")
75
76
  subject.should_receive(:create_dir_and_copy_impl).with("c")
76
77
 
77
- subject.create_dir_and_copy(%w(a b c))
78
+ subject.send(:create_dir_and_copy, %w(a b c))
78
79
  end
79
80
  end
80
81
 
@@ -120,22 +121,22 @@ describe BackupJenkins::Backup do
120
121
 
121
122
  it "should get the new file path of the file" do
122
123
  subject.should_receive(:new_file_path)
123
- subject.create_dir_and_copy_impl("filename")
124
+ subject.send(:create_dir_and_copy_impl, "filename")
124
125
  end
125
126
 
126
127
  it "should create directory new_directory" do
127
128
  FileUtils.should_receive(:mkdir_p).with("/this/is/a/new/path", :verbose => false)
128
- subject.create_dir_and_copy_impl("filename")
129
+ subject.send(:create_dir_and_copy_impl, "filename")
129
130
  end
130
131
 
131
132
  it "should copy old file to new file" do
132
133
  FileUtils.should_receive(:cp).with("filename", "/this/is/a/new/path/to_file", :verbose => false)
133
- subject.create_dir_and_copy_impl("filename")
134
+ subject.send(:create_dir_and_copy_impl, "filename")
134
135
  end
135
136
 
136
137
  it "should raise error if file to copy from doesn't exist" do
137
138
  FileTest.should_receive(:file?).and_return(false)
138
- expect{ subject.create_dir_and_copy_impl("filename") }.to raise_error
139
+ expect{ subject.send(:create_dir_and_copy_impl, "filename") }.to raise_error
139
140
  end
140
141
  end
141
142
 
@@ -152,14 +153,14 @@ describe BackupJenkins::Backup do
152
153
  end
153
154
 
154
155
  context do
155
- after { subject.create_tarball }
156
+ after { subject.send(:create_tarball) }
156
157
  it { Dir.should_receive(:chdir).with("directory") }
157
158
  it { subject.should_receive(:`).with("tar options directory.tar.bz2 .") }
158
159
  end
159
160
 
160
161
  it "should raise error if file doesn't exist" do
161
162
  FileTest.should_receive(:file?).and_return(false)
162
- expect{ subject.create_tarball }.to raise_error
163
+ expect{ subject.send(:create_tarball) }.to raise_error
163
164
  end
164
165
  end
165
166
 
@@ -200,7 +201,7 @@ describe BackupJenkins::Backup do
200
201
  subject.should_receive(:`).
201
202
  with("find home/jobs -maxdepth 3 -name config.xml -or -name nextBuildNumber").
202
203
  and_return("file1\nfile2\nfile3")
203
- subject.jobs_files.should == %w(file1 file2 file3)
204
+ subject.send(:jobs_files).should == %w(file1 file2 file3)
204
205
  end
205
206
  end
206
207
 
@@ -209,7 +210,8 @@ describe BackupJenkins::Backup do
209
210
  subject.should_receive(:backup_directory).and_return("backup_directory")
210
211
  config.should_receive(:jenkins).and_return({ "home" => "some_nice_house"})
211
212
 
212
- subject.new_file_path("some_nice_house/and/then/a/room").should == "backup_directory/and/then/a/room"
213
+ subject.send(:new_file_path, "some_nice_house/and/then/a/room").should ==
214
+ "backup_directory/and/then/a/room"
213
215
  end
214
216
  end
215
217
 
@@ -221,7 +223,7 @@ describe BackupJenkins::Backup do
221
223
  Dir.should_receive(:[]).with("home/plugins/*.jpi.pinned").and_return(["pinned"])
222
224
  Dir.should_receive(:[]).with("home/plugins/*.jpi.disabled").and_return(["disabled"])
223
225
 
224
- subject.plugin_files.should == %w(jpi pinned disabled)
226
+ subject.send(:plugin_files).should == %w(jpi pinned disabled)
225
227
  end
226
228
  end
227
229
 
@@ -230,7 +232,7 @@ describe BackupJenkins::Backup do
230
232
  subject.should_receive(:files_to_remove).and_return(['a_file'])
231
233
  FileUtils.should_receive(:rm).with('a_file')
232
234
 
233
- subject.remove_old_backups
235
+ subject.send(:remove_old_backups)
234
236
  end
235
237
  end
236
238
 
@@ -243,7 +245,7 @@ describe BackupJenkins::Backup do
243
245
  end
244
246
 
245
247
  it "should remove old files" do
246
- subject.files_to_remove.should == %w(old_file_1 old_file_2 old_file_3)
248
+ subject.send(:files_to_remove).should == %w(old_file_1 old_file_2 old_file_3)
247
249
  end
248
250
  end
249
251
 
@@ -253,19 +255,19 @@ describe BackupJenkins::Backup do
253
255
  config.stub(:verbose).and_return(false)
254
256
  end
255
257
 
256
- after { subject.remove_temporary_files }
258
+ after { subject.send(:remove_temporary_files) }
257
259
  it { FileUtils.should_receive(:rm_rf).with("backup_directory", :verbose => false) }
258
260
  end
259
261
 
260
262
  describe "#tar_options" do
261
263
  it "should be jcf" do
262
264
  config.should_receive(:verbose).and_return(false)
263
- subject.tar_options == "jcf"
265
+ subject.send(:tar_options) == "jcf"
264
266
  end
265
267
 
266
268
  it "should be vjcf" do
267
269
  config.should_receive(:verbose).and_return(true)
268
- subject.tar_options == "vjcf"
270
+ subject.send(:tar_options) == "vjcf"
269
271
  end
270
272
  end
271
273
 
@@ -287,7 +289,7 @@ describe BackupJenkins::Backup do
287
289
 
288
290
  it "should return files inside the userContent directory" do
289
291
  Dir.should_receive(:[]).with("home/userContent/*").and_return(["my_file"])
290
- subject.user_content_files.should == %w(my_file)
292
+ subject.send(:user_content_files).should == %w(my_file)
291
293
  end
292
294
  end
293
295
  end
@@ -113,21 +113,25 @@ describe BackupJenkins::CLI do
113
113
  end
114
114
 
115
115
  describe "#run" do
116
- after { subject.run }
116
+ context do
117
+ after { subject.run }
117
118
 
118
- it { subject.should_receive(:do_backup) }
119
- it { subject.should_receive(:upload_file) }
119
+ it { subject.should_receive(:do_backup) }
120
+ it { subject.should_receive(:upload_file) }
120
121
 
121
- it "should not upload file if only_local is set" do
122
- subject.instance_variable_set(:"@only_local", true)
122
+ it "should not upload file if only_local is set" do
123
+ subject.instance_variable_set(:"@only_local", true)
123
124
 
124
- subject.should_receive(:do_backup)
125
- subject.should_not_receive(:upload_file)
125
+ subject.should_receive(:do_backup)
126
+ subject.should_not_receive(:upload_file)
127
+ end
126
128
  end
127
129
 
128
130
  it "should clean up" do
129
131
  subject.should_receive(:do_backup).and_raise(Interrupt)
130
132
  backup.should_receive(:clean_up)
133
+
134
+ expect{ subject.run }.to raise_error SystemExit
131
135
  end
132
136
  end
133
137
 
@@ -24,7 +24,7 @@ describe BackupJenkins::Config do
24
24
  end
25
25
 
26
26
  describe "#hostname" do
27
- after { subject.hostname.should == 'hostname' }
27
+ after { subject.send(:hostname).should == 'hostname' }
28
28
  it { subject.should_receive(:`).with("hostname -s").and_return("hostname\n") }
29
29
  end
30
30
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backup_jenkins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &32124580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *32124580
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rake
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &32124160 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: '0'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *32124160
46
36
  description: Simple Jenkins config and plugin backup to S3
47
37
  email:
48
38
  - jcmuller@gmail.com
@@ -95,14 +85,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
85
  version: '0'
96
86
  requirements: []
97
87
  rubyforge_project:
98
- rubygems_version: 1.8.24
88
+ rubygems_version: 1.8.15
99
89
  signing_key:
100
90
  specification_version: 3
101
91
  summary: This gem allows you to get a backup instance of jenkins up and running pretty
102
92
  quickly
103
93
  test_files:
104
- - spec/lib/backup_jenkins/aws_spec.rb
94
+ - spec/lib/backup_jenkins/formatter_spec.rb
105
95
  - spec/lib/backup_jenkins/backup_spec.rb
106
96
  - spec/lib/backup_jenkins/cli_spec.rb
107
97
  - spec/lib/backup_jenkins/config_spec.rb
108
- - spec/lib/backup_jenkins/formatter_spec.rb
98
+ - spec/lib/backup_jenkins/aws_spec.rb