Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from matschaffer/process-opts
Browse files Browse the repository at this point in the history
Generalize process handling bit
  • Loading branch information
nullstyle committed May 26, 2015
2 parents 4e22cc7 + a06faf2 commit 149b9a6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
27 changes: 11 additions & 16 deletions bin/scc
Expand Up @@ -7,14 +7,22 @@ def run
$opts = Slop.parse do
banner 'Usage: scc -r RECIPE'

on 'stellar-core-bin', 'a path to a stellar-core executable (default to `which stellar-core`)', argument: true
on 'stellar-core-bin', 'a path to a stellar-core executable (defaults to `which stellar-core`)', argument: true

on 'docker-mode', 'mode to run the docker containers in', argument: true, default: 'standalone'

on 'r', 'recipe', 'a recipe file', argument: true #, required: true
on 'p', 'process', 'method for running stellar-core', argument: true, default: 'local'
end

recipe = load_recipe
commander = make_commander
process = commander.make_process($opts[:process])

opts = {
stellar_core_bin: $opts[:"stellar-core-bin"],
docker_mode: $opts[:"docker-mode"]
}
process = commander.make_process($opts[:process], opts)

#run recipe
transactor = StellarCoreCommander::Transactor.new(process)
Expand All @@ -28,20 +36,7 @@ end


def make_commander
stellar_core_bin = $opts[:"stellar-core-bin"]

if stellar_core_bin.blank?
search = `which stellar-core`.strip

if $?.success?
stellar_core_bin = search
else
$stderr.puts "Could not find a `stellar-core` binary, please use --stellar-core-bin to specify"
exit 1
end
end

StellarCoreCommander::Commander.new(stellar_core_bin).tap do |c|
StellarCoreCommander::Commander.new.tap do |c|
c.cleanup_at_exit!
end
end
Expand Down
33 changes: 15 additions & 18 deletions lib/stellar_core_commander/commander.rb
Expand Up @@ -7,30 +7,27 @@ class Commander
# Creates a new core commander
#
Contract String => Any
def initialize(stellar_core_bin)
@stellar_core_bin = stellar_core_bin
raise "no file at #{stellar_core_bin}" unless File.exist?(stellar_core_bin)

def initialize()
@processes = []
end

Contract String => Process
def make_process(type)
Contract String, Hash => Process
def make_process(type, opts = {})
tmpdir = Dir.mktmpdir("scc")

identity = Stellar::KeyPair.random
base_port = 39132 + (@processes.length * 3)

if type == 'local'
FileUtils.cp(@stellar_core_bin, "#{tmpdir}/stellar-core")
process = LocalProcess.new(tmpdir, base_port, identity)
elsif type == 'docker'
process = DockerProcess.new(tmpdir, base_port, identity)
else
raise "Unknown process type: #{type}"
end

process.tap do |p|
base_port = 39132 + @processes.map(&:required_ports).sum

process_class = case type
when 'local'
LocalProcess
when 'docker'
DockerProcess
else
raise "Unknown process type: #{type}"
end

process_class.new(tmpdir, base_port, identity, opts).tap do |p|
p.setup
@processes << p
end
Expand Down
5 changes: 5 additions & 0 deletions lib/stellar_core_commander/docker_process.rb
Expand Up @@ -6,6 +6,11 @@ module StellarCoreCommander
class DockerProcess < Process
include Contracts

Contract None => Num
def required_ports
3
end

Contract None => Any
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)
Expand Down
17 changes: 17 additions & 0 deletions lib/stellar_core_commander/local_process.rb
Expand Up @@ -6,6 +6,23 @@ class LocalProcess < Process
attr_reader :pid
attr_reader :wait

def initialize(working_dir, base_port, identity, opts)
stellar_core_bin = opts[:stellar_core_bin]
if stellar_core_bin.blank?
search = `which stellar-core`.strip

if $?.success?
stellar_core_bin = search
else
$stderr.puts "Could not find a `stellar-core` binary, please use --stellar-core-bin to specify"
exit 1
end
end

FileUtils.cp(stellar_core_bin, "#{working_dir}/stellar-core")
super
end

Contract None => Any
def forcescp
run_cmd "./stellar-core", ["--forcescp"]
Expand Down
7 changes: 6 additions & 1 deletion lib/stellar_core_commander/process.rb
Expand Up @@ -8,7 +8,7 @@ class Process
attr_reader :identity
attr_reader :server

def initialize(working_dir, base_port, identity)
def initialize(working_dir, base_port, identity, opts)
@working_dir = working_dir
@base_port = base_port
@identity = identity
Expand All @@ -19,6 +19,11 @@ def initialize(working_dir, base_port, identity)
end
end

Contract None => Num
def required_ports
2
end

Contract None => Any
def rm_working_dir
FileUtils.rm_rf working_dir
Expand Down

0 comments on commit 149b9a6

Please sign in to comment.