diff --git a/examples/installers/k8s/cryptpad.vsh b/examples/installers/k8s/cryptpad.vsh index b081bf0d..00196085 100755 --- a/examples/installers/k8s/cryptpad.vsh +++ b/examples/installers/k8s/cryptpad.vsh @@ -11,13 +11,17 @@ mut installer := cryptpad.get( create: true )! -// 2. Install CryptPad. -// This will generate the necessary Kubernetes YAML files and apply them to your cluster. -// installer.install()! // cryptpad.delete()! +// 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()! // 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_factory_.v b/lib/installers/k8s/cryptpad/cryptpad_factory_.v index b5e20dcf..502ada2a 100644 --- a/lib/installers/k8s/cryptpad/cryptpad_factory_.v +++ b/lib/installers/k8s/cryptpad/cryptpad_factory_.v @@ -156,8 +156,8 @@ pub fn play(mut plbook PlayBook) ! { // load from disk and make sure is properly intialized pub fn (mut self CryptpadServer) reload() ! { - switch(self.name) self = obj_init(self)! + set(self)! } @[params] diff --git a/lib/installers/k8s/cryptpad/cryptpad_model.v b/lib/installers/k8s/cryptpad/cryptpad_model.v index e612b2c3..80bfddc0 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] } @@ -32,12 +34,26 @@ pub mut: fn obj_init(mycfg_ CryptpadServer) !CryptpadServer { mut mycfg := mycfg_ + if mycfg.name == '' { + mycfg.name = 'cryptpad' + } + + // Replace the dashes, dots, and underscores with nothing + mycfg.name = mycfg.name.replace('_', '') + mycfg.name = mycfg.name.replace('-', '') + mycfg.name = mycfg.name.replace('.', '') + if mycfg.namespace == '' { - mycfg.namespace = mycfg.name + mycfg.namespace = '${mycfg.name}-cryptpad-namespace' + } + + if mycfg.namespace.contains('_') || mycfg.namespace.contains('.') { + console.print_stderr('namespace cannot contain _, was: ${mycfg.namespace}, use dashes instead.') + return error('namespace cannot contain _, was: ${mycfg.namespace}') } if mycfg.hostname == '' { - mycfg.hostname = mycfg.name + mycfg.hostname = '${mycfg.name}cryptpad' } mycfg.kube_client = kubernetes.get(create: true)! @@ -57,20 +73,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