Class: EtFullSystem::LocalCommand

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

Defined Under Namespace

Classes: RestProviderNotConfigured, ServiceUrlIncorrect, ServicesNotConfigured

Constant Summary collapse

DEFAULT_BASE_URL =
"http://localhost:3200"
LOCK_FILE =
File.join(Dir.tmpdir, 'et_full_system_traefik_rest_lockfile')
PROJECT_PATH =
Dir.pwd
GEM_PATH =
File.absolute_path('../../..', __dir__)

Instance Method Summary collapse

Instance Method Details

#bootObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/et_full_system/cli/local.rb', line 22

def boot
  STDERR.puts "boot - base_url is #{options[:base_url]}"
  json_setup_file = File.absolute_path('../../../foreman/traefik.json', __dir__)
  connect_retry_countdown = 10
  begin
    resp = HTTParty.put "#{options[:base_url]}/api/providers/rest", body: File.read(json_setup_file) , headers: {'Content-Type': 'application/json', 'Accept': 'application/json'}
    raise "Error from traefik when performing initial config says: #{resp.body}" unless (200..299).include? resp.code
    sleep 1
  rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED
    connect_retry_countdown -= 1
    if connect_retry_countdown.zero?
      raise "Could not connect to the traefik API after 10 retries (boot)"
    else
      STDERR.puts "boot - Retrying connection to traefik API in 5 seconds"
      sleep 5
      retry
    end
  end
end

#disable_adminObject



276
277
278
279
# File 'lib/et_full_system/cli/local.rb', line 276

def disable_admin
  invoker 'remove', 'admin_web'
  puts "Admin is now stopped"
end

#disable_apiObject



269
270
271
272
273
# File 'lib/et_full_system/cli/local.rb', line 269

def disable_api
  invoker 'remove', 'api_web'
  invoker 'remove', 'api_sidekiq'
  puts "api is now stopped"
end

#disable_atos_apiObject



263
264
265
266
# File 'lib/et_full_system/cli/local.rb', line 263

def disable_atos_api
  invoker 'remove', 'atos_api_web'
  puts "atos_api is now stopped"
end

#disable_ccd_exportObject



257
258
259
260
# File 'lib/et_full_system/cli/local.rb', line 257

def disable_ccd_export
  invoker 'remove', 'et_ccd_export_sidekiq'
  puts "ccd_export is now stopped"
end

#disable_et1Object



250
251
252
253
254
# File 'lib/et_full_system/cli/local.rb', line 250

def disable_et1
  invoker 'remove', 'et1_web'
  invoker 'remove', 'et1_sidekiq'
  puts "ET1 is now stopped"
end

#disable_et3Object



282
283
284
285
# File 'lib/et_full_system/cli/local.rb', line 282

def disable_et3
  invoker 'remove', 'et3_web'
  puts "ET3 is now stopped"
end

#enable_adminObject



238
239
240
241
# File 'lib/et_full_system/cli/local.rb', line 238

def enable_admin
  invoker 'add', 'admin_web'
  puts "Admin is now running"
end

#enable_apiObject



231
232
233
234
235
# File 'lib/et_full_system/cli/local.rb', line 231

def enable_api
  invoker 'add', 'api_web'
  invoker 'add', 'api_sidekiq'
  puts "api is now running"
end

#enable_atos_apiObject



225
226
227
228
# File 'lib/et_full_system/cli/local.rb', line 225

def enable_atos_api
  invoker 'add', 'atos_api_web'
  puts "atos_api is now running"
end

#enable_ccd_exportObject



219
220
221
222
# File 'lib/et_full_system/cli/local.rb', line 219

def enable_ccd_export
  invoker 'add', 'et_ccd_export_sidekiq'
  puts "ccd_export is now running"
end

#enable_et1Object



212
213
214
215
216
# File 'lib/et_full_system/cli/local.rb', line 212

def enable_et1
  invoker 'add', 'et1_web'
  invoker 'add', 'et1_sidekiq'
  puts "ET1 is now running"
end

#enable_et3Object



244
245
246
247
# File 'lib/et_full_system/cli/local.rb', line 244

def enable_et3
  invoker 'add', 'et3_web'
  puts "ET3 is now running"
end

#invoker(*args) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/et_full_system/cli/local.rb', line 203

def invoker(*args)
  cmd = "invoker #{args.join(' ')}"
  puts cmd
  result = `#{cmd}`
  puts result
  result
end

#restart_adminObject



308
309
310
311
# File 'lib/et_full_system/cli/local.rb', line 308

def restart_admin
  invoker 'reload', 'admin_web'
  puts "admin Has been restarted"
end

#restart_apiObject



295
296
297
298
299
# File 'lib/et_full_system/cli/local.rb', line 295

def restart_api
  invoker 'reload', 'api_web'
  invoker 'reload', 'api_sidekiq'
  puts "api Has been restarted"
end

#restart_atos_apiObject



314
315
316
317
# File 'lib/et_full_system/cli/local.rb', line 314

def restart_atos_api
  invoker 'reload', 'atos_api_web'
  puts "atos_api Has been restarted"
end

#restart_ccd_exportObject



320
321
322
323
# File 'lib/et_full_system/cli/local.rb', line 320

def restart_ccd_export
  invoker 'reload', 'et_ccd_export_sidekiq'
  puts "ccd_export Has been restarted"
end

#restart_et1Object



288
289
290
291
292
# File 'lib/et_full_system/cli/local.rb', line 288

def restart_et1
  invoker 'reload', 'et1_web'
  invoker 'reload', 'et1_sidekiq'
  puts "ET1 Has been restarted"
end

#restart_et3Object



302
303
304
305
# File 'lib/et_full_system/cli/local.rb', line 302

def restart_et3
  invoker 'reload', 'et3_web'
  puts "et3 Has been restarted"
end

#serverObject



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
# File 'lib/et_full_system/cli/local.rb', line 99

def server
  puts "Scheduling traefik config and file storage config"
  pid = fork do
    self.class.start(['boot']) unless options.in_docker_compose?
    EtFullSystem::Cli::Local::FileStorageCommand.start(['setup'])
  end
  Process.detach(pid)

  puts "Starting Invoker"
  unbundled do
    without = options[:without]
    if options.minimal?
      without = ['et1', 'et3', 'admin', 'api', 'ccd_export', 'atos_api']
    end
    if options.in_docker_compose?
      cmd = "RAILS_ENV=#{options[:rails_env]} FS_ROOT_PATH=#{PROJECT_PATH} FOREMAN_PATH=#{GEM_PATH}/foreman godotenv -f #{GEM_PATH}/foreman/.env invoker start \"#{GEM_PATH}/foreman/ComposeProcfile\" --port=4000"
    else
      cmd = "RAILS_ENV=#{options[:rails_env]} AZURITE_STORAGE_PATH=\"#{options[:azurite_storage_path]}\" FS_ROOT_PATH=#{PROJECT_PATH} FOREMAN_PATH=#{GEM_PATH}/foreman godotenv -f #{GEM_PATH}/foreman/.env invoker start \"#{GEM_PATH}/foreman/Procfile\" --port=4000"
    end
    STDERR.puts cmd
    unless without.empty?
      stop_cmds = without.reduce([]) do |acc, service|
        acc.concat(invoker_processes_for(service))
      end.map do |proc|
        "invoker remove #{proc}"
      end
      stop_cmd = stop_cmds.join(' && ')
      puts "---------------------- DISABLING SERVICES IN 30 SECONDS ---------------------------"
      puts "command is #{stop_cmd}"
      Process.fork do
        sleep 30
        puts `#{stop_cmd}`
      end
    end
    exec cmd
  end
end

#service_env(service) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/et_full_system/cli/local.rb', line 184

def service_env(service)
  lookup = {
    'atos_api' => :et_atos,
    'admin' => :et_admin,
    'api' => :et_api,
    'et1' => :et1,
    'et3' => :et3,
    'et_ccd_export' => :et_ccd_export
  }
  file = lookup.fetch(service)
  parsed = Dotenv.parse("#{GEM_PATH}/foreman/.env", "#{GEM_PATH}/foreman/#{file}.env")
  parsed.each_pair do |name, value|
    puts "#{name}=#{value}"
  end
rescue KeyError
  puts "The service must be one of #{lookup.keys}"
end

#setupObject



143
144
145
146
147
148
# File 'lib/et_full_system/cli/local.rb', line 143

def setup
  setup_depencencies
  install_bundler if rvm_installed?
  setup_ruby_versions unless !rvm_installed? || options.in_docker_compose?
  setup_services
end

#setup_depencenciesObject



163
164
165
166
167
# File 'lib/et_full_system/cli/local.rb', line 163

def setup_depencencies
  cmd = "bash --login -c \"cd #{File.join(PROJECT_PATH, 'support', 'fake_services')} && bundle install\""
  STDERR.puts cmd
  external_command cmd, 'setup_dependencies'
end

#setup_ruby_versionsObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/et_full_system/cli/local.rb', line 170

def setup_ruby_versions
  versions = Dir.glob(File.join(PROJECT_PATH, 'systems', '*', '.ruby-version')).map do |version_file|
    File.read(version_file).split("\n").first.gsub(/\Aruby-/, '')
  end.uniq - [RUBY_VERSION]

  versions.each do |version|
    puts "------------------------------------------------ SETTING UP ruby #{version} ---------------------------------------------------"
    cmd = "bash --login -c \"rvm install #{version}\""
    puts cmd
    external_command cmd, "ruby #{version} install"
  end
end

#setup_servicesObject



151
152
153
154
155
156
157
158
159
160
# File 'lib/et_full_system/cli/local.rb', line 151

def setup_services
  unbundled do
    setup_et1_service
    setup_et3_service
    setup_api_service
    setup_admin_service
    setup_atos_service
    setup_ccd_service
  end
end

#update_service_url(service, url) ⇒ Object



45
46
47
48
49
# File 'lib/et_full_system/cli/local.rb', line 45

def update_service_url(service, url)
  within_rest_lock do
    update_rest_backend_url(service, url)
  end
end

#wait_for_supportObject



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
# File 'lib/et_full_system/cli/local.rb', line 53

def wait_for_support
  connect_retry_countdown = 10
  setup_retry_countdown = 10

  begin
    resp = HTTParty.get "#{options[:base_url]}/api/rawdata", headers: {'Content-Type': 'application/json', 'Accept': 'application/json'}
    raise RestProviderNotConfigured if resp.code == 404 || resp.parsed_response.dig('routers', 'rest@internal').nil?
    raise ServicesNotConfigured if resp.parsed_response.dig('routers', 'et1@rest').nil?
  rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED
    connect_retry_countdown -= 1
    if connect_retry_countdown.zero?
      raise "Could not connect to the traefik API after 10 retries (wait_for_support)"
    else
      STDERR.puts "wait_for_support - Retrying connection to traefik API in 5 seconds"
      sleep 5
      retry
    end
  rescue RestProviderNotConfigured
    setup_retry_countdown -= 1
    if setup_retry_countdown.zero?
      raise "Could not find the REST provider in traefik after 10 retries"
    else
      STDERR.puts "Re checking for the REST provider in traefik in 5 seconds"
      sleep 5
      retry
    end
  rescue ServicesNotConfigured
    setup_retry_countdown -= 1
    if setup_retry_countdown.zero?
      raise "Could not find the ET1 router in traefik after 10 retries"
    else
      STDERR.puts "Re checking for the ET1 router in traefik in 5 seconds"
      sleep 5
      retry
    end
  end
  STDERR.puts "Support services now ready"
end