Module: Charmkit::Helpers
- Extended by:
- Forwardable
- Defined in:
- lib/charmkit/helpers/fs.rb,
lib/charmkit/helpers/crypt.rb,
lib/charmkit/helpers/runner.rb,
lib/charmkit/helpers/hookenv.rb,
lib/charmkit/helpers/template.rb
Defined Under Namespace
Classes: TemplateRenderer
Instance Method Summary collapse
- #action(item) ⇒ Object
- #action=(item) ⇒ Object
- #cat(src) ⇒ Object
- #cmd ⇒ Object
- #config(item) ⇒ Object
-
#file(dst, content) ⇒ Object
Create a file with data.
-
#gen_salt(min = 0, max = 20) ⇒ Object
Generate a password salt.
-
#hook_path ⇒ String
Hook path within a charm execution.
-
#inline_template(name, dst, **context) ⇒ Object
Reads from a embedded template in the rake task itself.
- #is_dir?(path) ⇒ Boolean
-
#is_file?(path) ⇒ Boolean
Check if file exists.
-
#is_installed?(package) ⇒ Boolean
Checks if a package is installed.
- #log(msg) ⇒ Object
-
#package(packages, *opts) ⇒ Object
Installs packages.
-
#path(pn) ⇒ Object
sugar over pathname.
- #resource(item) ⇒ Object
- #status(level = :maintenance, msg = "") ⇒ Object
-
#template(src, dst, **context) ⇒ Object
Reads from a erb file and renders the content with context.
- #unit(item) ⇒ Object
Instance Method Details
#action(item) ⇒ Object
31 32 33 34 |
# File 'lib/charmkit/helpers/hookenv.rb', line 31 def action(item) out, err = cmd.run "action-get '#{item}'" return out.chomp end |
#action=(item) ⇒ Object
36 37 38 39 |
# File 'lib/charmkit/helpers/hookenv.rb', line 36 def action=(item) out, err = cmd.run "action-set '#{item}'" return out.chomp end |
#cat(src) ⇒ Object
37 38 39 |
# File 'lib/charmkit/helpers/fs.rb', line 37 def cat(src) return File.read(src) end |
#cmd ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/charmkit/helpers/runner.rb', line 8 def cmd if ENV['DRYRUN'] TTY::Command.new(dryrun: true) else TTY::Command.new end end |
#config(item) ⇒ Object
16 17 18 19 |
# File 'lib/charmkit/helpers/hookenv.rb', line 16 def config(item) out, err = cmd.run "config-get '#{item}'" return out.chomp end |
#file(dst, content) ⇒ Object
Create a file with data
18 19 20 |
# File 'lib/charmkit/helpers/fs.rb', line 18 def file(dst, content) File.write(dst, content) end |
#gen_salt(min = 0, max = 20) ⇒ Object
Generate a password salt
7 8 9 |
# File 'lib/charmkit/helpers/crypt.rb', line 7 def gen_salt(min=0, max=20) (min...max).map { ('a'..'z').to_a[rand(26)] }.join end |
#hook_path ⇒ String
Hook path within a charm execution
6 7 8 |
# File 'lib/charmkit/helpers/hookenv.rb', line 6 def hook_path ENV['JUJU_CHARM_DIR'] end |
#inline_template(name, dst, **context) ⇒ Object
Reads from a embedded template in the rake task itself.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 |
#is_dir?(path) ⇒ Boolean
33 34 35 |
# File 'lib/charmkit/helpers/fs.rb', line 33 def is_dir?(path) return Dir.exists? path end |
#is_file?(path) ⇒ Boolean
Check if file exists
29 30 31 |
# File 'lib/charmkit/helpers/fs.rb', line 29 def is_file?(path) return File.exists? path end |
#is_installed?(package) ⇒ Boolean
Checks if a package is installed
63 64 65 66 67 68 69 |
# File 'lib/charmkit/helpers/fs.rb', line 63 def is_installed?(package) begin cmd.run "dpkg -s #{package}" and return true rescue return false end end |
#log(msg) ⇒ Object
41 42 43 |
# File 'lib/charmkit/helpers/hookenv.rb', line 41 def log(msg) cmd.run "juju-log #{msg}" end |
#package(packages, *opts) ⇒ Object
Installs packages
49 50 51 52 53 54 |
# File 'lib/charmkit/helpers/fs.rb', line 49 def package(packages, *opts) if opts.include?(:update_cache) cmd.run "apt-get update" end cmd.run "apt-get install -qyf #{packages.join(' ')}" end |
#path(pn) ⇒ Object
sugar over pathname
6 7 8 |
# File 'lib/charmkit/helpers/fs.rb', line 6 def path(pn) Pathname.new(pn) end |
#resource(item) ⇒ Object
21 22 23 24 |
# File 'lib/charmkit/helpers/hookenv.rb', line 21 def resource(item) out, err = cmd.run "resource-get '#{item}'" return out.chomp end |
#status(level = :maintenance, msg = "") ⇒ Object
10 11 12 13 14 |
# 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 |
#template(src, dst, **context) ⇒ Object
Reads from a erb file and renders the content with context
30 31 32 33 |
# 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 |
#unit(item) ⇒ Object
26 27 28 29 |
# File 'lib/charmkit/helpers/hookenv.rb', line 26 def unit(item) out, err = cmd.run "unit-get '#{item}'" return out.chomp end |