| 1 | package CCfnX::PackerDeployer { | ||
| 2 | use Moose::Role; | ||
| 3 | use JSON; | ||
| 4 | |||
| 5 | after deploy => sub { | ||
| 6 | my $self = shift; | ||
| 7 | |||
| 8 | my @lines = @{ $self->origin->Resource('Instance')->Properties->UserData->process_with_context($self->origin) }; | ||
| 9 | @lines = @{ $self->origin->Resource('Instance')->Properties->UserData->process_with_context($self->origin, \@lines) }; | ||
| 10 | chomp @lines; | ||
| 11 | |||
| 12 | my $att_name = 'Bucket'; | ||
| 13 | my $att = $self->origin->params->meta->find_attribute_by_name($att_name); | ||
| 14 | my $aws_access_key = $att->get_info($self->origin->params->$att_name, 'appbucket/codeuser/accesskey'); | ||
| 15 | my $aws_secret_key = $att->get_info($self->origin->params->$att_name, 'appbucket/codeuser/secretkey'); | ||
| 16 | |||
| 17 | my $packer_def = { | ||
| 18 | builders => [ { type => "docker", image => "ubuntu", export_path => "image.tar" } ], | ||
| 19 | provisioners => [ { type => "shell", inline => [ | ||
| 20 | '#!/bin/bash', | ||
| 21 | "export AWS_ACCESS_KEY_ID=$aws_access_key", | ||
| 22 | "export AWS_SECRET_ACCESS_KEY=$aws_secret_key", | ||
| 23 | "cat > /etc/skel/.aws_creds <<ALIASES", | ||
| 24 | "export AWS_ACCESS_KEY_ID=$aws_access_key", | ||
| 25 | "export AWS_SECRET_ACCESS_KEY=$aws_secret_key", | ||
| 26 | "ALIASES", | ||
| 27 | @lines, | ||
| 28 | |||
| 29 | ] | ||
| 30 | } | ||
| 31 | ] | ||
| 32 | }; | ||
| 33 | |||
| 34 | open (my $file, '>', 'packer.config'); | ||
| 35 | print $file encode_json($packer_def); | ||
| 36 | close $file; | ||
| 37 | |||
| 38 | #system 'packer','build'; | ||
| 39 | }; | ||
| 40 | |||
| 41 | before redeploy => sub { | ||
| 42 | die "PackerDeployer cannot redeploy yet. Please deploy again"; | ||
| 43 | }; | ||
| 44 | |||
| 45 | before undeploy => sub { | ||
| 46 | die "PackerDeployer cannot undeploy yet. Please manually delete resources"; | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; |