Class: StellarCoreCommander::DockerProcess

Inherits:
Process
  • Object
show all
Includes:
Contracts
Defined in:
lib/stellar_core_commander/docker_process.rb

Instance Attribute Summary

Attributes inherited from Process

#base_port, #identity, #server, #working_dir

Instance Method Summary collapse

Methods inherited from Process

#close_ledger, #close_timeout, #http_port, #initialize, #latest_ledger, #peer_port, #rm_working_dir, #sequence_for, #submit_transaction, #transaction_result, #wait_for_ready

Constructor Details

This class inherits a constructor from StellarCoreCommander::Process

Instance Method Details

#cleanupObject



58
59
60
61
62
63
# File 'lib/stellar_core_commander/docker_process.rb', line 58

def cleanup
  database.disconnect
  shutdown
  shutdown_state_container
  rm_working_dir
end

#container_nameObject



98
99
100
# File 'lib/stellar_core_commander/docker_process.rb', line 98

def container_name
  "c#{base_port}"
end

#databaseObject



73
74
75
# File 'lib/stellar_core_commander/docker_process.rb', line 73

def database
  @database ||= Sequel.postgres(database_name, host: docker_host, port: postgres_port, user: database_user, password: database_password)
end

#database_nameObject



78
79
80
# File 'lib/stellar_core_commander/docker_process.rb', line 78

def database_name
  "stellar"
end

#database_passwordObject



88
89
90
# File 'lib/stellar_core_commander/docker_process.rb', line 88

def database_password
  @database_password ||= SecureRandom.hex
end

#database_userObject



83
84
85
# File 'lib/stellar_core_commander/docker_process.rb', line 83

def database_user
  "postgres"
end

#docker_hostObject



108
109
110
111
112
113
114
115
# File 'lib/stellar_core_commander/docker_process.rb', line 108

def docker_host
  docker_uri = URI.parse(ENV['DOCKER_HOST'])
  if docker_uri.scheme == "tcp"
    docker_uri.host
  else
    "127.0.0.1"
  end
end

#dump_databaseObject



66
67
68
69
70
# File 'lib/stellar_core_commander/docker_process.rb', line 66

def dump_database
  Dir.chdir(working_dir) do
    `docker exec #{state_container_name} pg_dump -U #{database_user} --clean --no-owner #{database_name}`
  end
end

#http_hostObject



118
119
120
# File 'lib/stellar_core_commander/docker_process.rb', line 118

def http_host
  docker_host
end

#launch_state_containerObject



15
16
17
18
# File 'lib/stellar_core_commander/docker_process.rb', line 15

def launch_state_container
  run_cmd "docker", %W(run --name #{state_container_name} -p #{postgres_port}:5432 --env-file stellar-core.env -d stellar/stellar-core-state)
  raise "Could not create state container" unless $?.success?
end

#postgres_portObject



93
94
95
# File 'lib/stellar_core_commander/docker_process.rb', line 93

def postgres_port
  base_port + 2
end

#required_portsObject



10
11
12
# File 'lib/stellar_core_commander/docker_process.rb', line 10

def required_ports
  3
end

#runObject



40
41
42
43
# File 'lib/stellar_core_commander/docker_process.rb', line 40

def run
  raise "already running!" if running?
  launch_stellar_core
end

#running?Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/stellar_core_commander/docker_process.rb', line 46

def running?
  run_cmd "docker", %W(inspect #{container_name})
  $?.success?
end

#setupObject



34
35
36
37
# File 'lib/stellar_core_commander/docker_process.rb', line 34

def setup
  write_config
  launch_state_container
end

#shutdownObject



52
53
54
55
# File 'lib/stellar_core_commander/docker_process.rb', line 52

def shutdown
  return true unless running?
  run_cmd "docker", %W(rm -f #{container_name})
end

#shutdown_state_containerObject



21
22
23
24
# File 'lib/stellar_core_commander/docker_process.rb', line 21

def shutdown_state_container
  run_cmd "docker", %W(rm -f -v #{state_container_name})
  raise "Could not drop db: #{database_name}" unless $?.success?
end

#state_container_nameObject



103
104
105
# File 'lib/stellar_core_commander/docker_process.rb', line 103

def state_container_name
  "db#{container_name}"
end

#write_configObject



27
28
29
30
31
# File 'lib/stellar_core_commander/docker_process.rb', line 27

def write_config
  IO.write("#{working_dir}/.pgpass", "#{docker_host}:#{postgres_port}:*:#{database_user}:#{database_password}")
  FileUtils.chmod(0600, "#{working_dir}/.pgpass")
  IO.write("#{working_dir}/stellar-core.env", config)
end