#!/usr/bin/env php
register('update-binaries')
->setDescription("Install or update RoadRunner binaries in specified folder (current folder by default)")
->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.')
->setCode(function (InputInterface $input, OutputInterface $output) {
$output->writeln('Updating binary file of RoadRunner');
$finalFile = $input->getOption('location') . DIRECTORY_SEPARATOR . 'rr';
if (is_file($finalFile)) {
$output->writeln('RoadRunner binary file already exists!');
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Do you want overwrite it? [Y/n] ');
if (!$helper->ask($input, $output, $question)) {
return;
}
}
$output->writeln('Downloading RoadRunner archive for ' . RoadRunnerCLIHelper::getOSType() . '');
$progressBar = new ProgressBar($output);
$progressBar->setFormat('verbose');
$progressBar->start();
$zipFileName = tempnam('.', "rr_zip");
$zipFile = fopen($zipFileName, "w+");
$curlResource = curl_init();
curl_setopt($curlResource, CURLOPT_URL, RoadRunnerCLIHelper::getBinaryDownloadUrl());
curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlResource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlResource, CURLOPT_FILE, $zipFile);
curl_setopt($curlResource, CURLOPT_PROGRESSFUNCTION,
function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($progressBar) {
if ($download_size == 0) {
return;
}
$progressBar->setFormat('[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% ' . intval($download_size / 1024) . 'KB');
$progressBar->setMaxSteps($download_size);
$progressBar->setProgress($downloaded);
});
curl_setopt($curlResource, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($curlResource, CURLOPT_HEADER, 0);
curl_exec($curlResource);
curl_close($curlResource);
fclose($zipFile);
$progressBar->finish();
$output->writeln("");
$output->writeln('Unpacking ' . basename(RoadRunnerCLIHelper::getBinaryDownloadUrl()) . '');
$zipArchive = new ZipArchive();
$zipArchive->open($zipFileName);
$fileStreamFromZip = $zipArchive->getStream(
'roadrunner-' . RoadRunnerCLIHelper::getVersion() . '-' . RoadRunnerCLIHelper::getOSType() . '-amd64/rr'
);
$finalFileResource = fopen($finalFile, 'w');
if (!$fileStreamFromZip) {
throw new Exception('Unable to extract the file.');
}
while (!feof($fileStreamFromZip)) {
fwrite($finalFileResource, fread($fileStreamFromZip, 8192));
}
fclose($fileStreamFromZip);
fclose($finalFileResource);
$zipArchive->extractTo('.', []);
$zipArchive->close();
unlink($zipFileName);
chmod($finalFile, 0755);
$output->writeln('Binary file updated!');
})
->getApplication()
->register("init-config")
->setDescription("Inits default .rr.yaml config in specified folder (current folder by default)")
->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.')
->setCode(function (InputInterface $input, OutputInterface $output) {
if (is_file($input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml')) {
$output->writeln('Config file already exists!');
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Do you want overwrite it? [Y/n] ');
if (!$helper->ask($input, $output, $question)) {
return;
}
}
copy(
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.rr.yaml',
$input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml'
);
$output->writeln('Config file created!');
})
->getApplication()
->run();