Skip to content
Snippets Groups Projects
Commit 9908d019 authored by Takumaru Sekine's avatar Takumaru Sekine Committed by Yas Naoi
Browse files

Issue #3262802 by sekinet, yas: Remove tmp directory name in K8s launch template form

parent 82716dbe
No related branches found
No related tags found
No related merge requests found
Showing
with 80 additions and 20 deletions
......@@ -1049,7 +1049,12 @@ class AwsCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
/**
* {@inheritdoc}
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string {
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string {
return '';
}
......
......@@ -854,7 +854,7 @@ function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityVie
FALSE,
$is_metrics_server,
FALSE,
str_contains($source_type, 'git')
str_contains($source_type, 'Git')
)
);
$build['k8s']['cloud_context'] = $entity->cloud_context->view();
......
......@@ -86,7 +86,8 @@ class YamlUrlConstraintValidator extends ConstraintValidator implements Containe
}
$plugin_manager = \Drupal::service('plugin.manager.cloud_launch_template_plugin');
$files_arr = [];
$error_message = $plugin_manager->validateGit($entity, $files_arr);
$tmp_dir_name = '';
$error_message = $plugin_manager->validateGit($entity, $files_arr, $tmp_dir_name, TRUE);
if (!empty($error_message)) {
$this->context
->buildViolation($error_message)
......
......@@ -346,7 +346,12 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
/**
* {@inheritdoc}
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string {
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string {
$git_url_value = $entity->get('field_git_repository')->getValue();
$git_url = $git_url_value[0]['uri'];
$git_branch_value = $entity->get('field_git_branch')->getValue();
......@@ -355,10 +360,10 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
$git_username = $entity->get('field_git_username')->value;
$git_access_token = $entity->get('field_git_access_token')->value;
$dir_name = $this->getTempDirectoryNameFromEntityId($entity);
$tmp_dir_name = $this->getTempDirectoryNameFromEntityId($entity);
// Make the temporary working directory.
$result = $this->fileSystem->mkdir($dir_name);
$result = $this->fileSystem->mkdir($tmp_dir_name);
$errors = [];
if (!$result) {
......@@ -375,26 +380,36 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
$return_var = 0;
exec(
!empty($git_branch)
? "git clone -b $git_branch $git_url $dir_name"
: "git clone $git_url $dir_name",
? "git clone -b $git_branch $git_url $tmp_dir_name"
: "git clone $git_url $tmp_dir_name",
$output,
$return_var
);
if ($return_var !== 0) {
if ($delete_tmp_dir) {
$this->fileSystem->deleteRecursive($tmp_dir_name);
}
return $this->t('Unable to clone the git repository.');
}
$match = '/.*/';
$files_arr = [];
try {
foreach ($git_paths ?: [] as $path) {
$files_arr[] = $this->fileSystem->scanDirectory($dir_name . $path['value'], $match);
$files_arr[] = $this->fileSystem->scanDirectory($tmp_dir_name . $path['value'], $match);
}
}
catch (NotRegularDirectoryException $e) {
if ($delete_tmp_dir) {
$this->fileSystem->deleteRecursive($tmp_dir_name);
}
return $this->t('Git resource path might not be correct.');
}
if ($delete_tmp_dir) {
$this->fileSystem->deleteRecursive($tmp_dir_name);
}
return '';
}
......@@ -473,10 +488,12 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
}
if (str_contains($source_type, 'git')) {
$files_arr = [];
$error_message = $this->validateGit($entity, $files_arr);
$tmp_dir_name = '';
$error_message = $this->validateGit($entity, $files_arr, $tmp_dir_name);
if (!empty($error_message)) {
$this->messenger->addError($error_message);
$this->handleError($form, $fieldsets_def);
$this->fileSystem->deleteRecursive($tmp_dir_name);
return;
}
......@@ -586,8 +603,8 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
]);
}
$dir_name = $this->getTempDirectoryNameFromEntityId($entity);
$directory = str_replace($dir_name, '', dirname($file->uri));
$tmp_dir_name = $tmp_dir_name ?: $this->getTempDirectoryNameFromEntityId($entity);
$directory = str_replace($tmp_dir_name, '', dirname($file->uri));
if (!isset($form['yaml_files'][$directory])) {
$form['yaml_files'][$directory] = [
......@@ -643,6 +660,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
'%label' => $entity->label(),
]));
$this->handleError($form, $fieldsets_def);
$this->fileSystem->deleteRecursive($tmp_dir_name);
return;
}
......@@ -677,7 +695,7 @@ class K8sCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLaunc
],
];
$this->fileSystem->deleteRecursive($dir_name);
$this->fileSystem->deleteRecursive($tmp_dir_name);
}
$resources = $entity->get('field_launch_resources')->getValue();
......
......@@ -642,7 +642,12 @@ class OpenStackLaunchTemplatePlugin extends CloudPluginBase implements CloudLaun
/**
* {@inheritdoc}
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string {
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string {
return '';
}
......
......@@ -384,7 +384,12 @@ class VmwareCloudLaunchTemplatePlugin extends CloudPluginBase implements CloudLa
/**
* {@inheritdoc}
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string {
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string {
return '';
}
......
......@@ -55,10 +55,19 @@ interface CloudLaunchTemplatePluginInterface {
* The cloud launch template entity.
* @param array $files_arr
* Path of the cloned files.
* @param string $tmp_dir_name
* Temporary directory to be created by validation.
* @param bool $delete_tmp_dir
* Whether to remove the temporary directory.
*
* @return string
* The error message.
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string;
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string;
}
......@@ -239,11 +239,20 @@ class CloudLaunchTemplatePluginManager extends CloudPluginManager implements Clo
* The cloud launch template entity.
* @param array $files_arr
* Path of the cloned files.
* @param string $tmp_dir_name
* Temporary directory to be created by validation.
* @param bool $delete_tmp_dir
* Whether to remove the temporary directory.
*
* @return string
* The error message.
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string {
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string {
$plugin = $this->loadPluginVariant($entity->getCloudContext());
if ($plugin === FALSE) {
return $this->t('Cannot load launch template plugin: %cloud_context', [
......@@ -255,8 +264,7 @@ class CloudLaunchTemplatePluginManager extends CloudPluginManager implements Clo
'%cloud_context' => $entity->getCloudContext(),
]);
}
$files_arr = [];
return $plugin->validateGit($entity, $files_arr);
return $plugin->validateGit($entity, $files_arr, $tmp_dir_name, $delete_tmp_dir);
}
/**
......
......@@ -101,10 +101,19 @@ interface CloudLaunchTemplatePluginManagerInterface extends PluginManagerInterfa
* The cloud launch template entity.
* @param array $files_arr
* Path of the cloned files.
* @param string $tmp_dir_name
* Temporary directory to be created by validation.
* @param bool $delete_tmp_dir
* Whether to remove the temporary directory.
*
* @return string
* The error message.
*/
public function validateGit(CloudLaunchTemplateInterface $entity, array &$files_arr): string;
public function validateGit(
CloudLaunchTemplateInterface $entity,
array &$files_arr,
string &$tmp_dir_name,
bool $delete_tmp_dir = FALSE
): string;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment