Module: Duple::HerokuEndpoint

Defined in:
lib/duple/endpoint.rb

Instance Method Summary collapse

Instance Method Details

#appnameObject



19
20
21
# File 'lib/duple/endpoint.rb', line 19

def appname
  @appname ||= config.heroku_name(environment)
end

#capture_snapshotObject



66
67
68
# File 'lib/duple/endpoint.rb', line 66

def capture_snapshot
  heroku.run(appname, 'pgbackups:capture')
end

#db_configObject



27
28
29
# File 'lib/duple/endpoint.rb', line 27

def db_config
  @db_config ||= fetch_db_config
end

#execute(cmd) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/duple/endpoint.rb', line 70

def execute(cmd)
  if cmd.shell?
    heroku.run(appname, "run \"#{cmd.command}\"")
  elsif cmd.heroku?
    heroku.run(appname, cmd.command)
  end
end

#fetch_db_configObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/duple/endpoint.rb', line 45

def fetch_db_config
  # Run the heroku config command first, even if it's a dry run, so
  # that the command to get the config will show up in the dry run log.
  config_vars = heroku.capture(appname, "config")

  if config.dry_run?
    config.db_config(name, dry_run: true)
  else
    parse_config(config_vars)
  end
end

#herokuObject



23
24
25
# File 'lib/duple/endpoint.rb', line 23

def heroku
  @heroku ||= Duple::HerokuRunner.new(runner)
end

#latest_snapshot_timeObject



35
36
37
38
39
40
41
42
43
# File 'lib/duple/endpoint.rb', line 35

def latest_snapshot_time
  unless @snapshot_time
    response = heroku.capture(appname, 'pgbackups')
    last_line = response.split("\n").last
    timestring = last_line.match(/\w+\s+(?<timestamp>[\d\s\/\:\.]+)\s+.*/)[:timestamp]
    @snapshot_time = DateTime.strptime(timestring, '%Y/%m/%d %H:%M.%S')
  end
  @snapshot_time
end

#parse_config(config_vars) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
# File 'lib/duple/endpoint.rb', line 57

def parse_config(config_vars)
  db_url = config_vars.split("\n").detect { |l| l =~ /DATABASE_URL/ }
  raise ArgumentError.new("Missing DATABASE_URL variable for #{appname}") if db_url.nil?

  db_url.match(
    /postgres:\/\/(?<username>.*):(?<password>.*)@(?<host>.*):(?<port>\d*)\/(?<database>.*)/
  )
end

#snapshot_urlObject



31
32
33
# File 'lib/duple/endpoint.rb', line 31

def snapshot_url
  @snapshot_url ||= heroku.capture(appname, 'pgbackups:url').strip
end