Class: StellarCoreCommander::DockerProcess

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

Constant Summary

Constants inherited from Process

Process::DEFAULT_HOST, Process::SPECIAL_PEERS

Instance Attribute Summary collapse

Attributes inherited from Process

#atlas, #atlas_interval, #base_port, #host, #identity, #name, #network_passphrase, #server, #transactor, #unverified, #working_dir

Instance Method Summary collapse

Methods inherited from Process

#account_count, #account_row, #await_sync?, #balance_for, #catchup, #check_equal, #check_equal_ledger_objects, #check_ledger_sequence_is_prefix_of, #check_no_error_metrics, #checkdb_runs, #close_ledger, #close_timeout, #crashed?, #database, #database_host, #database_name, #database_password, #database_port, #database_uri, #database_url, #database_user, #db_store_state, #dsn, #dump_info, #dump_metrics, #dump_scp_state, #dump_server_query, #has_special_peers?, #history_archive_state, #http_port, #idname, #info, #info_field, #latest_ledger, #latest_ledger_hash, #launched?, #ledger_num, #load_generation_runs, #manual_close?, #metrics, #metrics_1m_rate, #metrics_count, #node_map_or_special_field, #objects_checked, #offer_count, #operations_per_second, #peer_connections, #peer_names, #peer_port, #quorum, #run, #run_cmd, #scp_ballots_prepared, #scp_quorum_heard, #sequence_for, #start_checkdb, #start_load_generation, #stopped?, #submit_transaction, #sync_timeout, #synced?, #ten_accounts, #ten_offers, #ten_trustlines, #transaction_result, #transactions_applied, #transactions_per_second, #trustline_count, #wait_for_ready

Constructor Details

#initialize(params) ⇒ DockerProcess

Returns a new instance of DockerProcess.



18
19
20
21
22
23
# File 'lib/stellar_core_commander/docker_process.rb', line 18

def initialize(params)
  @docker_state_image = params[:docker_state_image]
  @docker_core_image  = params[:docker_core_image]
  @docker_pull  = params[:docker_pull]
  super
end

Instance Attribute Details

#docker_core_imageObject (readonly)

Returns the value of attribute docker_core_image.



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

def docker_core_image
  @docker_core_image
end

#docker_state_imageObject (readonly)

Returns the value of attribute docker_state_image.



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

def docker_state_image
  @docker_state_image
end

Instance Method Details

#aws_credentials_volumeObject



181
182
183
184
185
186
187
# File 'lib/stellar_core_commander/docker_process.rb', line 181

def aws_credentials_volume
  if use_s3 and File.exists?("#{ENV['HOME']}/.aws")
    ["-v", "#{ENV['HOME']}/.aws:/root/.aws:ro"]
  else
    []
  end
end

#cleanupObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stellar_core_commander/docker_process.rb', line 103

def cleanup
  database.disconnect
  dump_database
  dump_logs
  dump_cores
  dump_scp_state
  dump_info
  dump_metrics
  shutdown
  shutdown_state_container
  shutdown_heka_container if atlas
end

#container_nameObject



149
150
151
# File 'lib/stellar_core_commander/docker_process.rb', line 149

def container_name
  "scc-#{idname}"
end

#crashObject



272
273
274
# File 'lib/stellar_core_commander/docker_process.rb', line 272

def crash
  docker %W(exec #{container_name} pkill -ABRT stellar-core)
end

#default_database_urlObject



138
139
140
141
# File 'lib/stellar_core_commander/docker_process.rb', line 138

def default_database_url
  @database_password ||= SecureRandom.hex
  "postgres://postgres:#{@database_password}@#{docker_host}:#{postgres_port}/stellar"
end

#docker_hostObject



164
165
166
167
168
# File 'lib/stellar_core_commander/docker_process.rb', line 164

def docker_host
  return host if host
  return URI.parse(ENV['DOCKER_HOST']).host if ENV['DOCKER_HOST']
  DEFAULT_HOST
end

#docker_pull?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/stellar_core_commander/docker_process.rb', line 176

def docker_pull?
  @docker_pull
end

#dump_coresObject



122
123
124
125
# File 'lib/stellar_core_commander/docker_process.rb', line 122

def dump_cores
  docker %W(run --volumes-from #{container_name} --rm -e MODE=local #{docker_core_image} /utils/core_file_processor.py)
  docker %W(cp #{container_name}:/cores .)
end

#dump_databaseObject



128
129
130
131
132
133
134
135
# File 'lib/stellar_core_commander/docker_process.rb', line 128

def dump_database
  fname = "#{working_dir}/database-#{Time.now.to_i}-#{rand 100000}.sql"
  $stderr.puts "dumping database to #{fname}"
  host_args = "-H tcp://#{docker_host}:#{docker_port}" if host
  sql = `docker #{host_args} exec #{state_container_name} pg_dump -U #{database_user} --clean --no-owner --no-privileges #{database_name}`
  File.open(fname, 'w') {|f| f.write(sql) }
  fname
end

#dump_logsObject



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

def dump_logs
  docker ["logs", container_name]
end

#heka_container_nameObject



159
160
161
# File 'lib/stellar_core_commander/docker_process.rb', line 159

def heka_container_name
  "scc-heka-#{idname}"
end

#heka_container_running?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/stellar_core_commander/docker_process.rb', line 85

def heka_container_running?
  container_running? heka_container_name
end

#history_get_commandObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/stellar_core_commander/docker_process.rb', line 213

def history_get_command
  cmds = Set.new
  localget = "cp /history/%s/{0} {1}"
  s3get = "aws s3 --region #{@s3_history_region} cp #{@s3_history_prefix}/%s/{0} {1}"
  @quorum.each do |q|
    if q == @name
      next
    end
    if SPECIAL_PEERS.has_key? q
      cmds.add SPECIAL_PEERS[q][:get]
    elsif use_s3
      cmds.add s3get
    else
      cmds.add localget
    end
  end

  if cmds.size == 0
    if use_s3
      cmds.add s3get
    else
      cmds.add localget
    end
  end

  if cmds.size != 1
    raise "Conflicting get commands: #{cmds.to_a.inspect}"
  end
  <<-EOS.strip_heredoc
    HISTORY_GET=#{cmds.to_a.first}
  EOS
end

#history_put_commandsObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/stellar_core_commander/docker_process.rb', line 247

def history_put_commands
  if has_special_peers?
    ""
  else
    if use_s3
      <<-EOS.strip_heredoc
        HISTORY_PUT=aws s3 --region #{@s3_history_region} cp {0} #{@s3_history_prefix}/%s/{1}
      EOS
    else
      <<-EOS.strip_heredoc
        HISTORY_PUT=cp {0} /history/%s/{1}
        HISTORY_MKDIR=mkdir -p /history/%s/{0}
      EOS
    end
  end
end

#hostnameObject



171
172
173
# File 'lib/stellar_core_commander/docker_process.rb', line 171

def hostname
  docker_host
end

#launch_heka_containerObject



31
32
33
34
35
36
37
38
39
# File 'lib/stellar_core_commander/docker_process.rb', line 31

def launch_heka_container
  $stderr.puts "launching heka container #{heka_container_name}"
  docker %W(run
    --name #{heka_container_name}
    --net container:#{container_name}
    --volumes-from #{container_name}
    -d stellar/heka
  )
end

#launch_processObject



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

def launch_process
  launch_state_container
  launch_stellar_core
  launch_heka_container if atlas
end

#launch_state_containerObject



42
43
44
45
46
# File 'lib/stellar_core_commander/docker_process.rb', line 42

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

#postgres_portObject



144
145
146
# File 'lib/stellar_core_commander/docker_process.rb', line 144

def postgres_port
  base_port + 2
end

#prepareObject



264
265
266
267
268
269
270
# File 'lib/stellar_core_commander/docker_process.rb', line 264

def prepare
  $stderr.puts "preparing #{idname} (dir:#{working_dir})"
  return unless docker_pull?
  docker %W(pull #{docker_state_image})
  docker %W(pull #{docker_core_image})
  docker %W(pull stellar/heka)
end

#required_portsObject



26
27
28
# File 'lib/stellar_core_commander/docker_process.rb', line 26

def required_ports
  3
end

#running?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/stellar_core_commander/docker_process.rb', line 80

def running?
  container_running? container_name
end

#setupObject



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

def setup
  write_config
end

#shared_history_volumeObject



202
203
204
205
206
207
208
209
210
# File 'lib/stellar_core_commander/docker_process.rb', line 202

def shared_history_volume
  if use_s3
    []
  else
    dir = File.expand_path("#{working_dir}/../history-archives")
    Dir.mkdir(dir) unless File.exists?(dir)
    ["-v", "#{dir}:/history"]
  end
end

#shutdownObject



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

def shutdown
  return true unless running?
  docker %W(stop #{container_name})
  docker %W(exec #{container_name} rm -rf /history)
  docker %W(rm -f #{container_name})
end

#shutdown_heka_containerObject



56
57
58
59
60
# File 'lib/stellar_core_commander/docker_process.rb', line 56

def shutdown_heka_container
  return true unless heka_container_running?
  docker %W(rm -f -v #{heka_container_name})
  raise "Could not stop heka container: #{heka_container_name}" unless $?.success?
end

#shutdown_state_containerObject



49
50
51
52
53
# File 'lib/stellar_core_commander/docker_process.rb', line 49

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

#state_container_nameObject



154
155
156
# File 'lib/stellar_core_commander/docker_process.rb', line 154

def state_container_name
  "scc-state-#{idname}"
end

#state_container_running?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/stellar_core_commander/docker_process.rb', line 90

def state_container_running?
  container_running? state_container_name
end

#use_s3Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/stellar_core_commander/docker_process.rb', line 190

def use_s3
  if @use_s3
    true
  else
    if host and (@quorum.size > 1)
      $stderr.puts "WARNING: multi-peer with remote docker host, but no s3; history will not be shared"
    end
    false
  end
end

#write_configObject



63
64
65
# File 'lib/stellar_core_commander/docker_process.rb', line 63

def write_config
  IO.write("#{working_dir}/stellar-core.env", config)
end