class Object

Constants

API_VERSION
SUPPORTED_RUBY_VERSION
VAULT_TOKEN_FILE
VERSION

semver.org/

Public Instance Methods

dump_json(structure) click to toggle source
# File lib/tiller/json.rb, line 8
def dump_json(structure)
  begin
    require 'oj'
    Oj.dump(structure, :mode => :compat)
  rescue LoadError
    structure.to_json
  end
end
handle_404() click to toggle source
# File lib/tiller/api/handlers/404.rb, line 1
def handle_404
  {
      :content => '{ "error" : "not implemented" }',
      :status => '404 Not Found'
  }
end
handle_config(api_version, tiller_api_hash) click to toggle source
# File lib/tiller/api/handlers/config.rb, line 4
def handle_config(api_version, tiller_api_hash)
  case api_version
    when 'v1','v2'
      {
          :content => dump_json(tiller_api_hash['config']),
          :status => '200 OK'
      }
    else
      handle_404
  end
end
handle_globals(api_version, tiller_api_hash) click to toggle source
# File lib/tiller/api/handlers/globals.rb, line 4
def handle_globals(api_version, tiller_api_hash)
  warning_json = { deprecation_warning: 'The v1 Tiller API is deprecated. Expect this endpoint to be removed in future versions.' }
  case api_version
    when 'v1'
      {
          :content => dump_json(tiller_api_hash['global_values'].merge(warning_json)),
          :status => '200 OK'
      }
    else
      handle_404
  end
end
handle_ping() click to toggle source
# File lib/tiller/api/handlers/ping.rb, line 1
def handle_ping
  {
      :content => "{ \"ping\": \"Tiller API v#{API_VERSION} OK\" }",
      :status => '200 OK'
  }
end
handle_template(api_version, tiller_api_hash, template) click to toggle source
# File lib/tiller/api/handlers/template.rb, line 4
def handle_template(api_version, tiller_api_hash, template)
  case api_version
    when 'v1','v2'
      if tiller_api_hash['templates'].has_key?(template)
        {
            :content => dump_json(tiller_api_hash['templates'][template]),
            :status => '200 OK'
        }
      else
        {
            :content => '{ "error" : "template not found" }',
            :status => '404 Not Found'
        }
      end
  end
end
handle_templates(api_version, tiller_api_hash) click to toggle source
# File lib/tiller/api/handlers/templates.rb, line 4
def handle_templates(api_version, tiller_api_hash)
  case api_version
    when 'v1','v2'
      {
          :content => dump_json(tiller_api_hash['templates'].keys),
          :status => '200 OK'
      }
      else
        handle_404
  end
end
helper_loader(helpers) click to toggle source
# File lib/tiller/loader.rb, line 42
def helper_loader(helpers)
  source_path = 'tiller/helper'
  loaded = []

  helpers.each do |h|
    require File.join(source_path,"#{h}.rb")
    loaded.push(h)
  end

  loaded
end
launch(cmd) click to toggle source

Fork and launch a process.

# File lib/tiller/util.rb, line 56
def launch(cmd)
  # If an array, then we use a different form of spawn() which
  # avoids a subshell. See https://github.com/markround/tiller/issues/8
  if cmd.is_a?(Array)
    final='cmd[0]'
    # Build up the list of arguments when using the array form of spawn()
    if cmd.size > 1
      (1..cmd.size-1).each {|c| final="#{final} , cmd[#{c}]" }
    end
    pid=eval "spawn(#{final})"
  else
    pid=spawn(cmd)
  end

  pid
end
loader(type,sources) click to toggle source
# File lib/tiller/loader.rb, line 4
def loader(type,sources)
  case type.to_s
    when 'Tiller::DataSource'
      source_path = 'tiller/data'
    when 'Tiller::TemplateSource'
      source_path = 'tiller/template'
    else
      raise "Unsupported by loader : #{type}"
  end

  # This looks a little counter-intuitive, but it's for a reason.
  # We load things this way so that we get an array that contains all the
  # classes loaded, *in the order specified in common.yaml*
  # This is very important, as it means we can specify the defaults DataSource
  # first, for example and then let later DataSources override values from it.
  # Otherwise, iterating through the available classes results in them being
  # returned in no particular useful order.

  classes = []
  sources.each do |s|
    require File.join(source_path,"#{s}.rb")
    classes |= type.subclasses
  end

  # Versioning check - if any of the loaded modules do not support the specified API version, we stop immediately.
  classes.each do |c|
    api_version = Tiller::config['plugin_api_version']
    if ! c.plugin_api_versions.include?(api_version)
      Tiller::log.fatal("ERROR : Plugin #{c} does not support specified API version #{api_version}")
      exit(EXIT_FAIL)
    end
  end


  classes
end
parse_options(config) click to toggle source
# File lib/tiller/options.rb, line 1
def parse_options(config)
  optparse = OptionParser.new do |opts|
    opts.on('-n', '--no-exec', 'Do not execute any processes') do
      config[:no_exec] = true
    end
    opts.on('-v', '--verbose', 'Display verbose output') do
      config[:verbose] = true
    end
    opts.on('-d', '--debug', 'Display debug output') do
      config[:debug] = true
    end
    opts.on('-a', '--api', 'Enable HTTP API') do
      config['api_enable'] = true
    end
    opts.on('-p', '--api-port [API_PORT]', 'HTTP API port') do |api_port|
      config['api_port'] = api_port
    end
    opts.on('-b', '--base-dir [BASE_DIR]', 'Override the tiller_base environment variable') do |base_dir|
      config[:tiller_base] = base_dir
    end
    opts.on('-l', '--lib-dir [LIB_DIR]', 'Override the tiller_lib environment variable') do |lib_dir|
      config[:tiller_lib] = lib_dir
    end
    opts.on('-e', '--environment [ENV]', 'Override the \'environment\' environment variable') do |environment|
      config[:environment] = environment
    end
    opts.on('-x', '--exec [EXEC]', 'Override the \'exec\' variable from common.yaml') do |exec|
      config[:alt_exec] = exec
    end
    opts.on('--md5sum', 'Only write templates if MD5 checksum for content has changed') do
      config['md5sum'] = true
    end
    opts.on('--md5sum-noexec', 'Do not execute a process if no templates were written or changed') do
      config['md5sum_noexec'] = true
    end
    opts.on('--plugin-api-version [VERS]', 'Specify the plugin API version to use') do |plugin_api_version|
      config['plugin_api_version'] = plugin_api_version
    end

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      puts 'Tiller also uses the environment variables tiller_base, environment'
      puts 'and tiller_lib (or they can be provided using the arguments shown above).'
      puts 'See https://github.com/markround/tiller for documentation and usage.'
      if config[:debug] == true
        puts 'Current configuration hash follows :'
        pp config 
      end
      exit
    end
  end

  optparse.parse!

  config
end
signal(sig, pid, options={}) click to toggle source

Pass signals on to child process

# File lib/tiller/util.rb, line 46
def signal(sig, pid, options={})
  puts "Caught signal #{sig}, passing to PID #{pid}" if options[:verbose]
  begin
    Process.kill(sig, pid)
  rescue Errno::ESRCH
    false
  end
end
tiller_api(tiller_api_hash) click to toggle source

The following is a VERY simple HTTP API, used for querying the status of Tiller after it has generated templates and forked a child process.

# File lib/tiller/api.rb, line 16
def tiller_api(tiller_api_hash)

  api_port = tiller_api_hash['config']['api_port']

  puts "Tiller API starting on port #{api_port}"

  server = TCPServer.new(api_port)

  loop do
    begin
      socket = server.accept
      request = socket.gets
      (method, uri, _http_version) = request.split

      if uri =~ /^\/v([0-9]+)\//
        api_version = uri.split('/')[1]
      end

      # Defaults
      response = handle_404

      # Routing
      case method
        when 'GET'
          case uri
            when '/ping'
              response = handle_ping
            when /^\/v([0-9]+)\/config/
              response = handle_config(api_version, tiller_api_hash)
            when /^\/v([0-9]+)\/globals/
              response = handle_globals(api_version, tiller_api_hash)
            when /^\/v([0-9]+)\/templates/
              response = handle_templates(api_version, tiller_api_hash)
            when /^\/v([0-9]+)\/template\//
              template = uri.split('/')[3]
              response = handle_template(api_version, tiller_api_hash, template)
          end
      end

      # Response
      socket.print "HTTP/1.1 #{response[:status]}\r\n" +
                   "Content-Type: application/json\r\n" +
                   "Server: Tiller #{VERSION} / API v#{API_VERSION}\r\n"
                   "Content-Length: #{response[:content].bytesize}\r\n" +
                   "Connection: close\r\n"
      socket.print "\r\n"
      socket.print response[:content]
      socket.close

    rescue Exception => e
      puts "Error : Exception in Tiller API thread : #{e.class.name}\n#{e}"
      next
    end
  end

end
warn_merge(key, old, new, type, source) click to toggle source

Warn if values are being merged

# File lib/tiller/util.rb, line 39
def warn_merge(key, old, new, type, source)
  Tiller::log.info("Merging duplicate #{type} values")
  Tiller::log.info("#{key} => '#{old}' being replaced by : '#{new}' from #{source}")
  new
end