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 <mahmmoud.hassanein@gmail.com>
This commit is contained in:
@@ -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()!
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}')
|
||||
}
|
||||
|
||||
@@ -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.')
|
||||
}
|
||||
|
||||
16
lib/installers/k8s/cryptpad/templates/config.js
Normal file
16
lib/installers/k8s/cryptpad/templates/config.js
Normal file
@@ -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',
|
||||
};
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user