Module: Tasks

Includes:
MCollective::RPC
Defined in:
lib/helpers/tasks.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/helpers/tasks.rb', line 55

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) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/helpers/tasks.rb', line 8

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?Boolean

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

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/helpers/tasks.rb', line 26

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) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/helpers/tasks.rb', line 68

def notification(message)
  return unless settings.chatops || settings.slack_webhook
  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!Object



127
128
129
130
131
132
133
134
135
# 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) ⇒ Object



49
50
51
52
53
# File 'lib/helpers/tasks.rb', line 49

def run_command(command)
  message = "forked: #{command}"
  system "#{command} &"
  message
end

#run_prefix_command(payload) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/helpers/tasks.rb', line 37

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_proxyObject

Deprecated TODO: Remove in release 3.0.0



100
101
102
103
104
105
106
107
108
# File 'lib/helpers/tasks.rb', line 100

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_settingsObject

Deprecated TODO: Remove in release 3.0.0



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/helpers/tasks.rb', line 80

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?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/helpers/tasks.rb', line 110

def types?
  return false unless settings.respond_to?(:generate_types=)
  return false if settings.generate_types.nil?
  settings.generate_types
end

#verify_signature?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/helpers/tasks.rb', line 123

def verify_signature?
  true unless settings.github_secret.nil?
end