Blame view

script/sync 2.57 KB
3a47d364c   Jianwei Han   Remove caches for...
1
2
3
4
5
6
7
8
9
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  #!/usr/bin/ruby
  # Sync binary snapshots to S3.
  
  require "pathname"
  require "tempfile"
  
  # Put us where we belong, in the root dir of our boxen repo.
  
  Dir.chdir Pathname.new(__FILE__).realpath + "../.."
  
  # Make sure our local dependencies are up to date.
  
  abort "Sorry, can't bootstrap." unless system "script/bootstrap"
  
  # Set up our local configuration, deps, and load path.
  
  load "config/basic.rb"
  
  require "aws-sdk"
  require "boxen/config"
  
  access_key  = ENV["BOXEN_S3_ACCESS_KEY"]
  secret_key  = ENV["BOXEN_S3_SECRET_KEY"]
  bucket_name = ENV["BOXEN_S3_BUCKET"]
  
  unless access_key && secret_key && bucket_name
    abort "Please set the BOXEN_S3_{ACCESS_KEY,SECRET_KEY,BUCKET} env vars."
  end
  
  s3 = AWS::S3.new :access_key_id => access_key, :secret_access_key => secret_key
  os = `sw_vers -productVersion`.strip.split(".")[0..1].join "."
  
  bucket = s3.buckets[bucket_name]
  config = Boxen::Config.load
  
  # Sync Homebrew packages.
  
  Dir.chdir "#{config.homedir}/homebrew/Cellar" do
    Dir["*/*"].each do |dir|
      name, version = File.split dir
  
      file = "homebrew/#{os}/#{name}-#{version}.tar.bz2"
      temp = Tempfile.new "homebrew"
      obj  = bucket.objects[file]
  
      next if obj.exists?
  
      printf "Snapshotting #{name} #{version}... "
      $stdout.flush
  
      system "tar", "-cjf", temp.path, dir
      puts "done."
  
      printf "Shipping #{name} #{version} to S3... "
      $stdout.flush
  
      obj.write :acl => :public_read, :file => temp.path
      puts "done."
    end
  end
  
  # Sync rbenv rubies.
  
  Dir.chdir "#{config.homedir}/rbenv/versions" do
    Dir["*"].each do |dir|
      next if File.symlink? dir
  
      version = File.basename dir
      file    = "rbenv/#{os}/#{version}.tar.bz2"
      temp    = Tempfile.new "rbenv"
      obj     = bucket.objects[file]
  
      next if obj.exists?
  
      printf "Snapshotting ruby #{version}... "
      $stdout.flush
  
      system "tar -cjf #{temp.path} #{version}"
      puts "done."
  
      printf "Shipping ruby #{version} to S3... "
      $stdout.flush
  
      obj.write :acl => :public_read, :file => temp.path
      puts "done."
    end
  end
  
  # Sync NVM nodes.
  
  Dir.chdir "#{config.homedir}/nodenv/versions" do
    Dir["*"].each do |dir|
      next if File.symlink? dir
  
      version = File.basename dir
      file    = "nodenv/#{os}/#{version}.tar.bz2"
      temp    = Tempfile.new "nodenv"
      obj     = bucket.objects[file]
  
      next if obj.exists?
  
      printf "Snapshotting node.js #{version}... "
      $stdout.flush
  
      system "tar -cjf #{temp.path} #{version}"
      puts "done."
  
      printf "Shipping node.js #{version} to S3... "
      $stdout.flush
  
      obj.write :acl => :public_read, :file => temp.path
      puts "done."
    end
  end