chg createFile for large files, fix LocationsKtTest.kt, rename const (NGINX_CONFIG_FILE)
This commit is contained in:
parent
b3b98d8a2b
commit
c4d5f38f9d
4 changed files with 55 additions and 49 deletions
|
@ -2,16 +2,14 @@ package org.domaindrivenarchitecture.provs.framework.extensions.server_software.
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.Prov
|
import org.domaindrivenarchitecture.provs.framework.core.Prov
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
|
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.remote
|
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.NginxConf
|
||||||
|
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.createNginxLocationFolders
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
|
||||||
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.NginxConf
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.createNginxLocationFolders
|
|
||||||
import kotlin.system.exitProcess
|
|
||||||
|
|
||||||
|
|
||||||
internal const val configFile = "/etc/nginx/nginx.conf"
|
internal const val NGINX_CONFIG_FILE = "/etc/nginx/nginx.conf"
|
||||||
|
|
||||||
|
|
||||||
fun Prov.provisionNginxStandAlone(config: NginxConf? = null) = requireAll {
|
fun Prov.provisionNginxStandAlone(config: NginxConf? = null) = requireAll {
|
||||||
|
@ -21,10 +19,10 @@ fun Prov.provisionNginxStandAlone(config: NginxConf? = null) = requireAll {
|
||||||
createNginxLocationFolders()
|
createNginxLocationFolders()
|
||||||
|
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
if (fileExists(configFile)) {
|
if (fileExists(NGINX_CONFIG_FILE)) {
|
||||||
cmd("sudo mv $configFile $configFile-orig")
|
cmd("sudo mv $NGINX_CONFIG_FILE $NGINX_CONFIG_FILE-orig")
|
||||||
}
|
}
|
||||||
createFile(configFile, config.conf, sudo = true)
|
createFile(NGINX_CONFIG_FILE, config.conf, sudo = true)
|
||||||
val configCheck = cmd("sudo nginx -t")
|
val configCheck = cmd("sudo nginx -t")
|
||||||
if (configCheck.success) {
|
if (configCheck.success) {
|
||||||
cmd("sudo service nginx restart")
|
cmd("sudo service nginx restart")
|
||||||
|
@ -35,12 +33,3 @@ fun Prov.provisionNginxStandAlone(config: NginxConf? = null) = requireAll {
|
||||||
ProvResult(true) // dummy
|
ProvResult(true) // dummy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun provisionRemote(vararg args: String) {
|
|
||||||
if (args.size != 2) {
|
|
||||||
println("Pls specify host and user for remote installation of nginx.")
|
|
||||||
exitProcess(1)
|
|
||||||
}
|
|
||||||
remote(args[0], args[1]).provisionNginxStandAlone()
|
|
||||||
}
|
|
|
@ -80,39 +80,32 @@ fun Prov.createFile(
|
||||||
text: String?,
|
text: String?,
|
||||||
posixFilePermission: String? = null,
|
posixFilePermission: String? = null,
|
||||||
sudo: Boolean = false,
|
sudo: Boolean = false,
|
||||||
overwriteIfExisting: Boolean = false
|
overwriteIfExisting: Boolean = true
|
||||||
): ProvResult = task {
|
): ProvResult = task {
|
||||||
val maxBlockSize = 100000
|
val maxBlockSize = 50000
|
||||||
|
|
||||||
if (!overwriteIfExisting && fileExists(fullyQualifiedFilename, sudo)) {
|
|
||||||
return@task ProvResult(true, "File $fullyQualifiedFilename already existing.")
|
|
||||||
}
|
|
||||||
val withSudo = if (sudo) "sudo " else ""
|
val withSudo = if (sudo) "sudo " else ""
|
||||||
|
|
||||||
posixFilePermission?.let {
|
posixFilePermission?.let {
|
||||||
ensureValidPosixFilePermission(posixFilePermission)
|
ensureValidPosixFilePermission(posixFilePermission)
|
||||||
}
|
}
|
||||||
|
if (!overwriteIfExisting && fileExists(fullyQualifiedFilename, sudo)) {
|
||||||
|
return@task ProvResult(true, "File $fullyQualifiedFilename already existing.")
|
||||||
|
}
|
||||||
|
|
||||||
val modeOption = posixFilePermission?.let { "-m $it"} ?: ""
|
val modeOption = posixFilePermission?.let { "-m $it"} ?: ""
|
||||||
|
|
||||||
// create empty file resp. clear file
|
// create empty file resp. clear file
|
||||||
cmd(withSudo + "install $modeOption /dev/null $fullyQualifiedFilename")
|
cmd(withSudo + "install $modeOption /dev/null $fullyQualifiedFilename")
|
||||||
|
|
||||||
if (text != null) {
|
if (text != null) {
|
||||||
if (text.length <= maxBlockSize) {
|
|
||||||
cmd(
|
|
||||||
"printf '%s' " + text
|
|
||||||
.escapeAndEncloseByDoubleQuoteForShell() + " | ${if (sudo) "sudo" else ""} tee $fullyQualifiedFilename > /dev/null"
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
val chunkedTest = text.chunked(maxBlockSize)
|
val chunkedTest = text.chunked(maxBlockSize)
|
||||||
for (chunk in chunkedTest) {
|
for (chunk in chunkedTest) {
|
||||||
cmd(
|
cmd(
|
||||||
"printf '%s' " + chunk
|
"printf '%s' " + chunk
|
||||||
.escapeAndEncloseByDoubleQuoteForShell() + " | ${if (sudo) "sudo" else ""} tee -a $fullyQualifiedFilename > /dev/null"
|
.escapeAndEncloseByDoubleQuoteForShell() + " | $withSudo tee -a $fullyQualifiedFilename > /dev/null"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ProvResult(true) // dummy
|
ProvResult(true) // dummy
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
cmd(withSudo + "touch $fullyQualifiedFilename")
|
cmd(withSudo + "touch $fullyQualifiedFilename")
|
||||||
}
|
}
|
||||||
|
@ -265,6 +258,7 @@ fun Prov.deleteDir(dir: String, path: String, sudo: Boolean = false): ProvResult
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --------------------- various functions ----------------------
|
||||||
fun Prov.userHome(): String {
|
fun Prov.userHome(): String {
|
||||||
val user = cmd("whoami").out?.trim()
|
val user = cmd("whoami").out?.trim()
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
|
@ -279,6 +273,15 @@ fun Prov.userHome(): String {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns number of bytes of a file or null if size could not be determined
|
||||||
|
*/
|
||||||
|
fun Prov.fileSize(filename: String): Int? {
|
||||||
|
val result = cmd("wc -c < $filename")
|
||||||
|
return result.out?.trim()?.toIntOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun ensureValidPosixFilePermission(posixFilePermission: String) {
|
private fun ensureValidPosixFilePermission(posixFilePermission: String) {
|
||||||
if (!Regex("^[0-7]{3}$").matches(posixFilePermission)) throw RuntimeException("Wrong file permission ($posixFilePermission), permission must consist of 3 digits as e.g. 664 ")
|
if (!Regex("^[0-7]{3}$").matches(posixFilePermission)) throw RuntimeException("Wrong file permission ($posixFilePermission), permission must consist of 3 digits as e.g. 664 ")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.extensions.server_software.
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContainsText
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContainsText
|
||||||
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.configFile
|
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.NGINX_CONFIG_FILE
|
||||||
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.provisionNginxStandAlone
|
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.provisionNginxStandAlone
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
@ -20,7 +20,7 @@ internal class LocationsKtTest {
|
||||||
// given
|
// given
|
||||||
val a = defaultTestContainer()
|
val a = defaultTestContainer()
|
||||||
a.provisionNginxStandAlone()
|
a.provisionNginxStandAlone()
|
||||||
a.createFile(configFile, NGINX_MINIMAL_CONF, sudo = true)
|
a.createFile(NGINX_CONFIG_FILE, NGINX_MINIMAL_CONF, sudo = true)
|
||||||
|
|
||||||
// when
|
// when
|
||||||
val res = a.nginxIncludeLocationFolders()
|
val res = a.nginxIncludeLocationFolders()
|
||||||
|
@ -28,10 +28,10 @@ internal class LocationsKtTest {
|
||||||
// then
|
// then
|
||||||
assertTrue(res.success)
|
assertTrue(res.success)
|
||||||
assertTrue(a.fileContainsText(
|
assertTrue(a.fileContainsText(
|
||||||
configFile, """listen 80;
|
NGINX_CONFIG_FILE, """listen 80;
|
||||||
include /etc/nginx/locations-enabled/port80*.conf
|
include /etc/nginx/locations-enabled/port80*.conf
|
||||||
include /etc/nginx/locations-enabled/port443*.conf"""))
|
include /etc/nginx/locations-enabled/port443*.conf"""))
|
||||||
// just 1 occurrence
|
// just 1 occurrence
|
||||||
assertEquals("1", a.cmd("grep -o 'listen 80;' $configFile | wc -l").out?.trim())
|
assertEquals("1", a.cmd("grep -o 'listen 80;' $NGINX_CONFIG_FILE | wc -l").out?.trim())
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,16 +11,12 @@ import java.io.File
|
||||||
internal class FilesystemKtTest {
|
internal class FilesystemKtTest {
|
||||||
|
|
||||||
val testtext = "tabs \t\t\t triple quotes \"\"\"" + """
|
val testtext = "tabs \t\t\t triple quotes \"\"\"" + """
|
||||||
\\ \\\ \\\\ \\\\\
|
newline
|
||||||
'\t%s \\ " ""
|
and apostrophe's ' '' ''' \' " "" \" and special chars ${'$'} {} ${'$'}\{something}<>äöüß!§@€%#|&/[]\\ äöüß %s %% %%% \\ \\\ \\\\ \\\\\ ${'$'}\notakotlinvariable ${'$'}notakotlinvariable and tabs and \t should be handled correctly
|
||||||
' "${'$'}arg")"
|
|
||||||
'%s' "${'$'}@" | 's/\([][!#${'$'}%&()*;<=>?\_`{|}]\)/\\\1/g;'
|
|
||||||
"${'$'}@" | sed -e 's/"/\\"/g'
|
|
||||||
apostrophe's ' " \" \' and special chars ${'$'} {} ${'$'}\{something}!§${'$'}%[]\\ äöüß ${'$'}\notakotlinvariable ${'$'}notakotlinvariable and tabs and \t are should be handled correctly
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun test_createFile_locally() {
|
fun createFile_locally() {
|
||||||
// given
|
// given
|
||||||
val prov = testLocal()
|
val prov = testLocal()
|
||||||
prov.createDir("tmp")
|
prov.createDir("tmp")
|
||||||
|
@ -38,7 +34,7 @@ internal class FilesystemKtTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ContainerTest
|
@ContainerTest
|
||||||
fun test_createFile_in_container() {
|
fun createFile_in_container() {
|
||||||
// given
|
// given
|
||||||
val prov = defaultTestContainer()
|
val prov = defaultTestContainer()
|
||||||
val filename = "testfile8"
|
val filename = "testfile8"
|
||||||
|
@ -54,6 +50,24 @@ internal class FilesystemKtTest {
|
||||||
assertEquals(testtext, prov.fileContent("sudo$filename"))
|
assertEquals(testtext, prov.fileContent("sudo$filename"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@ContainerTest
|
||||||
|
fun create_large_file_in_container() {
|
||||||
|
// given
|
||||||
|
val prov = defaultTestContainer()
|
||||||
|
val filename = "largetestfile"
|
||||||
|
val content = "012345äöüß".repeat(100000)
|
||||||
|
|
||||||
|
// when
|
||||||
|
val res = prov.createFile(filename, content)
|
||||||
|
val size = prov.fileSize(filename)
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertTrue(res.success)
|
||||||
|
assertEquals(1400000, size)
|
||||||
|
// assertEquals(testtext, prov.fileContent(filename))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ContainerTest
|
@ContainerTest
|
||||||
fun checkingCreatingDeletingFile() {
|
fun checkingCreatingDeletingFile() {
|
||||||
|
@ -255,7 +269,7 @@ internal class FilesystemKtTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ContainerTest
|
@ContainerTest
|
||||||
fun test_fileContainsText() {
|
fun fileContainsText() {
|
||||||
// given
|
// given
|
||||||
defaultTestContainer().createFile("testfilecontainingtext", "abc\n- def\nefg")
|
defaultTestContainer().createFile("testfilecontainingtext", "abc\n- def\nefg")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue