|
| 1 | +'use strict'; |
| 2 | +const Cp = require('child_process'); |
| 3 | +const Path = require('path'); |
| 4 | +const Consulite = require('consulite'); |
| 5 | +const consulHost = `${process.env.CONSUL}:8500`; |
| 6 | +const debug = require('debug')('config-postgres'); |
| 7 | +const isPreStart = process.argv[2] === 'preStart'; |
| 8 | +const isOnChange = process.argv[2] === 'onChange'; |
| 9 | + |
| 10 | +debug('running node script. preStart=%s, onChange=%s, env=%j', |
| 11 | + isPreStart, isOnChange, process.env); |
| 12 | + |
| 13 | +if (!isPreStart && !isOnChange) { |
| 14 | + throw new Error('run in an unexpected mode'); |
| 15 | +} |
| 16 | + |
| 17 | +lookupService((err, service) => { |
| 18 | + if (err) { |
| 19 | + throw err; |
| 20 | + } |
| 21 | + |
| 22 | + debug('service = %j', service); |
| 23 | + |
| 24 | + createPgConfFile(service); |
| 25 | + |
| 26 | + if (isPreStart && !service.isMaster) { |
| 27 | + createBaseBackup(service); |
| 28 | + createRecoveryFile(service); |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | + |
| 33 | +function createPgConfFile (service) { |
| 34 | + const template = '/etc/postgresql.conf.ctmpl'; |
| 35 | + const conf = '/etc/postgresql.conf'; |
| 36 | + |
| 37 | + debug('creating config file (%s) from template (%s).', conf, template); |
| 38 | + |
| 39 | + const cmd = 'consul-template -once' + |
| 40 | + ` -consul-addr ${consulHost}` + |
| 41 | + ` -template "${template}:${conf}` + |
| 42 | + (isOnChange ? ':pkill -SIGHUP postgres' : '') + '"'; |
| 43 | + const env = service.isMaster ? |
| 44 | + Object.assign({}, process.env, { POSTGRES_MASTER: '1' }) : |
| 45 | + process.env; |
| 46 | + |
| 47 | + Cp.execSync(cmd, { env }); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +function createRecoveryFile (service) { |
| 52 | + const template = '/etc/recovery.conf.ctmpl'; |
| 53 | + const conf = Path.join(process.env.PGDATA, 'recovery.conf'); |
| 54 | + |
| 55 | + debug('creating config file (%s) from template (%s).', conf, template); |
| 56 | + |
| 57 | + if (service.isMaster) { |
| 58 | + throw new Error('attempting to create recovery file for master'); |
| 59 | + } |
| 60 | + |
| 61 | + const cmd = 'consul-template -once' + |
| 62 | + ` -consul-addr ${consulHost}` + |
| 63 | + ` -template "${template}:${conf}"`; |
| 64 | + const env = Object.assign({}, process.env, { |
| 65 | + POSTGRES_MASTER_IP: service.address |
| 66 | + }); |
| 67 | + |
| 68 | + Cp.execSync(cmd, { env }); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +function createBaseBackup (service) { |
| 73 | + debug('creating base backup.'); |
| 74 | + |
| 75 | + if (service.isMaster) { |
| 76 | + throw new Error('attempting to create backup for master'); |
| 77 | + } |
| 78 | + |
| 79 | + const cmd = `pg_basebackup -h ${service.address} -D ${process.env.PGDATA}` + |
| 80 | + ` -U ${process.env.POSTGRES_USER} -vP -w --xlog-method=stream`; |
| 81 | + const env = Object.assign({}, process.env, { |
| 82 | + PGPASSWORD: process.env.POSTGRES_PASSWORD |
| 83 | + }); |
| 84 | + |
| 85 | + Cp.execSync(cmd, { env }); |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +function lookupService (callback) { |
| 90 | + const consul = new Consulite({ consul: consulHost }); |
| 91 | + const ip = process.env.CONTAINERPILOT_POSTGRES_IP; |
| 92 | + |
| 93 | + debug('looking up service in consul (%s)', consulHost); |
| 94 | + consul.getService('postgres', (err, service) => { |
| 95 | + if (err) { |
| 96 | + // If the service wasn't found, then this is the master. |
| 97 | + if (/postgres couldn't be found/.test(err.message)) { |
| 98 | + return callback(null, { |
| 99 | + isMaster: true, |
| 100 | + address: ip, |
| 101 | + port: 5432, |
| 102 | + executed: false |
| 103 | + }); |
| 104 | + } else { |
| 105 | + return callback(err); |
| 106 | + } |
| 107 | + } else { |
| 108 | + service.isMaster = service.address === ip; |
| 109 | + callback(null, service); |
| 110 | + } |
| 111 | + }); |
| 112 | +} |
0 commit comments