universal-ci-cd-scripts/src/main/groovy/script/deploy_ssh_profile_setup.groovy
2025-03-04 15:35:36 +03:00

70 lines
2.4 KiB
Groovy
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package script
/**
* Скрипт для начальной конфигурации SSH профиля для деплоя
* Создает профиль для подключения к стенду, а также настраивает пользователя для этих целей.
*/
@GrabResolver(name='gitea', root='https://git.tswf.io/api/packages/public-repos/maven')
@Grapes([
@Grab(group='io.tswf.groovy.better-groovy', module='better-groovy-scripting-shell', version='2.0.2-SNAPSHOT', changing = true),
@Grab(group='io.tswf.groovy.better-groovy', module='better-groovy-scripting-gitea', version='2.0.2-SNAPSHOT', changing = true)
])
import util.GlobalProperties
import util.ScriptLog
println """
#############################################################################
SSH Profile Setup Script
#############################################################################
"""
GlobalProperties.loadGlobalProperties()
ScriptLog.printf "Creating .ssh directory..."
def sshDir = new File("${System.getProperty("user.home")}/.ssh")
sshDir.mkdirsUnsafe()
// Записываем приватный ключ
ScriptLog.printf "Writing ssh private key..."
def sshPrivateKey = sshDir.subFile("id_rsa")
//TODO: может быть тут посолить?
def sshPrivateKeyBase64Content = System.getGlobalProperty("ci.deploy.ssh.profile.private-key-base64").removeAll(' ')
def sshPrivateKeyDecodedContent = Base64.decoder.decode(sshPrivateKeyBase64Content)
sshPrivateKey.bytes = sshPrivateKeyDecodedContent
sh 'chmod 400 ~/.ssh/id_rsa'
// Записываем ssh config
ScriptLog.printf "Writing ssh config..."
def sshProfileName = System.getGlobalProperty("ci.deploy.ssh.profile")
def sshHost = System.getGlobalProperty("ci.deploy.ssh.host")
def sshPort = System.getGlobalProperty("ci.deploy.ssh.port")
def sshUsername = System.getGlobalProperty("ci.deploy.ssh.username")
def sshConfigFile = sshDir.subFile("config")
sshConfigFile.text = """
Host ${sshProfileName}
HostName ${sshHost}
User ${sshUsername}
Port ${sshPort}
""".stripIndent(false)
// Выполняем тестовое подключение к SSH, чтобы убедиться в корректности настройки.
ScriptLog.printf "Validating ssh configuration..."
sh "ssh ${sshProfileName} -o StrictHostKeyChecking=no -tt 'echo Testing connection!'"
ScriptLog.printf "SSH profile configured successfully!"