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" 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, :run, 'Run a step' # flag nil, :helper, 'Execute a helper' flag nil, :remote, 'Remote execution' 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]", 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, :serverBase, 'Server Base', argument: :required option nil, :serverNumber, 'Server Number', argument: :required option nil, :privateKey, '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 if opts[:pwd] puts Dir.pwd GetCatalog.new.do(opts.fetch(:catalogTag, nil)); end if (opts[:util]) util = opts[:util] if (util == "gen-user") GenUser.new.do(args[0]) end if (util == "get-key-value") r = Registry.new puts r.getKeyValue (args[0]) end end if (opts[:reset]) GetCatalog.new.do(opts.fetch(:catalogTag, nil)); end if (opts[:lifecycle]) lifecycle = opts[:lifecycle] 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, "/opt/cloud-profile") # write to configure the registration of the service if (File.exists?("#{gitRoot}/configure.sh") == false) File.open("#{gitRoot}/configure.sh", 'a') { |file| file.puts("#!/bin/bash") } end File.open("#{gitRoot}/configure.sh", 'a') { |file| file.puts("canzea --enable #{ARGV.join(' ')}") } 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[:args] 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