Skip to content
This repository
Browse code

Added hability to set a public, private or loopback ip address as bind

address without having to harcode the actual ip.

There's an node['percona']['server']['bind_to'] attribute that accepts
the values 'public_ip', 'private_ip', 'loopback' or an interface name
like 'eth0'. Based on this, the recipe will find a corresponding ipv4
address, and override the node['percona']['server']['bind_address']
attribute.
  • Loading branch information...
commit b5637e1302c8e51467da80e018cdbde7bf7600b2 1 parent f0a0c29
Augusto Becciu authored October 11, 2012
71  libraries/config_helper.rb
... ...
@@ -0,0 +1,71 @@
  1
+require "ipaddr"
  2
+
  3
+module Percona
  4
+  module IPScope
  5
+    extend self
  6
+
  7
+    PRIVATE_RANGES = [IPAddr.new("10.0.0.0/8"), IPAddr.new("192.168.0.0/16")]
  8
+    LOOPBACK_RANGE = IPAddr.new("0.0.0.0/8")
  9
+
  10
+    def for(ipaddress)
  11
+      addr = IPAddr.new(ipaddress)
  12
+
  13
+      return :private if PRIVATE_RANGES.any? { |range| range.include? addr }
  14
+      return :loopback if LOOPBACK_RANGE.include? addr
  15
+      return :public
  16
+    end
  17
+  end
  18
+
  19
+  module ConfigHelper
  20
+    extend self
  21
+
  22
+    def bind_to(node, interface)
  23
+      case interface.to_sym
  24
+      when :public_ip
  25
+        find_public_ip(node)
  26
+      when :private_ip
  27
+        find_private_ip(node)
  28
+      when :loopback
  29
+        find_loopback_ip(node)
  30
+      else
  31
+        find_interface_ip(node, interface)
  32
+      end
  33
+    end
  34
+
  35
+    private
  36
+
  37
+    def find_public_ip(node)
  38
+      return node['cloud']['public_ipv4'] if node['cloud'] && node['cloud']['public_ipv4']
  39
+      find_ip(node, :private)
  40
+    end
  41
+
  42
+    def find_private_ip(node)
  43
+      return node['cloud']['local_ipv4'] if node['cloud'] && node['cloud']['local_ipv4']
  44
+      return node['cloud']['private_ipv4'] if node['cloud'] && node['cloud']['private_ipv4']
  45
+      return node['privateaddress'] if node['privateaddress']
  46
+      find_ip(node, :private)
  47
+    end
  48
+
  49
+    def find_loopback_ip(node)
  50
+      find_ip(node, :loopback)
  51
+    end
  52
+
  53
+    def find_ip(node, scope)
  54
+      node['network']['interfaces'].each do |ifce, attrs|
  55
+        next unless attrs['addresses']
  56
+        attrs['addresses'].each do |addr, data|
  57
+          next unless data['family'] == 'inet'
  58
+          return addr if IPScope.for(addr) == scope
  59
+        end
  60
+      end
  61
+    end
  62
+
  63
+    def find_interface_ip(node, interface)
  64
+      interfaces = node['network']['interfaces']
  65
+      return unless interfaces[interface]
  66
+      addr = interfaces[interface]['addresses'].find { |ip, attrs| attrs['family'] == 'inet' }
  67
+      addr && addr[0]
  68
+    end
  69
+  end
  70
+end
  71
+
13  recipes/configure_server.rb
@@ -6,6 +6,19 @@
6 6
 # construct an encrypted passwords helper -- giving it the node and bag name
7 7
 passwords = EncryptedPasswords.new(node, percona["encrypted_data_bag"])
8 8
 
  9
+if server['bind_to']
  10
+  ipaddr = Percona::ConfigHelper.bind_to(node, server['bind_to'])
  11
+  if ipaddr && server['bind_address'] != ipaddr
  12
+    node.override['percona']['server']['bind_address'] = ipaddr
  13
+    node.save
  14
+  end
  15
+
  16
+  log "Can't find ip address for #{server['bind_to']}" do
  17
+    level :warn
  18
+    only_if { ipaddr.nil? }
  19
+  end
  20
+end
  21
+
9 22
 datadir = mysqld["datadir"] || server["datadir"]
10 23
 user    = mysqld["user"] || server["user"]
11 24
 

0 notes on commit b5637e1

Please sign in to comment.
Something went wrong with that request. Please try again.