module Tasks

Public Instance Methods

authorized?() click to toggle source
# File lib/helpers/tasks.rb, line 116
def authorized?
  # TODO: add token-based authentication?
  @auth ||= Rack::Auth::Basic::Request.new(request.env)
  @auth.provided? && @auth.basic? && @auth.credentials &&
    @auth.credentials == [settings.user, settings.pass]
end
generate_types(environment) click to toggle source
# File lib/helpers/tasks.rb, line 51
def generate_types(environment)
  command = "#{settings.command_prefix} /opt/puppetlabs/puppet/bin generate types --environment #{environment}"

  message = run_command(command)
  LOGGER.info("message: #{message} environment: #{environment}")
  status_message = { status: :success, message: message.to_s, environment: environment, status_code: 200 }
  notification(status_message)
rescue StandardError => e
  LOGGER.error("message: #{e.message} trace: #{e.backtrace}")
  status_message = { status: :fail, message: e.message, trace: e.backtrace, environment: environment, status_code: 500 }
  notification(status_message)
end
ignore_env?(env) click to toggle source
# File lib/helpers/tasks.rb, line 4
def ignore_env?(env)
  list = settings.ignore_environments
  return false if list.nil? || list.empty?

  list.each do |l|
    # Even unquoted array elements wrapped by slashes becomes strings after YAML parsing
    # So we need to convert it into Regexp manually
    if l =~ %r{^/.+/$}
      return true if env =~ Regexp.new(l[1..-2])
    elsif env == 1
      return true
    end
  end

  false
end
ignore_event?() click to toggle source

Check to see if this is an event we care about. Default to responding to all events

# File lib/helpers/tasks.rb, line 22
def ignore_event?
  # Explicitly ignore Github ping events
  return true if request.env['HTTP_X_GITHUB_EVENT'] == 'ping'

  list = nil unless settings.repository_events
  event = request.env['HTTP_X_GITHUB_EVENT']

  # Negate this, because we should respond if any of these conditions are true
  !(list.nil? || (list == event) || list.include?(event))
end
notification(message) click to toggle source
# File lib/helpers/tasks.rb, line 64
def notification(message)
  return unless settings.chatops || settings.slack_webhook

  require 'plugins/chatops'

  slack_settings if settings.chatops == false && settings.slack_webhook != false
  PuppetWebhook::Chatops.new(settings.chatops_service,
                             settings.chatops_url,
                             settings.chatops_channel,
                             settings.chatops_user,
                             settings.chatops_options).notify(message)
end
protected!() click to toggle source
# File lib/helpers/tasks.rb, line 127
def protected!
  if authorized?
    LOGGER.info("Authenticated as user #{settings.user} from IP #{request.ip}")
  else
    response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
    LOGGER.error("Authentication failure from IP #{request.ip}")
    throw(:halt, [401, "Not authorized\n"])
  end
end
run_command(command) click to toggle source
# File lib/helpers/tasks.rb, line 45
def run_command(command)
  message = "forked: #{command}"
  system "#{command} &"
  message
end
run_prefix_command(payload) click to toggle source
# File lib/helpers/tasks.rb, line 33
def run_prefix_command(payload)
  IO.popen(settings.prefix_command, 'r+') do |io|
    io.write payload.to_s
    io.close_write
    begin
      io.readlines.first.chomp
    rescue StandardError
      ''
    end
  end
end
slack_proxy() click to toggle source

Deprecated TODO: Remove in release 3.0.0

# File lib/helpers/tasks.rb, line 99
def slack_proxy
  uri = URI(settings.slack_proxy_url)
  http_options = {
    proxy_address: uri.hostname,
    proxy_port: uri.port,
    proxy_from_env: false
  }
  http_options
end
slack_settings() click to toggle source

Deprecated TODO: Remove in release 3.0.0

# File lib/helpers/tasks.rb, line 79
def slack_settings
  settings.chatops_service = 'slack'
  LOGGER.warn('settings.slack_webhook is deprecated and will be removed in puppet_webhook 3.0.0')
  settings.chatops_url = settings.slack_webhook
  LOGGER.warn('settings.slack_user is deprecated and will be removed in puppet_webhook 3.0.0')
  settings.chatops_user = settings.slack_user
  LOGGER.warn('settings.slack_channel is deprecated and will be removed in puppet_webhook 3.0.0')
  settings.chatops_channel = settings.slack_channel
  LOGGER.warn('settings.slack_emoji is deprecated and will be removed in puppet_webhook 3.0.0')
  settings.chatops_options[:icon_emoji] = settings.slack_emoji
  LOGGER.warn('settings.slack_proxy_url is deprecated and will be removed in puppet_webhook 3.0.0')
  settings.chatops_options[:http_options] = if settings.slack_proxy_url
                                              slack_proxy
                                            else
                                              {}
                                            end
end
types?() click to toggle source
# File lib/helpers/tasks.rb, line 109
def types?
  return false unless settings.respond_to?(:generate_types=)
  return false if settings.generate_types.nil?

  settings.generate_types
end
verify_signature?() click to toggle source
# File lib/helpers/tasks.rb, line 123
def verify_signature?
  true unless settings.github_secret.nil?
end