Module: Charmkit::Plugins::Core::InstanceMethods

Defined in:
lib/charmkit/plugins/core.rb

Defined Under Namespace

Classes: TemplateRenderer

Instance Method Summary collapse

Instance Method Details

#cat(src) ⇒ Object



25
26
27
# File 'lib/charmkit/plugins/core.rb', line 25

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

#file(dst, content:) ⇒ Object



13
14
15
# File 'lib/charmkit/plugins/core.rb', line 13

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

#is_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/charmkit/plugins/core.rb', line 21

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

#is_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/charmkit/plugins/core.rb', line 17

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

#is_installed?(package) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/charmkit/plugins/core.rb', line 73

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

#package(packages, *opts) ⇒ Object

Installs packages

package [‘nginx-full’], :update_cache



66
67
68
69
70
71
# File 'lib/charmkit/plugins/core.rb', line 66

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

#run(data) ⇒ Object

Run commands optionally getting it’s output, error

run ‘ls -l /tmp’



32
33
34
35
36
37
38
39
# File 'lib/charmkit/plugins/core.rb', line 32

def run(data)
  if ENV['DRYRUN']
    puts "DRYRUN: #{data}"
  else
    cmd = TTY::Command.new(printer: :null)
    return cmd.run(data)
  end
end

#template(src, dst, **context) ⇒ Object



57
58
59
60
# File 'lib/charmkit/plugins/core.rb', line 57

def template(src, dst, **context)
  rendered = TemplateRenderer.render(File.read(src), context)
  File.write(dst, rendered)
end