Module: Postgres::Clone::Commands

Includes:
LocalCommands, Logger, RemoteCommands
Included in:
Executable
Defined in:
lib/postgres/clone/commands.rb

Constant Summary

Constants included from Logger

Logger::DEFAULT_COLOR

Instance Method Summary collapse

Methods included from RemoteCommands

#close_ssh_connections, #open_ssh_connection, #run_remote, #sudo_remote

Methods included from Logger

#log_message

Methods included from CommandLine

#build_command, #log_command

Methods included from LocalCommands

#run_local, #sudo_local

Instance Method Details

#copy_file(src_host, dst_host, file_path) ⇒ Object



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
# File 'lib/postgres/clone/commands.rb', line 13

def copy_file(src_host, dst_host, file_path)
  log_message("Copying #{file_path} from #{src_host} to #{dst_host}", header: '')

  if src_host == dst_host
    log_message(src_host, 'Source and destination host are the same, skipping file copy', color: :yellow, header: '')
    return
  end

  result =
    if local_host?(src_host)
      if file_exists?(dst_host, file_path)
        puts Rainbow("[#{dst_host}] File #{file_path} already exists!").green
        return if %w(yes y).include?(
          ask(Rainbow('Would you like to restore the existing dump (y/n)?').yellow).downcase
        )
        abort Rainbow("[#{dst_host}] File #{file_path} already exists!").red
      else
        run_local("scp #{file_path} #{dst_host}:#{file_path}")
      end
    elsif local_host?(dst_host)
      if file_exists?('localhost', file_path)
        puts Rainbow("[localhost] File #{file_path} already exists!").green
        return if %w(yes y).include?(
          ask(Rainbow('Would you like to restore the existing dump (y/n)?').yellow).downcase
        )
        abort Rainbow("[localhost] File #{file_path} already exists!").red
      else
        run_local("scp #{src_host}:#{file_path} #{file_path}")
      end
    else
      if file_exists?(dst_host, file_path)
        puts Rainbow("[#{dst_host}] File #{file_path} already exists!").green
        return if %w(yes y).include?(
          ask(Rainbow('Would you like to restore the existing dump (y/n)?').yellow).downcase
        )
        abort Rainbow("[#{dst_host}] File #{file_path} already exists!").red
      else
        run_remote(src_host, "scp #{file_path} #{dst_host}:#{file_path}")
      end
    end

  abort Rainbow('The file copy failed, cancelling database clone').red if result.failed?
end

#current_userObject



57
58
59
60
# File 'lib/postgres/clone/commands.rb', line 57

def current_user
  # TODO: remote
  Etc.getlogin
end

#disk_usage(host_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/postgres/clone/commands.rb', line 62

def disk_usage(host_name)
  result =
    if local_host?(host_name)
      run_local(free_disk_space_command)
    else
      run_remote(host_name, free_disk_space_command)
    end

  columns = result.output.split(/\s+/)

  abort Rainbow('Expected at least 5 columns of data from df -H').red unless columns.length >= 5

  { available: columns[3], size: columns[1] }
end

#file_exists?(host_name, file_path) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/postgres/clone/commands.rb', line 77

def file_exists?(host_name, file_path)
  log_message("Checking file existance: #{file_path}", host_name: host_name)

  result =
    if local_host?(host_name)
      run_local(file_exists_command(file_path))
    else
      run_remote(host_name, file_exists_command(file_path))
    end

  result.success?
end

#file_exists_command(file_path) ⇒ Object



90
91
92
# File 'lib/postgres/clone/commands.rb', line 90

def file_exists_command(file_path)
  "test -e #{file_path}"
end

#free_disk_space_commandObject



94
95
96
# File 'lib/postgres/clone/commands.rb', line 94

def free_disk_space_command
  'df -H / | grep "/$"'
end

#local_host?(host_name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/postgres/clone/commands.rb', line 98

def local_host?(host_name)
  host_name == 'localhost'
end

#postgres_create_database(host_name, database_name, template: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/postgres/clone/commands.rb', line 102

def postgres_create_database(host_name, database_name, template: nil)
  log_message("Creating database #{database_name}", host_name: host_name)

  result =
    if local_host?(host_name)
      run_local(
        postgres_create_database_command(database_name, template: template)
      )
    else
      sudo_remote(
        host_name,
        postgres_create_database_command(database_name, template: template),
        user: 'postgres'
      )
    end

  abort Rainbow('Failed to create database').red if result.failed?
end

#postgres_create_database_command(database_name, template: nil) ⇒ Object



121
122
123
# File 'lib/postgres/clone/commands.rb', line 121

def postgres_create_database_command(database_name, template: nil)
  "psql -c \"create database #{database_name}#{template.nil? ? '' : " with template #{template}"}\""
end

#postgres_dump_database(host_name, database_name, file_path) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/postgres/clone/commands.rb', line 125

def postgres_dump_database(host_name, database_name, file_path)
  log_message("Dumping database #{database_name} to #{file_path}", header: '', host_name: host_name)

  if file_exists?(host_name, file_path)
    puts Rainbow("[#{host_name}] File #{file_path} already exists!").green
    use_existing_file = %w(yes y).include?(
      ask(Rainbow('Would you like to restore the existing dump (y/n)?').yellow).downcase
    )
    return if use_existing_file
    abort Rainbow("Please delete #{host_name}:#{file_path} before continuing").red
  end

  result =
    if local_host?(host_name)
      run_local(
        postgres_dump_database_command(file_path, database_name)
      )
    else
      sudo_remote(
        host_name,
        postgres_dump_database_command(file_path, database_name),
        user: 'postgres'
      )
    end

  if result.failed?
    puts Rainbow('The database dump failed, cleaning up partial dump file').red
    if local_host?(host_name)
      sudo_local("rm #{file_path}")
    else
      sudo_remote(host_name, "rm #{file_path}")
    end
    abort Rainbow('Cancelling database clone').red
  end
end

#postgres_dump_database_command(file_path, source_database) ⇒ Object



161
162
163
# File 'lib/postgres/clone/commands.rb', line 161

def postgres_dump_database_command(file_path, source_database)
  "pg_dump --file=#{file_path} --no-acl --no-owner --format=custom #{source_database}"
end

#postgres_restore_database(host_name, database_name, file_path) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/postgres/clone/commands.rb', line 165

def postgres_restore_database(host_name, database_name, file_path)
  log_message("Restoring database #{database_name} from #{file_path}", header: '', host_name: host_name)

  abort Rainbow("File #{file_path} does not exist!").red unless file_exists?(host_name, file_path)

  # TODO: check to see if target database exists

  postgres_create_database(host_name, database_name)

  result =
    if local_host?(host_name)
      run_local(
        postgres_restore_database_command(file_path, database_name)
      )
    else
      sudo_remote(
        host_name,
        postgres_restore_database_command(file_path, database_name),
        user: 'postgres'
      )
    end

  abort Rainbow('The database restore failed').red if result.failed?
end

#postgres_restore_database_command(file_path, destination_database) ⇒ Object



190
191
192
# File 'lib/postgres/clone/commands.rb', line 190

def postgres_restore_database_command(file_path, destination_database)
  "pg_restore --dbname=#{destination_database} --jobs=4 -O #{file_path}"
end


194
195
196
197
198
199
200
# File 'lib/postgres/clone/commands.rb', line 194

def print_host_disk_space(host_name)
  log_message('Checking free disk space', header: '', host_name: host_name)

  usage = disk_usage(host_name)

  log_message("#{usage[:available]} free of #{usage[:size]} total", host_name: host_name)
end

#user_password(host_name, user) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/postgres/clone/commands.rb', line 202

def user_password(host_name, user)
  @user_passwords ||= {}
  @user_passwords["#{host_name}_#{user}"] ||= begin
    ask(Rainbow("#{user}'s password for #{host_name}? ").yellow, echo: false).tap do |_|
      puts
    end
  end
end