Simplify deploy logic

This commit is contained in:
Alexander Nozik 2022-06-20 11:55:18 +03:00
parent cfc7fa5161
commit 4484bbfac7
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
4 changed files with 57 additions and 72 deletions

View File

@ -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")
}
}
}

View File

@ -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,8 +99,10 @@ 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")
}
}
}

View File

@ -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)

View File

@ -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)