Class: CfnManage::CloudFormation::EnvironmentRunStop

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_manage/cf_start_stop_environment.rb

Constant Summary collapse

DEFAULT_PRIORITIES =
{
  'AWS::RDS::DBInstance' => '100',
  'AWS::RDS::DBCluster' => '100',
  'AWS::DocDB::DBCluster' => '100',
  'AWS::AutoScaling::AutoScalingGroup' => '200',
  'AWS::EC2::Instance' => '200',
  'AWS::EC2::SpotFleet' => '200',
  'AWS::Transfer::Server' => '200',
  'AWS::ECS::Cluster' => '250',
  'AWS::CloudWatch::Alarm' => '300'
}
TAGGED_RESOURCES =
%w(
  AWS::AutoScaling::AutoScalingGroup
  AWS::EC2::Instance
  AWS::ECS::Cluster
)

Instance Method Summary collapse

Constructor Details

#initializeEnvironmentRunStop

Returns a new instance of EnvironmentRunStop.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cfn_manage/cf_start_stop_environment.rb', line 34

def initialize()
  @environment_resources = []
  @s3_client = Aws::S3::Client.new(retry_limit: 20)
  @s3_bucket = ENV['SOURCE_BUCKET']
  @cf_client = Aws::CloudFormation::Client.new(retry_limit: 20)
  @credentials = CfnManage::AWSCredentials.get_session_credentials('start_stop_environment')
  if not @credentials.nil?
    @cf_client = Aws::CloudFormation::Client.new(credentials: @credentials, retry_limit: 20)
  end
rescue NoMethodError => e
  puts "Got No Method Error on CloudFormation::initialize, this often means that you're missing a AWS_DEFAULT_REGION"
rescue Aws::Sigv4::Errors::MissingCredentialsError => e
  puts "Got Missing Credentials Error on CloudFormation::initialize, this often means that AWS_PROFILE is unset, or no default credentials were provided."
end

Instance Method Details

#start_environment(stack_name) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/cfn_manage/cf_start_stop_environment.rb', line 50

def start_environment(stack_name)
  $log.info("Starting environment #{stack_name}")
  Common.visit_stack(@cf_client, stack_name, method(:collect_resources), true)
  do_start_assets
  configuration = { stack_running: true }
  save_item_configuration("environment_data/stack-#{stack_name}", configuration) unless CfnManage.dry_run?
  $log.info("Environment #{stack_name} started")
end

#start_resource(resource_id, resource_type) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cfn_manage/cf_start_stop_environment.rb', line 69

def start_resource(resource_id,resource_type)
  priority = DEFAULT_PRIORITIES[resource_type]
  options = {}
  
  if CfnManage.find_tags?
    tags = CfnManage::TagFinder.new(resource_id)
    tags.get_tags(resource_type)  
    priority = !tags.priority.nil? ? tags.priority : priority
    options = tags.options
    $log.debug("setting options #{options} for #{resource_id}")
  end
  
  start_stop_handler = CfnManage::StartStopHandlerFactory.get_start_stop_handler(
      resource_type,
      resource_id,
      options
  )
  
  @environment_resources << {
      id: resource_id,
      priority: priority,
      handler: start_stop_handler,
      type: resource_type
  }
  do_start_assets
end

#stop_environment(stack_name) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/cfn_manage/cf_start_stop_environment.rb', line 60

def stop_environment(stack_name)
  $log.info("Stopping environment #{stack_name}")
  Common.visit_stack(@cf_client, stack_name, method(:collect_resources), true)
  do_stop_assets
  configuration = { stack_running: false }
  save_item_configuration("environment_data/stack-#{stack_name}", configuration) unless CfnManage.dry_run?
  $log.info("Environment #{stack_name} stopped")
end

#stop_resource(resource_id, resource_type) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cfn_manage/cf_start_stop_environment.rb', line 96

def stop_resource(resource_id,resource_type)
  priority = DEFAULT_PRIORITIES[resource_type]
  options = {}
  
  if CfnManage.find_tags?
    tags = CfnManage::TagFinder.new(resource_id)
    tags.get_tags(resource_type)  
    priority = !tags.priority.nil? ? tags.priority : priority
    options = tags.options
  end
  
  start_stop_handler = CfnManage::StartStopHandlerFactory.get_start_stop_handler(
      resource_type,
      resource_id,
      options
  )

  @environment_resources << {
      id: resource_id,
      priority: priority,
      handler: start_stop_handler,
      type: resource_type
  }
  do_stop_assets
end