diff --git a/README.md b/README.md index 68f6f9c..a60697b 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ For the remote server please configure a config file (default file name: server- fqdn: "myhostname.com" node: ipv4: "192.168.56.123" # ip address -echo: true # for demo reasons only - deploy an echo app +echo: true # for demo reasons only - deploys an echo app ``` To add a grafana agent to your k3s installation add the following to the config: diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt index ca7e650..22e56cc 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt @@ -219,6 +219,10 @@ fun Prov.fileContentLargeFile(file: String, sudo: Boolean = false, chunkSize: In } +/** + * Appends text at the end of the specified file. Creates the file if not yet existing. + * If doNotAddIfExisting is true, then the text is not added (again) if already contained somewhere in the file. + */ fun Prov.addTextToFile( text: String, file: File, diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt index 09803b1..1dcb8ab 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt @@ -110,6 +110,8 @@ fun Prov.installK3s(k3sConfig: K3sConfig): ProvResult { cmd("kubectl set env deployment -n kube-system local-path-provisioner DEPLOY_DATE=\"$(date)\"") cmd("ln -sf $k3sKubeConfig " + k8sCredentialsDir + "admin.conf", sudo = true) + + configureShellAliases() } } @@ -205,3 +207,7 @@ private fun Prov.createK3sFileFromResourceTemplate( private fun File.templateName(): String { return this.name.replace(".yaml", ".template.yaml") } + +internal fun Prov.configureShellAliases() = task { + addTextToFile( "\nalias k=kubectl\n", File(".bash_aliases",)) +} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt index 4741e4d..82d1c23 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt @@ -494,4 +494,61 @@ internal class FilesystemKtTest { assertTrue(linkExists) assertEquals("textinlinkfilesudo", content) } + + @ContainerTest + fun test_addTextToFile() { + // given + val prov = defaultTestContainer() + val file = "testfile_with_added_text" + val text1 = "added text" + val text2 = "\nadded text2\n" + prov.deleteFile(file) + + // when + val res = prov.addTextToFile(text1, File(file)) // add text to non-existing file + val containsText = prov.fileContainsText(file, text1) + val res2 = prov.addTextToFile(text1, File(file)) // test idempotence + val fileContent2 = prov.fileContent(file) + + val res3 = prov.addTextToFile(text2, File(file)) // add second text + val containsText3 = prov.fileContainsText(file, text1) + val res4 = prov.addTextToFile(text1, File(file)) // test idempotence + val fileContent4 = prov.fileContent(file) + + // then + assertTrue(res.success) + assertTrue(containsText) + assertTrue(res2.success) + assertEquals(text1, fileContent2) + + assertTrue(res3.success) + assertTrue(containsText3) + assertTrue(res4.success) + assertEquals(text1 + text2, fileContent4) + } + @ContainerTest + fun test_addTextToFile_with_doNotAddIfExisting_false() { + // given + val prov = defaultTestContainer() + val file = "testfile_with_added_text_2" + val text1 = "added text1" + val text2 = "\nadded text2\n" + prov.deleteFile(file) + + // when + val res = prov.addTextToFile(text1, File(file), doNotAddIfExisting = false) // add text to non-existing file + val res2 = prov.addTextToFile(text1, File(file), doNotAddIfExisting = false) // add again + + val res3 = prov.addTextToFile(text2, File(file), doNotAddIfExisting = false) // add second text + val res4 = prov.addTextToFile(text1, File(file), doNotAddIfExisting = false) // add first text again + val fileContent = prov.fileContent(file) + + // then + assertTrue(res.success) + assertTrue(res2.success) + + assertTrue(res3.success) + assertTrue(res4.success) + assertEquals(text1 + text1 + text2 + text1, fileContent) + } }