Top Level Namespace

Defined Under Namespace

Modules: Capistrano

Instance Method Summary collapse

Instance Method Details

#pg_create_database(database) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/postgresinator/built-in.rb', line 150

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) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/postgresinator/built-in.rb', line 139

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) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
194
195
196
197
198
# File 'lib/postgresinator/built-in.rb', line 191

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) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
# File 'lib/postgresinator/built-in.rb', line 182

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", "-w", "#{database_name}'")
end

#pg_dump(host, args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/postgresinator/built-in.rb', line 70

def pg_dump(host, args)
  SSHKit.config.output_verbosity = "debug"
  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}'")
  SSHKit.config.output_verbosity = fetch(:postgres_log_level)
end

#pg_grant_database(database) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/postgresinator/built-in.rb', line 161

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) ⇒ Object



26
27
28
29
30
31
# File 'lib/postgresinator/built-in.rb', line 26

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", "/var/lib/postgresql/9.1/main/", "/postgresql-data/")
end

#pg_interactive(host) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/postgresinator/built-in.rb', line 82

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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/postgresinator/built-in.rb', line 92

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) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/postgresinator/built-in.rb', line 102

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) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/postgresinator/built-in.rb', line 111

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) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/postgresinator/built-in.rb', line 32

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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/postgresinator/built-in.rb', line 58

def pg_restore(host, args, clean)
  SSHKit.config.output_verbosity = "debug"
  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}'")
  SSHKit.config.output_verbosity = fetch(:postgres_log_level)
end

#pg_role_exists?(db_role) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
180
181
# File 'lib/postgresinator/built-in.rb', line 171

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", "-q", "'#{db_role}'")
end

#pg_run(host) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/postgresinator/built-in.rb', line 14

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",
    "--entrypoint", "/usr/lib/postgresql/9.1/bin/postgres",
    fetch(:postgres_image_name),
    "-D", shared_path.join('postgres', 'data'),
    "-c", "config_file=#{shared_path.join('postgres', 'conf', 'postgresql.conf')}")
end

#pg_ssl_crt(host) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/postgresinator/built-in.rb', line 49

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) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/postgresinator/built-in.rb', line 40

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) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/postgresinator/built-in.rb', line 120

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) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/postgresinator/built-in.rb', line 129

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