From b25530f0636655fcc31e9ffea4db6795b2f5035f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 10 Sep 2023 23:32:59 -0400 Subject: [PATCH] Successful build of server. Ready for testing. --- README.md | 12 +- modules/servers/VirtFusionDirect/hooks.php | 188 +++++++++++------- .../VirtFusionDirect/lib/ConfigureService.php | 34 ++++ .../VirtFusionDirect/lib/ModuleFunctions.php | 4 + 4 files changed, 161 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index f30a03f..32fe592 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,22 @@ This module requires VirtFusion v1.7.3 or higher as this is what it's based on. 1. Download the latest release from the [releases](https://github.com/EZSCALE/virtfusion-whmcs-module/releases) page. 2. Extract the contents of the archive and upload the modules folder to your WHMCS installation directory. +## :heavy_exclamation_mark: Important Notes :heavy_exclamation_mark: + +You must create two custom fields in WHMCS for this module to work. You need to configure the following custom fields on +each product you want to use this module with. + +| Field Name | Field Type | Description | Validation | Select Options | Admin Only | Required Field | Show on Order Form | Show on Invoice | +|--------------------------|------------|--------------------------|-------------|----------------|------------|--------------------|--------------------|-----------------| +| Initial Operating System | Text Box | Set to whatever you want | Leave Blank | Leave Blank | :x: | :white_check_mark: | :white_check_mark: | :x: | +| Initial SSH Key | Text Box | Set to whatever you want | Leave Blank | Leave Blank | :x: | :x: | :white_check_mark: | :x: | + ## What does this module change? This module changes the following things: - Adds configurable options to the product configuration page to allow the user to select the operating system and add - a ssh key to the initial deployment. + an ssh key to the initial deployment. ## TODO diff --git a/modules/servers/VirtFusionDirect/hooks.php b/modules/servers/VirtFusionDirect/hooks.php index 28ec62a..466f82f 100644 --- a/modules/servers/VirtFusionDirect/hooks.php +++ b/modules/servers/VirtFusionDirect/hooks.php @@ -7,81 +7,117 @@ if (!defined("WHMCS")) { die("This file cannot be accessed directly"); } -if (!function_exists('add_hook_os_templates')) { - /** - * @param array $vars - * @return array|null - * @throws JsonException - */ - function add_hook_os_templates(array $vars): ?array - { - if (!isset($vars['productinfo']['module']) || $vars['productinfo']['module'] !== 'VirtFusionDirect') { - return null; - } - - $cs = new ConfigureService(); - - $templates_data = $cs->fetchTemplates( - $cs->fetchPackageByDbId($vars['productinfo']['pid']) ?? $cs->fetchPackageId($vars['productinfo']['name']) - ); - - if (empty($templates_data)) { - return null; - } - - $dropdownOptions = []; - - foreach ($templates_data['data'] as $osCategory) { - foreach ($osCategory['templates'] as $template) { - $optionValue = $template['id']; - $optionLabel = $template['name'] . " " . $template['version'] . " " . $template['variant']; - $dropdownOptions[] = ['id' => $optionValue, 'name' => $optionLabel]; - } - } - - // Sort dropdownOptions alphabetically by the 'name' key - usort($dropdownOptions, function ($a, $b) { - return strcmp($a['name'], $b['name']); - }); - - $osTemplates = [ - 'id' => 'os_template', - 'optionname' => 'Initial Operating System', - 'optiontype' => 1, - 'options' => $dropdownOptions, - 'selectedvalue' => '' - ]; - - $sshKeys = $cs->getUserSshKeys($vars['loggedinuser']); - - $sshKeysOptions = [ - 'id' => 'ssh_key', - 'optionname' => 'Initial SSH Key', - 'optiontype' => 1, - 'options' => array_map(function ($sshKey) { - if ($sshKey['enabled'] === false) { - return null; - } - - return [ - 'id' => $sshKey['id'], - 'name' => $sshKey['name'] - ]; - }, $sshKeys['data'] ?? []), - 'selectedvalue' => '' - ]; - - $configurableoptions = $vars['configurableoptions']; - array_push( - $configurableoptions, - $osTemplates, - $sshKeysOptions - ); - - return [ - 'configurableoptions' => $configurableoptions, - ]; +add_hook('ClientAreaFooterOutput', 1, function ($vars) { + if (!isset($vars['productinfo']['module']) || $vars['productinfo']['module'] !== 'VirtFusionDirect') { + return null; } - add_hook('ClientAreaPageCart', 1, 'add_hook_os_templates'); -} \ No newline at end of file + $cs = new ConfigureService(); + + $templates_data = $cs->fetchTemplates( + $cs->fetchPackageByDbId($vars['productinfo']['pid']) ?? $cs->fetchPackageId($vars['productinfo']['name']) + ); + + if (empty($templates_data)) { + return null; + } + + $dropdownOptions = []; + + foreach ($templates_data['data'] as $osCategory) { + foreach ($osCategory['templates'] as $template) { + $optionValue = $template['id']; + $optionLabel = $template['name'] . " " . $template['version'] . " " . $template['variant']; + $dropdownOptions[] = ['id' => $optionValue, 'name' => $optionLabel]; + } + } + + // Sort dropdownOptions alphabetically by the 'name' key + usort($dropdownOptions, function ($a, $b) { + return strcmp($a['name'], $b['name']); + }); + + $sshKeys = $cs->getUserSshKeys($vars['loggedinuser']); + $sshKeysOptions = array_map(function ($sshKey) { + if ($sshKey['enabled'] === false) { + return null; + } + + return [ + 'id' => $sshKey['id'], + 'name' => $sshKey['name'] + ]; + }, $sshKeys['data'] ?? []); + + $osID = array_values(array_filter(array_map(function ($option) { + if ($option['textid'] === 'initialoperatingsystem') { + return $option['id']; + } + }, $vars['customfields']))); + + $sshID = array_values(array_filter(array_map(function ($option) { + if ($option['textid'] === 'initialsshkey') { + return $option['id']; + } + }, $vars['customfields']))); + + // Construct the JavaScript code + return " + + "; +}); \ No newline at end of file diff --git a/modules/servers/VirtFusionDirect/lib/ConfigureService.php b/modules/servers/VirtFusionDirect/lib/ConfigureService.php index 22f166f..3f5d433 100644 --- a/modules/servers/VirtFusionDirect/lib/ConfigureService.php +++ b/modules/servers/VirtFusionDirect/lib/ConfigureService.php @@ -116,4 +116,38 @@ class ConfigureService extends Module return isset($response['msg']) && $response['msg'] === "ext_relation_id not found" ? null : $response['data']; } + + /** + * @param int $id + * @param array $vars + * @return bool + */ + public function initServerBuild(int $id, array $vars): bool + { + $request = $this->initCurl($this->cp['token']); + + // Generate a random 8 character hostname + $hostname = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 8); + + $inputData = [ + "operatingSystemId" => $vars['customfields']['Initial Operating System'], + "name" => $hostname, + "sshKeys" => [ + $vars['customfields']['Initial SSH Key'] + ], + 'email' => true + ]; + + if (empty($vars['customfields']['Initial SSH Key'])) { + unset($inputData['sshKeys']); + } + + $request->addOption(CURLOPT_POSTFIELDS, json_encode($inputData)); + + $request->post( + sprintf("%s/servers/%d/build", $this->cp['url'], $id) + ); + + return true; + } } \ No newline at end of file diff --git a/modules/servers/VirtFusionDirect/lib/ModuleFunctions.php b/modules/servers/VirtFusionDirect/lib/ModuleFunctions.php index b2b37d5..4f5864c 100644 --- a/modules/servers/VirtFusionDirect/lib/ModuleFunctions.php +++ b/modules/servers/VirtFusionDirect/lib/ModuleFunctions.php @@ -163,6 +163,10 @@ class ModuleFunctions extends Module Database::systemOnServerCreate($params['serviceid'], $data); $this->updateWhmcsServiceParamsOnServerObject($params['serviceid'], $data); + // If the server is created successfully, we can initialize the server build. + $cs = new ConfigureService(); + $cs->initServerBuild($data->data->id, $params); + /** * * Server was created successfully.