module Sfp::Resource
Module
Sfp::Resource
must be included by every module. It provides standard methods which are used by Runtime
engine in mapping between SFP object and schema implementation.
accessible attributes
-
parent : holds instance of parent’s object
-
synchronized : an list of SFP procedures that must be executed in
serial
-
path : an absolute path of this instance
read-only attributes
-
state : holds the current state of this module instance
-
model : holds the model of desired state of this module
instance
methods:
-
init : invoked by
Runtime
engine after instantiating thismodule instance for initialization
-
update_state
: invoked byRuntime
engine to request this moduleinstance to update the current state which should be kept in attribute @state
-
to_model
: can be invoked by this module instance to set thecurrent state equals the desired state (model), or in short: @state == @model
-
resolve_state
: can be invoked by this module to resolve givenreference of current state either local or other module instances
-
resolve : an alias to method
resolve_state
-
resolve_model
: can be invoked by this module to resolve givenreference of desired state (model) either local or other module instances
-
log : return logger object
-
copy : copy a file, whose path is the first parameter, to
a destination path given in the second parameter
-
render : render given template file, whose path is the first
parameter, and the template's variable is a merged between a Hash in the second parameter with model; the result is returned as a string
-
render_file
: render given template file, whose path is the firstparameter, and the template's variable is a merged between a Hash on the second parameter with model; the result is written back to the file
Attributes
Public Class Methods
Helper
methods for resource module
# File lib/sfpagent/module.rb, line 87 def self.resolve(path) @@resource.resolve(path) end
Public Instance Methods
# File lib/sfpagent/module.rb, line 71 def init(model={}) @state = {} @model = (model.length <= 0 ? {} : Sfp.to_ruby(model)) @synchronized = [] end
# File lib/sfpagent/module.rb, line 77 def update_state @state = {} end
Protected Instance Methods
# File lib/sfpagent/module.rb, line 115 def copy(source, destination) shell "cp -rf #{source} #{destination}" end
# File lib/sfpagent/module.rb, line 131 def download(source, destination) def use_http_proxy?(uri) ENV['no_proxy'].to_s.split(',').each { |pattern| pattern.chop! if pattern[-1] == '*' return false if uri.host[0, pattern.length] == pattern } true end file = nil begin uri = URI.parse(source) http = nil if use_http_proxy?(uri) and ENV['http_proxy'].to_s.strip.length > 0 begin proxy = URI.parse(ENV['http_proxy']) http = Net::HTTP::Proxy(proxy.host, proxy.port).new(uri.host, uri.port) rescue Exception => e log.info "Invalid http_proxy=#{ENV['http_proxy']}" http = Net::HTTP.new(uri.host, uri.port) end else http = Net::HTTP.new(uri.host, uri.port) end http.request_get(uri.path) do |response| file = ::File.open(destination, 'wb') response.read_body do |segment| file.write segment end file.flush end ensure file.close if not file.nil? end end
# File lib/sfpagent/module.rb, line 107 def log Sfp::Agent.logger end
# File lib/sfpagent/module.rb, line 119 def render(string, map={}) model = @model.clone map.each { |k,v| model[k] = v } ::Sfp::Template.render(string, model) end
# File lib/sfpagent/module.rb, line 125 def render_file(file, map={}) model = @model.clone map.each { |k,v| model[k] = v } ::Sfp::Template.render_file(file, model) end
# File lib/sfpagent/module.rb, line 103 def resolve_model(path) Sfp::Agent.resolve_model(path) end
# File lib/sfpagent/module.rb, line 97 def resolve_state(path) Sfp::Agent.resolve(path) end
# File lib/sfpagent/module.rb, line 111 def shell(cmd) !!system(cmd) end
# File lib/sfpagent/module.rb, line 92 def to_model @state = {} @model.each { |k,v| @state[k] = v } end
# File lib/sfpagent/module.rb, line 132 def use_http_proxy?(uri) ENV['no_proxy'].to_s.split(',').each { |pattern| pattern.chop! if pattern[-1] == '*' return false if uri.host[0, pattern.length] == pattern } true end