<commit_before>package com.github.kerubistan.kerub.utils.junix.virt.virsh

import com.github.kerubistan.kerub.host.executeOrDie
import com.github.kerubistan.kerub.host.process
import com.github.kerubistan.kerub.model.SoftwarePackage
import com.github.kerubistan.kerub.model.display.RemoteConsoleProtocol
import com.github.kerubistan.kerub.model.hypervisor.LibvirtArch
import com.github.kerubistan.kerub.model.hypervisor.LibvirtCapabilities
import com.github.kerubistan.kerub.model.hypervisor.LibvirtGuest
import com.github.kerubistan.kerub.utils.LogLevel
import com.github.kerubistan.kerub.utils.equalsAnyOf
import com.github.kerubistan.kerub.utils.flag
import com.github.kerubistan.kerub.utils.getLogger
import com.github.kerubistan.kerub.utils.junix.common.Centos
import com.github.kerubistan.kerub.utils.junix.common.Debian
import com.github.kerubistan.kerub.utils.junix.common.Fedora
import com.github.kerubistan.kerub.utils.junix.common.OsCommand
import com.github.kerubistan.kerub.utils.junix.common.Ubuntu
import com.github.kerubistan.kerub.utils.junix.common.openSuse
import com.github.kerubistan.kerub.utils.silent
import com.github.kerubistan.kerub.utils.toBigInteger
import io.github.kerubistan.kroki.bytes.toBase64
import io.github.kerubistan.kroki.strings.substringBetween
import io.github.kerubistan.kroki.xml.xml
import org.apache.sshd.client.session.ClientSession
import java.io.OutputStream
import java.io.StringReader
import java.util.Properties
import java.util.UUID
import javax.xml.bind.JAXBContext

object Virsh : OsCommand {

	private val logger = getLogger()
	private val utf8 = charset("UTF-8")

	override fun providedBy(): List<Pair<(SoftwarePackage) -> Boolean, List<String>>> = listOf(
			{ distro: SoftwarePackage -> distro.name.equalsAnyOf(Centos, Fedora, openSuse) }
					to listOf("libvirt-client"),
			{ distro: SoftwarePackage -> distro.name.equalsAnyOf(Debian) } to listOf("libvirt-clients",
					"libvirt-daemon"),
			{ distro: SoftwarePackage -> distro.name.equalsAnyOf(Ubuntu) } to listOf("libvirt-bin")
	)

	fun setSecret(session: ClientSession, id: UUID, type: SecretType, value: String) {
		val secretDefFile = "/tmp/$id-secret.xml"
		val secretDef = xml(root = "secret", atts = *arrayOf("ephemeral" to "no", "private" to "yes")) {
			"uuid" { -id }
			"usage"("type" to type) {
				"target" { -id }
			}
		}.use { it.readBytes() }
		session.createSftpClient().use { sftp ->
			try {
				sftp.write(secretDefFile).use { file ->
					file.write(secretDef)
				}
				session.executeOrDie("virsh secret-define $secretDefFile")
				session.executeOrDie("virsh secret-set-value $id ${value.toByteArray().toBase64()}")
			} finally {
				sftp.remove(secretDefFile)
			}
		}

	}

	fun clearSecret(session: ClientSession, id: UUID) {
		session.executeOrDie("virsh secret-undefine $id")
	}

	fun create(session: ClientSession, id: UUID, domainDef: String) {
		val domainDefFile = "/tmp/$id.xml"
		logger.info("creating domain: \n {}", domainDef)
		session.createSftpClient().use { sftp ->
			try {
				sftp.write(domainDefFile).use { file ->
					file.write(domainDef.toByteArray(utf8))
				}
				session.executeOrDie("virsh create $domainDefFile")
			} finally {
				silent(level = LogLevel.Debug, actionName = "clear temporary domain file") { sftp.remove(domainDefFile) }
			}
		}
	}
<commit_msg>cleanup: generating the xml right into the outputstream<commit_after>package com.github.kerubistan.kerub.utils.junix.virt.virsh

import com.github.kerubistan.kerub.host.executeOrDie
import com.github.kerubistan.kerub.host.process
import com.github.kerubistan.kerub.model.SoftwarePackage
import com.github.kerubistan.kerub.model.display.RemoteConsoleProtocol
import com.github.kerubistan.kerub.model.hypervisor.LibvirtArch
import com.github.kerubistan.kerub.model.hypervisor.LibvirtCapabilities
import com.github.kerubistan.kerub.model.hypervisor.LibvirtGuest
import com.github.kerubistan.kerub.utils.LogLevel
import com.github.kerubistan.kerub.utils.equalsAnyOf
import com.github.kerubistan.kerub.utils.flag
import com.github.kerubistan.kerub.utils.getLogger
import com.github.kerubistan.kerub.utils.junix.common.Centos
import com.github.kerubistan.kerub.utils.junix.common.Debian
import com.github.kerubistan.kerub.utils.junix.common.Fedora
import com.github.kerubistan.kerub.utils.junix.common.OsCommand
import com.github.kerubistan.kerub.utils.junix.common.Ubuntu
import com.github.kerubistan.kerub.utils.junix.common.openSuse
import com.github.kerubistan.kerub.utils.silent
import com.github.kerubistan.kerub.utils.toBigInteger
import io.github.kerubistan.kroki.bytes.toBase64
import io.github.kerubistan.kroki.strings.substringBetween
import io.github.kerubistan.kroki.xml.xml
import org.apache.sshd.client.session.ClientSession
import java.io.OutputStream
import java.io.StringReader
import java.util.Properties
import java.util.UUID
import javax.xml.bind.JAXBContext

object Virsh : OsCommand {

	private val logger = getLogger()
	private val utf8 = charset("UTF-8")

	override fun providedBy(): List<Pair<(SoftwarePackage) -> Boolean, List<String>>> = listOf(
			{ distro: SoftwarePackage -> distro.name.equalsAnyOf(Centos, Fedora, openSuse) }
					to listOf("libvirt-client"),
			{ distro: SoftwarePackage -> distro.name.equalsAnyOf(Debian) } to listOf("libvirt-clients",
					"libvirt-daemon"),
			{ distro: SoftwarePackage -> distro.name.equalsAnyOf(Ubuntu) } to listOf("libvirt-bin")
	)

	fun setSecret(session: ClientSession, id: UUID, type: SecretType, value: String) {
		val secretDefFile = "/tmp/$id-secret.xml"
		session.createSftpClient().use { sftp ->
			try {
				sftp.write(secretDefFile).use { file ->
					xml(out = file, root = "secret", atts = *arrayOf("ephemeral" to "no", "private" to "yes")) {
						"uuid" { -id }
						"usage"("type" to type) {
							"target" { -id }
						}
					}
				}
				session.executeOrDie("virsh secret-define $secretDefFile")
				session.executeOrDie("virsh secret-set-value $id ${value.toByteArray().toBase64()}")
			} finally {
				sftp.remove(secretDefFile)
			}
		}

	}

	fun clearSecret(session: ClientSession, id: UUID) {
		session.executeOrDie("virsh secret-undefine $id")
	}

	fun create(session: ClientSession, id: UUID, domainDef: String) {
		val domainDefFile = "/tmp/$id.xml"
		logger.info("creating domain: \n {}", domainDef)
		session.createSftpClient().use { sftp ->
			try {
				sftp.write(domainDefFile).use { file ->
					file.write(domainDef.toByteArray(utf8))
				}
				session.executeOrDie("virsh create $domainDefFile")
			} finally {
				silent(level = LogLevel.Debug, actionName = "clear temporary domain file") { sftp.remove(domainDefFile) }
			}
		}
	}

{'fraction_non_alphanumeric': 0.12796279627962795, 'fraction_numerical': 0.0021002100210021, 'mean_word_length': 4.66921768707483, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2279240', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2244, 'n_words': 366}