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

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

#cmdObject



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

Examples:

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

Parameters:

  • dst

    String

  • content

    String

Returns:

  • nil



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

Parameters:

  • min (Integer) (defaults to: 0)

    mininum number of characters

  • max (Integer) (defaults to: 20)

    maximum number of characters



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_pathString

Hook path within a charm execution

Returns:



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.

Examples:

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

__END__
@@ vhost.conf
server { name <%= server_name %> }

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)


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

Examples:

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

Parameters:

  • path

    String

Returns:

  • (Boolean)

    Boolean



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

Examples:

is_install? "nginx-full"

Parameters:

  • package

    String

Returns:

  • (Boolean)

    Boolean



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

Examples:

package ['nginx-full'], :update_cache

Parameters:

  • packages

    Array

  • opts

    Hash

Returns:

  • Boolean



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

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



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