Last active
January 13, 2020 15:01
-
-
Save craigh411/913259e5b1207eec96bc1b4b4df30f98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use App\Ami; | |
use Carbon\Carbon; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Log; | |
use Symfony\Component\Process\Exception\ProcessFailedException; | |
use Symfony\Component\Process\Process; | |
class CreateNewAmiCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'create:ami {instanceId} {name}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Creates a new Amazon Machine Image for ECB2 instance'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$instanceId = $this->argument('instanceId'); | |
$name = $this->argument('name') . '-' . Carbon::now()->timestamp; | |
$process = new Process(sprintf( | |
'aws ec2 create-image --instance-id %s --name "%s" --description "%s"', | |
$instanceId, | |
$name, | |
$name . ' AMI' | |
)); | |
try { | |
$process->run(); | |
if ($process->isSuccessful()) { | |
$output = json_decode($process->getOutput(), true); | |
$imageId = $output['ImageId']; | |
Ami::create([ | |
'imageId' => $imageId, | |
'name' => $name, | |
'status' => 'pending', | |
]); | |
} | |
} catch (ProcessFailedException $e) { | |
Log::error('AMI Creation Failed' . $e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment