* working version

This commit is contained in:
David Danyi
2020-10-15 13:02:54 +02:00
parent 2cf81c493e
commit 11a8f4dc2b
39 changed files with 1904 additions and 86 deletions

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Runner\SSH;
use Exception;
use Zend\Http\Client;
class ArmImageDownloader
{
const ARM_URL = 'https://arm.rnd.ki.sw.ericsson.se/artifactory';
const ARM_IMAGE_PATH = '/proj-mtas-release-local/CXP9025660_9/%1$s/';
const IMAGE_NAME = '4_CXP9025660_9-%1$s.ova';
/** @var Client */
private $httpClient;
/** @var ConfigProvider */
private $configProvider;
public function __construct(Client $httpClient, ConfigProvider $appConfig)
{
$this->httpClient = $httpClient;
$this->configProvider = $appConfig;
}
public function downloadImage(string $rState): bool
{
$config = $this->configProvider->getTaskConfig()['config'];
$uri = self::ARM_URL . self::ARM_IMAGE_PATH . self::IMAGE_NAME;
$response = $this->httpClient->setStream(sprintf(self::IMAGE_NAME, $rState))
->setUri(sprintf($uri, $rState))
->setAuth($config['arm-user'], $config['arm-token'])
->send()
;
return $response->isSuccess();
}
public function downloadImageToRemote(string $host, string $path, string $rState): bool
{
$ssh = new SSH($host);
$ssh->execute("mkdir -p ${path}/${rState}");
$imageUrl = sprintf(self::ARM_URL . self::ARM_IMAGE_PATH . self::IMAGE_NAME, $rState);
$imageFile = sprintf(self::IMAGE_NAME, $rState);
$ssh->execute("curl ${imageUrl} --silent --output ${path}/${rState}/${imageFile}");
return true;
}
public function isRemoteImageMissing(string $host, string $path, string $rState): bool
{
$ssh = new SSH($host);
try {
$ssh->execute("ls -1 ${path}/${rState}/" . sprintf(self::IMAGE_NAME, $rState));
} catch (Exception $_) {
return true;
}
return false;
}
public function uncompressImage(string $host, string $path, string $rState): bool
{
$ssh = new SSH($host);
$imageFile = sprintf(self::IMAGE_NAME, $rState);
$imageDir = "${path}/${rState}";
$targetDir = "${imageDir}/uncompressed";
$ssh->execute("rm -rf ${targetDir}");
$ssh->execute("mkdir -p ${targetDir}");
$ssh->execute("tar xf ${imageDir}/$imageFile -C ${targetDir}");
return true;
}
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Interop\Container\ContainerInterface;
use Zend\Http\Client;
class ArmImageDownloaderFactory
{
public function __invoke(ContainerInterface $container): ArmImageDownloader
{
$client = new Client();
/** @var ConfigProvider $appConfig */
$appConfig = $container->get(ConfigProvider::class);
return new ArmImageDownloader($client, $appConfig);
}
}

View File

@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Runner\SSH;
use Exception;
use Zend\Json\Json;
class AtlasManager
{
const GLANCE_LIST = "openstack image list -f json";
const GLANCE_UPLOAD = "openstack image create --format json --public --disk-format qcow2 --file %s %s";
const IMAGE_NAME_BASE = "MTASv_4_CXP9031366-%s";
/** @var array */
private $envConfig;
public function __construct(array $envConfig)
{
$this->envConfig = $envConfig;
}
private static function openstackCommand(string $cmd): string
{
return sprintf("source openrc && %s", $cmd);
}
/**
* @param SSH $ssh
* @return array|null
* @throws Exception
*/
public function listImages(SSH $ssh): ?array
{
$glanceOutput = $ssh->execute(self::openstackCommand(self::GLANCE_LIST));
return Json::decode($glanceOutput, Json::TYPE_ARRAY);
}
/**
* @param SSH $ssh
* @param string $imageName
* @return bool
* @throws Exception
*/
public function isImageInGlance(SSH $ssh, string $imageName): bool
{
$glanceImages = $this->listImages($ssh);
$filtered = array_filter($glanceImages, function($record) use ($imageName) {
return $record['Name'] == $imageName;
});
$count = count($filtered);
if ($count > 1) {
throw new Exception("Multiple images exist with the same name, can't decide on what to use");
}
return $count == 1;
}
/**
* @param SSH $ssh
* @param string $imageFile
* @param string $dst
* @throws Exception
*/
public function copyImageToAtlas(SSH $ssh, string $imageFile, string $dst)
{
$ssh->execute("scp -q ${imageFile} ${dst}");
}
/**
* @param SSH $ssh
* @param string $imageName
* @return bool
*/
public function uploadImageToGlance(SSH $ssh, string $imageName): bool
{
try {
$imageFile = sprintf("/tmp/%s.qcow2", $imageName);
$out = $ssh->execute(self::openstackCommand(sprintf(
self::GLANCE_UPLOAD,
$imageFile,
$imageName
)));
return true;
} catch (Exception $_) {
return false;
}
}
/**
* @param string $host
* @param string $imageDir
* @param string $rState
* @param string $node
* @throws Exception
*/
public function ensureImageIsInGlance(string $host, string $imageDir, string $rState, string $node)
{
$vimCfg = $this->envConfig['node'][$node]['vim'];
$workerSSH = new SSH($host);
$atlasSSH = new SSH($vimCfg['ssh-alias']);
$imageName = sprintf(self::IMAGE_NAME_BASE, $rState);
if ($this->isImageInGlance($atlasSSH, $imageName)) {
return;
}
$imageSourceFile = sprintf("${imageDir}/${rState}/uncompressed/" . self::IMAGE_NAME_BASE . ".qcow2", $rState);
$atlasFileName = sprintf("/tmp/" . self::IMAGE_NAME_BASE . ".qcow2", $rState);
$imageDestinationFile = sprintf(
"%s@%s:${atlasFileName}",
$vimCfg['user'],
$vimCfg['address']
);
$this->copyImageToAtlas($workerSSH, $imageSourceFile, $imageDestinationFile);
$this->uploadImageToGlance($atlasSSH, $imageName);
$atlasSSH->execute("rm -rf ${atlasFileName}");
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Interop\Container\ContainerInterface;
class AtlasManagerFactory
{
public function __invoke(ContainerInterface $container): AtlasManager
{
/** @var ConfigProvider $appConfig */
$appConfig = $container->get(ConfigProvider::class);
$envConfig = $appConfig->getEnvironmentConfig();
return new AtlasManager($envConfig);
}
}

View File

@@ -4,24 +4,123 @@ declare(strict_types=1);
namespace App\Service;
use App\Command\SharedStorage;
use App\Runner\SSH;
use Doctrine\Common\Collections\ArrayCollection;
use Exception;
class Builder
{
const PACKAGE_BUILDER_SCRIPT = "prepare_package.sh";
use SharedStorage;
const WORKFLOW_PACKAGE_BUILDER_SCRIPT = "prepare_package.sh";
const VNF_PACKAGE_CREATOR_SCRIPT = "vnfPackageCreator_mtas.py";
const FULL_PKG = "CXP9034815_1";
const WF_PGK = "CXP9034788_1";
const VNF_PGK_REPO_PATH = "/vnflcm-ext/current/vnf_package_repo";
/** @var ConfigProvider */
private $configProvider;
public function __construct(ConfigProvider $configProvider, ArrayCollection $sharedStorage)
{
$this->configProvider = $configProvider;
$this->sharedStorage = $sharedStorage;
}
/**
* @param $host
* @param $path
* @return string Generated filename on remote host
* @param SSH $ssh
* @param string $path
* @param string $branch
* @throws Exception
*/
public function buildRemotePackage($host, $path): string
private function checkoutBranch(SSH $ssh, string $path, string $branch)
{
$ssh->execute("cd ${path} && git fetch origin && git checkout ${branch}");
}
/**
* @param string $host
* @param string $path
* @param string|null $branch
* @return string
* @throws Exception
*/
public function buildRemotePackage(string $host, string $path, ?string $branch = null): string
{
$runner = new SSH($host);
$out = $runner->execute(sprintf("%s/%s", $path, self::PACKAGE_BUILDER_SCRIPT));
if ($branch) {
$this->checkoutBranch($runner, $path, $branch);
}
$out = $runner->execute(sprintf("%s/%s", $path, self::WORKFLOW_PACKAGE_BUILDER_SCRIPT));
$lines = explode("\n", trim($out));
return array_pop($lines);
}
/**
* @param string $host
* @param string $imageStore
* @param string $rState
* @param string $node
* @param string $package
* @return string
* @throws Exception
*/
public function createOnboardPackage(string $host, string $imageStore, string $rState, string $node, string $package): string
{
$swDirectory = "${imageStore}/${rState}/uncompressed";
$ssh = new SSH($host);
$workDir = trim($ssh->execute("mktemp -d"));
$this->saveToShared("tmp-dir", $workDir);
$packageName = basename($package);
preg_match(sprintf("/%s-(R[0-9]+[A-Z]+[0-9]+)\.tar\.gz/", self::FULL_PKG), $packageName, $matches);
$rState = $matches[1];
$ssh->execute("mv ${package} ${workDir}");
$ssh->execute("tar -xzf ${workDir}/${packageName} -C ${workDir}");
$ssh->execute(sprintf("tar -xzf ${workDir}/%s-${rState}.tar.gz -C ${workDir}", self::WF_PGK));
try {
$ssh->execute("ls -l ${swDirectory}/prepareHot.bash");
} catch(Exception $_) {
return $ssh->execute(sprintf(
"cd ${workDir} && ./%s -sw %s -hf %s -wf . -i %s 2>&1",
self::VNF_PACKAGE_CREATOR_SCRIPT,
$swDirectory,
sprintf("$swDirectory/%s/%s", TemplateGenerator::GENERATED_HOT_DIR, TemplateGenerator::APP_STACK_DIR),
$node
));
}
$cmd = sprintf(
"cd ${workDir} && ./%s -sw %s -wf . -i %s 2>&1",
self::VNF_PACKAGE_CREATOR_SCRIPT,
$swDirectory,
$node
);
var_dump($cmd);
try {
return $ssh->execute($cmd);
} catch (Exception $_) {
throw new Exception("Couldn't create on-board package! Mismatching Workflow-MTAS packages?");
}
}
/**
* @param string $host
* @param string $node
* @param string $package
* @throws Exception
*/
public function onboardPackage(string $host, string $node, string $package): void
{
$envConfig = $this->configProvider->getEnvironmentConfig();
$lcmConfig = $envConfig['node'][$node]['vnf-lcm'];
$lcmUri = sprintf("%s@%s", $lcmConfig['user'], $lcmConfig['address']);
$pkgName = basename($package);
$runner = new SSH($host);
$sshOptions = "-o StrictHostKeyChecking=no";
$runner->execute(sprintf("scp $sshOptions $package $lcmUri:%s/", self::VNF_PGK_REPO_PATH));
$runner->execute(sprintf("ssh $sshOptions $lcmUri 'cd %s && unzip -o $pkgName'", self::VNF_PGK_REPO_PATH));
// $runner->execute(sprintf("ssh $sshOptions $lcmUri 'rm -f %s/$pkgName'", self::VNF_PGK_REPO_PATH));
}
}

View File

@@ -4,12 +4,17 @@ declare(strict_types=1);
namespace App\Service;
use Doctrine\Common\Collections\ArrayCollection;
use Interop\Container\ContainerInterface;
class BuilderFactory
{
public function __invoke(ContainerInterface $container): Builder
{
return new Builder();
/** @var ConfigProvider $configProvider */
$configProvider = $container->get(ConfigProvider::class);
/** @var ArrayCollection $sharedStorage */
$sharedStorage = $container->get(ArrayCollection::class);
return new Builder($configProvider, $sharedStorage);
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Runner\SSH;
use Exception;
class Cleanup
{
/** @var ConfigProvider */
private $configProvider;
public function __construct(ConfigProvider $configProvider)
{
$this->configProvider = $configProvider;
}
/**
* @param $host
* @param $imageStore
* @param $rState
* @param $tmpDir
* @throws Exception
*/
public function cleanup($host, $imageStore, $rState, $tmpDir)
{
$ssh = new SSH($host);
$ssh->execute("rm -rf ${imageStore}/${rState}/uncompressed");
$ssh->execute("rm -rf ${tmpDir}");
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Interop\Container\ContainerInterface;
class CleanupFactory
{
public function __invoke(ContainerInterface $container): Cleanup
{
/** @var ConfigProvider $configProvider */
$configProvider = $container->get(ConfigProvider::class);
return new Cleanup($configProvider);
}
}

View File

@@ -47,18 +47,18 @@ class ConfigProvider
throw new InvalidArgumentException("Couldn't locate $filename");
}
private function readConfigFile($configFile): ?iterable
private function readConfigFile($configFile): ?array
{
$configFile = $this->findConfigFile($configFile);
return Yaml::parseFile($configFile);
}
public function getEnvironmentConfig(): ?iterable
public function getEnvironmentConfig(): ?array
{
return $this->readConfigFile($this->envFile);
}
public function getTaskConfig(): ?iterable
public function getTaskConfig(): ?array
{
return $this->readConfigFile($this->taskFile);
}

View File

@@ -0,0 +1,160 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Runner\SSH;
use Exception;
use Symfony\Component\Yaml\Yaml;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
class TemplateGenerator
{
const LEGACY_GENERATOR_SCRIPT = "prepareHot.bash";
const JINJA_GENERATOR_SCRIPT = "hotgen.py";
const CONTEXT_TEMPLATE_FILE = "hotgen_context.yaml";
const FILLED_CONTEXT_FILE = "filled_hotgen_context.yaml";
const GENERATED_HOT_DIR = "hot_files";
const APP_STACK_DIR = "02_vApp_stack";
const LEGACY_ENV_FILE = "mtas_hot_environment_site_specific_data.yaml";
const JINJA_ENV_FILE = "env.yaml";
/** @var array */
private $envConfig;
/** @var array */
private $taskConfig;
public function __construct(array $envConfig, array $taskConfig)
{
$this->envConfig = $envConfig;
$this->taskConfig = $taskConfig;
}
/**
* @param string $template
* @param array $data
* @return string
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function fillJinjaTemplate(string $template, array $data): string
{
$twig = new Environment(new ArrayLoader(['env' => $template]));
return $twig->render('env', $data);
}
/**
* @param SSH $ssh
* @param string $envFile
* @param string $node
* @param string $key
* @throws Exception
*/
private function fillEnv(SSH $ssh, string $envFile, string $node, string $key)
{
$fileContents = $ssh->readFile($envFile);
$unfilledEnv = Yaml::parse($fileContents);
$mergedConfig = $this->envConfig['node'][$node]['env']['shared'] + $this->envConfig['node'][$node]['env'][$key];
foreach ($unfilledEnv['parameters'] as $key => &$value) {
if (array_key_exists($key, $mergedConfig)) {
$value = $mergedConfig[$key];
}
}
$ssh->saveFile($envFile, Yaml::dump($unfilledEnv, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE));
}
/**
* @param SSH $ssh
* @param string $envFile
* @param string $node
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws Exception
*/
private function prefillJinjaEnv(SSH $ssh, string $envFile, string $node)
{
$unfilledEnv = $ssh->readFile($envFile);
$data = $this->envConfig['node'][$node]['env']['shared'] + $this->envConfig['node'][$node]['env']['jinja'];
$filledTemplate = $this->fillJinjaTemplate($unfilledEnv, $data);
$ssh->saveFile($envFile, $filledTemplate);
}
/**
* @param SSH $ssh
* @param string $workDir
* @param string $node
* @throws Exception
*/
private function generateLegacyTemplates(SSH $ssh, string $workDir, string $node)
{
$params = [];
foreach($this->envConfig['node'][$node]['template-params']['legacy'] as $k => $v) {
$params[] = "$k $v";
}
$ssh->execute(sprintf(
"cd ${workDir} && ./%s %s",
self::LEGACY_GENERATOR_SCRIPT,
implode(" ", $params)
));
$this->fillEnv(
$ssh,
sprintf("$workDir/%s", self::LEGACY_ENV_FILE),
$node,
'legacy'
);
}
/**
* @param SSH $ssh
* @param string $workDir
* @param string $node
* @throws Exception
*/
private function generateJinjaTemplates(SSH $ssh, string $workDir, string $node)
{
$fileContents = $ssh->readFile(sprintf("%s/%s", $workDir, self::CONTEXT_TEMPLATE_FILE));
$filledContext = $this->fillJinjaTemplate(
$fileContents,
$this->envConfig['node'][$node]['template-params']['jinja']
);
$ssh->saveFile("${workDir}/" . self::FILLED_CONTEXT_FILE, $filledContext);
$ssh->execute(sprintf(
"${workDir}/%s $workDir/%s $workDir/%s",
self::JINJA_GENERATOR_SCRIPT,
self::GENERATED_HOT_DIR,
self::FILLED_CONTEXT_FILE));
$envFile = sprintf("$workDir/%s/%s/%s", self::GENERATED_HOT_DIR, self::APP_STACK_DIR, self::JINJA_ENV_FILE);
$this->prefillJinjaEnv($ssh, $envFile, $node);
$this->fillEnv($ssh, $envFile, $node, 'jinja');
}
/**
* @param string $host
* @param string $imageDir
* @param string $rState
* @param string $node
* @throws Exception
*/
public function generateTemplates(string $host, string $imageDir, string $rState, string $node)
{
$workDir = "${imageDir}/${rState}/uncompressed";
$ssh = new SSH($host);
try {
$ssh->execute("ls -1 $workDir/" . self::LEGACY_GENERATOR_SCRIPT);
$this->generateLegacyTemplates($ssh, $workDir, $node);
} catch (Exception $_) {
$this->generateJinjaTemplates($ssh, $workDir, $node);
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Interop\Container\ContainerInterface;
class TemplateGeneratorFactory
{
public function __invoke(ContainerInterface $container): TemplateGenerator
{
/** @var ConfigProvider $configProvider */
$configProvider = $container->get(ConfigProvider::class);
$envConfig = $configProvider->getEnvironmentConfig();
$taskConfig = $configProvider->getTaskConfig();
return new TemplateGenerator($envConfig, $taskConfig);
}
}