gitlyx / deploy.vsh
47 lines · 38 sloc · 1.52 KB · b0fb181a59d209d513aa2d5395800586b24ac388
Raw
1#!/usr/bin/env -S v run
2
3import os
4import term
5
6const server = 'gitly'
7const remote_path = '/var/www/gitly'
8const local_binary = 'gitly_linux'
9
10// Shared SSH connection so the key passphrase is only asked once.
11const control_path = os.join_path(os.temp_dir(), 'gitly_deploy_cm.sock')
12const ssh_opts = '-o ControlMaster=auto -o ControlPath=${control_path} -o ControlPersist=5m'
13
14fn main() {
15 if !os.exists(local_binary) {
16 eprintln(term.red('${local_binary} not found. Build it first with:'))
17 eprintln(' ~/code/v3/v -os linux -d use_openssl -cc clang -gc none -prealloc -cflags "-O2" -o ${local_binary} .')
18 exit(1)
19 }
20
21 // Open a single master connection up front (prompts for the passphrase once).
22 // Every rsync/ssh below reuses it via the control socket.
23 println('Opening shared SSH connection...')
24 exec_safe('ssh ${ssh_opts} -fN ${server}')
25 defer {
26 os.system('ssh ${ssh_opts} -O exit ${server} 2>/dev/null')
27 }
28
29 println('Step 1: Syncing binary, static/ and translations/...')
30 rsync := 'rsync -avz -e "ssh ${ssh_opts}"'
31 exec_safe('${rsync} ${local_binary} ${server}:${remote_path}/gitly')
32 exec_safe('${rsync} static/ ${server}:${remote_path}/static/')
33 exec_safe('${rsync} translations/ ${server}:${remote_path}/translations/')
34
35 println('\nStep 2: Restarting gitly...')
36 exec_safe('ssh ${ssh_opts} ${server} "sudo systemctl restart gitly"')
37
38 println(term.green('\nDeployment successful!'))
39}
40
41fn exec_safe(cmd string) {
42 println('>>> ${cmd}')
43 if os.system(cmd) != 0 {
44 eprintln(term.red('\n Error executing command.'))
45 exit(1)
46 }
47}
48