seira 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/seira/db.rb +15 -1
- data/lib/seira/db/create.rb +1 -118
- data/lib/seira/db/write_pgbouncer_yaml.rb +158 -0
- data/lib/seira/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fdaf37b83e2d2c8d1b9452276f51e5955e298d81ede2c7c7d26dc3fa10472c7
|
4
|
+
data.tar.gz: c8f70d1c979b06746bbb4a6cab24069f8cd0bf14ee5cc70b0990b18461678c68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec5a2c3c3d127d6266f5d75833f68f8edffc68d03c79d37afe391b4726d228adc4c1a8bb1ec3dc1735dc9dbebbece897e93195241e7b3a2c840b80a60eb80ee1
|
7
|
+
data.tar.gz: 6c767329a56b80239bca99d73f041633a59eaa0ec57c2e77b0b214e4076b761b207627884ac962d5659b43df73a595f12782f6a5a8c7a8ce1466077f9b09a0a8
|
data/lib/seira/db.rb
CHANGED
@@ -2,13 +2,20 @@ require 'securerandom'
|
|
2
2
|
require 'English'
|
3
3
|
|
4
4
|
require_relative 'db/alter_proxyuser_roles'
|
5
|
+
require_relative 'db/write_pgbouncer_yaml'
|
5
6
|
require_relative 'db/create'
|
6
7
|
|
7
8
|
module Seira
|
8
9
|
class Db
|
9
10
|
include Seira::Commands
|
10
11
|
|
11
|
-
VALID_ACTIONS = %w[
|
12
|
+
VALID_ACTIONS = %w[
|
13
|
+
help create delete list restart connect ps kill
|
14
|
+
analyze create-readonly-user psql table-sizes
|
15
|
+
index-sizes vacuum unused-indexes unused-indices
|
16
|
+
user-connections info alter-proxyuser-roles add
|
17
|
+
write-pgbouncer-yaml
|
18
|
+
].freeze
|
12
19
|
SUMMARY = "Manage your Cloud SQL Postgres databases.".freeze
|
13
20
|
|
14
21
|
attr_reader :app, :action, :args, :context
|
@@ -60,6 +67,8 @@ module Seira
|
|
60
67
|
run_info
|
61
68
|
when 'alter-proxyuser-roles'
|
62
69
|
run_alter_proxyuser_roles
|
70
|
+
when 'write-pgbouncer-yaml'
|
71
|
+
run_write_pgbouncer_yaml
|
63
72
|
else
|
64
73
|
fail "Unknown command encountered"
|
65
74
|
end
|
@@ -101,6 +110,7 @@ module Seira
|
|
101
110
|
user-connections: List number of connections per user
|
102
111
|
vacuum: Run a VACUUM ANALYZE
|
103
112
|
alter-proxyuser-roles: Update NOCREATEDB and NOCREATEROLE roles for proxyuser in cloud sql.
|
113
|
+
write-pbouncer-yaml: Produces a Kubernetes Deployment yaml to run Pgbouncer for specified database.
|
104
114
|
HELPTEXT
|
105
115
|
end
|
106
116
|
|
@@ -116,6 +126,10 @@ module Seira
|
|
116
126
|
Seira::Db::AlterProxyuserRoles.new(app: app, action: action, args: args, context: context).run
|
117
127
|
end
|
118
128
|
|
129
|
+
def run_write_pgbouncer_yaml
|
130
|
+
Seira::Db::WritePgbouncerYaml.new(app: app, args: args, context: context).run
|
131
|
+
end
|
132
|
+
|
119
133
|
def run_delete
|
120
134
|
name = "#{app}-#{args[0]}"
|
121
135
|
if gcloud("sql instances delete #{name}", context: context, format: :boolean)
|
data/lib/seira/db/create.rb
CHANGED
@@ -39,11 +39,10 @@ module Seira
|
|
39
39
|
end
|
40
40
|
|
41
41
|
set_secrets
|
42
|
-
write_pgbouncer_yaml
|
43
42
|
|
44
43
|
alter_proxy_user_roles if replica_for.nil?
|
45
44
|
|
46
|
-
puts "To use this database, deploy the pgbouncer config file that was created and use the ENV that was set."
|
45
|
+
puts "To use this database, use write-pgbouncer-yaml command and deploy the pgbouncer config file that was created and use the ENV that was set."
|
47
46
|
puts "To make this database the primary, promote it using the CLI and update the DATABASE_URL."
|
48
47
|
end
|
49
48
|
|
@@ -217,122 +216,6 @@ module Seira
|
|
217
216
|
def ips
|
218
217
|
@ips ||= Helpers.sql_ips(name, context: context)
|
219
218
|
end
|
220
|
-
|
221
|
-
def write_pgbouncer_yaml
|
222
|
-
# TODO: Clean this up by moving into a proper templated yaml file
|
223
|
-
pgbouncer_yaml = <<-FOO
|
224
|
-
---
|
225
|
-
apiVersion: apps/v1
|
226
|
-
kind: Deployment
|
227
|
-
metadata:
|
228
|
-
name: #{name}-pgbouncer
|
229
|
-
namespace: #{app}
|
230
|
-
labels:
|
231
|
-
app: #{app}
|
232
|
-
tier: #{pgbouncer_tier}
|
233
|
-
database: #{name}
|
234
|
-
spec:
|
235
|
-
replicas: 2
|
236
|
-
selector:
|
237
|
-
matchLabels:
|
238
|
-
app: #{app}
|
239
|
-
tier: #{pgbouncer_tier}
|
240
|
-
database: #{name}
|
241
|
-
strategy:
|
242
|
-
type: RollingUpdate
|
243
|
-
rollingUpdate:
|
244
|
-
maxSurge: 1
|
245
|
-
maxUnavailable: 1
|
246
|
-
template:
|
247
|
-
metadata:
|
248
|
-
labels:
|
249
|
-
app: #{app}
|
250
|
-
tier: #{pgbouncer_tier}
|
251
|
-
database: #{name}
|
252
|
-
spec:
|
253
|
-
containers:
|
254
|
-
- image: handshake/pgbouncer:0.2.0
|
255
|
-
name: pgbouncer
|
256
|
-
ports:
|
257
|
-
- containerPort: 6432
|
258
|
-
protocol: TCP
|
259
|
-
envFrom:
|
260
|
-
- secretRef:
|
261
|
-
name: #{pgbouncer_secret_name}
|
262
|
-
env:
|
263
|
-
- name: "PGPORT"
|
264
|
-
value: "6432"
|
265
|
-
- name: "PGDATABASE"
|
266
|
-
value: "#{@database_name || default_database_name}"
|
267
|
-
- name: "DB_HOST"
|
268
|
-
value: "#{ips[:private]}" # private IP for #{name}
|
269
|
-
- name: "DB_PORT"
|
270
|
-
value: "5432"
|
271
|
-
- name: "LISTEN_PORT"
|
272
|
-
value: "6432"
|
273
|
-
- name: "LISTEN_ADDRESS"
|
274
|
-
value: "*"
|
275
|
-
- name: "TCP_KEEPALIVE"
|
276
|
-
value: "1"
|
277
|
-
- name: "TCP_KEEPCNT"
|
278
|
-
value: "5"
|
279
|
-
- name: "TCP_KEEPIDLE"
|
280
|
-
value: "300" # see: https://git.io/vi0Aj
|
281
|
-
- name: "TCP_KEEPINTVL"
|
282
|
-
value: "300"
|
283
|
-
- name: "LOG_DISCONNECTIONS"
|
284
|
-
value: "0" # spammy, not needed
|
285
|
-
- name: "MAX_CLIENT_CONN"
|
286
|
-
value: "1000"
|
287
|
-
- name: "MIN_POOL_SIZE"
|
288
|
-
value: "20" # This and DEFAULT should be roughly cpu cores * 2. Don't set too high.
|
289
|
-
- name: "DEFAULT_POOL_SIZE"
|
290
|
-
value: "20"
|
291
|
-
- name: "MAX_DB_CONNECTIONS"
|
292
|
-
value: "20"
|
293
|
-
- name: "POOL_MODE"
|
294
|
-
value: "transaction"
|
295
|
-
readinessProbe:
|
296
|
-
exec:
|
297
|
-
command: ["psql", "-c", "SELECT 1;"]
|
298
|
-
initialDelaySeconds: 5
|
299
|
-
periodSeconds: 10
|
300
|
-
livenessProbe:
|
301
|
-
tcpSocket:
|
302
|
-
port: 6432
|
303
|
-
initialDelaySeconds: 15
|
304
|
-
periodSeconds: 20
|
305
|
-
resources:
|
306
|
-
requests:
|
307
|
-
cpu: 100m
|
308
|
-
memory: 300Mi
|
309
|
-
lifecycle:
|
310
|
-
preStop:
|
311
|
-
exec:
|
312
|
-
command: ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 20"]
|
313
|
-
---
|
314
|
-
apiVersion: v1
|
315
|
-
kind: Service
|
316
|
-
metadata:
|
317
|
-
name: #{pgbouncer_service_name}
|
318
|
-
namespace: #{app}
|
319
|
-
labels:
|
320
|
-
app: #{app}
|
321
|
-
tier: #{pgbouncer_tier}
|
322
|
-
spec:
|
323
|
-
type: ClusterIP
|
324
|
-
ports:
|
325
|
-
- protocol: TCP
|
326
|
-
port: 6432
|
327
|
-
targetPort: 6432
|
328
|
-
selector:
|
329
|
-
app: #{app}
|
330
|
-
tier: #{pgbouncer_tier}
|
331
|
-
database: #{name}
|
332
|
-
FOO
|
333
|
-
|
334
|
-
File.write("kubernetes/#{context[:cluster]}/#{app}/pgbouncer-#{name.gsub("#{app}-", '')}.yaml", pgbouncer_yaml)
|
335
|
-
end
|
336
219
|
end
|
337
220
|
end
|
338
221
|
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
module Seira
|
2
|
+
class Db
|
3
|
+
class WritePgbouncerYaml
|
4
|
+
include Seira::Commands
|
5
|
+
|
6
|
+
attr_reader :app, :name, :context
|
7
|
+
|
8
|
+
def initialize(app:, args:, context:)
|
9
|
+
if args.length != 1
|
10
|
+
puts 'Specify db name as positional argument'
|
11
|
+
exit(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
@app = app
|
15
|
+
@name = args[0]
|
16
|
+
@context = context
|
17
|
+
end
|
18
|
+
|
19
|
+
def run
|
20
|
+
write_pgbouncer_yaml
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def pgbouncer_secret_name
|
26
|
+
"#{name}-pgbouncer-secrets"
|
27
|
+
end
|
28
|
+
|
29
|
+
def pgbouncer_service_name
|
30
|
+
"#{name}-pgbouncer-service"
|
31
|
+
end
|
32
|
+
|
33
|
+
def pgbouncer_tier
|
34
|
+
name.gsub("#{app}-", "")
|
35
|
+
end
|
36
|
+
|
37
|
+
def ips
|
38
|
+
@ips ||= Helpers.sql_ips(name, context: context)
|
39
|
+
end
|
40
|
+
|
41
|
+
def write_pgbouncer_yaml
|
42
|
+
# TODO: Clean this up by moving into a proper templated yaml file
|
43
|
+
pgbouncer_yaml = <<-FOO
|
44
|
+
---
|
45
|
+
apiVersion: apps/v1
|
46
|
+
kind: Deployment
|
47
|
+
metadata:
|
48
|
+
name: #{name}-pgbouncer
|
49
|
+
namespace: #{app}
|
50
|
+
labels:
|
51
|
+
app: #{app}
|
52
|
+
tier: #{pgbouncer_tier}
|
53
|
+
database: #{name}
|
54
|
+
spec:
|
55
|
+
replicas: 2
|
56
|
+
selector:
|
57
|
+
matchLabels:
|
58
|
+
app: #{app}
|
59
|
+
tier: #{pgbouncer_tier}
|
60
|
+
database: #{name}
|
61
|
+
strategy:
|
62
|
+
type: RollingUpdate
|
63
|
+
rollingUpdate:
|
64
|
+
maxSurge: 1
|
65
|
+
maxUnavailable: 1
|
66
|
+
template:
|
67
|
+
metadata:
|
68
|
+
labels:
|
69
|
+
app: #{app}
|
70
|
+
tier: #{pgbouncer_tier}
|
71
|
+
database: #{name}
|
72
|
+
spec:
|
73
|
+
containers:
|
74
|
+
- image: handshake/pgbouncer:0.3.1
|
75
|
+
name: pgbouncer
|
76
|
+
ports:
|
77
|
+
- containerPort: 6432
|
78
|
+
protocol: TCP
|
79
|
+
envFrom:
|
80
|
+
- secretRef:
|
81
|
+
name: #{pgbouncer_secret_name}
|
82
|
+
env:
|
83
|
+
- name: "PGPORT"
|
84
|
+
value: "6432"
|
85
|
+
- name: "PGDATABASE"
|
86
|
+
value: "#{name}"
|
87
|
+
- name: "DB_HOST"
|
88
|
+
value: "#{ips[:private]}" # private IP for #{name}
|
89
|
+
- name: "DB_PORT"
|
90
|
+
value: "5432"
|
91
|
+
- name: "LISTEN_PORT"
|
92
|
+
value: "6432"
|
93
|
+
- name: "LISTEN_ADDRESS"
|
94
|
+
value: "*"
|
95
|
+
- name: "TCP_KEEPALIVE"
|
96
|
+
value: "1"
|
97
|
+
- name: "TCP_KEEPCNT"
|
98
|
+
value: "5"
|
99
|
+
- name: "TCP_KEEPIDLE"
|
100
|
+
value: "300" # see: https://git.io/vi0Aj
|
101
|
+
- name: "TCP_KEEPINTVL"
|
102
|
+
value: "300"
|
103
|
+
- name: "LOG_DISCONNECTIONS"
|
104
|
+
value: "0" # spammy, not needed
|
105
|
+
- name: "MAX_CLIENT_CONN"
|
106
|
+
value: "1000"
|
107
|
+
- name: "MIN_POOL_SIZE"
|
108
|
+
value: "20" # This and DEFAULT should be roughly cpu cores * 2. Don't set too high.
|
109
|
+
- name: "DEFAULT_POOL_SIZE"
|
110
|
+
value: "20"
|
111
|
+
- name: "MAX_DB_CONNECTIONS"
|
112
|
+
value: "20"
|
113
|
+
- name: "POOL_MODE"
|
114
|
+
value: "transaction"
|
115
|
+
readinessProbe:
|
116
|
+
exec:
|
117
|
+
command: ["psql", "-c", "SELECT 1;"]
|
118
|
+
initialDelaySeconds: 5
|
119
|
+
periodSeconds: 10
|
120
|
+
livenessProbe:
|
121
|
+
tcpSocket:
|
122
|
+
port: 6432
|
123
|
+
initialDelaySeconds: 15
|
124
|
+
periodSeconds: 20
|
125
|
+
resources:
|
126
|
+
requests:
|
127
|
+
cpu: 100m
|
128
|
+
memory: 300Mi
|
129
|
+
lifecycle:
|
130
|
+
preStop:
|
131
|
+
exec:
|
132
|
+
command: ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 20"]
|
133
|
+
---
|
134
|
+
apiVersion: v1
|
135
|
+
kind: Service
|
136
|
+
metadata:
|
137
|
+
name: #{pgbouncer_service_name}
|
138
|
+
namespace: #{app}
|
139
|
+
labels:
|
140
|
+
app: #{app}
|
141
|
+
tier: #{pgbouncer_tier}
|
142
|
+
spec:
|
143
|
+
type: ClusterIP
|
144
|
+
ports:
|
145
|
+
- protocol: TCP
|
146
|
+
port: 6432
|
147
|
+
targetPort: 6432
|
148
|
+
selector:
|
149
|
+
app: #{app}
|
150
|
+
tier: #{pgbouncer_tier}
|
151
|
+
database: #{name}
|
152
|
+
FOO
|
153
|
+
|
154
|
+
File.write("kubernetes/#{context[:cluster]}/#{app}/pgbouncer-#{name.gsub("#{app}-", '')}.yaml", pgbouncer_yaml)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
data/lib/seira/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seira
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Ringwelski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: highline
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- lib/seira/db.rb
|
128
128
|
- lib/seira/db/alter_proxyuser_roles.rb
|
129
129
|
- lib/seira/db/create.rb
|
130
|
+
- lib/seira/db/write_pgbouncer_yaml.rb
|
130
131
|
- lib/seira/jobs.rb
|
131
132
|
- lib/seira/node_pools.rb
|
132
133
|
- lib/seira/pods.rb
|