Class: RSMP::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rsmp/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

avoid Thor returnin 0 on failures, see github.com/coinbase/salus/pull/380/files

Returns:

  • (Boolean)


190
191
192
# File 'lib/rsmp/cli.rb', line 190

def self.exit_on_failure?
  true
end

Instance Method Details

#convertObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rsmp/cli.rb', line 168

def convert
  unless options[:in]
    puts "Error: Input option missing"
    exit
  end

  unless options[:out]
    puts "Error: Output option missing"
    exit
  end

  unless File.exist? options[:in]
    puts "Error: Input path file #{options[:in]} not found"
    exit
  end

  sxl = RSMP::Convert::Import::YAML.read options[:in]
  RSMP::Convert::Export::JSONSchema.write sxl, options[:out]
end

#siteObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/rsmp/cli.rb', line 19

def site
  settings = {}
  log_settings = { 'active' => true }

  if options[:config]
    if File.exist? options[:config]
      settings = YAML.load_file options[:config]
      log_settings = settings.delete('log') || {}
    else
      puts "Error: Config #{options[:config]} not found"
      exit
    end
  end

  if options[:id]
    settings['site_id'] = options[:id]
  end

  if options[:supervisors]
    settings['supervisors'] = []
    options[:supervisors].split(',').each do |supervisor|
      ip, port = supervisor.split ':'
      ip = '127.0.0.1' if ip.empty?
      port = '12111' if port.empty?
      settings['supervisors'] << {"ip"=>ip, "port"=>port}
    end
  end

  if options[:core]
    settings['core_versions'] = [options[:core]]
  end

  site_class = RSMP::Site
  site_type = options[:type] || settings['type']
  case site_type
    when 'tlc'
      site_class = RSMP::TLC::TrafficControllerSite
    else
      puts "Error: Unknown site type #{site_type}"
      exit
  end

  if options[:log]
    log_settings['path'] = options[:log]
  end

  if options[:json]
    log_settings['json'] = options[:json]
  end

  Async do |task|
    task.annotate 'cli'
    loop do
      begin
        site = site_class.new(site_settings: settings, log_settings: log_settings)
        site.start
        site.wait
      rescue Psych::SyntaxError => e
        puts "Cannot read config file #{e}"
        break
      rescue RSMP::Schema::UnknownSchemaTypeError => e
        puts "Cannot start site: #{e}"
        break
      rescue RSMP::Schema::UnknownSchemaVersionError => e
        puts "Cannot start site: #{e}"
        break
      rescue RSMP::ConfigurationError => e
        puts "Cannot start site: #{e}"
        break
      rescue RSMP::Restart
        site.stop
      end
    end
  end
rescue Interrupt
  # cntr-c
rescue Exception => e
  puts "Uncaught error: #{e}"
  puts caller.join("\n")
end

#supervisorObject



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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rsmp/cli.rb', line 108

def supervisor
  settings = {}
  log_settings = { 'active' => true }

  if options[:config]
    if File.exist? options[:config]
      settings = YAML.load_file options[:config]
      log_settings = settings.delete 'log'
    else
      puts "Error: Config #{options[:config]} not found"
      exit
    end
  end

  if options[:id]
    settings['site_id'] = options[:id]
  end

  if options[:ip]
    settings['ip'] = options[:ip]
  end

  if options[:port]
    settings['port'] = options[:port]
  end

  if options[:core]
    settings['guest'] = {}
    settings['guest']['core_versions'] = [options[:core]]
  end

  if options[:log]
    log_settings['path'] = options[:log]
  end

  if options[:json]
    log_settings['json'] = options[:json]
  end

  Async do |task|
    task.annotate 'cli'
    supervisor = RSMP::Supervisor.new(supervisor_settings:settings,log_settings:log_settings)
    supervisor.start
    supervisor.wait
  rescue Psych::SyntaxError => e
    puts "Cannot read config file #{e}"
  rescue RSMP::Schema::UnknownSchemaTypeError => e
    puts "Cannot start supervisor: #{e}"
  rescue RSMP::Schema::UnknownSchemaVersionError => e
    puts "Cannot start supervisor: #{e}"
  rescue RSMP::ConfigurationError => e
    puts "Cannot start supervisor: #{e}"
  end
rescue Interrupt
  # ctrl-c
end

#versionObject



7
8
9
# File 'lib/rsmp/cli.rb', line 7

def version
  puts RSMP::VERSION
end