Commit 891fc5986f086985fbd8d6a943f6bafab5503307

Authored by Georg Hopp
1 parent 0b306511

some error handling

... ... @@ -2,13 +2,12 @@ class DashboardController < ApplicationController
2 2 def index
3 3 @hosts = Host.all
4 4
5   - @hosts.map { |host|
6   - if host.lxd_config.auth == 'untrusted'
7   - session[:return_to] = request.env["REQUEST_URI"]
8   - redirect_to controller: 'hosts', action: 'auth', id: host.id
9   - return
10   - end
11   - }
  5 +# @hosts.map { |host|
  6 +# if host.connected and not host.authenticated
  7 +# session[:return_to] = request.env["REQUEST_URI"]
  8 +# redirect_to controller: 'hosts', action: 'auth', id: host.id
  9 +# end
  10 +# }
12 11 end
13 12 end
14 13 # vim: set et ts=2 sw=2:
... ...
... ... @@ -7,9 +7,7 @@ class Certificate < ActiveRecord::Base
7 7 def self.get
8 8 @@cert ||= find_by active: true
9 9 @@cert ||= create
10   - if @@cert.is_expired?
11   - @@cert = @@cert.update
12   - end
  10 + @@cert = @@cert.update if @@cert.expires_soon?
13 11 @@cert
14 12 end
15 13
... ...
... ... @@ -10,7 +10,7 @@ class Host < ActiveRecord::Base
10 10 super(true)
11 11 when super.expires_soon?
12 12 old = super
13   - new = Certificate.get.update
  13 + new = Certificate.get
14 14 Lxd::Certificate.new(api: api(old), certificate: new.to_s).add
15 15 self.certificate_id = new.id
16 16 self.save
... ... @@ -35,8 +35,16 @@ class Host < ActiveRecord::Base
35 35 Lxd::Certificate.new(api: api).add password
36 36 end
37 37
  38 + def connected
  39 + lxd_config != nil
  40 + end
  41 +
  42 + def authenticated
  43 + lxd_config != nil and lxd_config.auth == 'trusted'
  44 + end
  45 +
38 46 private
39   - def api certificate = nil
  47 + def api certificate=nil
40 48 @api ||= Lxd::API.get self, certificate
41 49 end
42 50 end
... ...
1 1 module Lxd::API
2 2 def self.get host, certificate = nil
3 3 certificate ||= host.certificate
  4 +
4 5 uri = URI.parse host.uri
5 6 con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443
6 7 con.use_ssl = true
... ... @@ -8,14 +9,33 @@ module Lxd::API
8 9 con.key = OpenSSL::PKey::RSA.new certificate.key
9 10 con.verify_mode = OpenSSL::SSL::VERIFY_NONE
10 11
11   - resp = self.call con, Net::HTTP::Get.new('/')
12   - return Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0'
13   - raise "unsupported api version"
  12 + resp = call con, Net::HTTP::Get.new('/')
  13 + api = Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0'
  14 + raise Lxd::API::Exception 'unsupported api version' unless api
  15 + return api unless block_given?
  16 + yield api
  17 + rescue Lxd::API::Exception => e
  18 + Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" }
  19 + nil
  20 + rescue => e
  21 + Rails.logger.error {
  22 + format(
  23 + 'Error connecting: %s, %s %s',
  24 + host.uri, e.message, e.backtrace.join("\n")
  25 + )
  26 + }
  27 + nil
  28 + ensure
  29 + con.close if block_given?
14 30 end
15 31
16 32 def self.call con, req
17 33 resp = con.request req
18   - raise "request failure: " + resp.code unless resp.code != 200
  34 + unless resp.code != 200
  35 + raise Lxd::API::Exception(
  36 + "request failure: (#{resp.code}) #{resp.message}"
  37 + )
  38 + end
19 39 JSON.parse resp.body
20 40 end
21 41
... ... @@ -25,6 +45,17 @@ module Lxd::API
25 45
26 46 def call req
27 47 handle_response(Lxd::API.call @con, req)
  48 + rescue Lxd::API::Exception => e
  49 + Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" }
  50 + nil
  51 + rescue => e
  52 + Rails.logger.error {
  53 + format(
  54 + 'Error connecting: %s, %s %s',
  55 + host.uri, e.message, e.backtrace.join("\n")
  56 + )
  57 + }
  58 + nil
28 59 end
29 60
30 61 def get uri
... ...
  1 +class Lxd::API::Exception < StandardError
  2 +end
  3 +# vim: set et ts=2 sw=2:
... ...
... ... @@ -48,7 +48,11 @@ class Lxd::API::V1_0
48 48 400 to 599: negative action result
49 49 600 to 999: future use
50 50 """
51   - raise "api error: (" + resp['error_code'].to_s + ") " + resp['error'] if resp['error_code'] and resp['error_code'] != 403
  51 + if resp['error_code'] and resp['error_code'] != 403
  52 + raise Lxd::API::Exception(
  53 + "api error: (#{resp['error_code']}) #{resp['error']}"
  54 + )
  55 + end
52 56 resp['metadata']
53 57 end
54 58 end
... ...
... ... @@ -4,6 +4,7 @@ class Lxd::Certificate
4 4 attr_accessor :api, :uri, :type, :certificate, :fingerprint
5 5
6 6 def self.all api
  7 + return [] unless api
7 8 api.certificates.map { |cert|
8 9 Lxd::Certificate.new({api: api}.merge cert)
9 10 }
... ...
... ... @@ -5,6 +5,7 @@ class Lxd::Config
5 5 :config, :environment, :public
6 6
7 7 def self.get api
  8 + return nil unless api
8 9 Lxd::Config.new({api: api}.merge api.config)
9 10 end
10 11
... ...
1 1 <h1>Dashboard#index</h1>
2   -<% Certificate.all.each do |cert| -%>
3   -<p>Fingerprint: <%= cert.cert_fpr %>&nbsp;
4   -Serial: <%= cert.cert.serial %></p>
5   -<% end -%>
  2 +<p>
  3 +<h2>Lex-deeit certificate</h2>
  4 +<h3>Fingerprint</h3>
  5 +<%= Certificate.get.cert_fpr %>
  6 +<h3>Serial</h3>
  7 +<%= Certificate.get.cert.serial %>
  8 +</p>
6 9 <hr/>
7 10 <% @hosts.each do |host| -%>
8   -<p><%= host.lxd_config.inspect %></p>
9   -<% host.lxd_certificates.each do |certificate| -%>
10   -<p><%= certificate.fingerprint %></p>
11   -<% end -%>
  11 + <p>
  12 + <h2><%= host.name %></h2>
  13 + <h3>Url:</h3>
  14 + <%= host.uri %>
  15 + <h3>Connection status:</h3>
  16 + <% case -%>
  17 + <% when host.authenticated -%>
  18 + authenticated
  19 + <% when host.connected -%>
  20 + connected
  21 + <% else -%>
  22 + not connected
  23 + <% end -%>
  24 + <% if host.authenticated -%>
  25 + <h3>Config:</h3>
  26 + <%= host.lxd_config.config %>
  27 + <h3>Host known certificates</h3>
  28 + <ul>
  29 + <% host.authenticated and host.lxd_certificates.each do |certificate| -%>
  30 + <li><%= certificate.fingerprint %></li>
  31 + <% end -%>
  32 + </ul>
  33 + <% end -%>
  34 + </p>
  35 + <hr/>
12 36 <% end -%>
  37 +<!-- vim: set ts=2 sw=2: -->
... ...
... ... @@ -19,14 +19,6 @@
19 19 <%= f.label :uri %><br>
20 20 <%= f.text_field :uri %>
21 21 </div>
22   - <div class="field">
23   - <%= f.label :password %><br>
24   - <%= f.password_field :password %>
25   - </div>
26   - <div class="field">
27   - <%= f.label :password_confirmation %><br>
28   - <%= f.password_field :password_confirmation %>
29   - </div>
30 22 <div class="actions">
31 23 <%= f.submit %>
32 24 </div>
... ...
Please register or login to post a comment