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)
  @host = options[:docker_host]
  @db_name = options[:database_name]
end

Instance Method Details

#adapter_nameObject



73
74
75
# File 'lib/conjure/service/database/postgres.rb', line 73

def adapter_name
  "postgresql"
end

#base_imageObject



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

def base_image
  @base_image ||= @host.images.create(
    label: "postgres",
    base_image: "ubuntu",
    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



69
70
71
# File 'lib/conjure/service/database/postgres.rb', line 69

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

#client_optionsObject



65
66
67
# File 'lib/conjure/service/database/postgres.rb', line 65

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

#containerObject



41
42
43
# File 'lib/conjure/service/database/postgres.rb', line 41

def container
  @container ||= server_image.run
end

#export(file) ⇒ Object



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

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

#import(file) ⇒ Object



60
61
62
63
# File 'lib/conjure/service/database/postgres.rb', line 60

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

#ip_addressObject



49
50
51
# File 'lib/conjure/service/database/postgres.rb', line 49

def ip_address
  container.ip_address
end

#nameObject



45
46
47
# File 'lib/conjure/service/database/postgres.rb', line 45

def name
  @db_name
end

#runObject



37
38
39
# File 'lib/conjure/service/database/postgres.rb', line 37

def run
  container
end

#server_imageObject



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

def server_image
  @server_image ||= @host.images.create(
    label: "pgserver",
    base_image: base_image,
    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