#!/usr/bin/env ruby # Copyright 2012 Hewlett-Packard Development Company, L.P. # All Rights Reserved. # # Author: Simon McCartney # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # require 'bundler' require 'optparse' require 'methadone' require 'stack-kicker' require 'stack' class App include Methadone::Main include Methadone::CLILogging main do |task| # set the formatter case options['log-format'] when :simple format = Proc.new { |severity, datetime, progname, msg| "#{msg}\n" } else format = Proc.new { |severity, datetime, progname, msg| "#{datetime} #{severity}: #{msg}\n" } end Stack.log_format(format) debug { "options = #{options}" } # pass the logger.level into the Stack module Stack.log_level(logger.level) if (task != 'show-stacks') config = Stack.select_stack(options[:stackfile], options['stack']) end # pass some command line options into the config config['skip-secgroup-sync-deletes'] = options['skip-secgroup-sync-deletes'].nil? ? false : true case task when 'validate' Stack.validate(config) when 'configure-knife' Stack.generate_knife_rb(config) when 'show-stacks' Stack.show_stacks(options[:stackfile]) when 'show-stack' Stack.show_stack(config) when 'show-running' Stack.show_running(config) when 'build' Stack.deploy_all(config) when 'delete' Stack.delete_all(config) when 'secgroup-sync' Stack.secgroup_sync(config) when 'ssh' debug { "ssh-host = #{options['ssh-host']}" } debug { "ssh-user = #{options['ssh-user']}" } debug { "ssh-command = #{options['ssh-command']}" } Stack.ssh(config, options['sshhost'], options['ssh-user'], options['ssh-command'] ) else error "Sorry, #{task} hasn't been implemented yet" end end # supplemental methods here # Declare command-line interface here description "create application stacks in the cloud from fundamental building blocks" # # Accept flags via: # on("--flag VAL","Some flag") # options[flag] will contain VAL # # Specify switches via: # on("--[no-]switch","Some switch") # options[:stackfile] = 'Stackfile' options['ssh-user'] = ENV['USER'] on("--stackfile Stackfile", "Specify an alternative Stackfile") on("--stack mystack", "Specify the stack in Stackfile that you want to work with") on("--ssh-host HOST", "Specify a single host, if none supplied, all hosts are executed against") on("--ssh-user USER", "User to be used for SSH access") on("--ssh-command \"do/something\"", "Command to be run over ssh") on("--skip-secgroup-sync-deletes", "Skip deletes during secgroup-sync, handy for running multiple stacks in the one account with overlapping group names") options['log-format'] = :default opts.on("-f", "--log-format FMT", [:default, :simple], "sets the log formatter (simple=msg, default=datetime severity msg") do |f| options['log-format'] = f end arg :task, "task to be performed validate|configure-knife|show-stacks|show-stack|show-running|build|replace|delete|secgroup-sync|ssh" version Stack::Kicker::VERSION use_log_level_option go! end