Class: Zold::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/node.rb

Overview

NODE command

Defined Under Namespace

Classes: WebrickLog

Instance Method Summary collapse

Constructor Details

#initialize(log: Log::Quiet.new) ⇒ Node

Returns a new instance of Node.



37
38
39
# File 'lib/zold/commands/node.rb', line 37

def initialize(log: Log::Quiet.new)
  @log = log
end

Instance Method Details

#run(args = []) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/zold/commands/node.rb', line 41

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = 'Usage: zold node [options]'
    o.string '--invoice',
      'The invoice you want to collect money to or the wallet ID'
    o.integer '--port',
      "TCP port to open for the Net (default: #{Remotes::PORT})",
      default: Remotes::PORT
    o.integer '--bind-port',
      "TCP port to listen on (default: #{Remotes::PORT})",
      default: Remotes::PORT
    o.string '--host', 'Host name (default: 127.0.0.1)',
      default: '127.0.0.1'
    o.string '--home', 'Home directory (default: .)',
      default: Dir.pwd
    o.integer '--strength',
      "The strength of the score (default: #{Score::STRENGTH})",
      default: Score::STRENGTH
    o.integer '--threads',
      'How many threads to use for scores finding (default: 4)',
      default: 4
    o.bool '--standalone',
      'Never communicate with other nodes (mostly for testing)',
      default: false
    o.bool '--ignore-score-weakness',
      'Ignore score weakness of incoming requests and register those nodes anyway',
      default: false
    o.bool '--never-reboot',
      'Don\'t reboot when a new version shows up in the network',
      default: false
    o.bool '--help', 'Print instructions'
  end
  if opts.help?
    @log.info(opts.to_s)
    return
  end
  raise '--invoice is mandatory' unless opts[:invoice]
  Front.set(:log, @log)
  Front.set(:logging, @log.debug?)
  FileUtils.mkdir_p(opts[:home])
  Front.set(:home, opts[:home])
  Front.set(
    :server_settings,
    Logger: WebrickLog.new(@log),
    AccessLog: []
  )
  if opts['standalone']
    remotes = Remotes::Empty.new
    @log.debug('Running in standalone mode! (will never talk to other remotes)')
  else
    remotes = Remotes.new(File.join(opts[:home], 'zold-remotes'))
  end
  Front.set(:ignore_score_weakness, opts['ignore-score-weakness'])
  wallets = Wallets.new(File.join(opts[:home], 'zold-wallets'))
  Front.set(:wallets, wallets)
  Front.set(:remotes, remotes)
  copies = File.join(opts[:home], 'zold-copies')
  Front.set(:copies, copies)
  address = "#{opts[:host]}:#{opts[:port]}".downcase
  Front.set(:address, address)
  Front.set(
    :entrance, Entrance.new(wallets, remotes, copies, address, log: @log)
  )
  Front.set(:root, Dir.pwd)
  Front.set(:port, opts['bind-port'])
  Front.set(:reboot, !opts['never-reboot'])
  invoice = opts[:invoice]
  unless invoice.include?('@')
    require_relative 'pull'
    Pull.new(wallets: wallets, remotes: remotes, copies: copies, log: @log).run(['pull', invoice])
    require_relative 'invoice'
    invoice = Invoice.new(wallets: wallets, log: @log).run(['invoice', invoice])
  end
  farm = Farm.new(invoice, File.join(opts[:home], 'farm'), log: @log)
  farm.start(
    opts[:host],
    opts[:port],
    threads: opts[:threads], strength: opts[:strength]
  )
  Front.set(:farm, farm)
  update = Thread.start do
    VerboseThread.new(@log).run do
      loop do
        sleep(60)
        require_relative 'remote'
        Remote.new(remotes: remotes, log: @log, farm: farm).run(%w[remote update --reboot])
        @log.debug('Regular update of remote nodes succeeded')
      end
    end
  end
  @log.debug("Starting up the web front at http://#{opts[:host]}:#{opts[:port]}...")
  begin
    Front.run!
  ensure
    farm.stop
    update.exit
  end
end