Module: Tasks

Defined in:
lib/helpers/tasks.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
# File 'lib/helpers/tasks.rb', line 145

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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/helpers/tasks.rb', line 69

def generate_types(environment)
  command = "#{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)


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

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)


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

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



166
167
168
169
170
171
172
173
# File 'lib/helpers/tasks.rb', line 166

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



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
124
125
126
127
128
129
130
131
132
133
# File 'lib/helpers/tasks.rb', line 82

def notify_slack(status_message)
  return unless settings.slack_webhook

  slack_channel = settings.slack_channel  || '#default'
  slack_user    = settings.slack_username || 'r10k'

  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: slack_channel,
             username: slack_user,
             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



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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/helpers/tasks.rb', line 48

def run_command(command)
  message = ''
  File.open(LOCKFILE, 'w+') do |file|
    # r10k has a small race condition which can cause failed deploys if two happen
    # more or less simultaneously. To mitigate, we just lock on a file and wait for
    # the other one to complete.
    file.flock(File::LOCK_EX)

    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
  end
  message
end

#run_prefix_command(payload) ⇒ Object



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

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)


135
136
137
138
# File 'lib/helpers/tasks.rb', line 135

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

#types?Boolean

Returns:

  • (Boolean)


140
141
142
143
# File 'lib/helpers/tasks.rb', line 140

def types?
  return false if settings.generate_tasks.nil?
  settings.generate_tasks
end

#verify_signature?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/helpers/tasks.rb', line 152

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