| 1 | #!/usr/bin/env -S v run |
| 2 | |
| 3 | import os |
| 4 | import term |
| 5 | |
| 6 | const server = 'gitly' |
| 7 | const remote_path = '/var/www/gitly' |
| 8 | const local_binary = 'gitly_linux' |
| 9 | |
| 10 | // Shared SSH connection so the key passphrase is only asked once. |
| 11 | const control_path = os.join_path(os.temp_dir(), 'gitly_deploy_cm.sock') |
| 12 | const ssh_opts = '-o ControlMaster=auto -o ControlPath=${control_path} -o ControlPersist=5m' |
| 13 | |
| 14 | fn 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 | |
| 41 | fn 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 | |