Module: KongCookbook::Helpers

Defined in:
libraries/cookbook_helpers.rb

Overview

Some helpers used in the kong cookbook.

Examples:

self.class.send(:include, ::KongCookbook::Helpers)
self.manage_ssl_certificate #=> true

Instance Method Summary collapse

Instance Method Details

#calculate_manage_cassandraBoolean

Calculates whether the Cassandra installation should be managed by the cookbook or not based only on the Kong configuration.

Returns true if the properties['hosts'] value includes a localhost machine: 'localhost' or '127.0.0.1'.

Examples:

self.calculate_manage_cassandra #=> true

Returns:

  • (Boolean)

    true if Cassandra should be installed locally.


148
149
150
151
152
# File 'libraries/cookbook_helpers.rb', line 148

def calculate_manage_cassandra
  hosts = [cassandra_properties['hosts']].flatten
  hosts = hosts.map { |x| x.split(':', 2).first }
  !(hosts & %w(localhost 127.0.0.1)).empty?
end

#calculate_manage_ssl_certificateBoolean

Calculates whether the SSL certificate should be managed by the cookbook or should not based only on the Kong configuration.

Returns true if both the node['kong']['kong.yml']['ssl_cert_path'] and node['kong']['kong.yml']['ssl_key_path'] are set.

Examples:

self.calculate_manage_ssl_certificate #=> true

Returns:

  • (Boolean)

    true if SSL certificate should be managed.


183
184
185
186
# File 'libraries/cookbook_helpers.rb', line 183

def calculate_manage_ssl_certificate
  node['kong']['kong.yml']['ssl_cert_path'].nil? ||
    node['kong']['kong.yml']['ssl_key_path'].nil?
end

#cassandra_hostsArray

Gets the Cassandra hosts from Node attributes.

Examples:

self.cassandra_hosts #=> ["localhost:9042", "db.example.com:9042"]

Returns:

  • (Array)

    hosts list.


96
97
98
# File 'libraries/cookbook_helpers.rb', line 96

def cassandra_hosts
  cassandra_properties['hosts']
end

#cassandra_localhost_hostString

Gets the Cassandra server address to be installed in local machine.

Reads the data from ['cassandra']['properties']['host'] Node attribute.

Examples:

self.cassandra_localhost_host #=> "localhost:9042"

Returns:

  • (String)

    the local server address in the 'host:port' format.


121
122
123
124
# File 'libraries/cookbook_helpers.rb', line 121

def cassandra_localhost_host
  localhosts = cassandra_localhosts
  localhosts.empty? ? cassandra_hosts.first : localhosts.first
end

#cassandra_localhost_portString

Gets the local Cassandra server port number.

Reads the data from ['cassandra']['properties']['host'] Node attribute.

Examples:

self.cassandra_localhost_port #=> "9042"

Returns:

  • (String)

    the Cassandra server port number.


134
135
136
# File 'libraries/cookbook_helpers.rb', line 134

def cassandra_localhost_port
  cassandra_localhost_host.split(':', 2)[1] || '9042'
end

#cassandra_localhostsArray

Gets the Cassandras hosts in local machine searching in ['cassandra']['properties']['host'] Node attribute.

Examples:

self.cassandra_localhosts #=> ["localhost:9042"]

Returns:

  • (Array)

    hosts list.


107
108
109
110
111
# File 'libraries/cookbook_helpers.rb', line 107

def cassandra_localhosts
  cassandra_hosts.select do |x|
    %w(localhost 127.0.0.1).include?(x.split(':', 2)[0])
  end
end

#cassandra_propertiesMash

Gets Cassandra properties from node attributes from node['kong']['kong.yml']['databases_available']['cassandra']\ ['properties']

Examples:

self.cassandra_properties  #=> {"hosts"=>"localhost:9042", "timeout"=>1000, "keyspace"=>"kong",
  #    "keepalive"=>60000}

Returns:

  • (Mash)

    Cassandra properties.


86
87
88
# File 'libraries/cookbook_helpers.rb', line 86

def cassandra_properties
  node['kong']['kong.yml']['databases_available']['cassandra']['properties']
end

#manage_cassandraBoolean

Calculates whether the Cassandra installation should be managed by the cookbook or should not based on the Node attributes* and the **Kong configuration.

Tries to read the value from node['kong']['manage_cassandra'] and calculates it if not set.

Examples:

self.manage_cassandra #=> true

Returns:

  • (Boolean)

    true if Cassandra should be installed locally.

See Also:


166
167
168
169
170
171
# File 'libraries/cookbook_helpers.rb', line 166

def manage_cassandra
  unless node['kong']['manage_cassandra'].nil?
    return node['kong']['manage_cassandra']
  end
  node.default['kong']['manage_cassandra'] = calculate_manage_cassandra
end

#manage_ssl_certificateBoolean

Calculates whether the SSL certificate should be managed by the cookbook or should not based on the Node attributes* and the **Kong configuration.

Tries to read the value from node['kong']['manage_ssl_certificate'] and calculates it if not set.

Examples:

self.manage_ssl_certificate #=> true

Returns:

  • (Boolean)

    true if SSL certificate should be managed.

See Also:


200
201
202
203
204
205
206
# File 'libraries/cookbook_helpers.rb', line 200

def manage_ssl_certificate
  unless node['kong']['manage_ssl_certificate'].nil?
    return node['kong']['manage_ssl_certificate']
  end
  node.default['kong']['manage_ssl_certificate'] =
    calculate_manage_ssl_certificate
end

#package_nameString

Gets the package name from node['kong']['package_file'] after making substitutions.

Examples:

self.package_name #=> "kong-0.4.2.squeeze_all.deb"

Returns:

  • (String)

    package name.

See Also:


49
50
51
# File 'libraries/cookbook_helpers.rb', line 49

def package_name
  format(node['kong']['package_file'], substitutions)
end

#package_pathString

Gets package file full path in disk.

Examples:

self.package_path #=> "/var/chef/cache/kong-0.4.2.squeeze_all.deb"

Returns:

  • (String)

    package path.


72
73
74
# File 'libraries/cookbook_helpers.rb', line 72

def package_path
  ::File.join(Chef::Config[:file_cache_path], package_name)
end

#package_urlString

Gets the package full URL after making substitutions.

Examples:

self.package_url  #=> "https://github.com/Mashape/kong/releases/download/0.4.2/"\
  #   "kong-0.4.2.squeeze_all.deb"

Returns:

  • (String)

    URL.

See Also:


62
63
64
# File 'libraries/cookbook_helpers.rb', line 62

def package_url
  format("#{node['kong']['mirror']}#{package_name}", substitutions)
end

#substitutionsHash

Gets some substitutions to apply to some attribute values using Kernel.format.

Examples:

self.substitutions #=> {:version=>"0.4.2"}

Returns:

  • (Hash)

    substitution list.


37
38
39
# File 'libraries/cookbook_helpers.rb', line 37

def substitutions
  { version: node['kong']['version'] }
end