Skip to content

Instantly share code, notes, and snippets.

@nicdev
Last active August 16, 2021 19:09
Show Gist options
  • Select an option

  • Save nicdev/26c5fc722d2141763ffcd6477a6a42c2 to your computer and use it in GitHub Desktop.

Select an option

Save nicdev/26c5fc722d2141763ffcd6477a6a42c2 to your computer and use it in GitHub Desktop.
Artisan command to update Vapor (AWS) secrets in bulk
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class UpdateSecrets extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'secrets:update {env=staging} {file=.secrets} {--deploy}';
protected $tempFile = 'tempsecret';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update AWS secrets in bulk via Vapor';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$secretsFile = $this->argument('file');
$env = $this->argument('env');
$this->info("Processing file {$secretsFile}");
$secretPairs = preg_split("/\r|\n/", file_get_contents($secretsFile), -1, PREG_SPLIT_NO_EMPTY);
$progressBar = $this->output->createProgressBar(count($secretPairs));
foreach($secretPairs as $sp) {
$secretParts = explode('=', $sp);
file_put_contents($this->tempFile, $secretParts[1] . PHP_EOL);
exec("vapor secret {$env} --name={$secretParts[0]} --file={$this->tempFile}");
$progressBar->advance();
}
unlink($this->tempFile);
$progressBar->finish();
$this->info("{$secretsFile} finished processing");
if($this->option('deploy')) {
$this->info("Deploying application {$this->argument('env')}");
exec("vapor deploy {$env}");
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment