1
|
/* -------------------------------------------------------------------------- */
|
2
|
/* Copyright 2016, Joachim Kraftmayer, Clyso GmbH - clyso.com */
|
3
|
/* */
|
4
|
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
|
5
|
/* not use this file except in compliance with the License. You may obtain */
|
6
|
/* a copy of the License at */
|
7
|
/* */
|
8
|
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
9
|
/* */
|
10
|
/* Unless required by applicable law or agreed to in writing, software */
|
11
|
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
12
|
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
13
|
/* See the License for the specific language governing permissions and */
|
14
|
/* limitations under the License. */
|
15
|
/* -------------------------------------------------------------------------- */
|
16
|
|
17
|
require 'date'
|
18
|
require 'fileutils'
|
19
|
|
20
|
class Rbdcache
|
21
|
def self.ls(auth, pool)
|
22
|
cache_dir = '/tmp/pools/'
|
23
|
|
24
|
if not File.directory?(cache_dir)
|
25
|
FileUtils.mkdir_p(cache_dir)
|
26
|
end
|
27
|
|
28
|
path= cache_dir + pool
|
29
|
time_interall_sec = 24 * 60 * 60
|
30
|
now = Time.now
|
31
|
|
32
|
if not File.directory?(cache_dir)
|
33
|
FileUtils.mkdir_p(cache_dir)
|
34
|
end
|
35
|
|
36
|
#Check if file exits
|
37
|
if not File.exist?( path )
|
38
|
# Cache file not exists
|
39
|
rbd_ls = `rbd #{auth} ls -p #{pool} --format xml`
|
40
|
File.open(path, 'w') { |file| file.write(rbd_ls) }
|
41
|
end
|
42
|
|
43
|
#check if cache is outdated or not
|
44
|
file_date = File.mtime( path )
|
45
|
if now > (file_date + time_interall_sec )
|
46
|
print "Time now"
|
47
|
rbd_ls = `rbd #{auth} ls -p #{pool} --format xml`
|
48
|
File.open(path, 'w') { |file| file.write(rbd_ls) }
|
49
|
else
|
50
|
#Cached in File
|
51
|
rbd_ls = File.read(path)
|
52
|
end
|
53
|
return rbd_ls
|
54
|
end
|
55
|
|
56
|
def self.info(auth=nil, pool, image)
|
57
|
cache_dir = '/tmp/pools/info/'
|
58
|
|
59
|
if not File.directory?(cache_dir)
|
60
|
FileUtils.mkdir_p(cache_dir)
|
61
|
end
|
62
|
|
63
|
cache_dir = cache_dir + pool
|
64
|
if not File.directory?(cache_dir)
|
65
|
FileUtils.mkdir_p(cache_dir)
|
66
|
end
|
67
|
|
68
|
cache_dir = '/tmp/pools/info/'
|
69
|
path = cache_dir + image
|
70
|
|
71
|
time_interall_sec = 24 * 60 * 60
|
72
|
now = Time.now
|
73
|
|
74
|
#Check if file exits
|
75
|
if not File.exist?( path )
|
76
|
# Cache file not exists
|
77
|
rbd_info = `rbd #{auth} info -p #{pool} --image #{image} --format xml`
|
78
|
File.open(path, 'w') { |file| file.write(rbd_info) }
|
79
|
end
|
80
|
|
81
|
#check if cache is outdated or not
|
82
|
file_date = File.mtime( path )
|
83
|
if now > (file_date + time_interall_sec )
|
84
|
rbd_info = `rbd #{auth} info -p #{pool} --image #{image} --format xml`
|
85
|
File.open(path, 'w') { |file| file.write(rbd_info) }
|
86
|
else
|
87
|
#Cached in File
|
88
|
#disabled # rbd_info = `rbd #{auth} info -p #{pool} --image #{image} --format xml`
|
89
|
rbd_info = File.read(path)
|
90
|
end
|
91
|
return rbd_info
|
92
|
|
93
|
end
|
94
|
end
|