host.rb
1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Host < ActiveRecord::Base
belongs_to :certificate
def certificate
# ensure that we always use a current, working non expired certificate.
case
when super.nil?
self.certificate_id = Certificate.get.id
self.save
super(true)
when super.expires_soon?
old = super
new = Certificate.get
Lxd::Certificate.new(api: api(old), certificate: new.to_s).add
self.certificate_id = new.id
self.save
@api = nil # enforce new api to get the new certificate used.
# finally remove the old certificate from lxd
Lxd::Certificate.new(api: api(new), fingerprint: old.cert_fpr).delete
super(true)
else
super
end
end
def lxd_config
Lxd::Config.get api
end
def lxd_certificates
Lxd::Certificate.all api
end
def lxd_authenticate password
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
end
end
# vim: ts=2 sw=2: