Class: LogicalConstruct::GroundControl::Provision::WebConfigure

Inherits:
Mattock::Task
  • Object
show all
Includes:
ResolutionProtocol
Defined in:
lib/logical-construct/ground-control/provision.rb

Instance Method Summary collapse

Methods included from ResolutionProtocol

#digest, #file_checksum, #generate_checksum, #web_path

Instance Method Details

#actionObject

XXX I would like this to become an actual RESTful client at some point, but seems it would mean building it from scratch



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/logical-construct/ground-control/provision.rb', line 52

def action
  require 'uri'
  require 'rest-client'
  require 'nokogiri'

  until (link = resolution_needed.first).nil?
    href = link['href']
    begin
      response = RestClient.post(uri(:path => href), :data => resolve(href))
    rescue RestClient::InternalServerError => ex
      require 'tempfile'
      file = Tempfile.open('provision-error.html')
      path = file.path
      file.close!

      File::open(path, "w") do |file|
        file.write ex.http_body
      end
      puts "Written error response to #{path}"
      puts "Try: chromium #{path}"
      fail "Unsuccessful response from server!"
    end
  end
end

#resolution_neededObject



43
44
45
46
47
# File 'lib/logical-construct/ground-control/provision.rb', line 43

def resolution_needed
  index = RestClient.get(uri(:path => '/'))
  body = Nokogiri::HTML(index.body)
  return body.xpath("//a[@rel='requirement']")
end

#resolve(path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/logical-construct/ground-control/provision.rb', line 26

def resolve(path)
  resolved = web_resolutions.fetch(path)
  if resolved.respond_to? :call
    resolved = resolved.call
  end
  return resolved
rescue KeyError
  puts "Can't find a resolution for #{path} in #{web_resolutions.keys.inspect} (ex #{resolutions.keys})"
  raise
end

#resolve_runtime_configurationObject



18
19
20
21
22
23
24
# File 'lib/logical-construct/ground-control/provision.rb', line 18

def resolve_runtime_configuration
  super
  self.target_url ||= "#{target_protocol}://#{target_address}:#{target_port}/"
  self.web_resolutions = Hash[resolutions.map do |name, value|
    [web_path(name), value]
  end]
end

#uri(options) ⇒ Object



37
38
39
40
41
# File 'lib/logical-construct/ground-control/provision.rb', line 37

def uri(options)
  uri_class = URI.scheme_list[target_protocol.upcase]
  uri_hash = {:host => target_address, :port => target_port}
  return uri_class.build(uri_hash.merge(options)).to_s
end