2024-11-16 22:50:41 -05:00
pipeline {
agent any
parameters {
2024-11-17 15:01:57 -05:00
string ( name: 'servicename' , description: "service name" )
2024-11-16 22:50:41 -05:00
string ( name: 'svcdesc' , description: "service description" )
2024-11-21 13:02:37 -05:00
string ( name: 'targetHost' , description: "system to live on" , defaultValue: "moloryb.lan" )
2024-11-17 15:01:57 -05:00
}
environment {
2024-12-03 01:47:52 -05:00
SUDOER_ALLOCES = credentials ( 'a674f816-2b35-4d60-ba60-7b66e86f3c5c' )
SUDOER_MOLORYB = credentials ( '1f3b965e-bcc0-4074-99f2-b64dddbf7de7' )
SUDOERSSHID = '2c48e1a9-22b2-455c-9959-6b29e86d3fb5'
SUDOERSSH = credentials ( '2c48e1a9-22b2-455c-9959-6b29e86d3fb5' )
JENKINS = credentials ( '68391381-e095-4b47-b956-d23055b0808e' )
2024-11-21 13:02:37 -05:00
GITEATOKEN = credentials ( 'd0e86441-2157-405f-8539-a9a9010c6ecf' )
2024-12-06 17:06:04 -05:00
GITEA_USR = 'jenkins'
2024-11-17 15:01:57 -05:00
}
2024-11-16 22:50:41 -05:00
stages {
2024-12-13 17:01:31 -05:00
stage ( "environment setup" ) { //my environment, here.
2024-11-16 22:50:41 -05:00
steps {
script {
2024-11-17 15:01:57 -05:00
if ( servicename . isEmpty ( ) ) {
error ( "servicename mandatory" )
}
if ( servicename . contains ( ' ' ) ) {
error ( "servicename cannot have spaces. try dashes." )
2024-11-16 22:50:41 -05:00
}
2024-11-17 16:02:56 -05:00
2024-12-06 23:46:36 -05:00
sh "" " # ! /bin/ bash
function testcmd ( ) {
if ! command - v \ $1 2 > & 1 > /dev/ null
then
echo "this agent doesn't have \$1"
exit 1
fi
}
testcmd mktemp
testcmd curl
testcmd git
testcmd sed
testcmd ssh
testcmd ssh - keyscan
testcmd ssh - keygen
testcmd scp
testcmd jq
"" "
2024-11-17 16:02:56 -05:00
switch ( targetHost ) {
2024-11-19 11:49:50 -05:00
case "alloces.lan" :
2024-12-03 01:47:52 -05:00
SUDOER_USR = SUDOER_ALLOCES_USR
SUDOER_PSW = SUDOER_ALLOCES_PSW
2024-11-21 00:13:06 -05:00
case "moloryb.lan" :
2024-12-03 01:47:52 -05:00
SUDOER_USR = SUDOER_MOLORYB_USR
SUDOER_PSW = SUDOER_MOLORYB_PSW
2024-11-17 16:02:56 -05:00
break
default :
2024-11-19 11:49:50 -05:00
error ( "target host not recognized. btw: yes .lan, all lowercase." )
2024-11-17 16:02:56 -05:00
}
2024-12-03 00:21:42 -05:00
env . pw_linuxserviceaccount = sh ( returnStdout: true , script: "mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) . trim ( )
2024-11-17 16:02:56 -05:00
echo env . pw_linuxserviceaccount
2024-12-03 00:21:42 -05:00
env . pw_productiondatabase = sh ( returnStdout: true , script: "mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) . trim ( )
2024-11-17 16:02:56 -05:00
echo env . pw_productiondatabase
2024-12-03 00:21:42 -05:00
env . pw_developmentdatabase = sh ( returnStdout: true , script: "mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) . trim ( )
2024-11-17 16:02:56 -05:00
echo env . pw_developmentdatabase
2024-11-16 22:50:41 -05:00
}
}
}
stage ( "gitea project" ) {
steps {
2024-11-17 16:02:56 -05:00
sh "" "
curl - X 'POST' \
2024-11-21 13:02:37 -05:00
'https://gitea.arg.rip/api/v1/repos/greyn/_template-service/generate' \
2024-11-17 16:02:56 -05:00
- H 'accept: application/json' \
2024-11-21 13:02:37 -05:00
- H 'Authorization: token ${env.GITEATOKEN}' \
2024-11-17 16:02:56 -05:00
- H 'Content-Type: application/json' \
- d ' {
2024-11-19 02:07:51 -05:00
"description" : "${svcdesc}" ,
"git_content" : true ,
"git_hooks" : true ,
"labels" : true ,
"name" : "${servicename}" ,
"owner" : "greyn" ,
"private" : false ,
"protected_branch" : true ,
"topics" : true ,
"webhooks" : true
} '
2024-11-17 16:02:56 -05:00
"" "
2024-11-19 02:07:51 -05:00
}
}
stage ( "jenkins pipeline" ) {
steps {
2024-11-21 13:02:37 -05:00
//the bad news is that it looks like it's not allowed to trigger just any old job remotely
//the good news is that this seems to pick it up pretty reliably
//sh """
// curl -X POST -L --user ${env.JENKINS_USR}:${env.JENKINS_PSW} \
// alloces.lan:8080/job/gitea.arg.rip/build
// """
2024-11-19 02:07:51 -05:00
timeout ( time: 5 , unit: 'MINUTES' ) {
sh "" "
strRes = ""
2024-11-21 13:02:37 -05:00
while [ - z "\$strRes" ] ;
2024-11-19 02:07:51 -05:00
do
sleep 5 ;
2024-12-03 00:21:42 -05:00
# curl - X GET $ { env . JENKINS_USR } : $ { env . JENKINS_PSW } @alloces.lan : 8080 /job/ gitea . arg . rip /api/ json
curl - X GET - s $ { env . JENKINS_USR } : $ { env . JENKINS_PSW } @alloces.lan : 8080 /job/ gitea . arg . rip /api/ json > f . json
2024-11-27 15:39:21 -05:00
strRes = \ $ ( jq '.jobs' f . json | jq '.[] | select(.name==\"${env.servicename}\")' )
rm f . json
2024-11-19 02:07:51 -05:00
done
"" "
2024-11-17 23:25:14 -05:00
}
2024-12-01 19:02:17 -05:00
withCredentials ( [ sshUserPrivateKey ( credentialsId: 'f42347e9-e3b5-44af-a1af-c5e7b9775fee' , keyFileVariable: 'PK' ) ] ) {
sh "" " # ! /bin/ bash
2024-11-19 02:07:51 -05:00
2024-12-01 19:02:17 -05:00
# mkdir - p ~ / . ssh
2024-12-06 22:29:52 -05:00
ssh - keyscan - t ed25519 gitea . arg . rip > > ~ /.ssh/ known_hosts
2024-12-01 19:02:17 -05:00
# cat ~ /.ssh/ known_hosts
# shit doesn ' t work . ssh in , git clone , get your shit set up for keys .
git - c core . sshCommand = "ssh -i '$PK'\" clone ssh: //git@gitea.arg.rip:8022/greyn/${servicename}.git
# fyi , future me: pushd is useless here , it pops out . Like it ' s loading a script and exiting .
2024-11-17 23:25:14 -05:00
"" "
2024-12-13 17:01:31 -05:00
script {
sh "" " # ! /bin/ bash
2024-12-02 22:00:17 -05:00
2024-12-13 17:01:31 -05:00
pushd $ { servicename }
2024-11-19 11:49:50 -05:00
2024-12-13 21:06:44 -05:00
databaseString = "Host=${targetHost};Database=${servicename};Username=${servicename};Password=${env.pw_productiondatabase};IncludeErrorDetail=true;"
2024-12-13 17:01:31 -05:00
databasecredsid = \ $ ( uuidgen )
httpBasicAuth = \ "http://${env.JENKINS_USR}:${env.JENKINS_PSW}@alloces.lan:8080/\"
urlGetData = \ "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\\" : \ \ ",//crumb)\"
CRUMB = \ $ ( curl - s - c cookies . txt \ "\${httpBasicAuth}\${urlGetData}\" )
curl - H \ $CRUMB - X POST \ "http://${env.JENKINS_USR}:${env.JENKINS_PSW}@alloces.lan:8080/job/gitea.arg.rip/job/${servicename}/credentials/store/folder/domain/_/createCredentials\" \
- - data - urlencode ' json = {
"" : "0" ,
"credentials" : {
"scope" : "GLOBAL" ,
"id" : "'" \ $databasecredsid "'" ,
2024-12-13 21:06:44 -05:00
"secret" : "\$databaseString" ,
2024-12-13 17:01:31 -05:00
"description" : "database connection string" ,
"\$class" : "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
} '
credsStr = "productiondatabase_connectionString=credentials(\\" \ $databasecredsid \ \ ")"
sed - i "s/productiondatabase_connectionString=creds/\$credsStr/" Jenkinsfile
2024-11-19 11:49:50 -05:00
2024-12-13 17:01:31 -05:00
rm cookies . txt
git add .
git commit - m \ "set up for database\"
2024-12-13 21:06:44 -05:00
cp appsettings . sample . json appsettings . json
2024-12-18 19:47:33 -05:00
sed - i "s/\\" DBConnectionString \ \ " *: *\\" [ ^ \ \ "]*\\" /\\"DBConnectionString\\":\\"\${databaseString}\\"/ " appsettings . json
2024-12-13 17:01:31 -05:00
"" "
2024-11-19 11:49:50 -05:00
2024-12-03 01:47:52 -05:00
sh "" " # ! /bin/ bash
2024-12-01 19:02:17 -05:00
usernameCredsId = \ $ ( uuidgen )
2024-11-19 02:07:51 -05:00
2024-12-03 01:47:52 -05:00
CRUMB = \ $ ( curl - c cookies . txt 'http://${env.JENKINS_USR}:${env.JENKINS_PSW}@alloces.lan:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' )
echo \ $CRUMB
2024-12-03 00:21:42 -05:00
curl - X POST 'http://${env.JENKINS_USR}:${env.JENKINS_PSW}@alloces.lan:8080/job/gitea.arg.rip/job/${servicename}/credentials/store/folder/domain/_/createCredentials' \
2024-11-19 02:07:51 -05:00
- - data - urlencode ' json = {
"" : "0" ,
"credentials" : {
"scope" : "GLOBAL" ,
2024-12-03 01:47:52 -05:00
"id" : "'" \ $usernameCredsId "'" ,
2024-11-19 11:49:50 -05:00
"username" : "${servicename}" ,
"password" : "${env.pw_linuxserviceaccount}" ,
"description" : "service account login" ,
"\$class" : "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
2024-11-19 02:07:51 -05:00
}
} '
2024-11-19 11:49:50 -05:00
certCredsId = \ $ ( uuidgen )
2024-11-19 02:07:51 -05:00
2024-12-03 01:47:52 -05:00
ssh - keygen - t ed25519 - f "${servicename}-ssh" - N ""
2024-12-18 19:47:33 -05:00
privatekeycontent = \ $ ( cat $ { servicename } - ssh ) \ $ '\\n'
privatekeycontent = " \ $privatekeycontent
"
2024-12-13 21:06:44 -05:00
echo "private key content: \$privatekeycontent hopefully newline isn't the issue."
2024-12-03 01:47:52 -05:00
pubkeycontent = \ $ ( cat $ { servicename } - ssh . pub )
CRUMB = \ $ ( curl - s 'http://${env.JENKINS_USR}:${env.JENKINS_PSW}@alloces.lan:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' )
echo \ $CRUMB
2024-12-03 00:21:42 -05:00
curl - X POST 'http://${env.JENKINS_USR}:${env.JENKINS_PSW}@alloces.lan:8080/job/gitea.arg.rip/job/${servicename}/credentials/store/folder/domain/_/createCredentials' \
2024-11-19 11:49:50 -05:00
- - data - urlencode ' json = {
"" : "0" ,
"credentials" : {
"scope" : "GLOBAL" ,
2024-12-13 17:01:31 -05:00
"id" : "'" \ $certCredsId "'" ,
2024-11-19 11:49:50 -05:00
"username" : "${servicename}" ,
"password" : "" ,
"privateKeySource" : {
2024-12-01 19:02:17 -05:00
"stapler-class" : "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey\$DirectEntryPrivateKeySource" ,
2024-12-18 19:47:33 -05:00
"privateKey" : "\$privatekeycontent" ,
2024-11-19 11:49:50 -05:00
} ,
"description" : "${servicename}" ,
"stapler-class" : "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
} ,
"description" : "service account ssh" ,
"\$class" : "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
}
} '
2024-12-07 00:25:43 -05:00
rm cookies . txt
pushd $ { servicename }
2024-11-19 02:07:51 -05:00
2024-12-07 00:25:43 -05:00
linuxServiceAccountStr = "linuxServiceAccount=credentials(\\" \ $usernameCredsId \ \ ")"
2024-12-13 17:01:31 -05:00
linuxServiceAccountIDStr = "linuxServiceAccountID=\\" \ $certCredsId \ \ ""
2024-12-07 00:25:43 -05:00
sed - i "s/linuxServiceAccount=creds/\$linuxServiceAccountStr/" Jenkinsfile
2024-12-13 17:01:31 -05:00
sed - i "s/linuxServiceAccountID=string/\$linuxServiceAccountIDStr/" Jenkinsfile
2024-11-19 11:49:50 -05:00
sed - i 's/targetHost=string/targetHost="${targetHost}"/' Jenkinsfile
2024-12-07 00:25:43 -05:00
git add .
git commit - m "linux account, service host"
2024-11-19 02:07:51 -05:00
2024-12-06 20:19:28 -05:00
git - c core . sshCommand = "ssh -i '${PK}'\" push
2024-11-19 11:49:50 -05:00
"" "
}
2024-11-17 16:02:56 -05:00
}
2024-11-17 15:01:57 -05:00
}
}
2024-11-16 22:50:41 -05:00
stage ( "service account" ) {
steps {
script {
2024-12-03 01:47:52 -05:00
withCredentials ( [ sshUserPrivateKey ( credentialsId: env . SUDOERSSHID , keyFileVariable: 'PK' ) ] )
2024-11-17 16:02:56 -05:00
{
2024-12-06 20:39:16 -05:00
sh "" " # ! /bin/ bash
scp - i \ "${PK}\" $ { servicename } - ssh . pub $ { SUDOER_USR } @ $ { targetHost } : ~ / ssh . pub
"" "
2024-12-06 20:19:28 -05:00
2024-12-03 01:47:52 -05:00
sh "" " # ! /bin/ bash
ssh - keyscan - t ed25519 $ { targetHost } > > ~ /.ssh/ known_hosts
2024-12-06 20:19:28 -05:00
2024-12-06 22:29:52 -05:00
curl - u '${env.GITEA_USR}:${env.GITEATOKEN}' https: //gitea.arg.rip/greyn/deployment/raw/branch/main/scripts/serviceaccount.sh --output serviceaccount.sh
2024-12-06 17:06:04 -05:00
sed - i 's/USERNAMETOADD=/USERNAMETOADD="${servicename}"/' serviceaccount . sh
sed - i 's/PASSWORDTOADD=/PASSWORDTOADD="${env.pw_linuxserviceaccount}"/' serviceaccount . sh
sed - i 's/SUDOER_PSW=/SUDOER_PSW="${SUDOER_PSW}"/' serviceaccount . sh
ssh - i \ "${PK}\" - tt $ { SUDOER_USR } @ $ { targetHost } < serviceaccount . sh
rm serviceaccount . sh
2024-11-19 11:49:50 -05:00
"" "
2024-11-17 16:02:56 -05:00
}
2024-11-16 22:50:41 -05:00
}
}
}
2024-11-17 13:24:01 -05:00
stage ( "db init" ) {
steps {
//i'm pretty sure "update" with nothing will init?
//meaning we don't have to init, first update will init
script {
2024-12-03 01:47:52 -05:00
withCredentials ( [ sshUserPrivateKey ( credentialsId: env . SUDOERSSHID , keyFileVariable: 'PK' ) ] )
2024-11-17 16:02:56 -05:00
{
2024-12-03 01:47:52 -05:00
sh "" " # ! /bin/ bash
2024-12-06 22:29:52 -05:00
curl - u '${env.GITEA_USR}:${env.GITEATOKEN}' https: //gitea.arg.rip/greyn/deployment/raw/branch/main/scripts/databases.sh --output databases.sh
sed - i 's/SUDOER_PSW=/SUDOER_PSW="${SUDOER_PSW}"/' databases . sh
sed - i 's/pw_productiondatabase=/pw_productiondatabase="${env.pw_productiondatabase}/' databases . sh
sed - i 's/pw_developmentdatabase=/pw_developmentdatabase="${env.pw_developmentdatabase}/' databases . sh
sed - i 's/servicename=/servicename="${servicename}"/' databases . sh
ssh - i \ "${PK}\" - tt $ { SUDOER_USR } @ $ { targetHost } < databases . sh
rm databases . sh
2024-12-03 01:47:52 -05:00
"" "
2024-11-17 16:02:56 -05:00
}
2024-11-17 13:24:01 -05:00
}
}
}
2024-11-16 22:50:41 -05:00
stage ( "initial service setup" ) {
steps {
2024-12-13 21:06:44 -05:00
sh "" " # ! /bin/ bash
2024-12-18 19:47:33 -05:00
ssh - i "${servicename}-ssh" - tt $ { servicename } @ $ { targetHost } "mkdir -p ~/dist"
scp - i "${servicename}-ssh" $ { servicename } /appsettings.json ${servicename}@${targetHost}:~/ dist / appsettings . json
2024-12-13 21:06:44 -05:00
2024-12-06 22:29:52 -05:00
ssh - i "${servicename}-ssh" - tt $ { servicename } @ $ { targetHost } "mkdir -p ~/.config/systemd/user/"
scp - i "${servicename}-ssh" $ { servicename } /${servicename}.service ${servicename}@${targetHost}:~/ . config /systemd/ user / $ { servicename } . service
ssh - i "${servicename}-ssh" - tt $ { servicename } @ $ { targetHost } 'systemctl --user daemon-reload'
ssh - i "${servicename}-ssh" - tt $ { servicename } @ $ { targetHost } 'systemctl --user enable ${servicename}.service'
"" "
2024-11-16 22:50:41 -05:00
}
}
2024-11-19 11:49:50 -05:00
}
post {
failure {
2024-11-21 13:02:37 -05:00
matrixSendMessage https: true , hostname: 'greyn.club' , port: 8448 , accessTokenCredentialsId: '040b63d1-2f14-4692-badb-114bddd7c5a5' , roomId: '!QmOCACetHdGDlNFsZP:greyn.club' , body: '1-click service failed :(' , formattedBody: "1-click service <b>failed</b> :("
2024-11-19 11:49:50 -05:00
}
success {
2024-12-07 00:25:43 -05:00
matrixSendMessage https: true , hostname: 'greyn.club' , port: 8448 , accessTokenCredentialsId: '040b63d1-2f14-4692-badb-114bddd7c5a5' , roomId: '!QmOCACetHdGDlNFsZP:greyn.club' , body: "${servicename} ready to go on ${targetHost}. username: ${servicename}, password: ${env.pw_linuxserviceaccount}. if db, prod db pw: ${env.pw_productiondatabase} and dev pw: ${env.pw_developmentdatabase}"
2024-11-19 11:49:50 -05:00
//TODO: archiveArtifacts the password data, then store them somewhere
2024-11-17 16:02:56 -05:00
}
2024-11-16 22:50:41 -05:00
}
}