Module: Kaigara::DSL

Includes:
Thor::Actions, Thor::Base, Thor::Shell
Included in:
Operation
Defined in:
lib/kaigara/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



7
8
9
10
11
# File 'lib/kaigara/dsl.rb', line 7

def self.included(receiver)
  receiver.send :include, Thor::Actions
  receiver.send :include, Thor::Base
  receiver.send :include, Thor::Shell
end

Instance Method Details

#debian_family?Boolean

Return true if the OS is Ubuntu or Debian If a block is given it’s executed if the statement is true

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/kaigara/dsl.rb', line 81

def debian_family?
  file_matches?("/etc/issue", /Ubuntu|Debian/i) do
    yield if block_given?
  end
end

#execute(cmd) ⇒ Object

One of the most important parts of DSL. You can use it directly in your operations.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kaigara/dsl.rb', line 14

def execute(cmd)
  Environment.load_variables(self)
  say "Running: #{cmd}", :yellow
  stdin, stdout, stderr, wait_thr = Open3.popen3({}, "bash -e")
  stdin.puts(cmd)
  Thread.new do
    stdout.each { |l| say(l, :green) }
  end
  Thread.new do
    stderr.each { |l| say(l, :red) }
  end
  stdin.close

  exit_status = wait_thr.value.exitstatus
  if exit_status != 0
    raise "Command `#{ cmd }` returned status code #{ exit_status }"
  end
end

#file(filepath, content, opts = {overwrite: true}) ⇒ Object

Creates a file with the specified content



134
135
136
137
138
139
140
141
142
# File 'lib/kaigara/dsl.rb', line 134

def file(filepath, content, opts = {overwrite: true})
  if !opts[:overwrite] and File.exists?(filepath)
    raise "File #{filepath} exists"
  end

  File.open(filepath, "w") do |fd|
    fd.write(content)
  end
end

#file_matches?(file, match) ⇒ Boolean

Return true if the ‘file` exists? and the content matches the `match` If a block is given it’s executed if the statement is true

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/kaigara/dsl.rb', line 67

def file_matches?(file, match)
  if File.exists?(file)
    if File.read(file).match(match)
      yield if block_given?
      return true
    end
  end
  false
end

#hostname(hostname) ⇒ Object

Configure the hostname of the machine



147
148
149
# File 'lib/kaigara/dsl.rb', line 147

def hostname(hostname)
  file("/etc/hostname", hostname)
end

#package(name, version = nil) ⇒ Object

Install the package depending on OS



121
122
123
124
125
126
127
128
129
# File 'lib/kaigara/dsl.rb', line 121

def package(name, version = nil)
  if debian_family?
    execute("apt-get install -y #{ name }")
  elsif redhat_family?
    execute("yum install -y #{ name }")
  else
    raise "OS not supported"
  end
end

#package_updateObject

Update the local repository cache



110
111
112
113
114
115
116
# File 'lib/kaigara/dsl.rb', line 110

def package_update
  if debian_family?
    execute("apt-get update")
  elsif redhat_family?
    execute("yum update -y")
  end
end

#redhat_family?Boolean

Return true if the OS is CentOS or RedHat If a block is given it’s executed if the statement is true

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/kaigara/dsl.rb', line 91

def redhat_family?
  file_matches?("/etc/redhat-release", /CentOS|Red Hat/i) do
    yield if block_given?
  end
end

#repo_extendedObject

Add common additional repositories



100
101
102
103
104
105
# File 'lib/kaigara/dsl.rb', line 100

def repo_extended
  redhat_family? do
    package("epel-release")
    package_update
  end
end

#script(name, path = nil) ⇒ Object

Renders a template, then executes the script. You should add shebang to your script.



56
57
58
59
60
61
# File 'lib/kaigara/dsl.rb', line 56

def script(name, path = nil)
  target = template(name, File.join( (path.nil? ? "" : path), name))
  File.chmod(0755, target)
  target.prepend('./') unless target.match(/^\//)
  execute("#{target}")
end

#template(source, target) ⇒ Object

Templates ERB template. You can use variables from metadata.rb in your templates. name - template name, without ‘.erb’ -p - destination file. If you don’t use it, the file renders to /path-in-resources/



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kaigara/dsl.rb', line 38

def template(source, target)
  Environment.load_variables(self)
  tpl_file = 'resources/' + source + '.erb'
  destination = target
  destination = "/#{tpl_file}" if destination.nil?
  destination.gsub!(/\.erb$/,'')
  context = instance_eval("binding")

  say "Rendering template #{tpl_file} to #{destination}", :yellow
  File.write(destination, ERB.new(File.read(tpl_file), nil, "-", "@output_buffer").result(context))

  return destination
end