Module: Charmkit::Helpers

Defined in:
lib/charmkit/helpers/fs.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

Instance Method Details

#action(item) ⇒ Object



24
25
26
27
# File 'lib/charmkit/helpers/hookenv.rb', line 24

def action(item)
  out, err = cmd.run "action-get '#{item}'"
  return out.chomp
end

#action=(item) ⇒ Object



29
30
31
32
# File 'lib/charmkit/helpers/hookenv.rb', line 29

def action=(item)
  out, err = cmd.run "action-set '#{item}'"
  return out.chomp
end

#cat(src) ⇒ Object



30
31
32
# File 'lib/charmkit/helpers/fs.rb', line 30

def cat(src)
  return File.read(src)
end

#cmdObject



5
6
7
# File 'lib/charmkit/helpers/runner.rb', line 5

def cmd
  return TTY::Command.new
end

#config(item) ⇒ Object



9
10
11
12
# File 'lib/charmkit/helpers/hookenv.rb', line 9

def config(item)
  out, err = cmd.run "config-get '#{item}'"
  return out.chomp
end

#file(dst, content) ⇒ Object

Create a file with data

Examples:

file "/etc/nginx/nginx.conf", "Some data"

Parameters:

  • dst

    String

  • content

    String

Returns:

  • nil



11
12
13
# File 'lib/charmkit/helpers/fs.rb', line 11

def file(dst, content)
  File.write(dst, content)
end

#inline_template(name, dst, **context) ⇒ Object

Reads from a embedded template in the rake task itself.

Examples:

inline_template('vhost.conf',
                '/etc/nginx/sites-enabled/default')
                server_name: "example.com")

Parameters:

  • src

    String - The data found after __END__

  • dst

    String - Save location

  • context

    Hash - variables to pass into template

Returns:

  • Boolean



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/charmkit/helpers/fs.rb', line 26

def is_dir?(path)
  return Dir.exists? path
end

#is_file?(path) ⇒ Boolean

Check if file exists

Examples:

is_file? "/etc/nginx/nginx.conf"

Parameters:

  • path

    String

Returns:

  • (Boolean)

    Boolean



22
23
24
# File 'lib/charmkit/helpers/fs.rb', line 22

def is_file?(path)
  return File.exists? path
end

#is_installed?(package) ⇒ Boolean

Checks if a package is installed

Examples:

is_install? "nginx-full"

Parameters:

  • package

    String

Returns:

  • (Boolean)

    Boolean



56
57
58
59
60
61
62
# File 'lib/charmkit/helpers/fs.rb', line 56

def is_installed?(package)
  begin
    cmd.run "dpkg -s #{package}" and return true
  rescue
    return false
  end
end

#log(msg) ⇒ Object



34
35
36
# File 'lib/charmkit/helpers/hookenv.rb', line 34

def log(msg)
  cmd.run "juju-log #{msg}"
end

#package(packages, *opts) ⇒ Object

Installs packages

Examples:

package ['nginx-full'], :update_cache

Parameters:

  • packages

    Array

  • opts

    Hash

Returns:

  • Boolean



42
43
44
45
46
47
# File 'lib/charmkit/helpers/fs.rb', line 42

def package(packages, *opts)
  if opts.include?(:update_cache)
    cmd.run "apt-get update"
  end
  cmd.run "apt-get install -qyf #{packages.join(' ')}"
end

#resource(item) ⇒ Object



14
15
16
17
# File 'lib/charmkit/helpers/hookenv.rb', line 14

def resource(item)
  out, err = cmd.run "resource-get '#{item}'"
  return out.chomp
end

#status(level = :maintenance, msg = "") ⇒ Object



3
4
5
6
7
# File 'lib/charmkit/helpers/hookenv.rb', line 3

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

Examples:

template('examples/my-demo-charm/templates/vhost.conf',
         '/tmp/nginx-data.conf',
         public_address: 'localhost',
         app_path: '/srv/app')

Parameters:

  • src

    String - file path of template

  • dst

    String - file path location to save

  • context

    Hash - parametized variables to pass to template

Returns:

  • Boolean



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



19
20
21
22
# File 'lib/charmkit/helpers/hookenv.rb', line 19

def unit(item)
  out, err = cmd.run "unit-get '#{item}'"
  return out.chomp
end