diff --git a/README.md b/README.md index 978db45..5616a9b 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ ACTIONS deploy Deploys a new stack migrate Migrates from green to blue stack remove Removes the given deployment + configure Configure the target account in the restacker.yml file dump Dumps the default configuration for a given template or module console Opens the AWS Console diff --git a/source/bin/restacker b/source/bin/restacker index 6b0450f..8ef5863 100755 --- a/source/bin/restacker +++ b/source/bin/restacker @@ -12,6 +12,7 @@ ACTIONS = { deploy: 'Deploys a new stack', migrate: 'Migrates from green to blue stack', remove: 'Removes the given deployment', + configure: 'Configure the target account in the restacker.yml file', dump: 'Dumps the default configuration for a given template or module', console: 'Opens the AWS Console' } @@ -116,6 +117,17 @@ begin puts(VERSION) || exit(0) if options[:version] usage("Please specify an ACTION") && exit(0) if action.nil? + if action == 'configure' + plane = options[:location] ? options[:location] : DEFAULT_PLANE + if !plane + puts "Location not specified, plane is set to default: [#{DEFAULT_PLANE}]" + end + puts "Configuring target plane [#{plane}]: " + RestackerConfig.load_config(plane.to_sym) + Restacker.configure(plane.to_sym) + exit(0) + end + if ACTIONS.keys.push(:aws).include?(action.to_sym) restacker = Restacker.new(options.to_h) unless ['dump'].include?(action) case action @@ -158,6 +170,16 @@ begin else usage "Please specify a stack name (-n) to remove" end + # when 'configure' + # plane = "" + # if !options[:location] + # puts "Location not specified, default plane #{DEFAULT_PLANE} used." + # plane = DEFAULT_PLANE + # else + # plane = options[:location] + # end + # puts "Configuring target account for plane [#{plane}]:\n" + # RestackerConfig.rc_configure(options[:location]) when 'dump' if options[:template] # puts "Dumping stack parameters" diff --git a/source/bin/rstkr b/source/bin/rstkr deleted file mode 100755 index 6b0450f..0000000 --- a/source/bin/rstkr +++ /dev/null @@ -1,185 +0,0 @@ -#!/usr/bin/env ruby -require 'optparse' -require 'ostruct' -require_relative '../lib/restacker' -require_relative '../lib/restacker_config' -require_relative '../lib/aws_cli' - -ACTIONS = { - list: 'Lists current stack names', - describe: 'Describes a stack parameters and template', - restack: 'Restacks a given deployment', - deploy: 'Deploys a new stack', - migrate: 'Migrates from green to blue stack', - remove: 'Removes the given deployment', - dump: 'Dumps the default configuration for a given template or module', - console: 'Opens the AWS Console' -} -ACTIONS_HELP = "ACTIONS\n\n" + ACTIONS.to_yaml.sub("---\n", '').gsub(': ', "\t").gsub(':', " ") -NOTES = "Notes: - - If no template file path is provided when restacking #{File.basename($0)} will use the same - template as if currently deployed. - - Deployed stack name will be in the form of NAME-DATE using today's date" - -class Parser - def self.parse(options) - opts = OpenStruct.new - - opt_parser = OptionParser.new do |o| - o.banner = "Usage: #{File.basename($0)} [ACTION] [OPTONS]\n\n#{ACTIONS_HELP}\n\nOPTIONS\n\n" - - o.on('-c PROFILE', '--credentials=PROFILE', 'Specify the AWS profile credential (~/.aws/config) to use') do |profile| - opts[:profile] = profile - end - - o.on('-d', '--debug', 'Show stack traces') do |debug| - opts[:debug] = debug - end - - o.on('-e PARAMS', '--environment-params=PARAMS', 'Parameters to add to the instance environment (/etc/profile.d) in the form of', 'k1=v1,k2=v2. E.g., -p SCORING_URL=https://scoring...') do |params| - opts[:env_params] = params - end - - o.on("-h", "--help", "Prints these wonderful lines...") do - puts o - puts "\n#{NOTES}" - exit - end - - o.on('-l LOCATION', '--location=LOCATION', 'Where to deploy, ksp, kvp, kcp...') do |location| - opts[:location] = location - end - - o.on('-m MODULE', '--migrate-module=MODULE', 'Migration module to execute') do |_module| - opts[:module] = _module - end - - o.on('-n NAME', '--name=NAME', 'Stack prefix stack name (alphanumeric with dashes)') do |name| - opts[:name] = name - end - - o.on('-o OPTIONS', '--migrate-options=OPTIONS', 'Options required by migration module to migrate from green to', 'blue stack in the form of k1=v1,k2=v2. E.g., -p EIP=1.2.3.4') do |options| - opts[:options] = options - end - - o.on('-p PARAMS', '--params=PARAMS', 'Parameters to override current stack parameters in the form of', 'k1=v1,k2=v2. E.g., -p AmiId=ami-a4jd7928') do |params| - opts[:params] = params - end - - o.on('-P FILE', '--parameters-file=FILE', 'YAML formated parameters file (as generated by the dump command) containing parameters to override stack parameters') do |file| - opts[:params_file] = file - end - - o.on('-r REGION', '--region=REGION', "The region where the stack to be restacked exists") do |region| - opts[:region] = region - end - - o.on('-t PATH', '--template=PATH', 'Template file path to override current stack template') do |template| - opts[:template] = template - end - - o.on('-u USERNAME', '--username=USERNAME', 'Your username, defaults to $USER') do |username| - opts[:username] = username - end - - o.on('-v', '--verbose', 'Show more output') do |verbose| - opts[:verbose] = verbose - end - - o.on('-V', '--version', 'Display the version') do |version| - opts[:version] = version - end - end - - begin - return opts, opt_parser.parse!(options) - rescue OptionParser::InvalidOption => e - puts e.message - exit(1) - end - end -end - -def usage(msg) - puts msg, "\n" - Parser.parse %w[--help] -end - -# parse options -options, unparsed = Parser.parse(ARGV) - -# set the username to $USER if not specified -options[:username] = ENV['USER'] if options[:username].nil? - -begin - action = unparsed.pop - puts(VERSION) || exit(0) if options[:version] - usage("Please specify an ACTION") && exit(0) if action.nil? - - if ACTIONS.keys.push(:aws).include?(action.to_sym) - restacker = Restacker.new(options.to_h) unless ['dump'].include?(action) - case action - when 'list' - puts "Listing stacks" - restacker.list_stacks - when 'desc', 'describe' - if options[:name] - puts "Describing #{options[:name]}" - restacker.describe_stack(options[:name]) - else - usage "Please specify a stack name (-n) to describe" - end - when 'restack' - if options[:name] - puts "Restacking #{options[:name]}" - restacker.restack_by_name(options[:name]) - puts "Now run migrate followed by remove" - else - usage "Please specify a stack name (-n) to restack" - end - when 'deploy' - if options[:template] && options[:name] - restacker.deploy_stack(options[:template], options[:name]) - else - usage "Please specify stack name (-n) and template (-t) to deploy" - end - when 'migrate' - if options[:name] && options[:options] && options[:module] - puts "Migrating #{options[:name]}, #{options[:options]}, #{options[:module]}" - # restacker = Restacker.new(options.to_h) - # restacker.restack_by_name(stack_name) - else - usage "Please specify a stack name (-n), options (-o) and module (-m) to migrate" - end - when 'remove' - if options[:name] - puts "Removing stack" - restacker.delete_stack(options[:name]) - else - usage "Please specify a stack name (-n) to remove" - end - when 'dump' - if options[:template] - # puts "Dumping stack parameters" - Restacker.dump_stack_params(options) - else - usage "Please specify a stack template (-t) or module (-m)" - end - when 'console' - AwsCli.new(options).console(options) - when 'aws' - AwsCli.new(options).cmd(options[:params], options[:debug]) - end - else - usage "Unknown ACTION: #{action}" - end -rescue => e - puts options[:debug] - if options[:debug] - puts e.class - puts e.message - usage $@ - else - usage e.message - end -end diff --git a/source/lib/base_stacker.rb b/source/lib/base_stacker.rb index 2a2f838..fc171fb 100644 --- a/source/lib/base_stacker.rb +++ b/source/lib/base_stacker.rb @@ -21,7 +21,7 @@ class BaseStacker def initialize(options) @plane = options[:location] ? options[:location].to_sym : DEFAULT_PLANE config = RestackerConfig.load_config(@plane) - # use default regio if not passed in from cli + # use default region if not passed in from cli config[:region] = options[:region] if options[:region] options[:region] = config[:region] unless options[:region] @cf, @creds = Auth.login(options, config, @plane) diff --git a/source/lib/restacker.rb b/source/lib/restacker.rb index 5452f0c..512b85c 100644 --- a/source/lib/restacker.rb +++ b/source/lib/restacker.rb @@ -289,4 +289,51 @@ def self.dump_stack_params(options) end end end + + def self.configure(location) + puts "config file location: #{CONFIG_FILE}" + config = YAML.load_file(CONFIG_FILE) + if !config + puts "Make sure your ~/.restacker/restacker.yml file is configured. \n + See source/restacker-sample.yml for example." + exit(1) + end + target = config.fetch(location, {}).fetch(:target) + + new_account_number, new_role_name, new_role_prefix = "" + + old_account_number = target[:account_number].to_s + # old_account_number[0...7] = "********" + + print "Label [\"#{target[:label]}\"]: " + new_label = gets.chomp + + loop do + print "Account Number [#{old_account_number}]: " + + new_account_number = gets.chomp + break if (new_account_number =~ /\d{12,}/ || new_account_number.empty?) + end + + loop do + print "Role Name [#{target[:role_name]}]: " + new_role_name = gets.chomp + break if (new_role_name =~ /[\w&&\S\-]/ || new_role_name.empty?) + end + + loop do + print "Role Prefix [#{target[:role_prefix]}]: " + new_role_prefix = gets.chomp + break if (new_role_prefix =~ /[\w&&\S\-\/]/ || new_role_prefix.empty?) + end + + target[:label] = new_label.empty? ? target[:label] : new_label + target[:account_number] = new_account_number.empty? ? target[:account_number] : new_account_number + target[:role_name] = new_role_name.empty? ? target[:role_name] : new_role_name + target[:role_prefix] = new_role_prefix.empty? ? target[:role_prefix] : new_role_prefix + + File.open(CONFIG_FILE, 'w') do |f| + f.write config.to_yaml + end + end end diff --git a/source/lib/restacker_config.rb b/source/lib/restacker_config.rb index e8ce24d..0dc40c7 100644 --- a/source/lib/restacker_config.rb +++ b/source/lib/restacker_config.rb @@ -1,5 +1,3 @@ - - class RestackerConfig def self.load_config(plane) begin @@ -22,5 +20,4 @@ def self.load_config(plane) end config[plane] end - end