Browse Source

some error handling

master
Georg Hopp 10 years ago
parent
commit
891fc5986f
  1. 13
      app/controllers/dashboard_controller.rb
  2. 4
      app/models/certificate.rb
  3. 10
      app/models/host.rb
  4. 39
      app/models/lxd/api.rb
  5. 3
      app/models/lxd/api/exception.rb
  6. 6
      app/models/lxd/api/v1_0.rb
  7. 1
      app/models/lxd/certificate.rb
  8. 1
      app/models/lxd/config.rb
  9. 39
      app/views/dashboard/index.html.erb
  10. 8
      app/views/hosts/_form.html.erb

13
app/controllers/dashboard_controller.rb

@ -2,13 +2,12 @@ class DashboardController < ApplicationController
def index
@hosts = Host.all
@hosts.map { |host|
if host.lxd_config.auth == 'untrusted'
session[:return_to] = request.env["REQUEST_URI"]
redirect_to controller: 'hosts', action: 'auth', id: host.id
return
end
}
# @hosts.map { |host|
# if host.connected and not host.authenticated
# session[:return_to] = request.env["REQUEST_URI"]
# redirect_to controller: 'hosts', action: 'auth', id: host.id
# end
# }
end
end
# vim: set et ts=2 sw=2:

4
app/models/certificate.rb

@ -7,9 +7,7 @@ class Certificate < ActiveRecord::Base
def self.get
@@cert ||= find_by active: true
@@cert ||= create
if @@cert.is_expired?
@@cert = @@cert.update
end
@@cert = @@cert.update if @@cert.expires_soon?
@@cert
end

10
app/models/host.rb

@ -10,7 +10,7 @@ class Host < ActiveRecord::Base
super(true)
when super.expires_soon?
old = super
new = Certificate.get.update
new = Certificate.get
Lxd::Certificate.new(api: api(old), certificate: new.to_s).add
self.certificate_id = new.id
self.save
@ -35,6 +35,14 @@ class Host < ActiveRecord::Base
Lxd::Certificate.new(api: api).add password
end
def connected
lxd_config != nil
end
def authenticated
lxd_config != nil and lxd_config.auth == 'trusted'
end
private
def api certificate=nil
@api ||= Lxd::API.get self, certificate

39
app/models/lxd/api.rb

@ -1,6 +1,7 @@
module Lxd::API
def self.get host, certificate = nil
certificate ||= host.certificate
uri = URI.parse host.uri
con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443
con.use_ssl = true
@ -8,14 +9,33 @@ module Lxd::API
con.key = OpenSSL::PKey::RSA.new certificate.key
con.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp = self.call con, Net::HTTP::Get.new('/')
return Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0'
raise "unsupported api version"
resp = call con, Net::HTTP::Get.new('/')
api = Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0'
raise Lxd::API::Exception 'unsupported api version' unless api
return api unless block_given?
yield api
rescue Lxd::API::Exception => e
Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" }
nil
rescue => e
Rails.logger.error {
format(
'Error connecting: %s, %s %s',
host.uri, e.message, e.backtrace.join("\n")
)
}
nil
ensure
con.close if block_given?
end
def self.call con, req
resp = con.request req
raise "request failure: " + resp.code unless resp.code != 200
unless resp.code != 200
raise Lxd::API::Exception(
"request failure: (#{resp.code}) #{resp.message}"
)
end
JSON.parse resp.body
end
@ -25,6 +45,17 @@ module Lxd::API
def call req
handle_response(Lxd::API.call @con, req)
rescue Lxd::API::Exception => e
Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" }
nil
rescue => e
Rails.logger.error {
format(
'Error connecting: %s, %s %s',
host.uri, e.message, e.backtrace.join("\n")
)
}
nil
end
def get uri

3
app/models/lxd/api/exception.rb

@ -0,0 +1,3 @@
class Lxd::API::Exception < StandardError
end
# vim: set et ts=2 sw=2:

6
app/models/lxd/api/v1_0.rb

@ -48,7 +48,11 @@ class Lxd::API::V1_0
400 to 599: negative action result
600 to 999: future use
"""
raise "api error: (" + resp['error_code'].to_s + ") " + resp['error'] if resp['error_code'] and resp['error_code'] != 403
if resp['error_code'] and resp['error_code'] != 403
raise Lxd::API::Exception(
"api error: (#{resp['error_code']}) #{resp['error']}"
)
end
resp['metadata']
end
end

1
app/models/lxd/certificate.rb

@ -4,6 +4,7 @@ class Lxd::Certificate
attr_accessor :api, :uri, :type, :certificate, :fingerprint
def self.all api
return [] unless api
api.certificates.map { |cert|
Lxd::Certificate.new({api: api}.merge cert)
}

1
app/models/lxd/config.rb

@ -5,6 +5,7 @@ class Lxd::Config
:config, :environment, :public
def self.get api
return nil unless api
Lxd::Config.new({api: api}.merge api.config)
end

39
app/views/dashboard/index.html.erb

@ -1,12 +1,37 @@
<h1>Dashboard#index</h1>
<% Certificate.all.each do |cert| -%>
<p>Fingerprint: <%= cert.cert_fpr %>&nbsp;
Serial: <%= cert.cert.serial %></p>
<% end -%>
<p>
<h2>Lex-deeit certificate</h2>
<h3>Fingerprint</h3>
<%= Certificate.get.cert_fpr %>
<h3>Serial</h3>
<%= Certificate.get.cert.serial %>
</p>
<hr/>
<% @hosts.each do |host| -%>
<p><%= host.lxd_config.inspect %></p>
<% host.lxd_certificates.each do |certificate| -%>
<p><%= certificate.fingerprint %></p>
<p>
<h2><%= host.name %></h2>
<h3>Url:</h3>
<%= host.uri %>
<h3>Connection status:</h3>
<% case -%>
<% when host.authenticated -%>
authenticated
<% when host.connected -%>
connected
<% else -%>
not connected
<% end -%>
<% if host.authenticated -%>
<h3>Config:</h3>
<%= host.lxd_config.config %>
<h3>Host known certificates</h3>
<ul>
<% host.authenticated and host.lxd_certificates.each do |certificate| -%>
<li><%= certificate.fingerprint %></li>
<% end -%>
</ul>
<% end -%>
</p>
<hr/>
<% end -%>
<!-- vim: set ts=2 sw=2: -->

8
app/views/hosts/_form.html.erb

@ -19,14 +19,6 @@
<%= f.label :uri %><br>
<%= f.text_field :uri %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit %>
</div>

Loading…
Cancel
Save