Module: FoundryWordpressTool::Database

Included in:
Base
Defined in:
lib/foundry_wordpress_tool/database.rb

Constant Summary collapse

FORWARDED_MYSQL_PORT =
33334
SNAPSHOT_TEMP_FILENAME =
'/tmp/fwd_snapshot.sql'

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
69
70
71
72
73
74
75
76
# File 'lib/foundry_wordpress_tool/database.rb', line 5

def self.included(thor)
  thor.class_eval do

    desc "db_pull", "Dump the remote database & import it locally"
    def db_pull
      load_environment_config()
      load_database_configs()
      say_status :db, "Exporting remote database to temporary snapshot"
      database_tunnel do
        dump_command = "mysqldump -u #{@remote_db[:username]} -h 127.0.0.1 -P #{FORWARDED_MYSQL_PORT} -p#{@remote_db[:password]} #{@remote_db[:name]} > #{SNAPSHOT_TEMP_FILENAME}"
        run dump_command
      end
      say_status :db, 'Importing temporary snapshot to local database'
      import_command = "cat #{SNAPSHOT_TEMP_FILENAME} | mysql -u #{@local_db[:username]} -h #{@local_db[:host]} -P #{@local_db[:port]} -p#{@local_db[:password]} #{@local_db[:name]}"
      run import_command
    end
  
    desc "db_push", "Dump the local database & import it remotely"
    def db_push
      load_environment_config()
      load_database_configs()
      say_status :db, 'Exporting local database to temporary snapshot'
      import_command = "mysqldump -u #{@local_db[:username]} -h #{@local_db[:host]} -P #{@local_db[:port]} -p#{@local_db[:password]} #{@local_db[:name]} > #{SNAPSHOT_TEMP_FILENAME}"
      run import_command
      say_status :db, "Importing temporary snapshot to remote database"
      database_tunnel do
        dump_command = "cat #{SNAPSHOT_TEMP_FILENAME} | mysql -u #{@remote_db[:username]} -h 127.0.0.1 -P #{FORWARDED_MYSQL_PORT} -p#{@remote_db[:password]} #{@remote_db[:name]}"
        run dump_command
      end
    end
    
    no_tasks do

      def load_database_configs
        say_status :db, 'Pulling remote & local MySQL settings from wp-config.php files'
        # read the remote SSH config:
        Net::SSH.start(@config['host'], @config['username']) do |ssh|
          ssh.exec! "cat #{File.join(@config['deploy_to'], 'wp-config.php')}" do |ch, stream, data|
            @remote_db = parse_db_settings_from_wordpress_config(data)
          end
        end    
        # read the local SSH config:
        @local_db = parse_db_settings_from_wordpress_config(File.open("wp-config.php", "rb").read)
      end

      def database_tunnel(&block)
        say_status :db, 'Opening SSH tunnel for MySQL'
        gateway = Net::SSH::Gateway.new(@config['host'], @config['username'])
        port = gateway.open(@remote_db[:host], @remote_db[:port], FORWARDED_MYSQL_PORT)
  
        begin
          yield
        ensure
          say_status :db, 'Closing SSH tunnel for MySQL'
          gateway.shutdown!
        end      
      end

      def parse_db_settings_from_wordpress_config(config_file_content)
        remote_db = {}
        remote_db[:name] = /define\(\'DB_NAME\'\,\s*\'([^']*)\'\)\;/.match(config_file_content)[1]
        remote_db[:username] = /define\(\'DB_USER\'\,\s*\'([^']*)\'\)\;/.match(config_file_content)[1]
        remote_db[:password] = /define\(\'DB_PASSWORD\'\,\s*\'([^']*)\'\)\;/.match(config_file_content)[1]
        remote_db[:host] = /define\(\'DB_HOST\'\,\s*\'([^']*)\'\)\;/.match(config_file_content)[1]
        remote_db[:port] = /define\(\'DB_PORT\'\,\s*\'([^']*)\'\)\;/.match(data)[1] rescue '3306'
        return remote_db
      end

    end # end no_tasks
    
  end
end