require 'cri' require 'fileutils' require 'pathname' require 'git' require "canzea/config" require "canzea/version" require "canzea/environment" require "helper-run-class" require "plan-step-class" require "ssh-base-cmd-class" require "template-runner" require "commands/config-git-commit" require "commands/get-catalog" require "commands/remote-run" require "commands/remote-bootstrap" require "commands/gen-user" require "commands/update-config" require "commands/apply-config" require "commands/add-env" require "commands/prepare-plan" module Canzea command = Cri::Command.define do name 'canzea' usage 'canzea [options]' aliases :ds, :stuff summary 'canzea cli' description 'Canzea command line interface.' flag :h, :help, 'show help for this command' do |value, cmd| puts cmd.help exit 0 end flag :v, :version, 'Version' do |value, cmd| puts Canzea::VERSION end flag nil, :pwd, 'PWD' flag nil, :verbose, 'Verbose' # flag nil, :run, 'Run a step' # flag nil, :helper, 'Execute a helper' flag nil, :remote, 'Remote execution' flag nil, :get, 'Get a file from the remote server' flag nil, :put, 'Put a file onto the remote server' flag nil, :encrypt, 'Encrypt' flag nil, :decrypt, 'Encrypt' flag nil, :init, 'Remote init execution' # flag nil, :enable, 'Enable the service' flag nil, :test, 'Run as a test' flag nil, :reset, 'Refresh catalog to latest' flag nil, :config_git_commit, 'Manage configuration files in git' option nil, :config, 'Config', argument: :required option nil, :lifecycle, 'Lifecycle action [install, configure, upgrade, uninstall]', argument: :required option nil, :util, "Utility action [gen-user, gen-cert, add-env, add-env-secret, prepare-plan, apply-config]", argument: :required option nil, :catalogTag, 'Specific tag of the catalog', argument: :required option nil, :gitRoot, 'Git root', argument: :required option nil, :role, 'Role', argument: :required option nil, :solution, 'Solution', argument: :required option nil, :task, 'Task', argument: :required option nil, :step, 'Step', argument: :required option nil, :blueprint, 'Blueprint', argument: :required option nil, :segment, 'Segment', argument: :required option nil, :status, 'Result will be put into the status message', argument: :required option nil, :serverBase, 'Server Base', argument: :required option nil, :serverNumber, 'Server Number', argument: :required option nil, :privateKey, 'Task', argument: :required option nil, :publicKey, 'Task', argument: :required option nil, :template, 'Template', argument: :required option nil, :action, 'Helper Action', argument: :required option nil, :args, 'Arguments (JSON format)', argument: :required run do |opts, args, cmd| test = opts.fetch(:test, false) if opts[:config] file = File.read(opts[:config]) Canzea::configure JSON.parse(file) end extraConfig = Canzea::config[:catalog_location] + "/config.json" if File.exists?(extraConfig) if (opts[:verbose]) puts "-- Reading #{extraConfig}" end file = File.read(extraConfig) Canzea::configure JSON.parse(file) end if opts[:pwd] puts Dir.pwd end if File.exists?(Canzea::config[:catalog_location]) == false GetCatalog.new.do(opts.fetch(:catalogTag, nil)); end if (opts[:reset]) GetCatalog.new.do(opts.fetch(:catalogTag, nil)); end if (opts[:util]) util = opts[:util] if (util == "add-env") AddEnv.new.add(args[0], args[1]) end if (util == "add-env-secret") AddEnv.new.addSecret(args[0], args[1]) end if (util == "gen-user") GenUser.new.do(args[0]) end if (util == "get-key-value") r = Registry.new puts r.getKeyValue (args[0]) end if (util == "prepare-plan") ac = PreparePlan.new ac.do opts[:blueprint], opts[:segment], opts[:step], test, opts[:privateKey], opts[:serverBase], opts[:serverNumber] end if (util == "apply-config") gitRoot = opts.fetch(:gitRoot, Canzea::config[:git_repo]) ac = ApplyConfig.new ac.do gitRoot, test, opts[:step], opts[:task] end end if (opts[:get] || opts[:put]) publicIp = File.read("#{Canzea::config[:pwd]}/vps-#{opts[:serverBase]}-#{opts[:serverNumber]}.json") if (opts[:get]) remote = RemoteCall.new remote.get publicIp, opts[:privateKey], args[0], args[1] end if (opts[:put]) remote = RemoteCall.new remote.put publicIp, opts[:privateKey], args[0], args[1] end end if (opts[:encrypt]) remote = RemoteCall.new remote.encrypt args[0], opts[:publicKey] end if (opts[:decrypt]) remote = RemoteCall.new remote.decrypt args[0], opts[:privateKey] end if (opts[:lifecycle]) lifecycle = opts[:lifecycle] AddEnv.new.injectEnvironmentVariables() if lifecycle == 'install' puts "-- Running install step #{opts[:role]} #{opts[:solution]}" ps = PlanStep.new ps.runPhaseInstall opts[:role], opts[:solution], test, Integer(opts.fetch(:task, 1)) if (test == false) gitRoot = opts.fetch(:gitRoot, Canzea::config[:git_repo]) conf = { :gitRoot => gitRoot, :role => opts[:role], :solution => opts[:solution] } ps = UpdateConfig.new ps.do opts[:role], opts[:solution], gitRoot end end if lifecycle == 'configure' puts "-- Running configure step #{opts[:role]} #{opts[:solution]}" ps = PlanStep.new ps.runPhaseConfigure opts[:role], opts[:solution], test, Integer(opts.fetch(:task, 1)) end if lifecycle == 'wire' puts "-- Running helper #{opts[:solution]} #{opts[:action]}" ps = HelperRun.new ps.run opts[:solution], opts[:action], opts.fetch(:args, {}), opts.fetch(:status,nil) end end if opts[:init] publicIp = File.read("#{Canzea::config[:pwd]}/vps-#{opts[:serverBase]}-#{opts[:serverNumber]}.json") RemoteInit.new.do publicIp, opts[:privateKey] end if opts[:remote] publicIp = File.read("#{Canzea::config[:pwd]}/vps-#{opts[:serverBase]}-#{opts[:serverNumber]}.json") RemoteRun.new.do publicIp, opts[:privateKey], opts[:role], opts[:solution] end if opts[:config_git_commit] gitRoot = opts.fetch(:gitRoot, Canzea::config[:git_repo]) cg = ConfigGitCommit.new cg.do gitRoot, args[0], opts[:template], (args.length == 1 ? {}:JSON.parse(args[1])) end end end command.run(ARGV) end