module Coral module Vagrant module Command class CoralBase < ::Vagrant.plugin("2", :command) #----------------------------------------------------------------------------- # Constructor / Destructor def initialize(argv, env) super @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv) @subcommands = ::Vagrant::Registry.new #------------ # Builders # # coral create --repo={cluster_repository} [ {project_path} ] # # {cluster_repository} -> Defaults to the coral cluster core # {project_path} -> Defaults to the current directory (must be empty) # # * calls: coral init # @subcommands.register(:create) { Coral::Vagrant::SubCommand::CoralCreate } # # coral add --repo={repository} {name} [ {type} ] # # {name} -> Name of the project (required) # {type} -> Defaults to module (can be: cluster, profile, module) # {repository} -> Defaults to a new template repository of {type} # @subcommands.register(:add) { Coral::Vagrant::SubCommand::CoralAdd } # # coral rm {name} [ {type} ] # # {name} -> Name of the project (required) # {type} -> Defaults to module (can be: cluster, profile, module) # @subcommands.register(:rm) { Coral::Vagrant::SubCommand::CoralRemove } #------------------------------ # Configuration / Deployment # # coral config --dir={config_dir} {config_name} [ {value} ] # # {config_name} -> Dot separated configuration name (dot = directory entry) # {value} -> Configuration value (if we are setting the configuration) # {config_dir} -> Root directory of configuration files (default: config) # @subcommands.register(:config) { Coral::Vagrant::SubCommand::CoralConfig } # # coral push --repos={repository_name},... [ {server_name} ... ] # # {server_name} -> Server name of a local or remote server to push attached repositories to # {repository_name} -> Local repository name for a local/remote repository relationship # @subcommands.register(:push) { Coral::Vagrant::SubCommand::CoralPush } # # coral send {provider} ... # # {provider} -> Remote cloud provider on which to send and initialize the coral cluster # # * calls: coral init # coral image # @subcommands.register(:send) { Coral::Vagrant::SubCommand::CoralSend } # # coral image --servers={server_name},... {provider} ... # # {provider} -> Remote cloud provider on which to create the image # {server_name} -> Server name of a cluster server to create an image of # @subcommands.register(:image) { Coral::Vagrant::SubCommand::CoralImage } #-------------- # Management # # coral init [ {server_name} ... ] # # {server_name} -> Server name of a cluster server to initialize # @subcommands.register(:init) { Coral::Vagrant::SubCommand::CoralInit } # # coral update [ {server_name} ... ] # # {server_name} -> Server name of a cluster server to update # @subcommands.register(:update) { Coral::Vagrant::SubCommand::CoralUpdate } # # coral run --dir={plan_dir} {plan_name} ... # # {plan_name} -> Name of JSON based execution plan to run # {plan_dir} -> Root directory of exexution plan files (default: config/plans) # # * could call anything # @subcommands.register(:run) { Coral::Vagrant::SubCommand::CoralRun } end #----------------------------------------------------------------------------- # Help def help opts = OptionParser.new do |opts| opts.banner = 'Usage: vagrant coral []' opts.separator '' opts.separator 'Available subcommands:' # Add the available subcommands as separators in order to print them # out as well. keys = [] @subcommands.each { |key, value| keys << key.to_s } keys.sort.each do |key| opts.separator " #{key}" end opts.separator '' opts.separator 'For help on any individual command run `vagrant coral COMMAND -h`' end @env.ui.info(opts.help, :prefix => false) end #----------------------------------------------------------------------------- # Execution def execute if @main_args.include?('-h') || @main_args.include?('--help') # Print the help for all the cluster commands. return help end # Fetch our command class if we have a subcommand. command_class = @subcommands.get(@sub_command.to_sym) if @sub_command # If there is no class mapping, we try running the subcommand as an # execution plan. if !command_class && @sub_command command_class = @subcommands.get(:run) @sub_args.unshift(@sub_command) end # If all else fails print the help and exit. return help if !command_class || !@sub_command @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}") # Initialize and execute the command class command_class.new(@sub_args, @env).execute end end end end end