Module: CFoundry::UploadHelpers

Included in:
V1::App, V2::App
Defined in:
lib/cfoundry/upload_helpers.rb

Constant Summary collapse

UPLOAD_EXCLUDE =

Default paths to exclude from upload payload.

%w{.git _darcs .svn}
RESOURCE_CHECK_LIMIT =

Minimum size for an application payload to bother checking resources.

64 * 1024

Instance Method Summary collapse

Instance Method Details



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cfoundry/upload_helpers.rb', line 97

def check_unreachable_links(path)
  files = Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)

  # only used for friendlier error message
  pwd = Pathname.pwd

  abspath = File.expand_path(path)
  unreachable = []
  files.each do |f|
    file = Pathname.new(f)
    if file.symlink? && !file.realpath.to_s.start_with?(abspath)
      unreachable << file.relative_path_from(pwd)
    end
  end

  unless unreachable.empty?
    root = Pathname.new(path).relative_path_from(pwd)
    raise CFoundry::Error,
      "Path contains links '#{unreachable}' that point outside '#{root}'"
  end
end

#determine_resources(path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cfoundry/upload_helpers.rb', line 124

def determine_resources(path)
  fingerprints, total_size = make_fingerprints(path)

  return if total_size <= RESOURCE_CHECK_LIMIT

  resources = @client.base.resource_match(fingerprints)

  resources.each do |resource|
    FileUtils.rm_f resource[:fn]
    resource[:fn].sub!("#{path}/", "")
  end

  resources
end

#find_sockets(path) ⇒ Object



119
120
121
122
# File 'lib/cfoundry/upload_helpers.rb', line 119

def find_sockets(path)
  files = Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
  files && files.select { |f| File.socket? f }
end

#make_fingerprints(path) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cfoundry/upload_helpers.rb', line 139

def make_fingerprints(path)
  fingerprints = []
  total_size = 0

  Dir.glob("#{path}/**/*", File::FNM_DOTMATCH) do |filename|
    next if File.directory?(filename)

    size = File.size(filename)

    total_size += size

    fingerprints << {
      :size => size,
      :sha1 => Digest::SHA1.file(filename).hexdigest,
      :fn => filename
    }
  end

  [fingerprints, total_size]
end

#prepare_package(path, to) ⇒ Object



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
# File 'lib/cfoundry/upload_helpers.rb', line 54

def prepare_package(path, to)
  if path =~ /\.(jar|war|zip)$/
    CFoundry::Zip.unpack(path, to)
  elsif war_file = Dir.glob("#{path}/*.war").first
    CFoundry::Zip.unpack(war_file, to)
  else
    check_unreachable_links(path)

    FileUtils.mkdir(to)

    files = Dir.glob("#{path}/{*,.[^\.]*}")

    exclude = UPLOAD_EXCLUDE
    if File.exists?("#{path}/.vmcignore")
      exclude += File.read("#{path}/.vmcignore").split(/\n+/)
    end

    # prevent initial copying if we can, remove sub-files later
    files.reject! do |f|
      exclude.any? do |e|
        File.fnmatch(f.sub(path + "/", ""), e)
      end
    end

    FileUtils.cp_r(files, to)

    find_sockets(to).each do |s|
      File.delete s
    end

    # remove ignored globs more thoroughly
    #
    # note that the above file list only includes toplevel
    # files/directories for cp_r, so this is where sub-files/etc. are
    # removed
    exclude.each do |e|
      Dir.glob("#{to}/#{e}").each do |f|
        FileUtils.rm_rf(f)
      end
    end
  end
end

#upload(path, check_resources = true) ⇒ Object

Upload application’s code to target. Do this after #create! and before #start!

path

A path pointing to either a directory, or a .jar, .war, or .zip file.

If a .vmcignore file is detected under the given path, it will be used to exclude paths from the payload, similar to a .gitignore.

check_resources

If set to ‘false`, the entire payload will be uploaded without checking the resource cache.

Only do this if you know what you’re doing.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cfoundry/upload_helpers.rb', line 31

def upload(path, check_resources = true)
  unless File.exist? path
    raise CFoundry::Error, "Invalid application path '#{path}'"
  end

  zipfile = "#{Dir.tmpdir}/#{@guid}.zip"
  tmpdir = "#{Dir.tmpdir}/.vmc_#{@guid}_files"

  FileUtils.rm_f(zipfile)
  FileUtils.rm_rf(tmpdir)

  prepare_package(path, tmpdir)

  resources = determine_resources(tmpdir) if check_resources

  packed = CFoundry::Zip.pack(tmpdir, zipfile)

  @client.base.upload_app(@guid, packed && zipfile, resources || [])
ensure
  FileUtils.rm_f(zipfile) if zipfile
  FileUtils.rm_rf(tmpdir) if tmpdir
end