Class: Capistrano::Former03::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/former03/mysql.rb

Instance Method Summary collapse

Constructor Details

#initializeMySQL

Returns a new instance of MySQL.



18
19
# File 'lib/capistrano/former03/mysql.rb', line 18

def initialize
end

Instance Method Details

#backup(t) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/capistrano/former03/mysql.rb', line 117

def backup(t)
  @task = t
  mysql_connections = get_mysql_connections

  mysql_connections.each_with_index do |c, index|
    cnf = mysql_cnf_upload(:backup, nil, index)
    database = config(fetch(:stage), index)[:database]
    # run backup
    @task.on release_roles :all do
      output_dir = fetch(:backup_dest_path)

      # test for backup dir
      execute "test -d \"#{output_dir}\" || mkdir -p \"#{output_dir}\""

      # backup mysqldb
      backupfile = mysql_connections.length > 1 ? "mysql#{index+1}.sql.gz" : "mysql.sql.gz"
      output = File.join(output_dir, backupfile)
      execute "mysqldump --defaults-file=#{cnf} #{database} | gzip > #{output}"
      execute "chmod 600 #{output}"

      # remove credentials
      execute "rm #{cnf}"
    end
  end


end

#cnf_path(name, index = 0) ⇒ Object



87
88
89
# File 'lib/capistrano/former03/mysql.rb', line 87

def cnf_path(name, index=0)
  File.join(deploy_to, 'shared', ".my.cnf_#{name}_#{index}")
end

#config(stage = nil, index = 0) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/capistrano/former03/mysql.rb', line 74

def config(stage=nil, index=0)

  mysql_connections = get_mysql_connections

  @config = mysql_connections.fetch(index)
  stage ||= task.fetch(:stage)
  @config[stage]
end

#deploy_toObject



83
84
85
# File 'lib/capistrano/former03/mysql.rb', line 83

def deploy_to
  @task.fetch(:deploy_to)
end

#get_mysql_connectionsObject



65
66
67
68
69
70
71
72
# File 'lib/capistrano/former03/mysql.rb', line 65

def get_mysql_connections()
  mysql_connections = fetch(:mysql_connections)

  # make sure mysql_connections is an array
  mysql_connections = [mysql_connections] unless mysql_connections.is_a? Array

  mysql_connections
end

#mysql_cnf(stage = nil, index = 0) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/capistrano/former03/mysql.rb', line 107

def mysql_cnf(stage=nil, index=0)
  c=config(stage, index)
  output = "[client]\n"
  output +="user=#{c[:username]}\n" if c.has_key?(:username)
  output +="password=#{c[:password]}\n" if c.has_key?(:password)
  output +="host=#{c[:host]}\n" if c.has_key?(:host)
  output +="port=#{c[:port]}\n" if c.has_key?(:port)
  output
end

#mysql_cnf_upload(name, stage = nil, index = 0) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/capistrano/former03/mysql.rb', line 91

def mysql_cnf_upload(name, stage=nil, index=0)
  dest = cnf_path(name, index)
  src = Tempfile.new("my#{index}.cnf")
  src.write(mysql_cnf(stage, index))
  src.flush
  @task.on release_roles :all do
    upload!(src.path, dest)
    execute "chmod 600 #{dest}"
  end

  # remove tempfile
  src.delete

  dest
end

#sync(t) ⇒ Object



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
# File 'lib/capistrano/former03/mysql.rb', line 28

def sync(t)
  @task = t
  src_stage = :production
  dst_stage = fetch(:stage)

  if src_stage == dst_stage
    fail "Source stage (#{src_stage}) is equal to destination stage (#{dst_stage})"
  end

  get_mysql_connections.each_with_index do |c, index|

    src_database = config(src_stage, index)[:database]
    dst_database = config(dst_stage, index)[:database]
    src_cnf = mysql_cnf_upload(:src, src_stage, index)
    dst_cnf = mysql_cnf_upload(:dst, dst_stage, index)

    @task.on release_roles :all do

      # remove all tables on dest
      tables = capture "mysql --defaults-file=#{dst_cnf} -e'SHOW TABLES;' #{dst_database}"
      tables = tables.split
      if tables.length > 1
        tables = tables[1..-1].map { |name| "`#{name}`" }.join(',')
        execute "mysql --defaults-file=#{dst_cnf} -e'DROP TABLES #{tables};' #{dst_database}"
      end

      # sync production to stage
      execute "mysqldump --defaults-file=#{src_cnf} #{src_database} | mysql --defaults-file=#{dst_cnf} #{dst_database}"

      # remove credentials
      execute "rm #{src_cnf}"
      execute "rm #{dst_cnf}"
    end

  end
end

#taskObject



21
22
23
24
25
26
# File 'lib/capistrano/former03/mysql.rb', line 21

def task
  if @task.nil?
    fail "No object handle"
  end
  @task
end

#templates(t) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/capistrano/former03/mysql.rb', line 145

def templates(t)
  @task = t

  fetch(:mysql_templates, []).each_with_index do |c, index|

    dest = File.join(fetch(:local_stage_path), c[:destination])

    puts("Evaluting template '#{c[:template]}' to '#{dest}'")

    # Read template
    erb = ERB.new(
        File.read(c[:template]),
        nil,
        '-',
    )

    begin
      File.open(dest, 'w') do |file|
        namespace = Namespace.new({
                                      config: config(nil, index),
                                      stage: fetch(:stage),
                                  })
        file.write(erb.result(namespace.get_binding))
      end
    rescue
      fail "MySQL Template could not be written, make sure the configs :mysql_connections and :mysql_templates match!"
    end

    File.exist?(c[:destination])
  end
end