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)


136
137
138
139
140
141
# File 'lib/helpers/tasks.rb', line 136

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/helpers/tasks.rb', line 62

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 }
  notify_slack(status_message) if slack?
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 }
  notify_slack(status_message) if slack?
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

#mco(branch) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/helpers/tasks.rb', line 157

def mco(branch)
  options = MCollective::Util.default_options
  options[:config] = settings.client_cfg
  client = rpcclient('r10k', exit_on_failure: false, options: options)
  client.discovery_timeout = settings.discovery_timeout
  client.timeout           = settings.client_timeout
  client.send('deploy', environment: branch)
end

#notify_slack(status_message) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/helpers/tasks.rb', line 75

def notify_slack(status_message)
  return unless settings.slack_webhook

  if settings.slack_proxy_url
    uri = URI(settings.slack_proxy_url)
    http_options = {
      proxy_address:  uri.hostname,
      proxy_port:     uri.port,
      proxy_from_env: false
    }
  else
    http_options = {}
  end

  notifier = Slack::Notifier.new settings.slack_webhook do
    defaults channel: '#general',
             username: 'puppet_webhook',
             icon_emoji: ':ocean:',
             http_options: http_options
  end

  if status_message[:branch]
    target = status_message[:branch]
  elsif status_message[:module]
    target = status_message[:module]
  end

  message = {
    author: 'r10k for Puppet',
    title: "r10k deployment of Puppet environment #{target}"
  }

  case status_message[:status_code]
  when 200
    message.merge!(
      color: 'good',
      text: "Successfully deployed #{target}",
      fallback: "Successfully deployed #{target}"
    )
  when 500
    message.merge!(
      color: 'bad',
      text: "Failed to deploy #{target}",
      fallback: "Failed to deploy #{target}"
    )
  end

  notifier.post text: message[:fallback], attachments: [message]
end

#protected!Object



147
148
149
150
151
152
153
154
155
# File 'lib/helpers/tasks.rb', line 147

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
54
55
56
57
58
59
60
# File 'lib/helpers/tasks.rb', line 49

def run_command(command)
  if Open3.respond_to?('capture3')
    stdout, stderr, exit_status = Open3.capture3(command)
    message = "triggered: #{command}\n#{stdout}\n#{stderr}"
  else
    message = "forked: #{command}"
    Process.detach(fork { exec "#{command} &" })
    exit_status = 0
  end
  raise "#{stdout}\n#{stderr}" if exit_status != 0
  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?Boolean

Returns:

  • (Boolean)


125
126
127
128
# File 'lib/helpers/tasks.rb', line 125

def slack?
  return false if settings.slack_webhook.nil?
  settings.slack_webhook
end

#types?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
# File 'lib/helpers/tasks.rb', line 130

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)


143
144
145
# File 'lib/helpers/tasks.rb', line 143

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