~brightbox/brightbox/domtrix-packaging

« back to all changes in this revision

Viewing changes to lib/domtrix_cloudsql/mys_restore_command.rb

  • Committer: Neil Wilson
  • Date: 2016-08-12 14:12:30 UTC
  • mfrom: (42.1.26)
  • Revision ID: git-v1:57d7253cf2daa4943cddeb32c6c19745c9f92983
MergeĀ tagĀ 'upstream/0.0.30'

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#
5
5
#  Mysql Restore Command
6
6
 
7
 
class MysRestoreCommand < DataCommand
 
7
class MysRestoreCommand < CloudsqlRestoreCommand
8
8
 
9
9
private
10
 
  
11
 
  include RootPrivileges
12
 
  include DomtrixConfig
13
 
  include CommandRunner
14
 
  include MysUriCommon
15
 
 
16
 
  def mys_restore_command
17
 
    "nice curl --silent --show-error --fail #{curl_token_option} #{target_uri_name} | tar --extract #{compression_tag} --directory #{data_area} ."
18
 
  end
19
 
 
20
 
  def get_magic_command
21
 
    "nice curl -r0-3 --silent --show-error --fail #{curl_token_option} #{target_uri_name}"
22
 
  end
23
 
 
24
 
  def service_running_command
25
 
    InitDetector.select(
26
 
      "systemctl is-active --quiet mysql",
27
 
      "status mysql 2>/dev/null | grep -q running"
28
 
    )
29
 
  end
30
 
 
31
 
  def stop_service_command
32
 
    InitDetector.select(
33
 
      "systemctl stop mysql",
34
 
      "stop --quiet mysql"
35
 
    )
36
 
  end
37
 
 
38
 
  def zero_data_area
39
 
    Syslog.info("#{self.class.name}: Zeroing MySQL data area - #{data_area}")
40
 
    FileUtils.rm_r(data_area_file_list)
41
 
  rescue SystemCallError => e
42
 
    raise RuntimeError, "Failed to zero data area: #{e.message}"
43
 
  end
44
 
 
45
 
  def determine_compression
46
 
    Syslog.debug("#{self.class.name}: Checking compression type")
47
 
    magic = cmd(get_magic_command, "Fetching magic number", "failed to fetch magic number from #{target_uri_display_name}").to_s
48
 
    case
49
 
    when magic.unpack('V') == [0x184D2204]
50
 
      Syslog.info("#{self.class.name}: Detected lz4 compressed archive")
51
 
      @compression_tag = "-Ilz4"
52
 
    when magic.unpack('n') == [0x1f8b]
53
 
      Syslog.info("#{self.class.name}: Detected gzip compressed archive")
54
 
      @compression_tag = "-z"
55
 
    else
56
 
      Syslog.info("#{self.class.name}: Assuming uncompressed archive")
57
 
      @compression_tag = ""
58
 
    end
59
 
  end
60
 
 
61
 
  attr_reader :compression_tag
62
 
 
63
 
  def run_restore
64
 
    Syslog.debug "Running restoration process"
65
 
    if system(service_running_command)
66
 
      run(stop_service_command, "stopped MySQL service", "failed to stop MySQL service")
67
 
    end
68
 
    zero_data_area
69
 
    determine_compression
70
 
    run(mys_restore_command, "restored MySQL data area", "failed to restore database from #{target_uri_display_name}")
71
 
  end
72
 
 
73
 
  def data_action
74
 
    run_restore
75
 
    @state="completed"
76
 
  end
 
10
 
 
11
  alias data_area mysql_data_area
 
12
  alias service_name mysql_service_name
77
13
 
78
14
end