forked from SPC/spc-site
Simplify deploy logic
This commit is contained in:
parent
cfc7fa5161
commit
4484bbfac7
@ -8,7 +8,7 @@ job("Deploy") {
|
|||||||
env["SPC_USER"] = Secrets("spc-webmaster-user")
|
env["SPC_USER"] = Secrets("spc-webmaster-user")
|
||||||
env["SPC_ID"] = Secrets("spc-webmaster-id")
|
env["SPC_ID"] = Secrets("spc-webmaster-id")
|
||||||
kotlinScript { api ->
|
kotlinScript { api ->
|
||||||
api.gradle("uploadDistribution", "reloadDistribution")
|
api.gradle("uploadDistribution")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,15 +75,23 @@ tasks.getByName("processResources").dependsOn(writeBuildDate)
|
|||||||
|
|
||||||
val host = System.getenv("SPC_HOST")
|
val host = System.getenv("SPC_HOST")
|
||||||
val user = System.getenv("SPC_USER")
|
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 {
|
val uploadDistribution by tasks.creating {
|
||||||
group = "distribution"
|
group = "distribution"
|
||||||
dependsOn("installDist")
|
dependsOn("installDist")
|
||||||
doLast {
|
doLast {
|
||||||
sshUploadDirectory(buildDir.resolve("install/spc-site"), host, user, "/opt") {
|
JSch {
|
||||||
addIdentity("spc-webmaster", identity.encodeToByteArray(), null, null)
|
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 {
|
val reloadDistribution by tasks.creating {
|
||||||
group = "distribution"
|
group = "distribution"
|
||||||
doLast {
|
doLast {
|
||||||
sshExecute(host, user, "sudo systemctl restart sciprog-site") {
|
JSch {
|
||||||
addIdentity("spc-webmaster", identity.encodeToByteArray(), null, null)
|
addIdentity("spc-webmaster", identityString.encodeToByteArray(), null, null)
|
||||||
|
}.useSession(host, user) {
|
||||||
|
execute("sudo systemctl restart $serviceName")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
|
@ -1,7 +1,4 @@
|
|||||||
import com.jcraft.jsch.ChannelSftp
|
import com.jcraft.jsch.*
|
||||||
import com.jcraft.jsch.JSch
|
|
||||||
import com.jcraft.jsch.Session
|
|
||||||
import com.jcraft.jsch.SftpATTRS
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -46,37 +43,57 @@ private fun ChannelSftp.recursiveFolderUpload(sourceFile: File, destinationPath:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun JSch.uploadDirectory(
|
fun Session.uploadDirectory(
|
||||||
file: File,
|
file: File,
|
||||||
host: String,
|
|
||||||
user: String,
|
|
||||||
targetDirectory: String,
|
targetDirectory: String,
|
||||||
port: Int = 22,
|
|
||||||
) {
|
) {
|
||||||
var session: Session? = null
|
|
||||||
var channel: ChannelSftp? = null
|
var channel: ChannelSftp? = null
|
||||||
try {
|
try {
|
||||||
session = getSession(user, host, port)
|
|
||||||
|
|
||||||
val config = Properties()
|
val config = Properties()
|
||||||
config["StrictHostKeyChecking"] = "no"
|
config["StrictHostKeyChecking"] = "no"
|
||||||
session.setConfig(config)
|
channel = openChannel("sftp") as ChannelSftp // Open SFTP Channel
|
||||||
session.connect() // Create SFTP Session
|
|
||||||
channel = session.openChannel("sftp") as ChannelSftp // Open SFTP Channel
|
|
||||||
channel.connect()
|
channel.connect()
|
||||||
channel.cd(targetDirectory) // Change Directory on SFTP Server
|
channel.cd(targetDirectory) // Change Directory on SFTP Server
|
||||||
channel.recursiveFolderUpload(file, targetDirectory)
|
channel.recursiveFolderUpload(file, targetDirectory)
|
||||||
} finally {
|
} finally {
|
||||||
channel?.disconnect()
|
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()
|
session?.disconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sshUploadDirectory(
|
fun JSch(configuration: JSch.() -> Unit): JSch = JSch().apply(configuration)
|
||||||
file: File,
|
|
||||||
host: String,
|
|
||||||
user: String,
|
|
||||||
targetDirectory: String,
|
|
||||||
port: Int = 22,
|
|
||||||
shellConfig: JSch.() -> Unit,
|
|
||||||
) = JSch().apply(shellConfig).uploadDirectory(file, host, user, targetDirectory, port)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user