module Charmkit::Helpers
Public Class Methods
Reads from a embedded template in the rake task itself.
@param src String - The data found after __END__ @param dst String - Save location @param context Hash - variables to pass into template @return Boolean
@example
inline_template('vhost.conf', '/etc/nginx/sites-enabled/default') server_name: "example.com") __END__ @@ vhost.conf server { name <%= server_name %> }
# File lib/charmkit/helpers/template.rb, line 50 def inline_template(name, dst, **context) templates = {} begin app, data = File.read(caller.first.split(":").first).split("__END__", 2) rescue Errno::ENOENT app, data = nil end data.strip! if data template = nil data.each_line do |line| if line =~ /^@@\s*(.*\S)\s*$/ template = String.new templates[$1.to_s] = template elsif template << line end end begin rendered = TemplateRenderer.render(templates[name], context) rescue puts "Unable to load inline template #{name}" exit 1 end File.write(dst, rendered) end end
Public Instance Methods
# File lib/charmkit/helpers/hookenv.rb, line 31 def action(item) out, err = cmd.run "action-get '#{item}'" return out.chomp end
# File lib/charmkit/helpers/hookenv.rb, line 36 def action=(item) out, err = cmd.run "action-set '#{item}'" return out.chomp end
# File lib/charmkit/helpers/fs.rb, line 22 def cat(src) return File.read(src) end
# File lib/charmkit/helpers/runner.rb, line 8 def cmd if ENV['DRYRUN'] TTY::Command.new(dryrun: true) else TTY::Command.new end end
# File lib/charmkit/helpers/hookenv.rb, line 16 def config(item) out, err = cmd.run "config-get '#{item}'" return out.chomp end
Create a file with data
@param dst String @param content String @return nil
@example
file "/etc/nginx/nginx.conf", "Some data"
# File lib/charmkit/helpers/fs.rb, line 18 def file(dst, content) File.write(dst, content) end
Hook path within a charm execution
@return [String]
# File lib/charmkit/helpers/hookenv.rb, line 6 def hook_path ENV['JUJU_CHARM_DIR'] end
Checks if a package is installed
@param package String @return Boolean
@example
is_install? "nginx-full"
# File lib/charmkit/helpers/fs.rb, line 48 def is_installed?(package) begin cmd.run "dpkg -s #{package}" and return true rescue return false end end
# File lib/charmkit/helpers/hookenv.rb, line 51 def log(msg) cmd.run "juju-log #{msg}" end
Installs packages
@param packages Array @param opts Hash @return Boolean
@example
package ['nginx-full'], :update_cache
# File lib/charmkit/helpers/fs.rb, line 34 def package(packages, *opts) if opts.include?(:update_cache) cmd.run "apt-get update" end cmd.run "apt-get install -qyf #{packages.join(' ')}" end
sugar over pathname
# File lib/charmkit/helpers/fs.rb, line 6 def path(pn) Pathname.new(pn) end
# File lib/charmkit/helpers/hookenv.rb, line 41 def relation(item) out, err = cmd.run "relation-get '#{item}'" return out.chomp end
# File lib/charmkit/helpers/hookenv.rb, line 46 def relation=(key, item) out, err = cmd.run "relation-set '#{key}=#{item}'" return out.chomp end
# File lib/charmkit/helpers/hookenv.rb, line 21 def resource(item) out, err = cmd.run "resource-get '#{item}'" return out.chomp end
# File lib/charmkit/helpers/hookenv.rb, line 10 def status(level=:maintenance, msg="") levels = [:maintenance, :active, :blocked, :waiting] raise unless levels.include?(level) cmd.run "status-set #{level.to_s} '#{msg}'" end
Reads from a erb file and renders the content with context
@param src String - file path of template @param dst String - file path location to save @param context Hash - parametized variables to pass to template @return Boolean
@example
template('examples/my-demo-charm/templates/vhost.conf', '/tmp/nginx-data.conf', public_address: 'localhost', app_path: '/srv/app')
# File lib/charmkit/helpers/template.rb, line 30 def template(src, dst, **context) rendered = TemplateRenderer.render(File.read(src), context) File.write(dst, rendered) end
# File lib/charmkit/helpers/hookenv.rb, line 26 def unit(item) out, err = cmd.run "unit-get '#{item}'" return out.chomp end
Private Instance Methods
Reads from a embedded template in the rake task itself.
@param src String - The data found after __END__ @param dst String - Save location @param context Hash - variables to pass into template @return Boolean
@example
inline_template('vhost.conf', '/etc/nginx/sites-enabled/default') server_name: "example.com") __END__ @@ vhost.conf server { name <%= server_name %> }
# File lib/charmkit/helpers/template.rb, line 50 def inline_template(name, dst, **context) templates = {} begin app, data = File.read(caller.first.split(":").first).split("__END__", 2) rescue Errno::ENOENT app, data = nil end data.strip! if data template = nil data.each_line do |line| if line =~ /^@@\s*(.*\S)\s*$/ template = String.new templates[$1.to_s] = template elsif template << line end end begin rendered = TemplateRenderer.render(templates[name], context) rescue puts "Unable to load inline template #{name}" exit 1 end File.write(dst, rendered) end end