class Readme

Readme

Creates README.md file from RB script file

Attributes

data[R]
result[R]

Public Class Methods

new(script_path, config_path) click to toggle source

Initialize Readme instance @param script_path (String) Path to main rb file (start.rb) @param config_path (String) Path to main config file (config.yaml)

# File lib/teuton/project/readme/readme.rb, line 38
def initialize(script_path, config_path)
  @path = {}
  @path[:script]   = script_path
  @path[:dirname]  = File.dirname(script_path)
  @path[:filename] = File.basename(script_path, '.rb')
  @path[:config]   = config_path
  reset
end

Public Instance Methods

expect(_cond, _args = {}) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 67
def expect(_cond, _args = {})
  @current[:actions] << @action
  result.reset
end
Also aliased as: expect_any, expect_none, expect_one
expect_any(_cond, _args = {})
Alias for: expect
expect_none(_cond, _args = {})
Alias for: expect
expect_one(_cond, _args = {})
Alias for: expect
get(value) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 75
def get(value)
  unless @config[:global][value].nil?
    @global_params[value] = @config[:global][value]
    return @config[:global][value]
  end

  return value.to_s.upcase if @setted_params.include? value

  @cases_params << value
  value.to_s.upcase
end
gett(value) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 93
def gett(value)
  a = get(value)
  return "[#{value}](\#required-params)" if @cases_params.include? value
  return "[#{value}](\#created-params)" if @setted_params[value]

  "[#{a}](\#global-params)" if @global_params.include? value
end
goal(desc, args = {})
Alias for: target
goto(host = :localhost, args = {}) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 30
def goto(host = :localhost, args = {})
  unless host == :localhost
    b = {}
    a = "#{host}_ip".to_sym
    if @config[:global][a].nil? && !@setted_params.include?(a)
      @cases_params << a
    end
    b[:ip] = @config[:global][a] if @config[:global][a]
    b[:ip] = @setted_params[a] if @setted_params[a]

    a = "#{host}_username".to_sym
    if @config[:global][a].nil? && !@setted_params.include?(a)
      @cases_params << a
    end
    b[:username] = @config[:global][a] if @config[:global][a]
    b[:username] = @setted_params[a] if @setted_params[a]

    a = "#{host}_password".to_sym
    if @config[:global][a].nil? && !@setted_params.include?(a)
      @cases_params << a
    end
    b[:password] = @config[:global][a] if @config[:global][a]
    b[:password] = @setted_params[a] if @setted_params[a]

    @required_hosts[host.to_s] = b
  end
  @action[:host] = host
  @action[:exec] = args[:exec] || 'noexec'
end
log(text = '', type = :info) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 109
def log(text = '', type = :info)
  @data[:logs] << "[#{type}]: " + text.to_s
end
method_missing(method) click to toggle source

If a method call is missing, then delegate to concept parent.

# File lib/teuton/project/readme/dsl.rb, line 88
def method_missing(method)
  a = method.to_s
  instance_eval("get(:#{a[0, a.size - 1]})") if a[a.size - 1] == '?'
end
process_content() click to toggle source
# File lib/teuton/project/readme/readme.rb, line 63
def process_content
  Application.instance.groups.each do |g|
    @current = { name: g[:name], readme: [], actions: [] }
    @data[:groups] << @current
    reset_action
    instance_eval(&g[:block])
  end
end
readme(text) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 11
def readme(text)
  if @action[:target].nil?
    # It's a group readme
    @current[:readme] << text
  else
    # It's a target readme
    @action[:readme] << text
  end
end
reset() click to toggle source
# File lib/teuton/project/readme/readme.rb, line 47
def reset
  app = Application.instance
  @config = ConfigFileReader.read(app.config_path)
  @verbose = app.verbose
  @result = Result.new
  @data = {}
  @data[:logs] = []
  @data[:groups] = []
  @data[:play] = []
  reset_action
  @setted_params = {}
  @cases_params = []
  @global_params = {}
  @required_hosts = {}
end
reset_action() click to toggle source
# File lib/teuton/project/readme/readme.rb, line 72
def reset_action
  @action = { readme: [] }
end
run(command, args = {}) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 60
def run(command, args = {})
  args[:exec] = command
  host = :localhost
  host = args[:on] if args[:on]
  goto(host, args)
end
set(key, value) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 101
def set(key, value)
  @setted_params[key] = value
end
show() click to toggle source
# File lib/teuton/project/readme/readme.rb, line 76
def show
  process_content
  show_head
  show_content
  show_tail
end
show_content() click to toggle source

Show README content

# File lib/teuton/project/readme/readme.rb, line 122
def show_content
  @data[:groups].each do |group|
    next if group[:actions].empty?

    puts "\n## #{group[:name].capitalize}\n\n"
    group[:readme].each { |line| puts "#{line}\n" }
    previous_host = nil
    group[:actions].each_with_index do |item, index|
      if item[:host].nil? && index.positive?
        item[:host] = group[:actions][0][:host]
      end
      if previous_host.nil? || item[:host] != previous_host
        previous_host = item[:host] || 'null'
        puts format(Lang::get(:goto), previous_host.upcase)
      end

      weight = ''
      weight = "(x#{item[:weight]}) " if item[:weight] != 1.0
      last = (item[:target].end_with?('.') ? '' : '.')
      puts "* #{weight}#{item[:target]}#{last}"
      item[:readme].each { |line| puts "    * #{line}\n" }
    end
  end
end
show_head() click to toggle source

Show README head

# File lib/teuton/project/readme/readme.rb, line 85
def show_head
  app = Application.instance
  puts '```'
  puts format(Lang::get(:testname), app.test_name)
  puts format(Lang::get(:date), Time.now)
  puts format(Lang::get(:version), Application::VERSION)
  puts '```'
  puts "\n"
  puts "# #{app.test_name}\n"

  i = 1
  unless @required_hosts.empty?
    puts Lang::get(:hosts)
    puts "\n"
    puts '| ID | Host | Configuration |'
    puts '| -- | ---- | ------------- |'
    @required_hosts.each_pair do |k, v|
      c = []
      v.each_pair { |k2,v2| c << "#{k2}=#{v2}" }
      puts "|#{i}|#{k.upcase}|#{c.join(', ')}|"
      i += 1
    end
    puts "\n> NOTE: SSH Service installation is required on every host."
  end

  unless @cases_params.empty?
    @cases_params.uniq!.sort!
    puts Lang::get(:params)
    @cases_params.uniq.each { |i| puts format('* %s', i) }
    puts "\n> NOTE:"
    puts "> * Teuton software must known this information!"
    puts "> * Save every ':param: value' into config file."
  end
end
show_tail() click to toggle source

Show README tail

# File lib/teuton/project/readme/readme.rb, line 149
def show_tail
  return if @global_params.empty?

  app = Application.instance
  puts "\n---"
  puts "# ANEXO"
  puts "\n\#\# Global params"
  puts Lang::get(:global)
  puts "\n"
  puts '| Global param | Value |'
  puts '| ------------ | ----- |'
  @global_params.each_pair { |k,v| puts "|#{k}|#{v}|" }
  puts "\n\#\# Created params"
  puts Lang::get(:created)
  puts "\n"
  puts '| Created params | Value |'
  puts '| -------------- | ----- |'
  @setted_params.each_pair { |k,v| puts "|#{k}|#{v}|" }
end
target(desc, args = {}) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 21
def target(desc, args = {})
  previous_host = @action[:host]
  @action = { target: desc, host: previous_host, readme: [] }
  weight = 1.0
  weight = args[:weight].to_f if args[:weight]
  @action[:weight] = weight
end
Also aliased as: goal
unique(_key, _value) click to toggle source
# File lib/teuton/project/readme/dsl.rb, line 105
def unique(_key, _value)
  # don't do nothing
end