Class: Conjure::Service::Database::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure/service/database/postgres.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Postgres

Returns a new instance of Postgres.



5
6
7
8
# File 'lib/conjure/service/database/postgres.rb', line 5

def initialize(options)
  @target = options[:target]
  @db_name = options[:database_name]
end

Instance Method Details

#adapter_nameObject



71
72
73
# File 'lib/conjure/service/database/postgres.rb', line 71

def adapter_name
  "postgresql"
end

#base_imageObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/conjure/service/database/postgres.rb', line 10

def base_image
  @base_image ||= @target.shell.prepare(
    label: "postgres",
    setup_commands: [
      "apt-get install -y python-software-properties software-properties-common",
      "add-apt-repository -y ppa:pitti/postgresql",
      "apt-get update",
      "apt-get install -y postgresql-9.2 postgresql-client-9.2 postgresql-contrib-9.2",
    ],
  )
end

#bin_pathObject



67
68
69
# File 'lib/conjure/service/database/postgres.rb', line 67

def bin_path
  "/usr/lib/postgresql/9.2/bin"
end

#client_optionsObject



63
64
65
# File 'lib/conjure/service/database/postgres.rb', line 63

def client_options
  "-U root -h #{ip_address}"
end

#containerObject



39
40
41
# File 'lib/conjure/service/database/postgres.rb', line 39

def container
  @container ||= server_image.run
end

#export(file) ⇒ Object



51
52
53
54
55
56
# File 'lib/conjure/service/database/postgres.rb', line 51

def export(file)
  File.open file, "w" do |f|
    f.write base_image.command("#{bin_path}/pg_dump #{client_options} #{@db_name}")
  end
  Log.info "[export] #{File.size file} bytes exported to #{file}"
end

#import(file) ⇒ Object



58
59
60
61
# File 'lib/conjure/service/database/postgres.rb', line 58

def import(file)
  base_image.command "#{bin_path}/psql #{client_options} -d #{@db_name} -f /files/#{File.basename file}", files: [file]
  Log.info "[import] #{File.size file} bytes imported from #{file}"
end

#ip_addressObject



47
48
49
# File 'lib/conjure/service/database/postgres.rb', line 47

def ip_address
  container.ip_address
end

#nameObject



43
44
45
# File 'lib/conjure/service/database/postgres.rb', line 43

def name
  @db_name
end

#runObject



35
36
37
# File 'lib/conjure/service/database/postgres.rb', line 35

def run
  container
end

#server_imageObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/conjure/service/database/postgres.rb', line 22

def server_image
  @server_image ||= base_image.prepare(
    label: "pgserver",
    setup_commands: [
      "service postgresql start; su postgres -c 'createuser -d -r -s root; createdb -O root root'; service postgresql stop",
      "echo 'host all all 0.0.0.0/0 trust' >>/etc/postgresql/9.2/main/pg_hba.conf",
      "echo \"listen_addresses='*'\" >>/etc/postgresql/9.2/main/postgresql.conf",
    ],
    daemon_command: "su postgres -c '#{bin_path}/postgres -c config_file=/etc/postgresql/9.2/main/postgresql.conf'",
    volumes: ["/var/lib/postgresql/9.2/main"],
  )
end