$aws = \Aws\Common\Aws::factory($awsConfig); /** * Test SQS connectivity */ /** @var \Aws\Sqs\SqsClient $sqsClient */ $sqsClient = $aws->get('sqs'); $queueUrl = $sqsConfig['amiBuildRequestQueueUrl']; try { echo "1. Checking for SQS connectivity\n\n"; echo "Attempting receiveMessage call on SQS '{$queueUrl}'...\n"; $work = $sqsClient->receiveMessage(["QueueUrl" => $queueUrl, "MaxNumberOfMessages" => 1, "VisibilityTimeout" => 1, "MessageAttributeNames" => []]); echo "Was able to make receiveWork call on SQS '{$queueUrl}' without error\n\n"; } catch (Exception $e) { shutDown("Error when attempting to access and/or receive work from SQS: '{$queueUrl}'" . ". Error message: {$e->getMessage()}"); } $cli = new \Io\Samk\AmiBuilder\Utils\Cli($logger, ["patterns" => ['/(aws_access_key)=([\\w\\+]+)(.*)/', '/(aws_secret_key)=([\\w\\+]+)(.*)/'], "replacements" => '$1=<$1>$3']); /** * Test Github connectivity */ try { echo "2. Checking git hub template repo connectivity\n\n"; $templatesConfig = $config->get('templates', true); $templatesRepo = $templatesConfig['templatesRepo']; $templatesCheckoutPath = $templatesConfig['checkoutPath']; if (!file_exists($templatesCheckoutPath)) { echo "Template repo not found locally, attempting Clone of repo '{$templatesRepo}' to local dir '{$templatesCheckoutPath}'...\n"; list($output, $returnCode) = $cli->execute("git clone {$templatesRepo} {$templatesCheckoutPath}"); checkCliResponse($output, $returnCode); } else { echo "Local checkout of template repo '{$templatesRepo}', exists at '{$templatesCheckoutPath}', attempting git fetch...\n"; list($output, $returnCode) = $cli->execute('git fetch --all', $templatesCheckoutPath);
} $messagePayload = array_change_key_case($messagePayload, CASE_LOWER); $template = ltrim(isset($messagePayload['templatename']) ? $messagePayload['templatename'] : null, ' /'); $templateSha = isset($messagePayload['sha']) ? $messagePayload['sha'] : null; if (!$template || !$templateSha) { $message = "Not all required work message attributes found and/or had empty values. " . "templateName : '{$template}', sha: '{$templateSha}' "; $logger->error($message); shutDown($message); } $executionMetrics['jobMessage'] = $message; // a lot of unneeded characters for digest message unset($executionMetrics['jobMessage']['ReceiptHandle']); $executionMetrics['jobBuildTemplate'] = $template; $executionMetrics['jobBuildTemplateSha'] = $templateSha; $logger->info("Found Work, request for template '{$template}' @ SHA '{$templateSha}'"); $cli = new \Io\Samk\AmiBuilder\Utils\Cli($logger, ["patterns" => ['/(aws_access_key)=([\\w\\+]+)(.*)/', '/(aws_secret_key)=([\\w\\+]+)(.*)/'], "replacements" => '$1=<$1>$3']); /** * update template checkout */ $templatesConfig = $config->get('templates'); $templatesRepo = $templatesConfig['templatesRepo']; $templatesCheckoutPath = $templatesConfig['checkoutPath']; if (!file_exists($templatesCheckoutPath)) { list($output, $returnCode) = $cli->execute("git clone {$templatesRepo} {$templatesCheckoutPath}"); } $logger->info("checking out local packer templates repo '{$templatesCheckoutPath}' to SHA '{$templateSha}'"); list($output, $returnCode) = $cli->execute('git fetch --all', $templatesCheckoutPath); list($output, $returnCode) = $cli->execute("git reset --hard origin/master", $templatesCheckoutPath); list($output, $returnCode) = $cli->execute("git checkout {$templateSha}", $templatesCheckoutPath); /** * Run packer and capture image region and name