Skip to content
Snippets Groups Projects
Commit 2f18cbd4 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3283635 by Xiaohua Guan, yas, baldwinlouie: Deploy Cloud Orchestrator...

Issue #3283635 by Xiaohua Guan, yas, baldwinlouie: Deploy Cloud Orchestrator by Cloud Cluster (CFn) (cloud_cluster_launch_form)
parent 847a1c07
No related branches found
No related tags found
No related merge requests found
Showing
with 198 additions and 22 deletions
......@@ -425,3 +425,14 @@ function cloud_cluster_update_9015(): void {
];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud_cluster');
}
/**
* Update the field_deployment_type of cloud server template.
*/
function cloud_cluster_update_9016(): void {
$files = [
'field.storage.cloud_launch_template.field_deployment_type.yml',
];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'cloud_cluster');
}
cloud_cluster_launch_template_radios:
cloud_cluster_launch_template_form:
version: 5.x-dev
js:
js/launch_template_form.js: {}
css:
theme:
css/cloud_cluster_launch_template_radios.css: {}
dependencies:
- core/jquery
......@@ -14,6 +14,8 @@ use Drupal\cloud_cluster\Entity\CloudClusterWorker;
use Drupal\cloud_cluster\Plugin\rest\resource\CloudClusterWorkerResource;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
......@@ -494,7 +496,12 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_form_common_alte
'callback' => 'cloud_cluster_ajax_callback_get_fields',
];
$form['#attached']['library'][] = 'cloud_cluster/cloud_cluster_launch_template_radios';
$launch_template = $form_state->getFormObject()->buildEntity($form, $form_state);
$templates = cloud_cluster_get_deployment_templates_by_type($launch_template->field_deployment_type->value);
$form['#attached']['library'][] = 'cloud_cluster/cloud_cluster_launch_template_form';
$form['#attached']['drupalSettings']['cloud_cluster']['field_deployment_template_default_values']
= array_keys($templates);
}
/**
......@@ -509,17 +516,45 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_form_common_alte
* Returns the data back as AjaxResponse object.
*/
function cloud_cluster_ajax_callback_get_fields(array $form, FormStateInterface $form_state): AttachmentsInterface {
$deployment_type = $form['cloud_cluster']['field_deployment_type']['widget']['#value'];
// Field deployment template.
$field_deployment_template = $form['cloud_cluster']['field_deployment_template'];
$field_deployment_template['#id'] = 'edit-field-deployment-template-wrapper';
$field_deployment_template['widget']['#options'] = cloud_cluster_get_deployment_templates_by_type($deployment_type);
$templates = cloud_cluster_get_deployment_templates_by_type($deployment_type);
$field_deployment_template['widget']['#options'] = $templates;
// Remove radio controls which should be hidden.
foreach (array_keys($field_deployment_template['widget']) as $key) {
if (strpos($key, '#') === 0) {
continue;
}
if (empty($templates[$key])) {
unset($field_deployment_template['widget'][$key]);
}
}
$response = new AjaxResponse();
$response->addCommand(
new ReplaceCommand('#edit-field-deployment-template-wrapper',
\Drupal::service('renderer')->render($field_deployment_template))
new ReplaceCommand(
'#edit-field-deployment-template-wrapper',
\Drupal::service('renderer')->render($field_deployment_template)
)
);
$response->addCommand(
new CssCommand(
'.field--name-field-deployment-template',
['display' => 'block']
)
);
$response->addCommand(
new InvokeCommand(
NULL,
'initOptions',
[]
)
);
return $response;
......@@ -581,25 +616,38 @@ function cloud_cluster_launch_template_field_orders(): array {
* @see options_allowed_values()
*/
function cloud_cluster_deployment_template_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable): array {
return cloud_cluster_get_deployment_templates_by_type($entity->field_deployment_type->value);
return cloud_cluster_get_deployment_templates_by_type(NULL);
}
/**
* Get deployment templates according to deployment type.
*
* @param string $deployment_type
* @param string|null $deployment_type
* The deployment type.
*
* @return array
* An array of deployment templates.
*/
function cloud_cluster_get_deployment_templates_by_type(string $deployment_type): array {
function cloud_cluster_get_deployment_templates_by_type(?string $deployment_type): array {
$templates = [];
if ($deployment_type === 'k8s') {
$templates = [
'amd64' => t('Template for AMD64'),
'arm64' => t('Template for ARM64'),
];
$template_path = realpath(
\Drupal::service('extension.path.resolver')->getPath('module', 'cloud_cluster')
) . '/templates';
foreach (scandir($template_path) ?: [] as $folder) {
if ($folder === '.' || $folder === '..') {
continue;
}
if ($deployment_type !== NULL && $deployment_type !== $folder) {
continue;
}
$template_definitions = Yaml::decode(file_get_contents("$template_path/$folder/templates.yml"));
foreach ($template_definitions ?: [] as $name => $template_definition) {
$templates[$name] = $template_definition['label'];
}
}
natcasesort($templates);
......@@ -614,7 +662,7 @@ function cloud_cluster_form_cloud_launch_template_cloud_cluster_launch_form_alte
/** @var \Drupal\cloud\Entity\CloudLaunchTemplate $cloud_launch_template */
$cloud_launch_template = \Drupal::routeMatch()->getParameter('cloud_launch_template');
$entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities('k8s');
$entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities($cloud_launch_template->field_deployment_type->value);
$cloud_contexts = [];
$account = \Drupal::currentUser();
foreach ($entities ?: [] as $entity) {
......
......@@ -17,8 +17,8 @@ settings:
value: k8s
label: Kubernetes
-
value: aws
label: AWS
value: aws_cloud
label: AWS Cloud
allowed_values_function: ''
module: options
locked: false
......
.field--widget-options-buttons fieldset.form-item {
.field--widget-options-buttons .form-item {
background-color: transparent;
}
.field--name-field-deployment-template {
display: none;
}
(function ($) {
'use strict';
$.fn.initOptions = function (data) {
if (!$('input[name=field_deployment_template]:checked').val()) {
$('input[name=field_deployment_template]').eq(0).attr('checked', true);
}
};
let field_deployment_template_default_values = drupalSettings.cloud_cluster.field_deployment_template_default_values;
$('input[name=field_deployment_template]').each(function () {
if (!field_deployment_template_default_values.includes($(this).val())) {
$(this).parents('.form-type-radio,.js-form-type-radio').remove();
}
});
$('.field--name-field-deployment-template').show();
$(this).initOptions();
})(jQuery);
......@@ -8,6 +8,7 @@ use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\cloud\Plugin\cloud\launch_template\CloudLaunchTemplatePluginInterface;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
......@@ -196,15 +197,30 @@ class CloudClusterCloudLaunchTemplatePlugin extends CloudPluginBase implements C
];
$deployment_type = $cloud_launch_template->field_deployment_type->value;
$template_path = realpath(
if ($deployment_type === 'aws_cloud') {
$this->messenger->addMessage(t('AWS cloud will be implemented.'));
return $route;
}
$template_definitions_path = realpath(
sprintf(
'%s/templates/%s/%s/cloud_orchestrator.yml',
'%s/templates/%s/templates.yml',
$this->extensionPathResolver->getPath('module', 'cloud_cluster'),
$deployment_type,
$cloud_launch_template->field_deployment_template->value
$deployment_type
)
);
$deployment_template = $cloud_launch_template->field_deployment_template->value;
$template_definitions = Yaml::decode(file_get_contents($template_definitions_path));
$template_definition = $template_definitions[$deployment_template];
$template_location = $template_definition['location'];
if (strpos($template_location, 'http://') === FALSE) {
$template_location = dirname($template_definitions_path) . '/' . $template_location;
}
$template_content = file_get_contents($template_location);
$parameters = [];
foreach ($form_state->getValue('parameters') ?: [] as $group) {
foreach ($group ?: [] as $name => $value) {
......@@ -212,7 +228,7 @@ class CloudClusterCloudLaunchTemplatePlugin extends CloudPluginBase implements C
}
}
$yaml = $this->twig->renderInline(file_get_contents($template_path), $parameters);
$yaml = $this->twig->renderInline($template_content, $parameters);
$service_name = "$deployment_type.cloud_orchestrator_manager";
if (!\Drupal::hasService($service_name)) {
......
groups:
- name: drupal
label: Drupal configuration
parameters:
- name: DrupalUserName
label: Drupal administrator username
default_value: cloud_admin
type: textfield
- name: DrupalPassword
label: Drupal administrator password
default_value: cloud_admin
type: password
- name: DrupalEmail
label: Drupal administrator email address
default_value: cloud_admin@example.com
type: email
- name: DrupalTimezone
label: Drupal default time zone
default_value: America/Los_Angeles
type: timezone
- name: database
label: Database configuration
parameters:
- name: MySQLUserName
label: MySQL administrator username
default_value: mysql_admin
type: textfield
- name: MySQLPassword
label: MySQL administrator password
default_value: mysql_admin_password
type: password
- name: DatabaseName
label: MySQL database name
default_value: cloud_orchestrator
type: textfield
- name: aws_ec2
label: Amazon EC2 Configuration
parameters:
- name: KeyName
label: EC2 key name
default_value: ''
type: textfield
cloud_orchestrator_full:
label: Cloud Orchestrator Full
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_full.yaml
cloud_orchestrator_full_manual_vpc:
label: Cloud Orchestrator Full Manual VPC
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_full_manual_vpc.yaml
cloud_orchestrator_single:
label: Cloud Orchestrator Single
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_single.yaml
cloud_orchestrator_single_manual_vpc:
label: Cloud Orchestrator Single Manual VPC
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_single_manual_vpc.yaml
cloud_orchestrator_docker:
label: Cloud Orchestrator Docker
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_docker.yaml
cloud_orchestrator_docker_manual_vpc:
label: Cloud Orchestrator Docker Manual VPC
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_docker_manual_vpc.yaml
cloud_orchestrator_docker_rds_manual_vpc:
label: Cloud Orchestrator RDS Manual VPC
location: https://cloud-orchestrator.s3.amazonaws.com/cfn/cloud_orchestrator_docker_rds_manual_vpc.yaml
amd64:
label: AMD64
location: amd64.yml
arm64:
label: ARM64
location: arm64.yml
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