#!/usr/bin/env ruby require 'optparse' require 'cfn_manage/version' require 'cfn_manage/cf_common' require 'cfn_manage/globals' require 'cfn_manage/cf_start_stop_environment' require 'logger' # exit with usage information def print_usage_exit(code) STDERR.puts(File.open("#{File.expand_path(File.dirname(__FILE__))}/usage.txt").read) exit code end # global options $options = {} $options['SOURCE_BUCKET'] = ENV['SOURCE_BUCKET'] $options['AWS_ASSUME_ROLE'] = ENV['AWS_ASSUME_ROLE'] # global logger $log = Logger.new(STDOUT) $log.level = Logger::INFO # always flush output STDOUT.sync = true # parse command line options OptionParser.new do |opts| Version = CfnManage::VERSION opts.banner = 'Usage: cfn_manage [command] [options]' opts.on('--source-bucket [BUCKET]') do |bucket| $options['SOURCE_BUCKET'] = bucket ENV['SOURCE_BUCKET'] = bucket end opts.on('--aws-role [ROLE]') do |role| ENV['AWS_ASSUME_ROLE'] = role end opts.on('--stack-name [STACK_NAME]') do |stack| $options['STACK_NAME'] = stack end opts.on('--asg-name [ASG]') do |asg| $options['ASG'] = asg end opts.on('--rds-instance-id [RDS_INSTANCE_ID]') do |rds| $options['RDS_INSTANCE_ID'] = rds end opts.on('--aurora-cluster-id [AURORA_CLUSTER_ID]') do |cluster| $options['AURORA_CLUSTER_ID'] = cluster end opts.on('--docdb-cluster-id [DOCDB_CLUSTER_ID]') do |cluster| $options['DOCDB_CLUSTER_ID'] = cluster end opts.on('--transfer-server-id [TRANSFER_SERVER_ID]') do |server| $options['TRANSFER_SERVER_ID'] = server end opts.on('--ec2-instance-id [EC2_INSTANCE_ID]') do |ec2| $options['EC2_INSTANCE_ID'] = ec2 end opts.on('--spot-fleet-id [SPOT_FLEET]') do |spot| $options['SPOT_FLEET'] = spot end opts.on('--ecs-cluster [ECS_CLUSTER]') do |ecs| $options['ECS_CLUSTER'] = ecs end opts.on('--alarm [ALARM]') do |alarm| $options['ALARM'] = alarm end opts.on('--asg-wait-state [WAIT_STATE]') do |state| allows_values = ['HealthyInASG','Running','HealthyInTargetGroup'] if !allows_values.include? state STDERR.puts("#{type} is not a valid value for `--asg-wait-state`. Use one of #{allows_values.join(',')}") exit 1 end CfnManage.asg_wait_state = state end opts.on('--ecs-wait-state [WAIT_STATE]') do |state| allows_values = ['Running','HealthyInTargetGroup'] if !allows_values.include? state STDERR.puts("#{type} is not a valid value for `--ecs-wait-state`. Use one of #{allows_values.join(',')}") exit 1 end CfnManage.ecs_wait_state = state end opts.on('-r [AWS_REGION]', '--region [AWS_REGION]') do |region| ENV['AWS_REGION'] = region end opts.on('-p [AWS_PROFILE]', '--profile [AWS_PROFILE]') do |profile| ENV['CFN_AWS_PROFILE'] = profile end # Boolean options with support for command line and environment variables opts.on('--dry-run') do CfnManage.dry_run end if ENV['DRY_RUN'] == '1' CfnManage.dry_run end opts.on('--debug') do $log.level = Logger::DEBUG end if ENV['CFN_DEBUG'] == '1' $log.level = Logger::DEBUGß end opts.on('--continue-on-error') do CfnManage.continue_on_error end if ENV['CFN_CONTINUE_ON_ERROR'] == '1' CfnManage.continue_on_error end opts.on('--wait-async') do CfnManage.skip_wait CfnManage.wait_async end if ENV['WAIT_ASYNC'] == '1' CfnManage.skip_wait CfnManage.wait_async end opts.on('--skip-wait') do CfnManage.skip_wait end if ENV['SKIP_WAIT'] == '1' CfnManage.skip_wait end opts.on('--ignore-missing-ecs-config') do CfnManage.ignore_missing_ecs_config end if ENV['IGNORE_MISSING_ECS_CONFIG'] == '1' CfnManage.ignore_missing_ecs_config end opts.on('--asg-suspend-termination') do CfnManage.asg_suspend_termination end if ENV['ASG_SUSPEND_TERMINATION'] == '1' CfnManage.asg_suspend_termination end opts.on('--tags') do CfnManage.find_tags end if ENV['CFN_TAGS'] == '1' CfnManage.find_tags end opts.on('--ecs-wait-container-instances') do CfnManage.ecs_wait_container_instances end if ENV['ECS_WAIT_CONTAINER_INSTANCES'] == '1' CfnManage.ecs_wait_container_instances end end.parse! command = ARGV[0] if command.nil? print_usage_exit(-1) end # execute action based on command case command when 'help' print_usage_exit(0) # asg commands when 'stop-asg' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['ASG'],'AWS::AutoScaling::AutoScalingGroup') when 'start-asg' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['ASG'],'AWS::AutoScaling::AutoScalingGroup') # rds commands when 'stop-rds' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['RDS_INSTANCE_ID'],'AWS::RDS::DBInstance') when 'start-rds' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['RDS_INSTANCE_ID'],'AWS::RDS::DBInstance') # aurora cluster commands when 'stop-aurora-cluster' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['AURORA_CLUSTER_ID'],'AWS::RDS::DBCluster') when 'start-aurora-cluster' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['AURORA_CLUSTER_ID'],'AWS::RDS::DBCluster') # docdb cluster commands when 'stop-docdb-cluster' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['DOCDB_CLUSTER_ID'],'AWS::DocDB::DBCluster') when 'start-docdb-cluster' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['DOCDB_CLUSTER_ID'],'AWS::DocDB::DBCluster') # docdb cluster commands when 'stop-transfer-server' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['TRANSFER_SERVER_ID'],'AWS::Transfer::Server') when 'start-transfer-server' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['TRANSFER_SERVER_ID'],'AWS::Transfer::Server') # ec2 instance when 'stop-ec2' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['EC2_INSTANCE_ID'],'AWS::EC2::Instance') when 'start-ec2' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['EC2_INSTANCE_ID'],'AWS::EC2::Instance') # spot fleet when 'stop-spot-fleet' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['SPOT_FLEET'],'AWS::EC2::SpotFleet') when 'start-spot-fleet' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['SPOT_FLEET'],'AWS::EC2::SpotFleet') # spot fleet when 'stop-ecs-cluster' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['ECS_CLUSTER'],'AWS::ECS::Cluster') when 'start-ecs-cluster' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['ECS_CLUSTER'],'AWS::ECS::Cluster') # cloudwatch alarm when 'disable-alarm' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_resource($options['ALARM'],'AWS::CloudWatch::Alarm') when 'enable-alarm' CfnManage::CloudFormation::EnvironmentRunStop.new().start_resource($options['ALARM'],'AWS::CloudWatch::Alarm') # stack commands when 'stop-environment' CfnManage::CloudFormation::EnvironmentRunStop.new().stop_environment($options['STACK_NAME']) when 'start-environment' CfnManage::CloudFormation::EnvironmentRunStop.new().start_environment($options['STACK_NAME']) end