Module: Openbanana

Extended by:
Rake::DSL
Defined in:
lib/openbanana.rb,
lib/openbanana/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.configObject

read config YML for environment



18
19
20
21
# File 'lib/openbanana.rb', line 18

def self.config
  config = Openbanana.load_yml
  return config[Rails.env]
end

.envObject

Defining this just to make stubbing in rspec easier



8
9
10
# File 'lib/openbanana.rb', line 8

def self.env
  ENV
end

.grant_for(config) ⇒ Object

generate SQL statements for DB creation, permission grant



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
# File 'lib/openbanana.rb', line 37

def self.grant_for(config)
  grants = []

  unless config['database']
    return nil
  end

  grants << "CREATE DATABASE #{config['database']};"

  if config['username']
    identify_by = ''
    if config['password']
      identify_by = "IDENTIFIED BY '#{config['password']}'"
    end

    unless config['networks']
      grants << "GRANT ALL PRIVILEGES ON #{config['database']}.* TO '#{config['username']}'@'%' #{identify_by};"
    else
      config['networks'].each do |network|
        grants << "GRANT ALL PRIVILEGES ON #{config['database']}.* TO '#{config['username']}'@'#{network}' #{identify_by};"
      end
    end
  end

  return grants
end

.grants(args = {}) ⇒ Object

generate grants



79
80
81
82
83
84
85
86
# File 'lib/openbanana.rb', line 79

def self.grants(args = {})
  config = args[:sharded] ? Openbanana.shard_config : Openbanana.config
  grants = []
  Openbanana.traverse(config) do |leaf_config|
    grants += Openbanana.grant_for(leaf_config)
  end
  return grants
end

.grants_task(args = {}) ⇒ Object

run task (here for DRY-ness)



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/openbanana.rb', line 89

def self.grants_task(args = {})
  username = ENV['DB_USER'] || 'root'
  password = ENV['DB_PASSWORD'] || ''
  host = nil
  if ENV['DB_HOST']
    host = "--host=#{ENV['DB_HOST']}"
  end

  Openbanana.grants(args).each do |grant|
    sh "mysql #{host} --user=#{username} --password=\"#{password}\" -e \"#{grant}\" || true"
  end
end

.load_yml(filename = 'database.yml') ⇒ Object

load config YML file



13
14
15
# File 'lib/openbanana.rb', line 13

def self.load_yml(filename = 'database.yml')
  YAML.load(ERB.new(File.read(File.join(Rails.root, 'config', filename))).result)
end

.shard_configObject

read config for sharded case using octopus shards.yml



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

def self.shard_config
  config = Openbanana.load_yml('shards.yml')
  config = config['octopus'] if config.keys == ['octopus']
  config = config[Rails.env]
  config = config['shards'] if config.keys == ['shards']
  return config
rescue Errno::ENOENT => e
  puts 'No shards.yml, using shards in database.yml'
  config = Openbanana.load_yml
  return Hash[config.select {|k,v| k.include?(Rails.env) && k != Rails.env}]
end

.traverse(obj, &block) ⇒ Object

run block on all contained hashes which have the key ‘database’



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openbanana.rb', line 65

def self.traverse(obj, &block)
  case obj
  when Hash
    if obj['database']
      block.call(obj)
    else
      obj.each {|k,v| traverse(v, &block)}
    end
  when Array
    obj.each {|v| traverse(v, &block)}
  end
end