8
8
# SPDX-License-Identifier: MIT
9
9
#
10
10
11
+ set -e
12
+
13
+ # Keep it simple in case we're running in a POSIX-shell
14
+ posix_abort () {
15
+ printf " ERROR: %s\n" " $@ " >&2
16
+ exit 1
17
+ }
18
+
19
+ # Fail fast with a concise message when not using bash
20
+ # Single brackets are needed here for POSIX compatibility
21
+ # shellcheck disable=SC2292
22
+ if [ -z " ${BASH_VERSION:- } " ]
23
+ then
24
+ posix_abort " Bash is required to interpret this script."
25
+ fi
26
+
27
+ # Default ssh config/key location
28
+ sshdir=" ${HOME} /.ssh"
29
+ sshconfig=" ${sshdir} /config"
30
+ sshconfigbak=" ${sshconfig} _$( date +%Y-%m-%d) .vsr"
31
+ sshkey=" ${sshdir} /vscode-remote-hpc"
32
+
11
33
# Either query for HPC username and headnode or take CLI arguments
12
34
# (mainly intended for testing!)
13
35
if [ -n " $1 " ]; then
@@ -17,20 +39,96 @@ if [ -n "$2" ]; then
17
39
headnode=" $2 "
18
40
fi
19
41
20
- # Default ssh config/key location
21
- sshdir=" ${HOME} /.ssh"
22
- sshconfig=" ${sshdir} /config"
23
- sshconfigbak=" ${sshconfig} _$( date +%Y-%m-%d) .vsr"
24
- sshkey=" ${sshdir} /vscode-remote-hpc"
42
+ # String formatters to prettify output
43
+ if [[ -t 1 ]]; then
44
+ tty_escape () { printf " \033[%sm" " $1 " ; }
45
+ else
46
+ tty_escape () { : ; }
47
+ fi
48
+ tty_mkbold () { tty_escape " 1;$1 " ; }
49
+ tty_blue=" $( tty_mkbold 34) "
50
+ tty_green=" $( tty_mkbold 32) "
51
+ tty_red=" $( tty_mkbold 31) "
52
+ tty_bold=" $( tty_mkbold 39) "
53
+ tty_reset=" $( tty_escape 0) "
54
+
55
+ shell_join ()
56
+ {
57
+ local arg
58
+ printf " %s" " $1 "
59
+ shift
60
+ for arg in " $@ "
61
+ do
62
+ printf " "
63
+ printf " %s" " ${arg// / \ } "
64
+ done
65
+ }
66
+
67
+ chomp ()
68
+ {
69
+ printf " %s" " ${1/ " $'\n'" / } "
70
+ }
71
+
72
+ announce ()
73
+ {
74
+ printf " ${tty_green} >>> %s <<<${tty_reset} \n" " $( shell_join " $@ " ) "
75
+ }
76
+
77
+ info ()
78
+ {
79
+ printf " ${tty_blue} %s${tty_reset} \n" " $( shell_join " $@ " ) "
80
+ }
81
+
82
+ error ()
83
+ {
84
+ printf " ${tty_red} FAILED:${tty_bold} %s${tty_reset} \n" " $( chomp " $1 " ) "
85
+ }
86
+
87
+ # Ensure errors are communicated properly
88
+ on_exit () {
89
+ if [ $? -ne 0 ]; then
90
+ error " Setup encountered an error. Examine previous error messages for details"
91
+ cleanup
92
+ exit 1
93
+ fi
94
+ info " Bye"
95
+ }
96
+ trap ' on_exit 2> /dev/null' SIGHUP SIGTERM SIGKILL EXIT
97
+
98
+ # Helper to undo any changes made to the user's machine
99
+ cleanup (){
100
+ if [[ -f " ${sshconfig} " ]]; then
101
+ cp -f " ${sshconfig} " " ${sshconfigbak} "
102
+ info " Wrote backup-copy ${sshconfigbak} of current ssh configuration file"
103
+ awk '
104
+ BEGIN {skip=0}
105
+ /^[ \t]*Host[ \t]+vscode-remote-hpc[ \t]*$/ {skip=1; next}
106
+ skip && /^[ \t]/ {next}
107
+ skip && /^[[:space:]]*$/ {next}
108
+ skip {skip=0}
109
+ {print}
110
+ ' " ${sshconfig} " > " ${sshconfig} .tmp" && mv " ${sshconfig} .tmp" " ${sshconfig} "
111
+ info " Block for vscode-remote-hpc has been removed from ${sshconfig} (if it was present)."
112
+ else
113
+ info " ${sshconfig} does not exist. Nothing to remove."
114
+ fi
115
+ info " Removing generated ssh key-pair"
116
+ rm -f " ${sshkey} "
117
+ rm -f " ${sshkey} .pub"
118
+ info " Done"
119
+ }
25
120
26
- echo " --- This script sets up VS Code remote connections to the HPC cluster ---"
121
+ # ----------------------------------------------------------------------
122
+ # START OF INSTALLATION SCRIPT
123
+ # ----------------------------------------------------------------------
124
+ announce " This script sets up VS Code remote connections to the HPC cluster"
27
125
28
126
# Check if vscode-remote-hpc has already been setup
29
127
if [[ -f " ${sshconfig} " && -f " ${sshkey} " ]]; then
30
128
if [[ $# -eq 0 ]]; then
31
- echo " It seems vscode-remote-hpc is already installed. How do you want to proceed?"
32
- echo " 1. Abort"
33
- echo " 2. Uninstall"
129
+ info " It seems vscode-remote-hpc is already installed. How do you want to proceed?"
130
+ info " 1. Abort"
131
+ info " 2. Uninstall"
34
132
read -p " Please choose an option (1 or 2): " choice < /dev/tty
35
133
else
36
134
choice=' 2'
@@ -40,42 +138,25 @@ if [[ -f "${sshconfig}" && -f "${sshkey}" ]]; then
40
138
exit
41
139
;;
42
140
2)
43
- if [[ -f " ${sshconfig} " ]]; then
44
- cp -f " ${sshconfig} " " ${sshconfigbak} "
45
- echo " Wrote backup-copy ${sshconfigbak} of current ssh configuration file"
46
- awk '
47
- BEGIN {skip=0}
48
- /^[ \t]*Host[ \t]+vscode-remote-hpc[ \t]*$/ {skip=1; next}
49
- skip && /^[ \t]/ {next}
50
- skip && /^[[:space:]]*$/ {next}
51
- skip {skip=0}
52
- {print}
53
- ' " ${sshconfig} " > " ${sshconfig} .tmp" && mv " ${sshconfig} .tmp" " ${sshconfig} "
54
- echo " Block for vscode-remote-hpc has been removed from ${sshconfig} (if it was present)."
55
- else
56
- echo " ${sshconfig} does not exist. Nothing to remove."
57
- fi
58
- echo " Removing generated ssh key-pair"
59
- rm -f " ${sshkey} "
60
- rm -f " ${sshkey} .pub"
61
- echo " Done"
62
- echo " All cleaned up, vscode-remote-hpc has been uninstalled. Bye. "
141
+ cleanup
142
+ announce " All cleaned up, vscode-remote-hpc has been uninstalled."
63
143
exit
64
144
;;
65
145
* )
66
- echo " Invalid choice. Aborting."
146
+ error " Invalid choice. Aborting."
67
147
exit
68
148
;;
69
149
esac
70
150
fi
71
151
72
152
# Query account/head node information
73
153
if [[ -z " ${uname+x} " ]]; then
74
- read -p " Please enter your HPC username: " uname < /dev/tty
154
+ info " Please enter your HPC username:"
155
+ read -p " " uname < /dev/tty
75
156
fi
76
157
if [[ -z " ${headnode+x} " ]]; then
77
- echo " Please enter the IP address or hostname of the cluster head node"
78
- read -p " (hub.esi.local at ESI, or 192.168.161.221 at CoBIC): " headnode < /dev/tty
158
+ info " Please enter the IP address or hostname of the cluster head node"
159
+ read -p " (hub.esi.local at ESI, or 192.168.161.221 at CoBIC): " headnode < /dev/tty
79
160
fi
80
161
81
162
# Put together configuration block for ssh config
104
185
if ! grep -qE ' ^[Hh]ost[[:space:]]+vscode-remote-hpc\b' " ${sshconfig} " ; then
105
186
echo " " >> " ${sshconfig} "
106
187
echo " ${configblock} " >> " ${sshconfig} "
107
- echo " Updated ssh configuration"
188
+ info " Updated ssh configuration"
108
189
else
109
- echo " VS Code remote HPC configuration already exists. No changes made."
190
+ info " VS Code remote HPC configuration already exists. No changes made."
110
191
fi
111
192
112
193
# If it does not exist already, create a new ssh key for vscode-remote-hpc
113
194
if [ ! -f " ${sshkey} " ]; then
114
195
if [[ $# -eq 0 ]]; then
115
- read -p " About to create and upload an ssh key to ${headnode} . You will be prompted for your cluster password. Press any key to continue " ans < /dev/tty
196
+ info " About to create and upload an ssh key to ${headnode} "
197
+ info " You will be prompted for your cluster password"
198
+ read -p " Press any key to continue " ans < /dev/tty
116
199
fi
117
200
machine=" ${HOST} "
118
201
if [ -z " ${machine} " ]; then
@@ -123,7 +206,7 @@ if [ ! -f "${sshkey}" ]; then
123
206
ssh-copy-id -i " ${sshkey} " " ${uname} @${headnode} "
124
207
fi
125
208
else
126
- echo " VS Code remote ssh key already exists. No changes made."
209
+ info " VS Code remote ssh key already exists. No changes made."
127
210
fi
128
211
129
- echo " -- All Done --- "
212
+ announce " All Done"
0 commit comments