From 4484bbfac7c9f0195bf0f14c1ac218a59cd3621d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 20 Jun 2022 11:55:18 +0300 Subject: [PATCH] Simplify deploy logic --- .space.kts | 2 +- build.gradle.kts | 24 ++++++++---- buildSrc/src/main/kotlin/execute.kt | 42 -------------------- buildSrc/src/main/kotlin/uploads.kt | 61 ++++++++++++++++++----------- 4 files changed, 57 insertions(+), 72 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/execute.kt diff --git a/.space.kts b/.space.kts index 095b758..288c9ea 100644 --- a/.space.kts +++ b/.space.kts @@ -8,7 +8,7 @@ job("Deploy") { env["SPC_USER"] = Secrets("spc-webmaster-user") env["SPC_ID"] = Secrets("spc-webmaster-id") kotlinScript { api -> - api.gradle("uploadDistribution", "reloadDistribution") + api.gradle("uploadDistribution") } } } diff --git a/build.gradle.kts b/build.gradle.kts index 581d7e0..b5af7ac 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -75,15 +75,23 @@ tasks.getByName("processResources").dependsOn(writeBuildDate) val host = System.getenv("SPC_HOST") val user = System.getenv("SPC_USER") -val identity = System.getenv("SPC_ID") +val identityString = System.getenv("SPC_ID") +val serviceName = "sciprog-site" -if (host != null && user != null || identity != null) { +if (host != null && user != null || identityString != null) { val uploadDistribution by tasks.creating { group = "distribution" dependsOn("installDist") doLast { - sshUploadDirectory(buildDir.resolve("install/spc-site"), host, user, "/opt") { - addIdentity("spc-webmaster", identity.encodeToByteArray(), null, null) + JSch { + addIdentity("spc-webmaster", identityString.encodeToByteArray(), null, null) + }.useSession(host, user) { + //stopping service during the upload + execute("sudo systemctl stop $serviceName") + uploadDirectory(buildDir.resolve("install/spc-site"), "/opt") + //adding executable flag to the entry point + execute("sudo chmod +x /opt/spc-site/bin/spc-site") + execute("sudo systemctl start $serviceName") } } } @@ -91,12 +99,14 @@ if (host != null && user != null || identity != null) { val reloadDistribution by tasks.creating { group = "distribution" doLast { - sshExecute(host, user, "sudo systemctl restart sciprog-site") { - addIdentity("spc-webmaster", identity.encodeToByteArray(), null, null) + JSch { + addIdentity("spc-webmaster", identityString.encodeToByteArray(), null, null) + }.useSession(host, user) { + execute("sudo systemctl restart $serviceName") } } } -}else { +} else { logger.error("Host, user or ID are not defined. Skipping deployment tasks.") } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/execute.kt b/buildSrc/src/main/kotlin/execute.kt deleted file mode 100644 index 2d5eb36..0000000 --- a/buildSrc/src/main/kotlin/execute.kt +++ /dev/null @@ -1,42 +0,0 @@ -import com.jcraft.jsch.ChannelExec -import com.jcraft.jsch.JSch -import com.jcraft.jsch.Session -import java.util.* - - -fun JSch.execute( - host: String, - user: String, - command: String, - port: Int = 22, -): String { - var session: Session? = null - var channel: ChannelExec? = null - try { - session = getSession(user, host, port) - - val config = Properties() - config["StrictHostKeyChecking"] = "no" - session.setConfig(config) - session.connect() - channel = session.openChannel("exec") as ChannelExec - channel.setCommand(command) - channel.inputStream = null - channel.setErrStream(System.err) - val input = channel.inputStream - channel.connect() - return input.use { it.readAllBytes().decodeToString() } - } finally { - channel?.disconnect() - session?.disconnect() - } -} - - -fun sshExecute( - host: String, - user: String, - command: String, - port: Int = 22, - shellConfig: JSch.() -> Unit, -): String = JSch().apply(shellConfig).execute(host, user, command, port) \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/uploads.kt b/buildSrc/src/main/kotlin/uploads.kt index 3dea743..19df10a 100644 --- a/buildSrc/src/main/kotlin/uploads.kt +++ b/buildSrc/src/main/kotlin/uploads.kt @@ -1,7 +1,4 @@ -import com.jcraft.jsch.ChannelSftp -import com.jcraft.jsch.JSch -import com.jcraft.jsch.Session -import com.jcraft.jsch.SftpATTRS +import com.jcraft.jsch.* import java.io.File import java.io.FileInputStream import java.util.* @@ -46,37 +43,57 @@ private fun ChannelSftp.recursiveFolderUpload(sourceFile: File, destinationPath: } } -fun JSch.uploadDirectory( +fun Session.uploadDirectory( file: File, - host: String, - user: String, targetDirectory: String, - port: Int = 22, ) { - var session: Session? = null var channel: ChannelSftp? = null try { - session = getSession(user, host, port) - val config = Properties() config["StrictHostKeyChecking"] = "no" - session.setConfig(config) - session.connect() // Create SFTP Session - channel = session.openChannel("sftp") as ChannelSftp // Open SFTP Channel + channel = openChannel("sftp") as ChannelSftp // Open SFTP Channel channel.connect() channel.cd(targetDirectory) // Change Directory on SFTP Server channel.recursiveFolderUpload(file, targetDirectory) } finally { channel?.disconnect() + } +} + +fun Session.execute( + command: String, +): String { + var channel: ChannelExec? = null + try { + channel = openChannel("exec") as ChannelExec + channel.setCommand(command) + channel.inputStream = null + channel.setErrStream(System.err) + val input = channel.inputStream + channel.connect() + return input.use { it.readAllBytes().decodeToString() } + } finally { + channel?.disconnect() + } +} + +inline fun JSch.useSession( + host: String, + user: String, + port: Int = 22, + block: Session.() -> Unit, +) { + var session: Session? = null + try { + session = getSession(user, host, port) + val config = Properties() + config["StrictHostKeyChecking"] = "no" + session.setConfig(config) + session.connect() + session.block() + } finally { session?.disconnect() } } -fun sshUploadDirectory( - file: File, - host: String, - user: String, - targetDirectory: String, - port: Int = 22, - shellConfig: JSch.() -> Unit, -) = JSch().apply(shellConfig).uploadDirectory(file, host, user, targetDirectory, port) +fun JSch(configuration: JSch.() -> Unit): JSch = JSch().apply(configuration)