| 1 | package CCfnX::VagrantDeployer { | ||
| 2 | use Moose::Role; | ||
| 3 | use Paws; | ||
| 4 | use Paws::Credential::ProviderChain; | ||
| 5 | |||
| 6 | after deploy => sub { | ||
| 7 | my $self = shift; | ||
| 8 | |||
| 9 | my @lines = @{ $self->origin->Resource('Instance')->Properties->UserData->process_with_context($self->origin) }; | ||
| 10 | @lines = @{ $self->origin->Resource('Instance')->Properties->UserData->process_with_context($self->origin, \@lines) }; | ||
| 11 | |||
| 12 | my $content = join '', @lines; | ||
| 13 | $content =~ s/\\/\\\\/; | ||
| 14 | |||
| 15 | warn "I'm extending a 900 second (15 min) token with you're actual credentials to vagrant"; | ||
| 16 | |||
| 17 | my $creds = Paws::Credential::ProviderChain->new(); | ||
| 18 | |||
| 19 | my $aws_access_key = $creds->access_key; | ||
| 20 | my $aws_secret_key = $creds->secret_key; | ||
| 21 | my $aws_token = $creds->session_token; | ||
| 22 | |||
| 23 | my $vag_file = ' | ||
| 24 | VAGRANTFILE_API_VERSION = "2" | ||
| 25 | |||
| 26 | $script = <<SCRIPT | ||
| 27 | #!/bin/bash | ||
| 28 | # Create temporary credentials | ||
| 29 | export AWS_ACCESS_KEY_ID=' . $aws_access_key . ' | ||
| 30 | export AWS_SECRET_ACCESS_KEY=' . $aws_secret_key . ' | ||
| 31 | export AWS_SESSION_TOKEN=' . $aws_token . ' | ||
| 32 | cat > /etc/skel/.aws_creds <<ALIASES | ||
| 33 | export AWS_ACCESS_KEY_ID=' . $aws_access_key . ' | ||
| 34 | export AWS_SECRET_ACCESS_KEY=' . $aws_secret_key . ' | ||
| 35 | export AWS_SESSION_TOKEN=' . $aws_token . ' | ||
| 36 | ALIASES | ||
| 37 | ' . $content . ' | ||
| 38 | # Clean temporary credentials | ||
| 39 | rm -f /etc/skel/.aws_creds | ||
| 40 | find /home -type f -name ".aws_creds" -delete | ||
| 41 | SCRIPT | ||
| 42 | |||
| 43 | Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | ||
| 44 | config.vm.box = "' . $self->params->{ami} . '" | ||
| 45 | config.vm.synced_folder ".", "/vagrant", disabled: true | ||
| 46 | config.vm.network "private_network", type: "dhcp" | ||
| 47 | config.vm.provider :virtualbox do |vb| | ||
| 48 | vb.gui = true | ||
| 49 | vb.customize ["modifyvm", :id, "--memory", "1024"] | ||
| 50 | end | ||
| 51 | config.vm.provision "shell", inline: $script | ||
| 52 | end | ||
| 53 | '; | ||
| 54 | |||
| 55 | open (my $file, '>', 'Vagrantfile'); | ||
| 56 | print $file $vag_file; | ||
| 57 | close $file; | ||
| 58 | |||
| 59 | #print $vag_file; | ||
| 60 | #system 'vagrant','up'; | ||
| 61 | }; | ||
| 62 | |||
| 63 | before redeploy => sub { | ||
| 64 | die "VagrantDeployer cannot redeploy yet. Please deploy again"; | ||
| 65 | }; | ||
| 66 | |||
| 67 | before undeploy => sub { | ||
| 68 | system 'vagrant','destroy','-f'; | ||
| 69 | #die "VagrantDeployer cannot undeploy yet. Please manually delete resources"; | ||
| 70 | }; | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | 1; |