From 891fc5986f086985fbd8d6a943f6bafab5503307 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 12 May 2016 22:24:54 +0200 Subject: [PATCH] some error handling --- app/controllers/dashboard_controller.rb | 13 ++++---- app/models/certificate.rb | 4 +-- app/models/host.rb | 12 ++++++-- app/models/lxd/api.rb | 39 ++++++++++++++++++++--- app/models/lxd/api/exception.rb | 3 ++ app/models/lxd/api/v1_0.rb | 6 +++- app/models/lxd/certificate.rb | 1 + app/models/lxd/config.rb | 1 + app/views/dashboard/index.html.erb | 41 ++++++++++++++++++++----- app/views/hosts/_form.html.erb | 8 ----- 10 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 app/models/lxd/api/exception.rb diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 21e702e..26ec0fa 100644 --- a/app/controllers/dashboard_controller.rb +++ b/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: diff --git a/app/models/certificate.rb b/app/models/certificate.rb index 229532e..3fa9406 100644 --- a/app/models/certificate.rb +++ b/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 diff --git a/app/models/host.rb b/app/models/host.rb index 207233c..57a2846 100644 --- a/app/models/host.rb +++ b/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,8 +35,16 @@ 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 + def api certificate=nil @api ||= Lxd::API.get self, certificate end end diff --git a/app/models/lxd/api.rb b/app/models/lxd/api.rb index f8a5ae1..250b957 100644 --- a/app/models/lxd/api.rb +++ b/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 diff --git a/app/models/lxd/api/exception.rb b/app/models/lxd/api/exception.rb new file mode 100644 index 0000000..d497d63 --- /dev/null +++ b/app/models/lxd/api/exception.rb @@ -0,0 +1,3 @@ +class Lxd::API::Exception < StandardError +end +# vim: set et ts=2 sw=2: diff --git a/app/models/lxd/api/v1_0.rb b/app/models/lxd/api/v1_0.rb index 1a1e934..18fd3be 100644 --- a/app/models/lxd/api/v1_0.rb +++ b/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 diff --git a/app/models/lxd/certificate.rb b/app/models/lxd/certificate.rb index ce53c83..061a6b4 100644 --- a/app/models/lxd/certificate.rb +++ b/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) } diff --git a/app/models/lxd/config.rb b/app/models/lxd/config.rb index 9d6fc43..a85a693 100644 --- a/app/models/lxd/config.rb +++ b/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 diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index c295325..3e4deb8 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -1,12 +1,37 @@

Dashboard#index

-<% Certificate.all.each do |cert| -%> -

Fingerprint: <%= cert.cert_fpr %>  -Serial: <%= cert.cert.serial %>

-<% end -%> +

+

Lex-deeit certificate

+

Fingerprint

+<%= Certificate.get.cert_fpr %> +

Serial

+<%= Certificate.get.cert.serial %> +


<% @hosts.each do |host| -%> -

<%= host.lxd_config.inspect %>

-<% host.lxd_certificates.each do |certificate| -%> -

<%= certificate.fingerprint %>

-<% end -%> +

+

<%= host.name %>

+

Url:

+ <%= host.uri %> +

Connection status:

+ <% case -%> + <% when host.authenticated -%> + authenticated + <% when host.connected -%> + connected + <% else -%> + not connected + <% end -%> + <% if host.authenticated -%> +

Config:

+ <%= host.lxd_config.config %> +

Host known certificates

+ + <% end -%> +

+
<% end -%> + diff --git a/app/views/hosts/_form.html.erb b/app/views/hosts/_form.html.erb index 8a8fcb7..6757d83 100644 --- a/app/views/hosts/_form.html.erb +++ b/app/views/hosts/_form.html.erb @@ -19,14 +19,6 @@ <%= f.label :uri %>
<%= f.text_field :uri %> -
- <%= f.label :password %>
- <%= f.password_field :password %> -
-
- <%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation %> -
<%= f.submit %>