Class: CapistranoAutoMultiInstall::ConfigureDatabase

Inherits:
Handlers
  • Object
show all
Defined in:
lib/capistrano_auto_multi_install/configure_database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Handlers

#capistrano_instance, #capture, #exists?, #fetch, #logger, #put, #put_with_power, #run, #run_with_power, #sudo

Constructor Details

#initialize(*args) ⇒ ConfigureDatabase

Returns a new instance of ConfigureDatabase.



6
7
8
9
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 6

def initialize(*args)
  super *args
  @username = fetch(:mysql_user)
end

Instance Attribute Details

#admin_passwordObject

Returns the value of attribute admin_password.



4
5
6
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 4

def admin_password
  @admin_password
end

#admin_usernameObject

Returns the value of attribute admin_username.



4
5
6
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 4

def admin_username
  @admin_username
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 4

def password
  @password
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 4

def username
  @username
end

Instance Method Details

#check_or_create_databaseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 11

def check_or_create_database
  log 'Checking if '+fetch(:application)+'_production database exists'
  log "Creating it if it doesn't exist yet"
  
  @admin_username = fetch(:mysql_admin_name)
  @admin_password = fetch(:mysql_admin_password)
  @admin_password = '-p'+@admin_password unless @admin_password.empty?
  
  run_as_admin("'CREATE DATABASE IF NOT EXISTS `#{fetch(:application)}_production` \
DEFAULT CHARACTER SET utf8  \
DEFAULT COLLATE utf8_general_ci;'")
  
  log "Database done"
end

#check_or_create_userObject



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
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/capistrano_auto_multi_install/configure_database.rb', line 27

def check_or_create_user
  log 'Checking if '+fetch(:mysql_user)+' has already been created and granted correctly'
  log 'Errors messages reporting a missing user can be safely ignored'
  
  grants = capture_as_admin "'SHOW GRANTS FOR #{@username}@localhost;'", ";true;"
  
  ok = true
  
  required_grants = ["SELECT","INSERT","UPDATE","DELETE","CREATE","DROP", "INDEX", "LOCK TABLES", "ALTER", "CREATE VIEW"]
  if grants =~ /ERROR/ # User is not present
    log 'User not present, creating him'
    ok = false
    # now let's create him
    @password = fetch(:mysql_user_password)
    run_as_admin "\"CREATE USER '#{@username}'@localhost IDENTIFIED BY '#{@password}'\""
  else
    log "User present, checking his rights"
    
    # First we've gotta filter the permissions by table.
    # here we wanna make sure he has the necessary rights on "application"_production
    grants = grants.split("\n").delete_if{ |grant| !(grant =~ Regexp.new(fetch(:application)+'_production'))}[0]
    
    # Are all "required_grants" present?
    required_grants.each do |grant|
      ok = grants =~ Regexp.new(grant) if ok
    end
  end
  # Ok = true => User exists and has the right rights on the right table
  if ok
    log 'Permissions are ok'
  else
    log 'Permissions are missing, adding them'
    flush = "&& mysqladmin flush-privileges -u #{@admin_username} #{@admin_password}"
    run_as_admin "\"GRANT "+required_grants.join(',')+" ON #{fetch(:application)}_production.* TO '#{@username}'@localhost\"", flush
  end
  log "Alright, now database and user are correctly set up"
  
  
  
  # Create database.yml
  mysql_password = fetch(:mysql_user_password)
  mysql_user =  fetch(:mysql_user)
  application = fetch(:application)
  
  conf = ERB.new(File.read(File.dirname(__FILE__)+"/database.yml.erb")).result(binding)
  run_with_power "mkdir -p #{fetch(:shared_path)}/db", fetch(:runner)
  run_with_power "mkdir -p #{fetch(:shared_path)}/config", fetch(:runner)
  put_with_power conf, "#{fetch(:shared_path)}/config/database.yml", fetch(:runner)
  
  
  # syslinks it
  
  run_with_power "ln -nfs #{fetch(:shared_path)}/config/database.yml #{fetch(:current_path)}/config/database.yml", fetch(:runner)
end