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

read-only attributes

methods:

Attributes

model[R]
parent[RW]
path[RW]
state[R]
synchronized[RW]

Public Class Methods

resolve(path) click to toggle source

Helper methods for resource module

# File lib/sfpagent/module.rb, line 87
def self.resolve(path)
        @@resource.resolve(path)
end

Public Instance Methods

init(model={}) click to toggle source
# File lib/sfpagent/module.rb, line 71
def init(model={})
        @state = {}
        @model = (model.length <= 0 ? {} : Sfp.to_ruby(model))
        @synchronized = []
end
update_state() click to toggle source
# File lib/sfpagent/module.rb, line 77
def update_state
        @state = {}
end

Protected Instance Methods

copy(source, destination) click to toggle source
# File lib/sfpagent/module.rb, line 115
def copy(source, destination)
        shell "cp -rf #{source} #{destination}"
end
download(source, destination) click to toggle source
# 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
log() click to toggle source
# File lib/sfpagent/module.rb, line 107
def log
        Sfp::Agent.logger
end
render(string, map={}) click to toggle source
# 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
render_file(file, map={}) click to toggle source
# 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
resolve(path)
Alias for: resolve_state
resolve_model(path) click to toggle source
# File lib/sfpagent/module.rb, line 103
def resolve_model(path)
        Sfp::Agent.resolve_model(path)
end
resolve_state(path) click to toggle source
# File lib/sfpagent/module.rb, line 97
def resolve_state(path)
        Sfp::Agent.resolve(path)
end
Also aliased as: resolve
shell(cmd) click to toggle source
# File lib/sfpagent/module.rb, line 111
def shell(cmd)
        !!system(cmd)
end
to_model() click to toggle source
# File lib/sfpagent/module.rb, line 92
def to_model
        @state = {}
        @model.each { |k,v| @state[k] = v }
end
use_http_proxy?(uri) click to toggle source
# 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