class Object

Public Instance Methods

ask_reload_or_restart(name, host) click to toggle source
# File lib/postgresinator/pg.rb, line 144
def ask_reload_or_restart(name, host)
  warn "A config file has changed for #{name} on #{host}, please specify " +
    "whether you would like to have PostgreSQL reload the config, or restart itself"
  set :reload_or_restart, ""
  while fetch(:reload_or_restart).empty?
    ask :reload_or_restart, ""
  end
  case fetch(:reload_or_restart).chomp.downcase
  when "reload"
    execute("docker", "kill", "-s", "SIGHUP", name)
  when "restart"
    restart_container(name)
  else
    warn "Please enter 'reload' or 'restart'"
    ask_reload_or_restart(name, host)
  end
end
existing_container_start_or_restart_if_needed(host) click to toggle source
# File lib/postgresinator/pg.rb, line 128
def existing_container_start_or_restart_if_needed(host)
  name = host.properties.postgres_container_name
  if container_is_running?(name)
    if container_is_restarting?(name)
      restart_container(name)
    elsif host.properties.config_file_changed
      ask_reload_or_restart(name, host)
    else
      info("No config file changes for #{name} " +
           "on #{host} and it is already running; we're setup!")
    end
  else
    start_container(name)
  end
end
install_recovery_conf() click to toggle source
# File lib/postgresinator/pg.rb, line 116
def install_recovery_conf
  as 'root' do
    path = "#{fetch(:postgres_data_path)}/recovery.conf"
    template_path = File.expand_path("#{fetch(:postgres_templates_path)}/recovery.conf.erb")
    generated_config_file = ERB.new(File.new(template_path).read).result(binding)
    upload! StringIO.new(generated_config_file), "/tmp/recovery_conf"
    execute("mv", "/tmp/recovery_conf", path)
    execute("chown", "#{fetch(:postgres_uid)}:#{fetch(:postgres_gid)}", path)
    execute("chmod", "0640", path)
  end
end
install_ssl_key_crt(host) click to toggle source
# File lib/postgresinator/pg.rb, line 97
def install_ssl_key_crt(host)
  as 'root' do
    [fetch(:postgres_ssl_key), fetch(:postgres_ssl_crt)].each do |file|
      if test("[", "-L", file, "]") or file_exists?(file)
        execute("rm", file)
      end
    end
    pg_ssl_key(host)
    pg_ssl_crt(host)
    execute("rm", fetch(:postgres_ssl_csr))
    execute("chmod", "0600", fetch(:postgres_ssl_key))
    execute("chmod", "0600", fetch(:postgres_ssl_crt))
    [fetch(:postgres_ssl_key), fetch(:postgres_ssl_crt)].each do |file|
      execute("chown", "#{fetch(:postgres_uid)}:#{fetch(:postgres_gid)}", file)
      execute("chmod", "0600", file)
    end
  end
end
pg_confirm_database_overwrite?(database_name) click to toggle source
# File lib/postgresinator/db.rb, line 139
def pg_confirm_database_overwrite?(database_name)
  warn "There is already data in the database '#{database_name}' on #{host} in the container " +
    "'#{host.properties.postgres_container_name}' which stores it's data in #{fetch(:postgres_data_path)} on the host."
  warn "If you continue, you must be positive you want to overwrite the existing data."
  ask :yes_or_no, "Are you positive?"
  case fetch(:yes_or_no).chomp.downcase
  when "yes"
    true
  when "no"
    false
  else
    warn "Please enter 'yes' or 'no'"
    pg_confirm_database_overwrite?(database_name)
  end
end
pg_confirm_file_overwrite?(dump_file) click to toggle source
# File lib/postgresinator/db.rb, line 125
def pg_confirm_file_overwrite?(dump_file)
  warn "A file named #{dump_file} already exists on #{host} in /tmp. If you continue, you will overwrite it."
  ask :yes_or_no, "Are you positive?"
  case fetch(:yes_or_no).chomp.downcase
  when "yes"
    true
  when "no"
    false
  else
    warn "Please enter 'yes' or 'no'"
    pg_confirm_file_overwrite?(dump_file)
  end
end
pg_create_database(database) click to toggle source
# File lib/postgresinator/built-in.rb, line 172
def pg_create_database(database)
  execute(
    "echo", "\"CREATE", "DATABASE", "\\\"#{database[:name]}\\\"",
    "WITH", "OWNER", "\\\"#{database[:db_role]}\\\"", "TEMPLATE",
    "template0", "ENCODING", "'UTF8';\"", "|",
    "docker", "run", "--rm", "--interactive",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'"
  )
end
pg_create_role(db_role, password) click to toggle source
# File lib/postgresinator/built-in.rb, line 159
def pg_create_role(db_role, password)
  execute(
    "echo", "\"CREATE", "ROLE", "\\\"#{db_role}\\\"",
    "WITH", "LOGIN", "ENCRYPTED", "PASSWORD", "'#{password}'",
    "REPLICATION", "CREATEDB;\"", "|",
    "docker", "run", "--rm", "--interactive",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'"
  )
end
pg_database_empty?(database_name) click to toggle source
# File lib/postgresinator/built-in.rb, line 221
def pg_database_empty?(database_name)
  test(
    "docker", "run", "--rm", "--tty",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash", fetch(:postgres_image_name), "-lc",
    "'/usr/bin/psql", "-U", "postgres", "-d", database_name,
    "--host", fetch(:postgres_socket_path),
    "-c", "\"\\dt\"", "|", "grep", "-qi", "\"no relations found\"'"
  )
end
pg_database_exists?(database_name) click to toggle source
# File lib/postgresinator/built-in.rb, line 210
def pg_database_exists?(database_name)
  test(
    "docker", "run", "--rm",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
     fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", fetch(:postgres_socket_path), "-lqt", "|",
    "cut", "-d\\|", "-f1", "|", "grep", "-qw", "#{database_name}'"
  )
end
pg_dump(host, args) click to toggle source
# File lib/postgresinator/built-in.rb, line 82
def pg_dump(host, args)
  execute(
    "docker", "run", "--rm",
    "--volume", "/tmp:/tmp:rw",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/pg_dump", "-U", "postgres",
    "--host", fetch(:postgres_socket_path), "-F", "tar",
    "-v", args.database_name, ">", "/tmp/#{args.dump_file}'"
  )
end
pg_grant_database(database) click to toggle source
# File lib/postgresinator/built-in.rb, line 185
def pg_grant_database(database)
  execute(
    "echo", "\"GRANT", "ALL", "PRIVILEGES", "ON", "DATABASE",
    "\\\"#{database[:name]}\\\"", "to", "\\\"#{database[:db_role]}\\\";\"", "|",
    "docker", "run", "--rm", "--interactive",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'"
  )
end
pg_init(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 30
def pg_init(host)
  execute(
    "docker", "run", "--rm", "--user", "root",
    "--volume", "#{fetch(:postgres_data_path)}:/postgresql-data:rw",
    "--entrypoint", "/usr/bin/rsync",
    fetch(:postgres_image_name), "-ah", fetch(:postgres_main_dir), "/postgresql-data/"
  )
end
pg_interactive(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 94
def pg_interactive(host)
  [
    "ssh", "-t", "#{host}", "\"docker", "run", "--rm", "--interactive", "--tty",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    "#{fetch(:postgres_image_name)}",
    "-lic", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'\""
  ].join(' ')
end
pg_interactive_print(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 104
def pg_interactive_print(host)
  [
    "docker", "run", "--rm", "--interactive", "--tty",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    "#{fetch(:postgres_image_name)}",
    "-lic", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'"
  ].join(' ')
end
pg_list_databases(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 114
def pg_list_databases(host)
  capture(
    "docker", "run", "--rm",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", fetch(:postgres_socket_path),
    "-a", "-c", "\"\\l\"'"
  ).lines.each { |l| info l }
end
pg_list_roles(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 125
def pg_list_roles(host)
  capture(
    "docker", "run", "--rm",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", fetch(:postgres_socket_path),
    "-c", "\"\\du\"'"
  ).lines.each { |l| info l }
end
pg_replicate(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 38
def pg_replicate(host)
  execute(
    "docker", "run", "--rm", "--user", "postgres",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/usr/bin/pg_basebackup",
    fetch(:postgres_image_name),
    "-w", "-h", fetch(:domain), "-p", fetch(:master_container_port),
    "-U", "replicator", "-D", fetch(:postgres_data_path), "-v", "-x"
  )
end
pg_restore(host, args, clean) click to toggle source
# File lib/postgresinator/built-in.rb, line 70
def pg_restore(host, args, clean)
  execute(
    "docker", "run", "--rm",
    "--volume", "/tmp:/tmp:rw",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/pg_restore", "-U", "postgres",
    "--host", fetch(:postgres_socket_path), clean,
    "-d", args.database_name, "-F", "tar", "-v", "/tmp/#{args.dump_file}'"
  )
end
pg_role_exists?(db_role) click to toggle source
# File lib/postgresinator/built-in.rb, line 197
def pg_role_exists?(db_role)
  test(
    "echo", "\"SELECT", "*", "FROM", "pg_user",
    "WHERE", "usename", "=", "'#{db_role}';\"", "|",
    "docker", "run", "--rm", "--interactive",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'", "|",
    "grep", "-qw", "'#{db_role}'"
  )
end
pg_run(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 15
def pg_run(host)
  execute(
    "docker", "run", "--detach", "--tty", "--user", "postgres",
    "--name", host.properties.postgres_container_name,
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--expose", 5432,
    "--publish", "0.0.0.0:#{host.properties.postgres_port}:5432",
    "--restart", "always",
    # TODO switch to universal entrypoints instead of version specific ones
    "--entrypoint", fetch(:postgres_entrypoint),
    fetch(:postgres_image_name),
    "-D", fetch(:postgres_data_path),
    "-c", "config_file=#{fetch(:postgres_config_path)}/postgresql.conf"
  )
end
pg_ssl_crt(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 59
def pg_ssl_crt(host)
  execute(
    "docker", "run", "--rm", "--user", "root",
    "--entrypoint", "/usr/bin/openssl",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    fetch(:postgres_image_name), "req", "-x509", "-text",
    "-in", fetch(:postgres_ssl_csr),
    "-key", fetch(:postgres_ssl_key),
    "-out", fetch(:postgres_ssl_crt)
  )
end
pg_ssl_key(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 48
def pg_ssl_key(host)
  execute(
    "docker", "run", "--rm", "--user", "root",
    "--entrypoint", "/usr/bin/openssl",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    fetch(:postgres_image_name), "req", "-nodes", "-newkey", "rsa:2048",
    "-keyout", fetch(:postgres_ssl_key),
    "-out", fetch(:postgres_ssl_csr),
    "-subj", "\"/C=US/ST=Oregon/L=Portland/O=My Company/OU=Operations/CN=localhost\""
  )
end
pg_streaming_master(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 136
def pg_streaming_master(host)
  capture(
    "echo", "\"SELECT", "*", "FROM", "pg_stat_replication;\"", "|",
    "docker", "run", "--rm", "--interactive",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres", "-xa",
    "--host", "#{fetch(:postgres_socket_path)}'"
  ).lines.each { |l| info l }
end
pg_streaming_slave(host) click to toggle source
# File lib/postgresinator/built-in.rb, line 147
def pg_streaming_slave(host)
  capture(
    "echo", "\"SELECT", "now()", "-", "pg_last_xact_replay_timestamp()",
    "AS", "replication_delay;\"", "|",
    "docker", "run", "--rm", "--interactive",
    "--volume", "#{fetch(:deploy_to)}:#{fetch(:deploy_to)}:rw",
    "--entrypoint", "/bin/bash",
    fetch(:postgres_image_name),
    "-c", "'/usr/bin/psql", "-U", "postgres",
    "--host", "#{fetch(:postgres_socket_path)}'"
  ).lines.each { |l| info l }
end