backup_jenkins 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,5 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = BackupJenkins::VERSION
17
17
 
18
18
  gem.add_dependency("aws-sdk")
19
+ gem.add_dependency("hashie")
20
+
19
21
  gem.add_development_dependency("rake")
20
22
  end
@@ -54,7 +54,7 @@ module BackupJenkins
54
54
 
55
55
  # TODO change this to use a time decay algorithm
56
56
  def files_to_remove
57
- files - files.last(config.backup["backups_to_keep"]["remote"])
57
+ files - files.last(config.backup.backups_to_keep.remote)
58
58
  end
59
59
 
60
60
  def files
@@ -66,20 +66,20 @@ module BackupJenkins
66
66
  end
67
67
 
68
68
  def backup_files_for_all_hosts
69
- s3_files.with_prefix(config.backup["file_name_base"])
69
+ s3_files.with_prefix(config.backup.file_name_base)
70
70
  end
71
71
 
72
72
  def setup_aws
73
73
  s3 = initialize_s3_object
74
- @bucket = s3.buckets[config.aws["bucket_name"]]
75
- @bucket = s3.buckets.create(config.aws["bucket_name"]) unless @bucket.exists?
74
+ @bucket = s3.buckets[config.aws.bucket_name]
75
+ @bucket = s3.buckets.create(config.aws.bucket_name) unless @bucket.exists?
76
76
  raise "Couldn't create bucket!" unless @bucket.exists?
77
77
  end
78
78
 
79
79
  def initialize_s3_object
80
80
  ::AWS::S3.new(
81
- :access_key_id => config.aws["access_key"],
82
- :secret_access_key => config.aws["secret"]
81
+ :access_key_id => config.aws.access_key,
82
+ :secret_access_key => config.aws.secret
83
83
  )
84
84
  end
85
85
 
@@ -42,7 +42,7 @@ module BackupJenkins
42
42
  attr_reader :config
43
43
 
44
44
  def backup_directory
45
- @backup_directory ||= "#{config.backup["dir_base"]}/#{config.base_file_name}_#{timestamp}"
45
+ @backup_directory ||= "#{config.backup.dir_base}/#{config.base_file_name}_#{timestamp}"
46
46
  end
47
47
 
48
48
  def timestamp
@@ -72,21 +72,21 @@ module BackupJenkins
72
72
  end
73
73
 
74
74
  def new_file_path(file_name)
75
- "#{backup_directory}/#{file_name.gsub(%r{#{config.jenkins["home"]}}, "")}".gsub(%r{//}, '/')
75
+ "#{backup_directory}/#{file_name.gsub(%r{#{config.jenkins.home}}, "")}".gsub(%r{//}, '/')
76
76
  end
77
77
 
78
78
  def plugin_files
79
- Dir["#{config.jenkins["home"]}/plugins/*.jpi"] +
80
- Dir["#{config.jenkins["home"]}/plugins/*.jpi.pinned"] +
81
- Dir["#{config.jenkins["home"]}/plugins/*.jpi.disabled"]
79
+ Dir["#{config.jenkins.home}/plugins/*.jpi"] +
80
+ Dir["#{config.jenkins.home}/plugins/*.jpi.pinned"] +
81
+ Dir["#{config.jenkins.home}/plugins/*.jpi.disabled"]
82
82
  end
83
83
 
84
84
  def user_content_files
85
- Dir["#{config.jenkins["home"]}/userContent/*"]
85
+ Dir["#{config.jenkins.home}/userContent/*"]
86
86
  end
87
87
 
88
88
  def jobs_files
89
- `find #{config.jenkins["home"]}/jobs -maxdepth 3 -name config.xml -or -name nextBuildNumber`.split(/\n/)
89
+ `find #{config.jenkins.home}/jobs -maxdepth 3 -name config.xml -or -name nextBuildNumber`.split(/\n/)
90
90
  end
91
91
 
92
92
  def create_tarball
@@ -110,17 +110,17 @@ module BackupJenkins
110
110
 
111
111
  def files_to_remove
112
112
  glob_of_backup_files -
113
- glob_of_backup_files.last(config.backup["backups_to_keep"]["local"])
113
+ glob_of_backup_files.last(config.backup.backups_to_keep.local)
114
114
  end
115
115
 
116
116
  def glob_of_backup_files
117
- Dir["#{config.backup["dir_base"]}/#{config.base_file_name}_*tar.bz2"]
117
+ Dir["#{config.backup.dir_base}/#{config.base_file_name}_*tar.bz2"]
118
118
  end
119
119
 
120
120
  def backup_files
121
121
  glob_of_backup_files.sort.map do |file|
122
122
  {
123
- :key => file.gsub(%r{#{config.backup["dir_base"]}/}, ''),
123
+ :key => file.gsub(%r{#{config.backup.dir_base}/}, ''),
124
124
  :content_length => File.size(file)
125
125
  }
126
126
  end
@@ -9,6 +9,9 @@ module BackupJenkins
9
9
  end
10
10
  end
11
11
 
12
+ # TODO Some options depend on the config file being correct. Some other
13
+ # options exit. Mark different options with whether they do one thing or
14
+ # another.
12
15
  def parse_options
13
16
  options.each do |opt, arg|
14
17
  case opt
@@ -3,7 +3,7 @@ module BackupJenkins
3
3
  attr_reader :s3
4
4
 
5
5
  def initialize(path = default_config_file_path)
6
- @config = config_file(path)
6
+ @config = Hashie::Mash.new(config_file(path))
7
7
  end
8
8
 
9
9
  def valid?
@@ -11,7 +11,7 @@ module BackupJenkins
11
11
  end
12
12
 
13
13
  def base_file_name
14
- "#{backup["file_name_base"]}_#{hostname}"
14
+ "#{backup.file_name_base}_#{hostname}"
15
15
  end
16
16
 
17
17
  def override(options = {})
@@ -44,18 +44,21 @@ module BackupJenkins
44
44
  end
45
45
 
46
46
  def aws_valid?
47
- aws["access_key"] && aws["secret"] && aws["bucket_name"]
47
+ aws.access_key &&
48
+ aws.secret &&
49
+ aws.bucket_name
48
50
  end
49
51
 
50
52
  def jenkins_valid?
51
- !!jenkins["home"]
53
+ !!jenkins.home
52
54
  end
53
55
 
54
56
  def backup_valid?
55
- backup["dir_base"] && backup["file_name_base"] &&
56
- backup["backups_to_keep"] &&
57
- backup["backups_to_keep"]["remote"] &&
58
- backup["backups_to_keep"]["local"]
57
+ backup.dir_base &&
58
+ backup.file_name_base &&
59
+ backup.backups_to_keep &&
60
+ backup.backups_to_keep.remote &&
61
+ backup.backups_to_keep.local
59
62
  end
60
63
 
61
64
  def hostname
@@ -60,7 +60,7 @@ module BackupJenkins
60
60
  end
61
61
 
62
62
  def extract_data_from_filename(filename)
63
- sans_base = filename.gsub(/#{config.backup["file_name_base"]}_/, '').gsub(/\.tar\.bz2/, '')
63
+ sans_base = filename.gsub(/#{config.backup.file_name_base}_/, '').gsub(/\.tar\.bz2/, '')
64
64
  (hostname, date, time) = sans_base.split("_")
65
65
  formatted_date = Time.parse(date << time).strftime("%Y/%m/%d %H:%M")
66
66
  [formatted_date, hostname, filename]
@@ -1,3 +1,3 @@
1
1
  module BackupJenkins
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'aws'
3
3
  require 'getoptlong'
4
+ require 'hashie'
4
5
  require 'time'
5
6
  require 'yaml'
6
7
 
@@ -1,26 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BackupJenkins::AWS do
4
- let(:config) { stub }
5
- let(:s3_mocks) { stub(:buckets => stub(:[] => stub(:exists? => true), :create => true)) }
4
+ let(:config) { stub(:aws => aws, :backup => backup) }
5
+ let(:s3_mocks) {
6
+ stub(:buckets => stub(:[] => stub(:exists? => true), :create => true))
7
+ }
8
+ let(:aws) {
9
+ stub(
10
+ :access_key => "some_key",
11
+ :secret => "some_secret",
12
+ :bucket_name => "some_bucket"
13
+ )
14
+ }
15
+ let(:backup) {
16
+ stub(:backups_to_keep => stub(:remote => 2), :file_name_base => "jenkins")
17
+ }
6
18
 
7
19
  before do
8
20
  BackupJenkins::Config.stub(:new).and_return(config)
9
-
10
- config.stub(:aws).and_return({ "access_key" => "some_key", "secret" => "some_secret" })
11
- config.stub(:backup).and_return({ "backups_to_keep" => { "remote" => 2 }, "file_name_base" => "jenkins" })
12
-
13
21
  ::AWS::S3.stub(:new).and_return(s3_mocks)
14
22
  end
15
23
 
16
24
  describe "#setup_aws" do
17
- after do
18
- subject
19
- end
25
+ after { subject }
20
26
 
21
- it "should instantiate an S3 object" do
22
- ::AWS::S3.should_receive(:new).and_return(s3_mocks)
23
- end
27
+ it { ::AWS::S3.should_receive(:new).and_return(s3_mocks) }
24
28
 
25
29
  it "shuld create bucket" do
26
30
  s3_mocks.buckets.should_receive(:[]).and_return(mock(:exists? => false))
@@ -53,7 +57,7 @@ describe BackupJenkins::AWS do
53
57
 
54
58
  describe "#backup_files_for_all_hosts" do
55
59
  it "should return the right files" do
56
- config.should_receive(:backup).and_return("file_name_base" => "base_file_name")
60
+ config.should_receive(:backup).and_return(mock(:file_name_base => "base_file_name"))
57
61
  objects = mock
58
62
  objects.should_receive(:with_prefix).with("base_file_name").and_return([1, 2, 3])
59
63
  subject.should_receive(:s3_files).and_return(objects)
@@ -16,7 +16,7 @@ describe BackupJenkins::Backup do
16
16
  describe "#backup_files" do
17
17
  it "should get a listing for all the files in backup directory" do
18
18
  file = "dir/base_1234"
19
- config.stub(:backup).and_return("dir_base" => "dir")
19
+ config.stub_chain(:backup, :dir_base).and_return("dir")
20
20
  config.stub(:base_file_name).and_return("base")
21
21
 
22
22
  File.should_receive(:size).with(file).and_return("size")
@@ -31,7 +31,7 @@ describe BackupJenkins::Backup do
31
31
  end
32
32
 
33
33
  describe "#backup_directory" do
34
- let(:backup) { { "dir_base" => "/path/to/some/dir_base" } }
34
+ let(:backup) { mock(:dir_base => "/path/to/some/dir_base") }
35
35
 
36
36
  it "should return the dir base + the base file name + time stamp" do
37
37
  config.should_receive(:backup).and_return(backup)
@@ -81,7 +81,7 @@ describe BackupJenkins::Backup do
81
81
 
82
82
  describe "#list_local_files" do
83
83
  before do
84
- config.stub(:backup).and_return("dir_base" => "base")
84
+ config.stub_chain(:backup, :dir_base).and_return("base")
85
85
  config.stub(:base_file_name).and_return("base_file_name")
86
86
  end
87
87
 
@@ -195,7 +195,7 @@ describe BackupJenkins::Backup do
195
195
  end
196
196
 
197
197
  describe "#jobs_files" do
198
- before { config.stub(:jenkins).and_return({ "home" => "home" }) }
198
+ before { config.stub_chain(:jenkins, :home).and_return("home") }
199
199
 
200
200
  it "should return the config.xml and nextBuildNumber files in job directories" do
201
201
  subject.should_receive(:`).
@@ -208,7 +208,7 @@ describe BackupJenkins::Backup do
208
208
  describe "#new_file_path" do
209
209
  it "should return backup_directory/something" do
210
210
  subject.should_receive(:backup_directory).and_return("backup_directory")
211
- config.should_receive(:jenkins).and_return({ "home" => "some_nice_house"})
211
+ config.should_receive(:jenkins).and_return(mock(:home => "some_nice_house"))
212
212
 
213
213
  subject.send(:new_file_path, "some_nice_house/and/then/a/room").should ==
214
214
  "backup_directory/and/then/a/room"
@@ -216,7 +216,7 @@ describe BackupJenkins::Backup do
216
216
  end
217
217
 
218
218
  describe "#plugin_files" do
219
- before { config.stub(:jenkins).and_return({ "home" => "home" }) }
219
+ before { config.stub_chain(:jenkins, :home).and_return("home") }
220
220
 
221
221
  it "returns a collection of directories including all the files that have .jpi" do
222
222
  Dir.should_receive(:[]).with("home/plugins/*.jpi").and_return(["jpi"])
@@ -238,7 +238,7 @@ describe BackupJenkins::Backup do
238
238
 
239
239
  describe "#files_to_remove" do
240
240
  before do
241
- config.stub(:backup).and_return("backups_to_keep" => { "local" => 2 })
241
+ config.stub_chain(:backup, :backups_to_keep, :local).and_return(2)
242
242
  subject.stub(:glob_of_backup_files).and_return(
243
243
  %w(old_file_1 old_file_2 old_file_3 old_file_4 old_file_5)
244
244
  )
@@ -278,14 +278,13 @@ describe BackupJenkins::Backup do
278
278
 
279
279
  describe "#timestamp" do
280
280
  it "should return the current time with the desired timestamp from the config" do
281
- config.stub(:backup).and_return("timestamp" => "%Y%m%d_%H%M")
282
281
  Time.stub(:now).and_return(Time.parse("2013/01/01 12:34PM"))
283
282
  subject.send(:timestamp).should == "20130101_1234"
284
283
  end
285
284
  end
286
285
 
287
286
  describe "#user_content_files" do
288
- before { config.stub(:jenkins).and_return({ "home" => "home" }) }
287
+ before { config.stub_chain(:jenkins, :home).and_return("home") }
289
288
 
290
289
  it "should return files inside the userContent directory" do
291
290
  Dir.should_receive(:[]).with("home/userContent/*").and_return(["my_file"])
@@ -30,8 +30,8 @@ describe BackupJenkins::Config do
30
30
 
31
31
  describe "#config_file" do
32
32
  it "should load file" do
33
- YAML.should_receive(:load_file).and_return("configuration")
34
- subject.instance_variable_get(:"@config").should == "configuration"
33
+ YAML.should_receive(:load_file).and_return({ :a => :b })
34
+ subject.instance_variable_get(:"@config").should == Hashie::Mash.new({ :a => :b })
35
35
  end
36
36
 
37
37
  it "should exit with non 0 on error" do
@@ -11,7 +11,7 @@ class SpecFormatterIncluder
11
11
  end
12
12
 
13
13
  describe BackupJenkins::Formatter do
14
- let(:config) { mock(:backup => { "file_name_base" => "jenkins" }) }
14
+ let(:config) { mock(:backup => mock(:file_name_base => "jenkins")) }
15
15
  subject { SpecFormatterIncluder.new(config) }
16
16
 
17
17
  describe "#format_backup_file_data" do
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.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ 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: &32124580 !ruby/object:Gem::Requirement
16
+ requirement: &29093460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *32124580
24
+ version_requirements: *29093460
25
+ - !ruby/object:Gem::Dependency
26
+ name: hashie
27
+ requirement: &29093040 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *29093040
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rake
27
- requirement: &32124160 !ruby/object:Gem::Requirement
38
+ requirement: &29092620 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,7 +43,7 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *32124160
46
+ version_requirements: *29092620
36
47
  description: Simple Jenkins config and plugin backup to S3
37
48
  email:
38
49
  - jcmuller@gmail.com