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)


166
167
168
# File 'lib/rsmp/cli.rb', line 166

def self.exit_on_failure?
  true
end

Instance Method Details

#convertObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rsmp/cli.rb', line 144

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



18
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
# File 'lib/rsmp/cli.rb', line 18

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[:log]
    log_settings['path'] = options[:log]
  end

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

  site_class = RSMP::Site
  if options[:type]
    case options[:type]
      when 'tlc'
        site_class = RSMP::TLC::TrafficControllerSite
      else
        site_class = RSMP::Site
    end
  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 RSMP::Restart
        site.stop
      end
    end
  end
rescue Interrupt
  # cntr-c
rescue RSMP::Schema::UnknownSchemaTypeError => e
  puts "Cannot start site: #{e}"
rescue RSMP::Schema::UnknownSchemaVersionError => e
  puts "Cannot start site: #{e}"
rescue Psych::SyntaxError => e
  puts "Cannot read config file #{e}"
rescue Exception => e
  puts "Uncaught error: #{e}"
  puts caller.join("\n")
end

#supervisorObject



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
139
# File 'lib/rsmp/cli.rb', line 95

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[: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
  end
rescue Interrupt
  # ctrl-c
rescue RSMP::ConfigurationError => e
  puts "Cannot start supervisor: #{e}"
end

#versionObject



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

def version
  puts RSMP::VERSION
end