Module: IcingaCertService::ZoneHandler

Included in:
Client
Defined in:
lib/cert-service/zone_handler.rb

Overview

Client Class to create on-the-fly a certificate to connect automaticly as satellite to an icinga2-master

Instance Method Summary collapse

Instance Method Details

#add_zone(zone = nil) ⇒ Hash, #read

add a satellite zone to ‘zones.conf’

Examples:

add_zone('icinga2-satellite')

Parameters:

  • zone (String) (defaults to: nil)

Returns:

  • (Hash, #read)

    if config already created:

    • :status [Integer] 200

    • :message [String] Message

  • nil if successful



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cert-service/zone_handler.rb', line 21

def add_zone( zone = nil )

  return { status: 500, message: 'no zone defined' } if zone.nil?

  zone_file =  '/etc/icinga2/zones.conf'

  if(File.exist?(zone_file))

    file     = File.open(zone_file, 'r')
    contents = file.read

    regexp_long = / # Match she-bang style C-comment
      \/\*          # Opening delimiter.
      [^*]*\*+      # {normal*} Zero or more non-*, one or more *
      (?:           # Begin {(special normal*)*} construct.
        [^*\/]      # {special} a non-*, non-\/ following star.
        [^*]*\*+    # More {normal*}
      )*            # Finish "Unrolling-the-Loop"
      \/            # Closing delimiter.
    /x
    result = contents.gsub(regexp_long, '')
    scan_zone = result.scan(/object Zone(.*)"(?<zone>.+\S)"/).flatten

    return { status: 200, message: format('the configuration for the zone %s already exists', zone) } if( scan_zone.include?(zone) == true )
  end

  logger.debug(format('i miss an configuration for zone \'%s\'', zone))

  begin
    result = write_template(
      template: 'templates/zones.conf.erb',
      destination_file: zone_file,
      environment: {
        zone: zone,
        icinga_master: @icinga_master
      }
    )
    # logger.debug(result)

  rescue => error
    logger.error(error.to_s)

    return { status: 500, message: error.to_s }
  end

  { status: 200, message: format('configuration for zone %s has been created', zone) }

end