From 8846c9c170a5b1a5a2a6d94762e8fdc301dc61c1 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 28 Jan 2022 23:37:33 +0100 Subject: [PATCH] [skip ci] add k3s waiting for cert-manager --- .../ubuntu/filesystem/base/Filesystem.kt | 41 ++++++++++++++----- .../provs/server/infrastructure/K3s.kt | 6 ++- .../filesystem/base/FilesystemKtTest.kt | 17 +++++--- 3 files changed, 47 insertions(+), 17 deletions(-) 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 b7e68f1..81cb309 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 @@ -79,23 +79,44 @@ fun Prov.createFile( fullyQualifiedFilename: String, text: String?, posixFilePermission: String? = null, - sudo: Boolean = false -): ProvResult = - def { - val withSudo = if (sudo) "sudo " else "" - posixFilePermission?.let { - ensureValidPosixFilePermission(posixFilePermission) - cmd(withSudo + "install -m $posixFilePermission /dev/null $fullyQualifiedFilename") - } - if (text != null) { + sudo: Boolean = false, + overwriteIfExisting: Boolean = false +): ProvResult = task { + val maxBlockSize = 100000 + + if (!overwriteIfExisting && fileExists(fullyQualifiedFilename, sudo)) { + return@task ProvResult(true, "File $fullyQualifiedFilename already existing.") + } + val withSudo = if (sudo) "sudo " else "" + + posixFilePermission?.let { + ensureValidPosixFilePermission(posixFilePermission) + } + val modeOption = posixFilePermission?.let { "-m $it"} ?: "" + + // create empty file resp. clear file + cmd(withSudo + "install $modeOption /dev/null $fullyQualifiedFilename") + + if (text != null) { + if (text.length <= maxBlockSize) { cmd( "printf '%s' " + text .escapeAndEncloseByDoubleQuoteForShell() + " | ${if (sudo) "sudo" else ""} tee $fullyQualifiedFilename > /dev/null" ) } else { - cmd(withSudo + "touch $fullyQualifiedFilename") + val chunkedTest = text.chunked(maxBlockSize) + for (chunk in chunkedTest) { + cmd( + "printf '%s' " + chunk + .escapeAndEncloseByDoubleQuoteForShell() + " | ${if (sudo) "sudo" else ""} tee -a $fullyQualifiedFilename > /dev/null" + ) + } + ProvResult(true) // dummy } + } else { + cmd(withSudo + "touch $fullyQualifiedFilename") } +} fun Prov.createSecretFile( 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 ebb7d54..193d0ee 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt @@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.server.infrastructure import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.ProvResult +import org.domaindrivenarchitecture.provs.framework.core.repeatTaskUntilSuccess import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.* private const val k3sConfigFile = "/etc/rancher/k3s/config.yaml" @@ -132,7 +133,10 @@ fun Prov.provisionK3sCertManager(endpoint: CertManagerEndPoint) = task { sudo = true ) cmd("kubectl apply -f $certManagerDeployment", sudo = true) - cmd("kubectl apply -f $certManagerIssuer", sudo = true) + + repeatTaskUntilSuccess(10, 10) { + cmd("kubectl apply -f $certManagerIssuer", sudo = true) + } } /* 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 9d6a5b4..986b80c 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 @@ -1,5 +1,6 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base +import org.domaindrivenarchitecture.provs.framework.core.getResourceAsText import org.domaindrivenarchitecture.provs.test.defaultTestContainer import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.testLocal @@ -39,18 +40,22 @@ internal class FilesystemKtTest { @ContainerTest fun test_createFile_in_container() { // given - val prov = defaultTestContainer() - val filename = "testfile8" + val prov = testLocal() //defaultTestContainer() + val filename = "tmp/testfile11" + + val testtext = getResourceAsText("org/domaindrivenarchitecture/provs/infrastructure/k3s/" + "cert-manager.yaml") + + println("aaaaaaaaaaaaaaaaaa len: " + testtext.length) // when - val res = prov.createFile(filename, testtext) - val res2 = prov.createFile("sudo$filename", testtext, sudo = true) + val res = prov.createFile(filename, testtext, overwriteIfExisting = true) +// val res2 = prov.createFile("sudo$filename", testtext, sudo = true) // then assertTrue(res.success) - assertTrue(res2.success) +// assertTrue(res2.success) assertEquals(testtext, prov.fileContent(filename)) - assertEquals(testtext, prov.fileContent("sudo$filename")) +// assertEquals(testtext, prov.fileContent("sudo$filename")) } @Test