| 1 | package CloudDeploy::CommandLine::Undeploy { | ||
| 2 | use MooseX::App; | ||
| 3 | use CCfnX::Deployment; | ||
| 4 | use CloudDeploy::Utils; | ||
| 5 | use CloudDeploy::DeploymentCollection; | ||
| 6 | use Term::ANSIColor; | ||
| 7 | use Moose::Util::TypeConstraints qw/enum/; | ||
| 8 | |||
| 9 | parameter deployment => (is => 'ro', isa => 'Str'); | ||
| 10 | option comments => (is => 'ro', isa => 'Str|Undef', documentation => "Comments to include with deployment"); | ||
| 11 | option mode => ( | ||
| 12 | is => 'ro', | ||
| 13 | isa=> enum([ qw/cfn deployment_engine/ ]), | ||
| 14 | documentation => "Whether to activate the new deployment engine or not. Needed for all non CloudFormation related deployments. One of: cfn, deployment_engine", | ||
| 15 | default=>'cfn' | ||
| 16 | ); | ||
| 17 | |||
| 18 | sub run { | ||
| 19 | my ($self) = @_; | ||
| 20 | |||
| 21 | if($self->mode eq 'cfn'){ | ||
| 22 | my $deployer = CCfnX::Deployment->new_with_roles( | ||
| 23 | { name => $self->deployment, | ||
| 24 | account => $ENV{CPSD_AWS_ACCOUNT}, | ||
| 25 | access_key => $ENV{AWS_ACCESS_KEY_ID}, | ||
| 26 | secret_key => $ENV{AWS_SECRET_ACCESS_KEY}, | ||
| 27 | }, | ||
| 28 | 'CCfnX::CloudFormationDeployer', 'CCfnX::PersistentDeployment' | ||
| 29 | ); | ||
| 30 | |||
| 31 | # Initialize deployment from MongoDB | ||
| 32 | $deployer->get_from_mongo; | ||
| 33 | $deployer->comments($self->comments); | ||
| 34 | |||
| 35 | # Do not remove the eval! This is required to catch exceptions when stack does not exist on AWS. | ||
| 36 | # If you cancel a deployment or are waiting for a stack deletion, at one point the stack won't | ||
| 37 | # exist and we need to catch the exception to carry on anyway without dying. | ||
| 38 | my $stack; | ||
| 39 | eval { | ||
| 40 | $stack = $deployer->cfn->DescribeStacks(StackName => $deployer->name)->Stacks->[0]; | ||
| 41 | }; | ||
| 42 | |||
| 43 | if (defined $stack) { | ||
| 44 | my $resources; | ||
| 45 | eval { | ||
| 46 | $resources = $deployer->cfn->ListStackResources(StackName => $deployer->name); | ||
| 47 | }; | ||
| 48 | if ($@){ | ||
| 49 | die "CloudFormation threw an error trying to describe stack resources. Review stack status and try again"; | ||
| 50 | } | ||
| 51 | |||
| 52 | print color('bold red'), | ||
| 53 | "\nWARNING: You are about to delete ALL resources from this stack on AWS!\n\n", | ||
| 54 | color('reset'); | ||
| 55 | print color('bold yellow'), "Resources included in this stack:\n\n"; | ||
| 56 | |||
| 57 | foreach my $resource (@{ $resources->StackResourceSummaries }) { | ||
| 58 | print $resource->LogicalResourceId, " (", $resource->ResourceType, ")\n"; | ||
| 59 | } | ||
| 60 | |||
| 61 | print color('bold red'), "\nType the stack name to confirm you want to delete ALL resources: "; | ||
| 62 | chomp(my $input=<STDIN>); | ||
| 63 | print color('reset'), "\n"; | ||
| 64 | |||
| 65 | if ($deployer->name ne $input) { | ||
| 66 | die "Name not matched. Aborting delete stack.\n"; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | $deployer->undeploy; | ||
| 71 | } elsif ($self->mode eq 'deployment_engine') { | ||
| 72 | |||
| 73 | my $module = load_class($self->deployment); | ||
| 74 | my $obj = $module->{class}->new(params => $module->{params_class}->new_with_options(argv => $self->extra_argv)); | ||
| 75 | my $deployer = $obj->get_deployer({ | ||
| 76 | account => $ENV{CPSD_AWS_ACCOUNT}, | ||
| 77 | },'CCfnX::DeploymentEngine'); | ||
| 78 | |||
| 79 | $deployer->undeploy; | ||
| 80 | } | ||
| 81 | |||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |