From 6746d885f8c93b1cbe24c91ae206ad0ccb766a9f Mon Sep 17 00:00:00 2001 From: peternashaat Date: Mon, 3 Nov 2025 13:06:47 +0000 Subject: [PATCH] feat(cryptpad): Refactor installer for dynamic configuration This commit refactors the CryptPad Kubernetes installer to be more dynamic and configurable structure. Key changes include: - **Dynamic Configuration**: The installer now generates its configuration based on parameters passed from the `.vsh` script, with sensible defaults for any unspecifie d values. - **Templated `config.js`**: Introduced a new `config.js` template to allow for greater flexibility and easier maintenance of the CryptPad configuration. - **Improved Code Structure**: The source code has been updated to be more modular and maintainable. - **Updated Documentation**: The `README.md` has been updated to include instructions on how to run the installer and customize the installation. Co-authored-by: Mahmoud-Emad --- examples/installers/k8s/cryptpad.vsh | 13 +++--- lib/installers/k8s/cryptpad/README.md | 10 +++++ .../k8s/cryptpad/cryptpad_actions.v | 4 +- lib/installers/k8s/cryptpad/cryptpad_model.v | 42 ++++++++++++++----- .../k8s/cryptpad/templates/config.js | 16 +++++++ .../k8s/cryptpad/templates/cryptpad.yaml | 17 +------- 6 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 lib/installers/k8s/cryptpad/templates/config.js diff --git a/examples/installers/k8s/cryptpad.vsh b/examples/installers/k8s/cryptpad.vsh index b081bf0d..a1f021a1 100755 --- a/examples/installers/k8s/cryptpad.vsh +++ b/examples/installers/k8s/cryptpad.vsh @@ -11,13 +11,16 @@ mut installer := cryptpad.get( create: true )! -// 2. Install CryptPad. +// 2. Configure the installer (all settings are optional with sensible defaults) +installer.hostname = 'mycryptpad' +installer.namespace = 'cryptpad' + +// 3. Install CryptPad. // This will generate the necessary Kubernetes YAML files and apply them to your cluster. -// installer.install()! -// cryptpad.delete()! +installer.install()! // println('CryptPad installation started.') // println('You can access it at https://${installer.hostname}.gent01.grid.tf') -// 3. To destroy the deployment, you can run the following: -installer.destroy()! +// 4. To destroy the deployment, you can run the following: +// installer.destroy()! diff --git a/lib/installers/k8s/cryptpad/README.md b/lib/installers/k8s/cryptpad/README.md index e90a1baa..09e478ec 100644 --- a/lib/installers/k8s/cryptpad/README.md +++ b/lib/installers/k8s/cryptpad/README.md @@ -31,6 +31,16 @@ installer.install()! ## Usage +### Running the Installer + +You can run the installer directly from the command line using the example script: + +```bash +./examples/installers/k8s/cryptpad.vsh +``` + +This will install CryptPad with the default settings. To customize the installation, you can edit the `cryptpad.vsh` file. + ### Create an Instance ```v diff --git a/lib/installers/k8s/cryptpad/cryptpad_actions.v b/lib/installers/k8s/cryptpad/cryptpad_actions.v index 605123b8..f094d5b6 100644 --- a/lib/installers/k8s/cryptpad/cryptpad_actions.v +++ b/lib/installers/k8s/cryptpad/cryptpad_actions.v @@ -60,7 +60,7 @@ fn install() ! { // 4. Apply the YAML files using kubernetes client console.print_info('Applying Gateway YAML file to the cluster...') - res1 := k8s.apply_yaml('/tmp/tfgw-cryptpad.yaml')! + res1 := k8s.apply_yaml(installer.tfgw_cryptpad_path)! if !res1.success { return error('Failed to apply tfgw-cryptpad.yaml: ${res1.stderr}') } @@ -72,7 +72,7 @@ fn install() ! { // 6. Apply Cryptpad YAML console.print_info('Applying Cryptpad YAML file to the cluster...') - res2 := k8s.apply_yaml('/tmp/cryptpad.yaml')! + res2 := k8s.apply_yaml(installer.cryptpad_path)! if !res2.success { return error('Failed to apply cryptpad.yaml: ${res2.stderr}') } diff --git a/lib/installers/k8s/cryptpad/cryptpad_model.v b/lib/installers/k8s/cryptpad/cryptpad_model.v index e612b2c3..2d7cb458 100644 --- a/lib/installers/k8s/cryptpad/cryptpad_model.v +++ b/lib/installers/k8s/cryptpad/cryptpad_model.v @@ -15,6 +15,7 @@ pub mut: hostname string // The CryptPad hostname backends string // The backends for the TFGW namespace string // The namespace for the CryptPad deployment + config_js string // Generated config.js content } @[heap] @@ -23,8 +24,9 @@ pub mut: name string = 'cryptpad' hostname string namespace string - cryptpad_path string = '/tmp/cryptpad.yaml' - tfgw_cryptpad_path string = '/tmp/tfgw-cryptpad.yaml' + cryptpad_path string = '/tmp/cryptpad/cryptpad.yaml' + tfgw_cryptpad_path string = '/tmp/cryptpad/tfgw-cryptpad.yaml' + config_js_path string = '/tmp/cryptpad/config.js' kube_client kubernetes.KubeClient @[skip] } @@ -57,20 +59,40 @@ fn configure() ! { backends_str_builder.writeln(' - "http://[${ip}]:80"') } - config_values := ConfigValues{ + // Create config_values for template generation + mut config_values := ConfigValues{ hostname: installer.hostname backends: backends_str_builder.str() namespace: installer.namespace + config_js: '' } - console.print_info('Generating YAML files from templates...') - temp := $tmpl('./templates/tfgw-cryptpad.yaml') - mut temp_path := pathlib.get_file(path: installer.tfgw_cryptpad_path, create: true)! - temp_path.write(temp)! + // Generate config.js + config_js_raw := $tmpl('./templates/config.js') - temp2 := $tmpl('./templates/cryptpad.yaml') - mut temp_path2 := pathlib.get_file(path: installer.cryptpad_path, create: true)! - temp_path2.write(temp2)! + // Indent the configs for proper YAML formatting (4 spaces for ConfigMap data) + config_js_lines := config_js_raw.split('\n') + mut config_js_indented := strings.new_builder(config_js_raw.len + 100) + for line in config_js_lines { + if line.len > 0 { + config_js_indented.writeln(' ${line}') + } + } + + // Update config_values with the generated and indented configs + config_values.config_js = config_js_indented.str() + + // Ensure the output directory exists + _ := pathlib.get_dir(path: '/tmp/cryptpad', create: true)! + + console.print_info('Generating YAML files from templates...') + tfgw_yaml := $tmpl('./templates/tfgw-cryptpad.yaml') + mut tfgw_path := pathlib.get_file(path: installer.tfgw_cryptpad_path, create: true)! + tfgw_path.write(tfgw_yaml)! + + cryptpad_yaml := $tmpl('./templates/cryptpad.yaml') + mut cryptpad_path := pathlib.get_file(path: installer.cryptpad_path, create: true)! + cryptpad_path.write(cryptpad_yaml)! console.print_info('YAML files generated successfully.') } diff --git a/lib/installers/k8s/cryptpad/templates/config.js b/lib/installers/k8s/cryptpad/templates/config.js new file mode 100644 index 00000000..4ea56167 --- /dev/null +++ b/lib/installers/k8s/cryptpad/templates/config.js @@ -0,0 +1,16 @@ +module.exports = { + httpUnsafeOrigin: 'https://@{config_values.hostname}.gent01.grid.tf', + httpSafeOrigin: 'https://@{config_values.hostname}sb.gent01.grid.tf', + httpAddress: '0.0.0.0', + httpPort: 80, + + websocketPort: 3003, + websocketPath: '/cryptpad_websocket', + + blockPath: './block', + blobPath: './blob', + dataPath: './data', + filePath: './datastore', + logToStdout: true, + logLevel: 'info', +}; \ No newline at end of file diff --git a/lib/installers/k8s/cryptpad/templates/cryptpad.yaml b/lib/installers/k8s/cryptpad/templates/cryptpad.yaml index 4c099792..d0ac7902 100644 --- a/lib/installers/k8s/cryptpad/templates/cryptpad.yaml +++ b/lib/installers/k8s/cryptpad/templates/cryptpad.yaml @@ -10,22 +10,7 @@ metadata: namespace: @{config_values.namespace} data: config.js: | - module.exports = { - httpUnsafeOrigin: 'https://@{config_values.hostname}.gent01.grid.tf', - httpSafeOrigin: 'https://@{config_values.hostname}sb.gent01.grid.tf', - httpAddress: '0.0.0.0', - httpPort: 80, - - websocketPort: 3003, - websocketPath: '/cryptpad_websocket', - - blockPath: './block', - blobPath: './blob', - dataPath: './data', - filePath: './datastore', - logToStdout: true, - logLevel: 'info', - }; +@{config_values.config_js} --- apiVersion: apps/v1 kind: Deployment