/* -------------------------------------------------------------------------- */ /* Copyright 2016, Joachim Kraftmayer, Clyso GmbH - clyso.com */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); you may */ /* not use this file except in compliance with the License. You may obtain */ /* a copy of the License at */ /* */ /* http://www.apache.org/licenses/LICENSE-2.0 */ /* */ /* Unless required by applicable law or agreed to in writing, software */ /* distributed under the License is distributed on an "AS IS" BASIS, */ /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ /* See the License for the specific language governing permissions and */ /* limitations under the License. */ /* -------------------------------------------------------------------------- */ require 'date' require 'fileutils' class Rbdcache def self.ls(auth, pool) cache_dir = '/tmp/pools/' if not File.directory?(cache_dir) FileUtils.mkdir_p(cache_dir) end path= cache_dir + pool time_interall_sec = 24 * 60 * 60 now = Time.now if not File.directory?(cache_dir) FileUtils.mkdir_p(cache_dir) end #Check if file exits if not File.exist?( path ) # Cache file not exists rbd_ls = `rbd #{auth} ls -p #{pool} --format xml` File.open(path, 'w') { |file| file.write(rbd_ls) } end #check if cache is outdated or not file_date = File.mtime( path ) if now > (file_date + time_interall_sec ) print "Time now" rbd_ls = `rbd #{auth} ls -p #{pool} --format xml` File.open(path, 'w') { |file| file.write(rbd_ls) } else #Cached in File rbd_ls = File.read(path) end return rbd_ls end def self.info(auth=nil, pool, image) cache_dir = '/tmp/pools/info/' if not File.directory?(cache_dir) FileUtils.mkdir_p(cache_dir) end cache_dir = cache_dir + pool if not File.directory?(cache_dir) FileUtils.mkdir_p(cache_dir) end cache_dir = '/tmp/pools/info/' path = cache_dir + image time_interall_sec = 24 * 60 * 60 now = Time.now #Check if file exits if not File.exist?( path ) # Cache file not exists rbd_info = `rbd #{auth} info -p #{pool} --image #{image} --format xml` File.open(path, 'w') { |file| file.write(rbd_info) } end #check if cache is outdated or not file_date = File.mtime( path ) if now > (file_date + time_interall_sec ) rbd_info = `rbd #{auth} info -p #{pool} --image #{image} --format xml` File.open(path, 'w') { |file| file.write(rbd_info) } else #Cached in File #disabled # rbd_info = `rbd #{auth} info -p #{pool} --image #{image} --format xml` rbd_info = File.read(path) end return rbd_info end end