# frozen_string_literal: true require_relative "base" module Neetob class CLI module Sre class CheckEssentialEnv < Base REQUIRED_KEYS = [ "RAILS_ENV", "RAILS_LOG_TO_STDOUT", "RACK_ENV", "RAILS_SERVE_STATIC_FILES", "HONEYBADGER_API_KEY", "HONEYBADGER_JS_API_KEY", "NODE_MODULES_CACHE", "YARN_CACHE", "YARN_PRODUCTION" ] REQUIRED_KEYS_HEROKU = [ "HEROKU_APP_NAME" ] REQUIRED_KEYS_NEETODEPLOY = [ "NEETODEPLOY_APP_NAME" ] attr_reader :app def initialize(app = nil) super() @app = app end def run if app.nil? APPS_LIST[:heroku].each do |app| check_envs_heroku(app) end APPS_LIST[:neetodeploy].each do |app| check_envs_neetodeploy(app) end else is_heroku(app) ? check_envs_heroku(app) : check_envs_neetodeploy(app) end end private def check_envs_heroku(app) begin envs = JSON.parse(`heroku config -a #{app} --json`) required_keys = REQUIRED_KEYS + REQUIRED_KEYS_HEROKU compare_envs(required_keys, envs, app) rescue => exception ui.error(exception.message) end end def check_envs_neetodeploy(app) # TODO: Optimize once github.com/bigbinary/neeto-deploy-web/issues/3745 is done begin env_table = `neetodeploy env list -a #{app}` envs = {} env_table.each_line do |line| match = line.match(/^\| (\w+) +\| (.+?) +\|$/) envs[match[1]] = match[2] if match end required_keys = REQUIRED_KEYS + REQUIRED_KEYS_NEETODEPLOY compare_envs(required_keys, envs, app) rescue => exception ui.error(exception.message) end end def compare_envs(required_keys, envs, app) all_keys_present = required_keys.all? { |key| envs.has_key?(key) } all_keys_present ? ui.success("#{app} has all required envs") : ui.error("#{app} doesn't have all required envs. Missing #{required_keys - envs.keys}") end end end end end