Last active
January 20, 2017 02:57
-
-
Save zetta/db3d95ec6a22a5733f0e0b9f20e451f1 to your computer and use it in GitHub Desktop.
my utils for aws stuff
This file contains 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
#! /usr/bin/php | |
<?php | |
require_once "util.php"; | |
$region = get_argument(1, 'ap-northeast-1'); | |
$currentVersions = $deprecatedVersions = []; | |
echo "[INFO] Fetching information about used versions...\n"; | |
// $applications = get_json("aws elasticbeanstalk describe-applications --region ${region} | jq '.Applications'"); | |
$environments = get_json("aws elasticbeanstalk describe-environments --region ${region} | jq '.Environments'"); | |
foreach ($environments as $environment) { | |
$currentVersions[] = $environment['VersionLabel']; | |
} | |
$versions = get_json("aws elasticbeanstalk describe-application-versions --region ${region} | jq '.ApplicationVersions'"); | |
foreach ($versions as $version) { | |
if (!in_array($version['VersionLabel'], $currentVersions)) { | |
$deprecatedVersions[] = $version; | |
} | |
} | |
usort($deprecatedVersions, function($a, $b) { | |
return $a['VersionLabel'] > $b['VersionLabel']; | |
}); | |
if (count($deprecatedVersions)) { | |
echo "[INFO] ".count($deprecatedVersions)." unused versions in ${region} were found \n"; | |
foreach ($deprecatedVersions as $version) { | |
echo "- ${version['VersionLabel']} \n"; | |
} | |
$continue = ask("do yo want delete this versions? [y/n]"); | |
if ('y' == $continue) { | |
foreach ($deprecatedVersions as $version) { | |
echo "[INFO] Deleting ${version['VersionLabel']} from ${version['ApplicationName']}... \n"; | |
execute(sprintf( | |
"aws elasticbeanstalk delete-application-version --application-name %s --version-label %s --delete-source-bundle --region %s", | |
$version['ApplicationName'], | |
$version['VersionLabel'], | |
$region | |
)); | |
} | |
} | |
} else { | |
echo "[INFO] Nothing to clear this time \n"; | |
} | |
echo "bye!\n"; |
This file contains 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
#! /usr/bin/php | |
<?php | |
require_once "util.php"; | |
$environment = translate_environment(get_argument(1, null)); | |
$region = guess_region($environment) ?: guess_region(get_argument(2)) ; | |
$remoteCommand = get_argument(2); | |
$remoteCommand = $remoteCommand == $region ? get_argument(3) : $remoteCommand; | |
if (!$environment) { | |
die("usage aws-go <environment> [region] [command]\n"); | |
} | |
$commands = [ | |
'tail' => 'tail -f /var/log/tomcat8/catalina.out -n 100', | |
'restart' => 'sudo service tomcat8 restart', | |
]; | |
if ($remoteCommand == 'restart') { | |
$command = "aws elasticbeanstalk restart-app-server --environment-name $environment --region $region"; | |
echo execute($command); | |
exit(0); | |
} | |
if ($remoteCommand && isset($commands[$remoteCommand])) { | |
$remoteCommand = $commands[$remoteCommand]; | |
} else if ($remoteCommand) { | |
echo "[NOTICE] NO COMMAND [$remoteCommand] found"; | |
} | |
if (preg_match('/i-[a-z0-9]{3,10}/', $environment)) { | |
$instanceId = $environment; | |
} else { | |
$command = "aws elasticbeanstalk describe-environment-resources --environment-name $environment --region $region"; | |
$env = json_decode(execute($command), true); | |
if (!$env) { | |
die("Cant find the environment $environment\n"); | |
} | |
if (count($env['EnvironmentResources']['Instances']) > 1) { | |
$instanceId = select_instance($env['EnvironmentResources']['Instances']); | |
} else { | |
$instanceId = $env['EnvironmentResources']['Instances'][0]['Id']; | |
} | |
} | |
$command = "aws ec2 describe-instances --instance-ids $instanceId --region $region"; | |
$instance = json_decode(execute($command), true); | |
$instance = $instance['Reservations'][0]['Instances'][0]; | |
$publicIp = isset($instance['PublicIpAddress']) ? $instance['PublicIpAddress'] : null; | |
// $publicIp = null; | |
// print_r($instance); | |
// die(); | |
$privateIp = isset($instance['PrivateIpAddress']) ? $instance['PrivateIpAddress'] : null; | |
if ($publicIp) { | |
$command = "ssh -v -i ~/.ssh/ciab.pem ec2-user@$publicIp"; | |
} else { | |
$jump = get_jump_server($region); | |
$proxyCommand = "ssh -v -p 443 -i ~/.ssh/id_rsa.cgg carlos.clemente@$jump nc $privateIp 22 "; | |
$command = "ssh -tt -o ProxyCommand='$proxyCommand' -i ~/.ssh/ciab.pem ec2-user@$privateIp"; | |
} | |
if ($remoteCommand) { | |
$command = "$command '$remoteCommand'"; | |
} | |
echo "[DEBUG] ", $command, "\n"; | |
passthru($command); | |
echo "bye!\n"; |
This file contains 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 | |
function get_argument($index, $default = null) { | |
global $argv; | |
return isset($argv[$index]) ? $argv[$index] : $default; | |
} | |
function translate_environment($environment) { | |
return str_replace('.', '-', strtr(strtr($environment, [ | |
'sg.' => 'singapore.', | |
'hk.' => 'hongkong.', | |
'tw.' => 'taiwan.', | |
'id.' => 'indonesia.', | |
'ph.' => 'philippines.', | |
'th.' => 'thailand.', | |
'my.' => 'malaysia.', | |
'be.' => 'belgium.', | |
'fi.' => 'finland.', | |
'no.' => 'norway.', | |
'dk.' => 'denmark.', | |
'pt.' => 'portugal.', | |
'mx.' => 'mexico.', | |
]),[ | |
'.prod' => '.production', | |
'.stage' => '.staging', | |
'.stg' => '.staging', | |
// '.st' => '.staging', | |
'.int' => '.integration', | |
])); | |
} | |
function select_instance($instances) { | |
while (!$id) { | |
$selected = ask("More than one instance is up, please select [1-".count($instances)."]"); | |
if (isset($instances[$selected-1])) { | |
$id = $instances[$selected-1]['Id']; | |
} | |
} | |
return $id; | |
} | |
function ask($question) { | |
echo "[QUESTION] ${question}: "; | |
return trim(fgets(STDIN)); | |
} | |
function guess_region($environment) { | |
list($country, $env) = explode('-', $environment); | |
$map = [ | |
'singapore' => 'ap-northeast-1', | |
'hongkong' => 'ap-northeast-1', | |
'taiwan' => 'ap-northeast-1', | |
'indonesia' => 'ap-northeast-1', | |
'philippines' => 'ap-northeast-1', | |
'thailand' => 'ap-northeast-1', | |
'malaysia' => 'ap-northeast-1', | |
'belgium' => 'eu-central-1', | |
'finland' => 'eu-central-1', | |
'norway' => 'eu-central-1', | |
'denmark' => 'eu-central-1', | |
'portugal' => 'eu-central-1', | |
'mexico' => 'us-west-1', | |
'ap' => 'ap-northeast-1', | |
'eu' => 'eu-central-1', | |
'us' => 'us-west-1', | |
]; | |
return $map[$country]; | |
} | |
function get_json($command) { | |
return json_decode(execute($command), true); | |
} | |
function execute($command) { | |
$buffer = []; | |
$output = ''; | |
echo "[DEBUG] ", $command, "\n"; | |
exec($command, $buffer); | |
foreach($buffer as $line) { | |
$output .= $line; | |
} | |
return $output; | |
} | |
function get_jump_server($region) { | |
$servers = [ | |
'ap-northeast-1' => 'jumpserver.compareglobal.co.uk', | |
'ap-southeast-1' => 'access.ap-southeast-1.compareglobal.co.uk', | |
'eu-central-1' => 'access.eu-central-1.compareglobal.co.uk', | |
'us-west-1' => 'access.us-west-1.compareglobal.co.uk', | |
]; | |
return $servers[$region]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remember to replace my username with yours (and maybe tha path)
Copy all 3 files into your home directory (~/bin/aws/)
Installation is easy as
usage
get the latest log
restart one environment
connect to another non-CIAB environment
$ aws-go ciab-config-server-boot ap-northeast-1 # if more than 2 instances are up the script will ask which instance we want to connect to
example
Clean up old application versions (we have a limit of 500 versions)
# aws-clean <region> $ aws-clean ap-northeast-1