Skip to content

Instantly share code, notes, and snippets.

@dranney-sugarcrm
Created July 18, 2017 22:56
Show Gist options
  • Save dranney-sugarcrm/ff0449c6f85573434e272a102c13cb08 to your computer and use it in GitHub Desktop.
Save dranney-sugarcrm/ff0449c6f85573434e272a102c13cb08 to your computer and use it in GitHub Desktop.
<?php
/**
* copy_prod_sqs_to_dev.php
*
* Now that heartbeats are sent to an SQS queue it's a lot harder to get test data. The dev SQS queue occasionally
* gets stuff from test on-demand instances. If you're trying to do testing that can mean waiting around for hours
* until maybe something comes in.
*
* This script simply takes everything currently in the production SQS queue and copies it to the dev SQS queue.
*
* @link IAPPS-7168
* @author dranney
* @since 2017-07-18
*
*/
define('sugarEntry', true);
require_once ('include/entryPoint.php');
global $current_user;
$current_user = new User();
$current_user->getSystemUser();
require_once('vendor/aws/aws-sdk-php/src/Aws/Sqs/SqsClient.php');
class CopyHeartbeatsFromProdToDev {
/**
* Define what Encryption method we are using
*/
const ENC_METHOD = "aes-256-cbc";
public $dev_connection_arr = array(
'key' => "******",
'secret' => "*******",
'queue' => "updates_hbsync_queue",
'region' => "us-west-1",
'decrypt_key' => "*******",
'max_messages' => 10, // Max number of messages to pull at a time from the SQS Queue (10 is the highest)
'process_limit' => 100,
);
public $prod_connection_arr = array(
'key' => "********",
'secret' => "*********",
'queue' => "updates_hbsync_queue",
'region' => "us-west-1",
'decrypt_key' => "**********",
'max_messages' => 10, // Max number of messages to pull at a time from the SQS Queue (10 is the highest)
'process_limit' => 100,
);
function __construct() {
$this->dev_connection_arr['client'] = $this->getHBConnection($this->dev_connection_arr);
$this->dev_connection_arr['queueURL'] = $this->dev_connection_arr['client']->getQueueUrl(array('QueueName' => $this->dev_connection_arr['queue']));
$this->prod_connection_arr['client'] = $this->getHBConnection($this->prod_connection_arr);
$this->prod_connection_arr['queueURL'] = $this->prod_connection_arr['client']->getQueueUrl(array('QueueName' => $this->prod_connection_arr['queue']));
}
/**
* This method establishes a connection to the heartbeat SQS queue.
*
* @link IAPPS-2865
* @since 2015-02-11
* @author dranney
* @param void
* @return void
*/
public function getHBConnection($in_config) {
$outSQSClient = "";
$this->outMessage(
"About to create an sqsClient with:\n"
. "Key: " . $in_config['key'] . "\n"
. "Secret: " . $in_config['secret'] . "\n"
. "Queue: " . $in_config['queue'] . "\n"
. "Region: " . $in_config['region']
);
$outSQSClient = \AWS\Sqs\SqsClient::factory(
array(
'key' => $in_config['key'],
'secret' => $in_config['secret'],
'queue' => $in_config['queue'],
'region' => $in_config['region'],
)
);
return $outSQSClient;
}
/**
* This method performs the actual copy operation
* @link IAPPS-7168
* @author dranney
* @since 2017-07-18
* @param void
* @return void
*/
public function doCopy() {
$num_heartbeats = 0;
$num_tries = 0;
$max_tries = 5; // After this many tries getting nothing back, stop pulling messages
$done = false;
while (!$done) {
$message = $this->prod_connection_arr['client']->receiveMessage(
array(
'QueueUrl' => $this->prod_connection_arr['queueURL']->get('QueueUrl'),
'MaxNumberOfMessages' => $this->prod_connection_arr['max_messages'],
'AttributeNames' => array(
'SentTimestamp',
),
)
);
if (count($message->get("Messages")) > 0) {
// We got messages off the queue
$num_tries = 0;
foreach ($message->get("Messages") as $cur_message_arr) {
$raw_message = base64_decode($cur_message_arr['Body']);
list($value, $iv) = explode(":^:", $raw_message);
$prod_json_str = openssl_decrypt($value, self::ENC_METHOD, $this->prod_connection_arr['decrypt_key'], OPENSSL_RAW_DATA, $iv);
$new_iv = $this->generateIv();
$dev_encrypted = openssl_encrypt($prod_json_str, self::ENC_METHOD, $this->dev_connection_arr['decrypt_key'], OPENSSL_RAW_DATA, $new_iv);
$params = array(
'MessageBody' => $dev_encrypted,
'QueueUrl' => $this->dev_connection_arr['queueURL']->get('QueueUrl'),
);
$this->dev_connection_arr['client']->sendMessage($params);
$num_heartbeats++;
if ($num_heartbeats >= $this->process_limit) {
// We now have the max number of heartbeats we want to process for this run
$done = true;
break;
}
}
} else {
// We didn't get any messages from this receive call.
// It's possible there are more in the queue and AWS just didn't get any this time.
$num_tries++;
if ($num_tries > $max_tries) {
// We've tried to get messages a number of times in a row and haven't gotten anything, so we're done here.
$done = true;
}
}
}
print "Copied " . $num_heartbeats . " heartbeats from prod to dev\n";
}
/**
* This method simply writes a message out to the screen in a standard format
* if we're in verbose mode.
* @author dranney
* @since 2015-02-25
* @link IAPPS-2865
* @param string - $in_message - A message to display
* @return void
*/
public function outMessage($in_message) {
if ($this->verbose) {
print date("Y-m-d H:i:s") . " : HeartbeatCollection: " . $in_message . "\n";
}
}
/**
* Generate a random IV string to use in the encryption method
*
* @return string
*/
public function generateIv()
{
return openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::ENC_METHOD));
}
}
$copyObj = new CopyHeartbeatsFromProdToDev();
$copyObj->doCopy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment