Class: Anvil::Manifest

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/anvil/manifest.rb

Constant Summary collapse

PUSH_THREAD_COUNT =
40

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#anvil_metadata_dir, #is_url?, #json_decode, #json_encode, #read_anvil_metadata, #write_anvil_metadata

Constructor Details

#initialize(dir = nil, options = {}) ⇒ Manifest

Returns a new instance of Manifest.



19
20
21
22
23
24
# File 'lib/anvil/manifest.rb', line 19

def initialize(dir=nil, options={})
  @dir = dir
  @ignore = options[:ignore] || []
  @manifest = @dir ? directory_manifest(@dir, :ignore => @ignore) : {}
  @cache_url = options[:cache]
end

Instance Attribute Details

#cache_urlObject (readonly)

Returns the value of attribute cache_url.



15
16
17
# File 'lib/anvil/manifest.rb', line 15

def cache_url
  @cache_url
end

#dirObject (readonly)

Returns the value of attribute dir.



16
17
18
# File 'lib/anvil/manifest.rb', line 16

def dir
  @dir
end

#manifestObject (readonly)

Returns the value of attribute manifest.



17
18
19
# File 'lib/anvil/manifest.rb', line 17

def manifest
  @manifest
end

Instance Method Details

#add(filename) ⇒ Object



111
112
113
# File 'lib/anvil/manifest.rb', line 111

def add(filename)
  @manifest[filename] = file_manifest(filename)
end

#build(options = {}) ⇒ Object



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
# File 'lib/anvil/manifest.rb', line 26

def build(options={})
  uri  = URI.parse("#{anvil_host}/manifest/build")
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  req = Net::HTTP::Post.new uri.request_uri

  env = options[:env] || {}

  req.initialize_http_header "User-Agent" => Anvil.agent
  req["User-Agent"] = Anvil.agent

  Anvil.headers.each do |name, val|
    next if name.to_s.strip == ""
    req[name] = val.to_s
  end

  req.set_form_data({
    "buildpack" => options[:buildpack],
    "cache"     => @cache_url,
    "env"       => json_encode(options[:env] || {}),
    "keepalive" => "1",
    "manifest"  => self.to_json,
    "type"      => options[:type]
  })

  slug_url = nil

  http.request(req) do |res|
    slug_url = res["x-slug-url"]
    @cache_url = res["x-cache-url"]

    begin
      res.read_body do |chunk|
        yield chunk.gsub("\000\000\000", "")
      end
    rescue EOFError
      puts
      raise Anvil::Builder::BuildError, "terminated unexpectedly"
    end

    code = if res["x-exit-code"].nil?
      manifest_id = Array(res["x-manifest-id"]).first
      Integer(String.new(anvil["/exit/#{manifest_id}"].get.to_s))
    else
      res["x-exit-code"].first.to_i
    end

    raise Anvil::Builder::BuildError, "exited #{code}" unless code.zero?
  end

  slug_url
end

#manifest_by_hash(manifest) ⇒ Object



89
90
91
92
93
# File 'lib/anvil/manifest.rb', line 89

def manifest_by_hash(manifest)
  manifest.inject({}) do |ax, (name, file)|
    ax.update file["hash"] => file.merge("name" => name)
  end
end

#missingObject



95
96
97
98
99
100
# File 'lib/anvil/manifest.rb', line 95

def missing
  mbh = manifest_by_hash(@manifest)
  json_decode(anvil["/manifest/diff"].post(:manifest => self.to_json).to_s).inject({}) do |ax, hash|
    ax.update hash => mbh[hash]
  end
end

#saveObject



84
85
86
87
# File 'lib/anvil/manifest.rb', line 84

def save
  res = anvil["/manifest"].post(:manifest => self.to_json)
  res.headers[:location]
end

#to_jsonObject



107
108
109
# File 'lib/anvil/manifest.rb', line 107

def to_json
  json_encode(@manifest)
end

#upload(missing, &blk) ⇒ Object



102
103
104
105
# File 'lib/anvil/manifest.rb', line 102

def upload(missing, &blk)
  upload_hashes missing, &blk
  missing.length
end