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



128
129
130
# File 'lib/anvil/manifest.rb', line 128

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/anvil/manifest.rb', line 26

def build(options={})
  uri  = URI.parse("#{anvil_host}/manifest/build")

  if uri.scheme == "https"
    proxy = https_proxy
  else
    proxy = http_proxy
  end

  if proxy
    proxy_uri = URI.parse(proxy)
    http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
  else
    http = Net::HTTP.new(uri.host, uri.port)
  end

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

  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



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

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

#missingObject



112
113
114
115
116
117
# File 'lib/anvil/manifest.rb', line 112

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



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

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

#to_jsonObject



124
125
126
# File 'lib/anvil/manifest.rb', line 124

def to_json
  json_encode(@manifest)
end

#upload(missing, &blk) ⇒ Object



119
120
121
122
# File 'lib/anvil/manifest.rb', line 119

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