class GeoCLI
Create GeoCLI
for the command requires
GeoCLI
context
Attributes
env_name[RW]
environment[RW]
no_color[RW]
CLI FLAGS AND OPTIONS
verbose[RW]
CLI FLAGS AND OPTIONS
Public Instance Methods
add_commands()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 189 def add_commands plan_cmd apply_cmd destroy_cmd graph_cmd status_cmd end
create_environment(name, &block)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 63 def create_environment(name, &block) return @environment if @environment if name != @env_name puts "Not loading environment #{name} as env_name is #{@env_name}" if @verbose return NullObject.new end @environment = GeoEngineer::Environment.new(name, &block) init_tmp_dir(name) init_terraform_files() @environment end
global_options()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 169 def global_options global_option('-e', '--environment <name>', "Environment to use") @verbose = true global_option('--quiet', 'reduce the noisy outputs (default they are on)') { @verbose = false } @no_color = '' global_option('--no-color', 'removes color from the terraform output') { String.disable_colorization = true @no_color = ' -no-color' } end
graph_cmd()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 158 def graph_cmd command :graph do |c| c.syntax = 'geo graph [<geo_files>]' c.description = 'Generate and graph of the environment resources to GraphViz' action = lambda do |args, options| puts env.to_dot end c.action init_action(:graph, &action) end end
init_action(action_name) { |args, options| ... }
click to toggle source
This defines the typical action in geo engineer
-
require the environment
-
require the geo files
-
ensure everything is valid
-
execute the action
-
execute the after hook
# File lib/geoengineer/cli/geo_cli.rb, line 135 def init_action(action_name) lambda do |args, options| require_environment(options) require_geo_files(args) throw "Environment not set" unless @environment @environment.execute_lifecycle(:before, action_name.to_sym) errs = @environment.errors.flatten.sort unless errs.empty? print_validation_errors(errs) exit 1 end yield args, options @environment.execute_lifecycle(:after, action_name.to_sym) end end
init_terraform_files()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 46 def init_terraform_files @terraform_file = "terraform.tf.json" @terraform_state_file = "terraform.tfstate" @plan_file = "plan.terraform" files = [ "#{@tmpdir}/#{@terraform_state_file}.backup", "#{@tmpdir}/#{@terraform_file}", "#{@tmpdir}/#{@terraform_state_file}", "#{@tmpdir}/#{@plan_file}" ] files.each do |file| File.delete(file) if File.exist?(file) end end
init_tmp_dir(name)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 41 def init_tmp_dir(name) @tmpdir = "#{Dir.pwd}/tmp/#{name}" FileUtils.mkdir_p @tmpdir end
print_validation_errors(errs)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 109 def print_validation_errors(errs) puts errs.map { |s| "ERROR: #{s}".colorize(:red) } puts "Total Errors #{errs.length}" end
require_all_projects()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 90 def require_all_projects Dir["#{Dir.pwd}/projects/**/*.rb"].each do |project_file| puts "LOADING #{project_file}" if @verbose require project_file end end
require_environment(options)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 80 def require_environment(options) @env_name = options.environment || ENV['GEO_ENV'] || 'staging' puts "Using environment '#{@env_name}'\n" if @verbose begin require_from_pwd "environments/#{@env_name}" rescue LoadError puts "unable to load 'environments/#{@env_name}'" if @verbose end end
require_from_pwd(file)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 76 def require_from_pwd(file) require "#{Dir.pwd}/#{file}" end
require_geo_files(args)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 104 def require_geo_files(args) return require_all_projects if args.empty? args.each { |project_file| require_project_file(project_file) } end
require_project_file(project_file)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 97 def require_project_file(project_file) if !File.exist?(project_file) && !File.exist?("#{project_file}.rb") throw "The file \"#{project_file}\" does not exist" end require_from_pwd project_file end
run()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 197 def run program :name, 'GeoEngineer' program :version, GeoEngineer::VERSION program :description, 'GeoEngineer will help you Terraform your resources' always_trace! # check terraform installed return puts "Please install terraform" unless terraform_installed? # global_options global_options # Require any patches to the way geo works require_from_pwd '.geo' if File.file?("#{Dir.pwd}/.geo.rb") # Add commands add_commands execute_lifecycle(:after, :add_commands) # Execute the CLI run! end
shell_exec(cmd, verbose = @verbose)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 114 def shell_exec(cmd, verbose = @verbose) stdin, stdout_and_stderr, wait_thr = Open3.popen2e({}, *cmd) puts(">> #{cmd}\n") if verbose stdout_and_stderr.each do |line| puts(line) if verbose end puts("<< Exited with status: #{wait_thr.value.exitstatus}\n\n") if verbose stdin.close stdout_and_stderr.close wait_thr.value end
terraform_installed?()
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 184 def terraform_installed? terraform_version = shell_exec('which terraform') terraform_version.exitstatus.zero? end
yes?(question)
click to toggle source
# File lib/geoengineer/cli/geo_cli.rb, line 153 def yes?(question) answer = ask question answer.strip.upcase.start_with? "YES" end