Class: S3boxCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/s3box.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



6
7
8
# File 'lib/commands/s3box.rb', line 6

def self.synopsis
  "This command updates available boxes from an s3 repo. See README for where to put AWS creds.  Virtualbox provider currently assumed"
end

Instance Method Details

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/commands/s3box.rb', line 10

def execute

  opt_hash = {}
  opt_hash[:clean] = false
  v_opts = OptionParser.new do |opts|
    opts.banner = 'Usage: vagrant s3box [-c] '
    opts.on( "-c", "--clean", "Remove boxes which are no longer backed by S3" ) do |opt|
      opt_hash[:clean] = opt
    end
    opts.on( "-p", "--pretend","Pretend but don't actually do anything" ) do |opt|
      opt_hash[:pretend] = opt
    end
  end

  argv = parse_options(v_opts)


  begin
    aws_creds = YAML.load_file("#{ENV['HOME']}/.s3_keys")
  rescue Exception => e
    @env.ui.error "You need a valid .s3_keys yaml file in your HOME dir, currently using: #{ENV['HOME']} as HOME path  See README for details"
  end

  AWS::S3::Base.establish_connection!(aws_creds)

  s3_boxes = AWS::S3::Bucket.find 'livefyre-vagrant-boxes'

  s3_boxes.reject {|box| File.extname(box.key)}
  # pp s3_boxes
  
  boxes_to_check = s3_boxes.select {|box| argv.include?(File.basename(box.key, '.box'))} unless argv.empty?
  boxes_to_check ||= s3_boxes
  # @env.ui.info boxes_to_check.inspect

  # Create or update boxes from S3

  boxes_to_check.each do |s3_box|
    box_name = File.basename(s3_box.key, '.box')
    local_box = @env.boxes.find(box_name, 'virtualbox', '~> 0')
    # puts local_box.directory unless local_box.nil?

    unless local_box.nil?
      local_date = DateTime.parse File.mtime(local_box.directory).utc.to_s
      s3_date = DateTime.parse s3_box.about['last-modified']
      # puts "NK: #{s3_date} > #{local_date} #{(s3_date > local_date)}"
    end

    if opt_hash[:pretend]
      @env.ui.info "Would add/update: #{box_name}, but skipping due to pretend" if (local_box.nil? || (s3_date > local_date))
    else
      # NK stupid force will result in an error instead of skipping if I try to add an existing box that doesn't need an update
      if (local_box.nil? || (s3_date > local_date))
        # puts "NK: #{s3_date} > #{local_date} #{(s3_date > local_date)}"
        @env.action_runner.run(Vagrant::Action.action_box_add, {
          :box_name     => box_name,
          :box_provider => 'virtualbox',
          :box_url      => s3_box.url,
          :box_force    => (!local_box.nil? && (s3_date > local_date))
          })
      end
    end
  end


  # Remove local boxes no longer on S3
  if opt_hash[:clean]
    box_list = boxes_to_check.map {|b| File.basename(b.key, '.box')}
    @env.boxes.all.each do |local_box|
      local_box_name = local_box[0]
      if opt_hash[:pretend]
        @env.ui.info "Would remove #{local_box_name} but skipping due to pretend" unless box_list.include?(local_box_name)
      else
        @env.boxes.find(local_box_name, 'virtualbox').destroy! unless box_list.include?(local_box_name)
      end
    end
  end

end