forked from engineerOfLies/rabbitmqphp_example
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbundler.php
More file actions
159 lines (134 loc) · 4.8 KB
/
bundler.php
File metadata and controls
159 lines (134 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
function getBundleNameFromArgs($argv) {
if (count($argv) < 2) {
echo "Error: Provide a bundle name first.\n";
echo "Usage: php bundler.php (bundle_name)\n";
exit(1);
}
return $argv[1];
}
function calculateChecksums($directory) {
$checksums = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($iterator as $file) {
if ($file->isFile()) {
$filePath = $file->getPathname();
$checksums[$filePath] = hash_file('sha256', $filePath);
}
}
return $checksums;
}
function getModifiedFilesByChecksum($directory, $checksumFile) {
$modifiedFiles = [];
$currentChecksums = calculateChecksums($directory);
$previousChecksums = file_exists($checksumFile) ? json_decode(file_get_contents($checksumFile), true) : [];
foreach ($currentChecksums as $filePath => $currentChecksum) {
if (!isset($previousChecksums[$filePath]) || $currentChecksum !== $previousChecksums[$filePath]) {
$modifiedFiles[] = $filePath;
}
}
file_put_contents($checksumFile, json_encode($currentChecksums));
return $modifiedFiles;
}
function getDependencies($file) {
$dependencies = [];
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension === 'php') {
$content = file_get_contents($file);
preg_match_all('/(include|require|require_once)\s*[\'"](.*?)[\'"]/', $content, $matches);
$dependencies = array_merge($dependencies, $matches[2]);
}
if ($extension === 'ini') {
$content = parse_ini_file($file);
foreach ($content as $key => $value) {
if (is_string($value) && file_exists($value)) {
$dependencies[] = $value;
}
}
}
return array_filter($dependencies, 'file_exists');
}
function createBundle($files, $finalBundleName) {
$tempDir = '/tmp/bundle_temp/';
if (is_dir($tempDir)) {
exec("rm -rf {$tempDir}");
}
mkdir($tempDir, 0777, true);
foreach ($files as $file) {
$relativePath = str_replace('/home/matt/project/rabbitmqphp_example/', '', $file);
$dest = $tempDir . $relativePath;
$destDir = dirname($dest);
if (!is_dir($destDir)) {
mkdir($destDir, 0777, true);
}
copy($file, $dest);
}
$bundlePath = "/tmp/{$finalBundleName}.tar.gz";
$command = "tar -czvf $bundlePath -C $tempDir .";
exec($command, $output, $returnVar);
exec("rm -rf {$tempDir}");
if ($returnVar === 0) {
echo "Bundle created: $bundlePath\n";
return $bundlePath;
} else {
echo "Error creating bundle.\n";
return null;
}
}
function getNextVersionAndRegister($bundleName, $remotePath) {
$client = new rabbitMQClient("testRabbitMQ.ini", "deploymentMQ");
$request = [
'type' => 'get_version_and_register',
'bundle_name' => $bundleName,
'path' => $remotePath
];
$response = $client->send_request($request);
if ($response['status'] === 'success') {
return $response['version'];
} else {
echo "Error registering bundle: " . $response['message'] . "\n";
exit(1);
}
}
function transferBundleWithSCP($bundlePath, $remoteServer, $remotePath, $username, $sshKey) {
$command = "scp -i $sshKey $bundlePath $username@$remoteServer:$remotePath";
exec($command, $output, $returnVar);
if ($returnVar === 0) {
echo "Bundle successfully transferred to $remoteServer.\n";
return true;
} else {
echo "Failed to transfer bundle: " . implode("\n", $output) . "\n";
return false;
}
}
$bundleName = getBundleNameFromArgs($argv);
// change directory for other vms
$projectDirectory = '/home/matt/project/rabbitmqphp_example';
$checksumFile = '/etc/deploy/checksums.json';
$modifiedFiles = getModifiedFilesByChecksum($projectDirectory, $checksumFile);
if (empty($modifiedFiles)) {
echo "No modified files found.\n";
exit;
}
$allFiles = $modifiedFiles;
foreach ($modifiedFiles as $file) {
$dependencies = getDependencies($file);
$allFiles = array_merge($allFiles, $dependencies);
}
$allFiles = array_unique($allFiles);
$remoteServer = '192.168.194.182';
$remotePath = '/var/deploy/bundles/';
$username = 'omarh';
$sshKey = '../../.ssh/id_rsa';
$version = getNextVersionAndRegister($bundleName, $remotePath);
$finalBundleName = "{$bundleName}_v{$version}";
$finalPath = "{$remotePath}{$finalBundleName}.tar.gz";
$bundlePath = createBundle($allFiles, $finalBundleName);
if ($bundlePath) {
if (transferBundleWithSCP($bundlePath, $remoteServer, $remotePath, $username, $sshKey)) {
echo "Bundle successfully registered and transferred: {$finalBundleName}\n";
}
}