Net-FullAuto

 view release on metacpan or  search on metacpan

lib/Net/FullAuto/Cloud/fa_amazon.pm  view on Meta::CPAN

         };
      }
      {
         $SIG{CHLD}="DEFAULT";
         my $cmd="aws configure";
         use IO::Pty;
         my $pty = IO::Pty->new;
         my $slave = $pty->slave;
         $pty->slave->set_raw();
         $pty->set_raw();
         my $pid = fork(); die "bad fork: $!\n" unless defined $pid;
         if (!$pid) {
            $pty->close();
            $pty->make_slave_controlling_terminal();
            open( STDIN,  ">&", $slave ) or die "Couldn't dup stdin:  $!";
            open( STDOUT, ">&", $slave ) or die "Couldn't dup stdout: $!";
            open( STDERR, ">&", $slave ) or die "Couldn't dup stderr: $!";
            exec $cmd;
         } else {
            $pty->close_slave();
            my $line='';
            while ( !$pty->eof ) {
               while (defined($_ = $pty->getc)) {
                  $line.=$_;
                  if ($line=~/Access Key ID \[None\]:\s*$/) {
                     for (1..length $line) {
                        $pty->ungetc(ord);
                     }
                     $line='';
                     $pty->print("$main::aws->{access_id}\n");
                  } elsif ($line=~/Secret Access Key \[None\]:\s*$/) {
                     for (1..length $line) {
                        $pty->ungetc(ord);
                     }
                     $line='';
                     $pty->print("$main::aws->{secret_key}\n");
                  } elsif ($line=~/Default region name \[None\]:\s*$/) {
                     for (1..length $line) {
                        $pty->ungetc(ord);
                     }
                     $line='';
                     $pty->print("$region\n");
                  } elsif ($line=~/Default output format \[None\]:\s*$/) {
                     for (1..length $line) {
                         $pty->ungetc(ord);
                     }
                     $pty->print("\n");
                  }
               }
            }
            wait();
         }

         #cleanup pty for next run
         $pty->close();
         system("${sudo}cp -R $homedir/.aws /home/$username")
            unless $homedir eq "/home/$username";
         my $group=$username;
         $group='Administrators' if $username eq 'Administrator';
         if ($username eq 'root') {
            system("${sudo}chown -R $username:$group /$username/.aws");
            system("${sudo}chmod 755 /$username/.aws");
         } else {
            system("${sudo}chown -R $username:$group /home/$username/.aws");
            system("${sudo}chmod 755 /home/$username/.aws");
         }
      }
   };
   if ($handle) {
      $handle->print('aws configure');
      my $prompt=$handle->prompt();
      while (1) {
         my $output=Net::FullAuto::FA_Core::fetch($handle);
         last if $output=~/$prompt/;
         print $output;
         if ($output=~/Access Key ID \[None\]:\s*$/) {
            $handle->print($main::aws->{access_id});
         } elsif ($output=~/Secret Access Key \[None\]:\s*$/) {
            $handle->print($main::aws->{secret_key});
         } elsif ($output=~/Default region name \[None\]:\s*$/) {
            $handle->print($region);
         } elsif ($output=~/Default output format \[None\]:\s*$/) {
            $handle->print("\n");
         } sleep 1;
      }
      my $group=$handle_username;
      #$handle->cmd($sudo.
      #   "cp -Rv $homedir/.aws /home/$handle_username",'__display__');
      $group='Administrators' if $handle_username eq 'Administrator';
      if ($handle_username eq 'root') {
         $handle->cmd($sudo.
            "chown -Rv $handle_username:$group /$handle_username/.aws",
            '__display__');
         $handle->cmd($sudo.
            "chmod -v 755 /$handle_username/.aws",'__display__');
      } else {
         $handle->cmd($sudo.
            "chown -Rv $handle_username:$group /home/$handle_username/.aws",
            '__display__');
         $handle->cmd($sudo.
            "chmod -v 755 /home/$handle_username/.aws",'__display__');
      }
   }
};

my $configure_aws2=sub {

   package configure_aws2;

   my $aws_access_key_id = $_[0]||'';
   my $aws_secret_access_key = $_[1]||'';

   my $banner=<<'END';

     ___              _            _                      _  __
    / __|_ _ ___ __ _| |_ ___     /_\  __ __ ___ ______  | |/ /___ _  _ ___
   | (__| '_/ -_) _` |  _/ -_)   / _ \/ _/ _/ -_|_-<_-<  | ' </ -_) || (_-<
    \___|_| \___\__,_|\__\___|  /_/ \_\__\__\___/__/__/  |_|\_\___|\_, /__/
                                                                   |__/
END
   $banner.=<<END;
                  .-------------------. 
   When you click | Create access key | and the Access key ID and Secret
                  '-------------------'
   access key strings will be displayed. You will not have access to the
   secret access key again after the dialog box closes.

   Copy & Paste [SHIFT]+[INS] the Access key ID & Secret access key here:


   Access key ID                                       Use [TAB] key to switch
                      ]I[{1,"$aws_access_key_id",30} focus of input boxes

   Secret access key
                      ]I[{2,"$aws_secret_access_key",55}

END

   my %configure_aws2=(

      Name => 'configure_aws2',
      Input  => 1,
      Banner => $banner,
      Result => $Net::FullAuto::Cloud::fa_amazon::aws_configure,

   );
   return \%configure_aws2;

};

our $configure_aws=sub {

   my $aws_access_key_id = $_[0]||'';
   my $aws_secret_access_key = $_[1]||'';
   my $handle = $_[2]||'';
   if ($handle) { $main::handle=$handle };
 
   my $banner=<<'END';



( run in 1.528 second using v1.01-cache-2.11-cpan-71847e10f99 )