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)}
boxes_to_check = s3_boxes.select {|box| argv.include?(File.basename(box.key, '.box'))} unless argv.empty?
boxes_to_check ||= s3_boxes
boxes_to_check.each do |s3_box|
box_name = File.basename(s3_box.key, '.box')
local_box = @env.boxes.find(box_name, 'virtualbox', '~> 0')
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']
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
if (local_box.nil? || (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
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
|