Commit 891fc5986f086985fbd8d6a943f6bafab5503307

Authored by Georg Hopp
1 parent 0b306511

some error handling

@@ -2,13 +2,12 @@ class DashboardController < ApplicationController @@ -2,13 +2,12 @@ class DashboardController < ApplicationController
2 def index 2 def index
3 @hosts = Host.all 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 end 11 end
13 end 12 end
14 # vim: set et ts=2 sw=2: 13 # vim: set et ts=2 sw=2:
@@ -7,9 +7,7 @@ class Certificate < ActiveRecord::Base @@ -7,9 +7,7 @@ class Certificate < ActiveRecord::Base
7 def self.get 7 def self.get
8 @@cert ||= find_by active: true 8 @@cert ||= find_by active: true
9 @@cert ||= create 9 @@cert ||= create
10 - if @@cert.is_expired?  
11 - @@cert = @@cert.update  
12 - end 10 + @@cert = @@cert.update if @@cert.expires_soon?
13 @@cert 11 @@cert
14 end 12 end
15 13
@@ -10,7 +10,7 @@ class Host < ActiveRecord::Base @@ -10,7 +10,7 @@ class Host < ActiveRecord::Base
10 super(true) 10 super(true)
11 when super.expires_soon? 11 when super.expires_soon?
12 old = super 12 old = super
13 - new = Certificate.get.update 13 + new = Certificate.get
14 Lxd::Certificate.new(api: api(old), certificate: new.to_s).add 14 Lxd::Certificate.new(api: api(old), certificate: new.to_s).add
15 self.certificate_id = new.id 15 self.certificate_id = new.id
16 self.save 16 self.save
@@ -35,8 +35,16 @@ class Host < ActiveRecord::Base @@ -35,8 +35,16 @@ class Host < ActiveRecord::Base
35 Lxd::Certificate.new(api: api).add password 35 Lxd::Certificate.new(api: api).add password
36 end 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 private 46 private
39 - def api certificate = nil 47 + def api certificate=nil
40 @api ||= Lxd::API.get self, certificate 48 @api ||= Lxd::API.get self, certificate
41 end 49 end
42 end 50 end
1 module Lxd::API 1 module Lxd::API
2 def self.get host, certificate = nil 2 def self.get host, certificate = nil
3 certificate ||= host.certificate 3 certificate ||= host.certificate
  4 +
4 uri = URI.parse host.uri 5 uri = URI.parse host.uri
5 con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443 6 con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443
6 con.use_ssl = true 7 con.use_ssl = true
@@ -8,14 +9,33 @@ module Lxd::API @@ -8,14 +9,33 @@ module Lxd::API
8 con.key = OpenSSL::PKey::RSA.new certificate.key 9 con.key = OpenSSL::PKey::RSA.new certificate.key
9 con.verify_mode = OpenSSL::SSL::VERIFY_NONE 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 end 30 end
15 31
16 def self.call con, req 32 def self.call con, req
17 resp = con.request req 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 JSON.parse resp.body 39 JSON.parse resp.body
20 end 40 end
21 41
@@ -25,6 +45,17 @@ module Lxd::API @@ -25,6 +45,17 @@ module Lxd::API
25 45
26 def call req 46 def call req
27 handle_response(Lxd::API.call @con, req) 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 end 59 end
29 60
30 def get uri 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,7 +48,11 @@ class Lxd::API::V1_0
48 400 to 599: negative action result 48 400 to 599: negative action result
49 600 to 999: future use 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 resp['metadata'] 56 resp['metadata']
53 end 57 end
54 end 58 end
@@ -4,6 +4,7 @@ class Lxd::Certificate @@ -4,6 +4,7 @@ class Lxd::Certificate
4 attr_accessor :api, :uri, :type, :certificate, :fingerprint 4 attr_accessor :api, :uri, :type, :certificate, :fingerprint
5 5
6 def self.all api 6 def self.all api
  7 + return [] unless api
7 api.certificates.map { |cert| 8 api.certificates.map { |cert|
8 Lxd::Certificate.new({api: api}.merge cert) 9 Lxd::Certificate.new({api: api}.merge cert)
9 } 10 }
@@ -5,6 +5,7 @@ class Lxd::Config @@ -5,6 +5,7 @@ class Lxd::Config
5 :config, :environment, :public 5 :config, :environment, :public
6 6
7 def self.get api 7 def self.get api
  8 + return nil unless api
8 Lxd::Config.new({api: api}.merge api.config) 9 Lxd::Config.new({api: api}.merge api.config)
9 end 10 end
10 11
1 <h1>Dashboard#index</h1> 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 <hr/> 9 <hr/>
7 <% @hosts.each do |host| -%> 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 <% end -%> 36 <% end -%>
  37 +<!-- vim: set ts=2 sw=2: -->
@@ -19,14 +19,6 @@ @@ -19,14 +19,6 @@
19 <%= f.label :uri %><br> 19 <%= f.label :uri %><br>
20 <%= f.text_field :uri %> 20 <%= f.text_field :uri %>
21 </div> 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 <div class="actions"> 22 <div class="actions">
31 <%= f.submit %> 23 <%= f.submit %>
32 </div> 24 </div>
Please register or login to post a comment