Class: Zold::Remote

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

Overview

Remote command

Instance Method Summary collapse

Constructor Details

#initialize(remotes:, log: Log::Quiet.new) ⇒ Remote

Returns a new instance of Remote.



39
40
41
42
# File 'lib/zold/commands/remote.rb', line 39

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

Instance Method Details

#add(host, port) ⇒ Object



104
105
106
107
108
# File 'lib/zold/commands/remote.rb', line 104

def add(host, port)
  @remotes.add(host, port)
  @log.info("#{host}:#{port} added to the list")
  @log.info("There are #{@remotes.all.count} remote nodes in the list")
end

#cleanObject



94
95
96
97
# File 'lib/zold/commands/remote.rb', line 94

def clean
  @remotes.clean
  @log.debug('All remote nodes deleted')
end

#remove(host, port) ⇒ Object



110
111
112
113
114
# File 'lib/zold/commands/remote.rb', line 110

def remove(host, port)
  @remotes.remove(host, port)
  @log.info("#{host}:#{port} removed from the list")
  @log.info("There are #{@remotes.all.count} remote nodes in the list")
end

#resetObject



99
100
101
102
# File 'lib/zold/commands/remote.rb', line 99

def reset
  @remotes.reset
  @log.debug('Remote nodes set back to default')
end

#run(args = []) ⇒ Object



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
# File 'lib/zold/commands/remote.rb', line 44

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold remote <command> [options]
Available commands:
#{Rainbow('remote show').green}
  Show all registered remote nodes
#{Rainbow('remote clean').green}
  Remove all registered remote nodes
#{Rainbow('remote reset').green}
  Restore it back to the default list of nodes
#{Rainbow('remote add').green} host [port]
  Add a new remote node
#{Rainbow('remote remove').green} host [port]
  Remove the remote node
#{Rainbow('remote update').green}
  Check each registered remote node for availability
Available options:"
    o.bool '--ignore-score-weakness',
      'Don\'t complain when their score is too weak',
      default: false
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  command = mine[0]
  case command
  when 'show'
    show
  when 'clean'
    clean
  when 'reset'
    reset
  when 'add'
    add(mine[1], mine[2] ? mine[2].to_i : Remotes::PORT)
  when 'remove'
    remove(mine[1], mine[2] ? mine[2].to_i : Remotes::PORT)
  when 'update'
    update(opts)
    update(opts, false)
  else
    raise "Unknown command '#{command}'"
  end
end

#showObject



87
88
89
90
91
92
# File 'lib/zold/commands/remote.rb', line 87

def show
  @remotes.all.each do |r|
    score = Rainbow("/#{r[:score]}").color(r[:score] > 0 ? :green : :red)
    @log.info(r[:host] + Rainbow(":#{r[:port]}").gray + score)
  end
end

#update(opts, deep = true) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/zold/commands/remote.rb', line 116

def update(opts, deep = true)
  capacity = []
  @remotes.iterate(@log) do |r|
    res = r.http('/remotes').get
    raise "#{res.code} \"#{res.message}\"" unless res.code == '200'
    json = JSON.parse(res.body)
    score = Score.parse_json(json['score'])
    raise "Invalid score #{score}" unless score.valid?
    raise "Expired score #{score}" if score.expired?
    raise "Score too weak: #{score.strength}" if score.strength < Score::STRENGTH && !opts['ignore-score-weakness']
    raise "Masqueraded as #{score.host}:#{score.port}" if r.host != score.host || r.port != score.port
    @remotes.rescore(score.host, score.port, score.value)
    if deep
      json['all'].each do |s|
        add(s['host'], s['port']) unless @remotes.exists?(s['host'], s['port'])
      end
    end
    capacity << { host: score.host, port: score.port, count: json['all'].count }
    @log.info("#{r}: #{Rainbow(score.value).green} (#{json['version']})")
  end
  max_capacity = capacity.map { |c| c[:count] }.max || 0
  capacity.each do |c|
    @remotes.error(c[:host], c[:port]) if c[:count] < max_capacity
  end
  total = @remotes.all.size
  if total.zero?
    @log.debug("The list of remotes is #{Rainbow('empty').red}!")
    @log.debug("Run 'zold remote add b1.zold.io` and then `zold update`")
  else
    @log.debug("There are #{total} known remotes")
  end
end