Module: Tasks
- Defined in:
- lib/helpers/tasks.rb
Overview
rubocop:disable Style/Documentation
Instance Method Summary collapse
- #authorized? ⇒ Boolean
- #generate_types(environment) ⇒ Object
- #ignore_env?(env) ⇒ Boolean
-
#ignore_event? ⇒ Boolean
Check to see if this is an event we care about.
- #notification(message) ⇒ Object
- #protected! ⇒ Object
- #run_command(command) ⇒ Object
- #run_prefix_command(payload) ⇒ Object
-
#slack_proxy ⇒ Object
Deprecated TODO: Remove in release 3.0.0.
-
#slack_settings ⇒ Object
Deprecated TODO: Remove in release 3.0.0.
- #types? ⇒ Boolean
- #verify_signature? ⇒ Boolean
Instance Method Details
#authorized? ⇒ Boolean
116 117 118 119 120 121 |
# File 'lib/helpers/tasks.rb', line 116 def # 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
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/helpers/tasks.rb', line 51 def generate_types(environment) command = "#{settings.command_prefix} /opt/puppetlabs/puppet/bin generate types --environment #{environment}" = run_command(command) LOGGER.info("message: #{} environment: #{environment}") = { status: :success, message: .to_s, environment: environment, status_code: 200 } notification() rescue StandardError => e LOGGER.error("message: #{e.} trace: #{e.backtrace}") = { status: :fail, message: e., trace: e.backtrace, environment: environment, status_code: 500 } notification() end |
#ignore_env?(env) ⇒ Boolean
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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? ⇒ Boolean
Check to see if this is an event we care about. Default to responding to all events
22 23 24 25 26 27 28 29 30 31 |
# 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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/helpers/tasks.rb', line 64 def notification() 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.).notify() end |
#protected! ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/helpers/tasks.rb', line 127 def protected! if 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
45 46 47 48 49 |
# File 'lib/helpers/tasks.rb', line 45 def run_command(command) = "forked: #{command}" system "#{command} &" end |
#run_prefix_command(payload) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
Deprecated TODO: Remove in release 3.0.0
99 100 101 102 103 104 105 106 107 |
# File 'lib/helpers/tasks.rb', line 99 def slack_proxy uri = URI(settings.slack_proxy_url) = { proxy_address: uri.hostname, proxy_port: uri.port, proxy_from_env: false } end |
#slack_settings ⇒ Object
Deprecated TODO: Remove in release 3.0.0
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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.[:icon_emoji] = settings.slack_emoji LOGGER.warn('settings.slack_proxy_url is deprecated and will be removed in puppet_webhook 3.0.0') settings.[:http_options] = if settings.slack_proxy_url slack_proxy else {} end end |
#types? ⇒ Boolean
109 110 111 112 113 114 |
# 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? ⇒ Boolean
123 124 125 |
# File 'lib/helpers/tasks.rb', line 123 def verify_signature? true unless settings.github_secret.nil? end |