-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_vnc.sh
More file actions
executable file
·1946 lines (1805 loc) · 76 KB
/
Copy pathremote_vnc.sh
File metadata and controls
executable file
·1946 lines (1805 loc) · 76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -Eeuo pipefail
script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${script_directory}/cluster_helpers.sh"
cluster_name="bluehive3"
partition_name="doppelbock"
cpu_count=8
gpu_count=0
memory_gb=64
time_hours=120
requested_node=""
root_override=""
open_vnc_viewer=true
restart_existing_job=false
startup_timeout_seconds=300
opencodex_startup_timeout_seconds=600
image_build_timeout_seconds=1800
environment_build_timeout_seconds=10800
environment_name="default"
environment_mode="mutable"
vnc_geometry="2560x1440"
local_opencodex_port=10102
start_ssh_control_script="${script_directory}/start_ssh_control.sh"
read_user_password_script="${script_directory}/read_user_password.sh"
remote_tools_script="${script_directory}/remote_tools.sh"
vnc_bundle_directory="${script_directory}/remote_vnc"
managed_identity_file="${XDG_DATA_HOME:-${HOME}/.local/share}/unix-scripts/id_ed25519"
identity_file="${REMOTE_VNC_IDENTITY_FILE:-${managed_identity_file}}"
shared_image_default="/scratch/snormanh_lab/shared/remote-vnc/images/ubuntu-vnc-xfce-g3_24.04.sif"
print_usage() {
cat <<'EOF'
Usage: remote_vnc.sh [options]
Start or reuse an independent VNC Slurm job. A mutable job starts OpenCodex,
Codex app-server, and a public-key-only SSH service inside its persistent
Apptainer environment. Connection settings come from the profile and job state;
standard SSH aliases share the resulting tunnels. The blhc3 command opens Fish
inside the container; bluehive-host-shell returns to the allocated host.
The resource options match remote_sshd.sh. They apply when a new VNC job is
submitted; an already-running VNC job is reused and its actual allocation is
reported.
Options:
-p, --partition PARTITION Slurm partition (default: doppelbock)
-a, --cluster CLUSTER Cluster name (default: bluehive3)
-c, --cpus CPUS CPUs for a new job (default: 8)
-g, --gpus GPUS GPUs for a new job; 0 disables GPUs (default: 0)
-m, --memory MEMORY Memory in GiB for a new job (default: 64)
-t, --time HOURS Time limit in hours for a new job (default: 120)
-w, --node NODE Request a specific compute node
-r, --root PATH Override REMOTE_SHARED_ROOT
--env NAME Persistent environment name (default: default)
--geometry WIDTHxHEIGHT Initial/fallback VNC size (default: 2560x1440)
--immutable Run the original read-only VNC image
--restart Replace the current VNC job with a newly managed job
--no-open Prepare SSH and VNC without opening a VNC viewer
-h, --help Show this help
After startup:
blhc3
ssh blhc3
blhc3 'slab'
blhc3 'sq'
Configuration:
First use asks for the remote profile and can save it under ~/.config.
EOF
}
log_message() {
printf '[remote-vnc] %s\n' "$*"
}
fail() {
printf '[remote-vnc] Error: %s\n' "$*" >&2
exit 1
}
require_option_value() {
local option_name="$1"
local option_value="${2:-}"
[[ -n "${option_value}" && "${option_value}" != -* ]] ||
fail "${option_name} requires a value"
}
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--partition)
require_option_value "$1" "${2:-}"
partition_name="$2"
shift 2
;;
-a|--cluster)
require_option_value "$1" "${2:-}"
cluster_name="$2"
shift 2
;;
--cluster=*)
cluster_name="${1#*=}"
shift
;;
-c|--cpus)
require_option_value "$1" "${2:-}"
cpu_count="$2"
shift 2
;;
-g|--gpus)
require_option_value "$1" "${2:-}"
gpu_count="$2"
shift 2
;;
-m|--memory)
require_option_value "$1" "${2:-}"
memory_gb="$2"
shift 2
;;
-t|--time)
require_option_value "$1" "${2:-}"
time_hours="$2"
shift 2
;;
-w|--node)
require_option_value "$1" "${2:-}"
requested_node="$2"
shift 2
;;
-r|--root)
require_option_value "$1" "${2:-}"
root_override="$2"
shift 2
;;
--root=*)
root_override="${1#*=}"
[[ -n "${root_override}" ]] || fail "--root requires a value"
shift
;;
--env)
require_option_value "$1" "${2:-}"
environment_name="$2"
shift 2
;;
--env=*)
environment_name="${1#*=}"
[[ -n "${environment_name}" ]] || fail "--env requires a value"
shift
;;
--geometry)
require_option_value "$1" "${2:-}"
vnc_geometry="$2"
shift 2
;;
--geometry=*)
vnc_geometry="${1#*=}"
[[ -n "${vnc_geometry}" ]] || fail "--geometry requires a value"
shift
;;
--immutable)
environment_mode="immutable"
shift
;;
--no-open)
open_vnc_viewer=false
shift
;;
--restart)
restart_existing_job=true
shift
;;
-h|--help)
print_usage
exit 0
;;
--)
shift
break
;;
*)
fail "unknown argument: $1"
;;
esac
done
[[ $# -eq 0 ]] || fail "unexpected positional arguments: $*"
require_cluster "${cluster_name}" || exit 1
[[ "${partition_name}" =~ ^[A-Za-z0-9._-]+$ ]] ||
fail "invalid partition name: ${partition_name}"
[[ "${cpu_count}" =~ ^[1-9][0-9]*$ ]] || fail "CPUS must be a positive integer"
[[ "${gpu_count}" =~ ^[0-9]+$ ]] || fail "GPUS must be a non-negative integer"
[[ "${memory_gb}" =~ ^[1-9][0-9]*$ ]] || fail "MEMORY must be a positive integer"
[[ "${time_hours}" =~ ^[1-9][0-9]*$ ]] || fail "HOURS must be a positive integer"
[[ "${environment_name}" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] ||
fail "invalid environment name: ${environment_name}"
if [[ "${vnc_geometry}" =~ ^([0-9]+)x([0-9]+)$ ]]; then
vnc_width="${BASH_REMATCH[1]}"
vnc_height="${BASH_REMATCH[2]}"
else
fail "invalid VNC geometry: ${vnc_geometry} (expected WIDTHxHEIGHT)"
fi
((vnc_width >= 1024 && vnc_width <= 7680 &&
vnc_height >= 768 && vnc_height <= 4320)) ||
fail "VNC geometry must be between 1024x768 and 7680x4320"
if [[ -n "${requested_node}" ]]; then
[[ "${requested_node}" =~ ^[A-Za-z0-9._-]+$ ]] ||
fail "invalid node name: ${requested_node}"
fi
for required_command in ssh scp sftp ssh-keygen lsof nc curl awk sed cksum mktemp launchctl plutil; do
command -v "${required_command}" >/dev/null 2>&1 ||
fail "required command is unavailable: ${required_command}"
done
[[ -x "${start_ssh_control_script}" ]] ||
fail "SSH control script is missing or is not executable: ${start_ssh_control_script}"
[[ -r "${read_user_password_script}" ]] ||
fail "user configuration script is missing: ${read_user_password_script}"
[[ -r "${remote_tools_script}" ]] ||
fail "remote tool helper is missing: ${remote_tools_script}"
[[ -d "${vnc_bundle_directory}" ]] ||
fail "VNC bundle is missing: ${vnc_bundle_directory}"
vnc_ssh_alias="$(cluster_shortcut "${cluster_name}")" || exit 1
vnc_host_key_alias="remote-vnc-${cluster_name}"
known_hosts_file="${HOME}/.ssh/known_hosts_remote_vnc_${cluster_name}"
local_connection_state_file="${HOME}/.ssh/remote_vnc_${cluster_name}.env"
local_user_name="$(id -un)"
local_user_id="$(id -u)"
ssh_control_path="/tmp/unix-scripts-vnc-${local_user_id}-${cluster_name}.sock"
launch_agent_label="com.${local_user_name}.remote-vnc.${cluster_name}"
launch_agent_domain="gui/$(id -u)"
launch_agent_directory="${HOME}/Library/LaunchAgents"
launch_agent_file="${launch_agent_directory}/${launch_agent_label}.plist"
launch_agent_stdout="${HOME}/Library/Logs/remote-vnc-${cluster_name}.out.log"
launch_agent_stderr="${HOME}/Library/Logs/remote-vnc-${cluster_name}.err.log"
remote_requested_node="${requested_node:-__REMOTE_VNC_SCHEDULER__}"
login_control_path="$(cluster_control_path "${cluster_name}")" || exit 1
[[ "${login_control_path}" == /* && "${login_control_path}" != *%* ]] ||
fail "could not resolve the login SSH ControlPath: ${login_control_path}"
ensure_login_control_master() {
local control_check
control_check="$(
/usr/bin/ssh -F /dev/null -S "${login_control_path}" -O check \
"${login_ssh_target}" 2>&1 || true
)"
if [[ "${control_check}" == *"Master running"* ]]; then
return 0
fi
if [[ -S "${login_control_path}" ]]; then
unlink "${login_control_path}"
elif [[ -e "${login_control_path}" ]]; then
fail "login ControlPath exists but is not a socket: ${login_control_path}"
fi
log_message "Starting the login SSH master; approve the Duo request."
"${start_ssh_control_script}" -a "${cluster_name}" ||
fail "could not start the login SSH master"
control_check="$(
/usr/bin/ssh -F /dev/null -S "${login_control_path}" -O check \
"${login_ssh_target}" 2>&1 || true
)"
[[ "${control_check}" == *"Master running"* ]] ||
fail "the login SSH master is not running at ${login_control_path}"
}
export PASSWORD="${PASSWORD:-}"
READ_USER_PASSWORD_INITIALIZE_VNC_PORT=true
# shellcheck disable=SC1090
source "${read_user_password_script}"
unset READ_USER_PASSWORD_INITIALIZE_VNC_PORT
remote_user_name="${REMOTE_USER}"
[[ "${remote_user_name}" =~ ^[A-Za-z0-9._-]+$ ]] ||
fail "invalid remote user name: ${remote_user_name}"
configured_remote_ssh_port="${REMOTE_VNC_SSH_PORT}"
[[ "${configured_remote_ssh_port}" =~ ^[0-9]+$ ]] &&
((configured_remote_ssh_port >= 44000 && configured_remote_ssh_port <= 44999)) ||
fail "REMOTE_VNC_SSH_PORT must be between 44000 and 44999"
managed_launcher_comment="remote-vnc-managed-v18:${environment_name}:${environment_mode}:${configured_remote_ssh_port}:${vnc_geometry}"
CLUSTER="${cluster_name}"
HOSTNAME="$(cluster_hostname "${cluster_name}")" || exit 1
login_ssh_target="${remote_user_name}@${HOSTNAME}"
if [[ -n "${root_override}" ]]; then
REMOTE_SHARED_ROOT="${root_override}"
export REMOTE_SHARED_ROOT
fi
[[ -n "${REMOTE_SHARED_ROOT}" ]] || fail "REMOTE_SHARED_ROOT is empty"
shared_image_path="${REMOTE_VNC_SHARED_IMAGE:-${shared_image_default}}"
log_message \
"Cluster=${cluster_name} partition=${partition_name} CPUs=${cpu_count}" \
"GPUs=${gpu_count} memory=${memory_gb}G time=${time_hours}h" \
"node=${requested_node:-scheduler}"
log_message "Environment=${environment_name} mode=${environment_mode}"
log_message "VNC geometry=${vnc_geometry}"
log_message "REMOTE_SHARED_ROOT=${REMOTE_SHARED_ROOT}"
if [[ "${REMOTE_VNC_SSH_PORT_CREATED}" == "true" ]]; then
log_message \
"Saved fixed remote SSH port ${configured_remote_ssh_port} to ${REMOTE_CONFIG_FILE}."
else
log_message "Fixed remote SSH port=${configured_remote_ssh_port}"
fi
ensure_login_control_master
ensure_vnc_identity() {
local identity_directory
local temporary_public_key
identity_directory="$(dirname "${identity_file}")"
mkdir -p "${identity_directory}"
chmod 700 "${identity_directory}" 2>/dev/null || true
if [[ ! -e "${identity_file}" && ! -e "${identity_file}.pub" ]]; then
log_message "Creating a dedicated SSH key: ${identity_file}"
ssh-keygen -q -t ed25519 -N '' -f "${identity_file}" ||
fail "could not create the VNC SSH identity"
elif [[ -r "${identity_file}" && ! -e "${identity_file}.pub" ]]; then
temporary_public_key="$(mktemp "${identity_file}.pub.XXXXXX")"
ssh-keygen -y -f "${identity_file}" > "${temporary_public_key}" ||
fail "could not derive the VNC SSH public key"
chmod 644 "${temporary_public_key}"
mv "${temporary_public_key}" "${identity_file}.pub"
fi
[[ -r "${identity_file}" && -r "${identity_file}.pub" ]] ||
fail "SSH identity or public key is missing: ${identity_file}"
chmod 600 "${identity_file}"
ssh-keygen -lf "${identity_file}.pub" >/dev/null ||
fail "invalid SSH public key: ${identity_file}.pub"
}
ensure_vnc_identity
REMOTE_TOOLS_CONTROL_PATH="${login_control_path}"
export CLUSTER HOSTNAME USER REMOTE_USER REMOTE_SHARED_ROOT
export REMOTE_TOOLS_CONTROL_PATH
# shellcheck disable=SC1090
source "${remote_tools_script}"
log_message "Checking and copying the VNC bundle when needed..."
ensure_remote_vnc_bundle || fail "could not prepare the remote VNC bundle"
vnc_root_directory="${REMOTE_SHARED_ROOT%/}/remote-vnc"
vnc_release_directory="${REMOTE_VNC_RELEASE_DIRECTORY}"
vnc_user_service_directory="${vnc_root_directory}/users/${remote_user_name}"
remote_job_launcher_file="${vnc_release_directory}/remote_vnc_job.sh"
remote_helper_file="${vnc_release_directory}/remote_vnc_job_sshd.sh"
remote_authorized_keys_file="${vnc_user_service_directory}/state/remote-vnc-authorized-key.pub"
remote_key_temporary_file="${remote_authorized_keys_file}.tmp.$$"
remote_bh_env_config_file="${vnc_user_service_directory}/state/bh-env-config.env"
remote_bh_env_wrapper="${vnc_user_service_directory}/bin/bh-env"
remote_tools_ssh_bash_args "${vnc_user_service_directory}" <<'REMOTE_PREPARE'
set -Eeuo pipefail
user_service_directory="$1"
umask 077
private_directories=(
"${user_service_directory}"
"${user_service_directory}/state"
"${user_service_directory}/state/jobs"
"${user_service_directory}/state/remote-ssh"
"${user_service_directory}/state/sbatch"
"${user_service_directory}/logs"
"${user_service_directory}/images"
"${user_service_directory}/environments"
"${user_service_directory}/bin"
)
mkdir -p "${private_directories[@]}"
chmod 700 "${private_directories[@]}"
chmod g-s "${private_directories[@]}"
persistent_ssh_directory="${user_service_directory}/state/remote-ssh"
persistent_server_key="${persistent_ssh_directory}/server-key"
persistent_server_public_key="${persistent_server_key}.pub"
persistent_server_key_lock="${persistent_ssh_directory}/server-key.lock"
temporary_key_directory=""
cleanup_temporary_key_directory() {
if [[ -n "${temporary_key_directory}" &&
-d "${temporary_key_directory}" ]]; then
rm -f \
"${temporary_key_directory}/server-key" \
"${temporary_key_directory}/server-key.pub"
rmdir "${temporary_key_directory}" 2>/dev/null || true
fi
}
trap cleanup_temporary_key_directory EXIT
validate_persistent_server_key() {
local derived_public_key
local recorded_public_key
[[ -s "${persistent_server_key}" &&
-s "${persistent_server_public_key}" ]] || return 1
ssh-keygen -lf "${persistent_server_key}" >/dev/null 2>&1 || return 1
ssh-keygen -lf "${persistent_server_public_key}" >/dev/null 2>&1 || return 1
derived_public_key="$(
ssh-keygen -y -f "${persistent_server_key}" |
awk 'NF >= 2 { print $1 " " $2; exit }'
)" || return 1
recorded_public_key="$(
awk 'NF >= 2 { print $1 " " $2; exit }' \
"${persistent_server_public_key}"
)"
[[ -n "${derived_public_key}" &&
"${derived_public_key}" == "${recorded_public_key}" ]]
}
exec 9> "${persistent_server_key_lock}"
flock -w 30 9 || {
printf 'Timed out waiting for the persistent SSH host-key lock: %s\n' \
"${persistent_server_key_lock}" >&2
exit 2
}
[[ ! -L "${persistent_server_key}" &&
! -L "${persistent_server_public_key}" ]] || {
printf 'Persistent SSH host-key files must not be symbolic links: %s\n' \
"${persistent_ssh_directory}" >&2
exit 2
}
if [[ ! -e "${persistent_server_key}" &&
! -e "${persistent_server_public_key}" ]]; then
temporary_key_directory="$(
mktemp -d "${persistent_ssh_directory}/.server-key.XXXXXX"
)"
ssh-keygen -q -t ed25519 -N '' \
-f "${temporary_key_directory}/server-key"
chmod 600 "${temporary_key_directory}/server-key"
chmod 644 "${temporary_key_directory}/server-key.pub"
mv "${temporary_key_directory}/server-key" "${persistent_server_key}"
mv "${temporary_key_directory}/server-key.pub" \
"${persistent_server_public_key}"
rmdir "${temporary_key_directory}"
temporary_key_directory=""
persistent_server_key_status="created"
elif [[ -s "${persistent_server_key}" &&
! -e "${persistent_server_public_key}" ]]; then
temporary_key_directory="$(
mktemp -d "${persistent_ssh_directory}/.server-key.XXXXXX"
)"
ssh-keygen -y -f "${persistent_server_key}" \
> "${temporary_key_directory}/server-key.pub"
chmod 644 "${temporary_key_directory}/server-key.pub"
mv "${temporary_key_directory}/server-key.pub" \
"${persistent_server_public_key}"
rmdir "${temporary_key_directory}"
temporary_key_directory=""
persistent_server_key_status="repaired public key"
elif [[ ! -s "${persistent_server_key}" ]]; then
printf 'Persistent SSH host-key private file is missing or empty: %s\n' \
"${persistent_server_key}" >&2
exit 2
else
persistent_server_key_status="reused"
fi
chmod 600 "${persistent_server_key}" "${persistent_server_key_lock}"
chmod 644 "${persistent_server_public_key}"
validate_persistent_server_key || {
printf 'Persistent SSH host-key pair is invalid or mismatched under %s.\n' \
"${persistent_ssh_directory}" >&2
exit 2
}
printf '[remote-vnc] Persistent SSH host key %s: %s\n' \
"${persistent_server_key_status}" "${persistent_server_public_key}" >&2
REMOTE_PREPARE
/usr/bin/scp -F /dev/null -q -O -o BatchMode=yes \
-o "ControlPath=${login_control_path}" \
"${identity_file}.pub" \
"${login_ssh_target}:${remote_key_temporary_file}" ||
fail "could not upload the Mac public key"
remote_tools_ssh_bash_args \
"${remote_key_temporary_file}" "${remote_authorized_keys_file}" <<'REMOTE_INSTALL_KEY'
set -Eeuo pipefail
temporary_key_file="$1"
authorized_keys_file="$2"
ssh-keygen -lf "${temporary_key_file}" >/dev/null
chmod 600 "${temporary_key_file}"
mv "${temporary_key_file}" "${authorized_keys_file}"
REMOTE_INSTALL_KEY
remote_tools_ssh_bash_args \
"${vnc_release_directory}" \
"${vnc_user_service_directory}" \
"${remote_bh_env_config_file}" \
"${remote_bh_env_wrapper}" \
"default" \
"${shared_image_path}" \
"${environment_build_timeout_seconds}" <<'REMOTE_INSTALL_BH_ENV'
set -Eeuo pipefail
release_directory="$1"
user_service_directory="$2"
config_file="$3"
wrapper_file="$4"
default_environment_name="$5"
base_image_path="$6"
environment_build_timeout_seconds="$7"
bh_env_script="${release_directory}/bh-env.sh"
[[ -x "${bh_env_script}" ]] || {
printf 'bh-env release script is missing: %s\n' "${bh_env_script}" >&2
exit 2
}
mkdir -p \
"${user_service_directory}/bin" \
"$(dirname "${config_file}")" \
"${HOME}/.local/bin"
chmod 700 \
"${user_service_directory}/bin" \
"$(dirname "${config_file}")" \
"${HOME}/.local/bin" 2>/dev/null || true
temporary_config_file="${config_file}.tmp.$$"
{
printf 'BH_ENV_RELEASE_DIRECTORY=%q\n' "${release_directory}"
printf 'BH_ENV_USER_SERVICE_DIRECTORY=%q\n' "${user_service_directory}"
printf 'BH_ENV_DEFAULT_NAME=%q\n' "${default_environment_name}"
printf 'BH_ENV_BASE_IMAGE=%q\n' "${base_image_path}"
printf 'BH_ENV_BUILD_TIMEOUT_SECONDS=%q\n' \
"${environment_build_timeout_seconds}"
} > "${temporary_config_file}"
chmod 600 "${temporary_config_file}"
mv "${temporary_config_file}" "${config_file}"
temporary_wrapper_file="${wrapper_file}.tmp.$$"
{
printf '#!/usr/bin/env bash\n'
printf 'set -Eeuo pipefail\n'
printf 'exec %q --config %q "$@"\n' "${bh_env_script}" "${config_file}"
} > "${temporary_wrapper_file}"
chmod 700 "${temporary_wrapper_file}"
mv "${temporary_wrapper_file}" "${wrapper_file}"
home_command="${HOME}/.local/bin/bh-env"
if [[ ! -e "${home_command}" && ! -L "${home_command}" ]]; then
ln -s "${wrapper_file}" "${home_command}"
elif [[ -L "${home_command}" ]]; then
existing_target="$(readlink "${home_command}")"
case "${existing_target}" in
*/remote-vnc/users/*/bin/bh-env|"${wrapper_file}")
ln -sfn "${wrapper_file}" "${home_command}"
;;
*)
printf '[remote-vnc] Existing bh-env symlink was left unchanged: %s -> %s\n' \
"${home_command}" "${existing_target}" >&2
;;
esac
else
printf '[remote-vnc] Existing file was left unchanged: %s\n' \
"${home_command}" >&2
printf '[remote-vnc] Use %s directly.\n' "${wrapper_file}" >&2
fi
REMOTE_INSTALL_BH_ENV
ready_record="$(
remote_tools_ssh_bash_args \
"${vnc_release_directory}" \
"${vnc_user_service_directory}" \
"${shared_image_path}" \
"${partition_name}" \
"${cpu_count}" \
"${gpu_count}" \
"${memory_gb}" \
"${time_hours}" \
"${remote_requested_node}" \
"${startup_timeout_seconds}" \
"${image_build_timeout_seconds}" \
"${remote_job_launcher_file}" \
"${remote_helper_file}" \
"${remote_authorized_keys_file}" \
"${managed_launcher_comment}" \
"${restart_existing_job}" \
"${environment_name}" \
"${environment_mode}" \
"${environment_build_timeout_seconds}" \
"${configured_remote_ssh_port}" \
"${vnc_geometry}" \
"${opencodex_startup_timeout_seconds}" <<'REMOTE_START'
set -Eeuo pipefail
release_directory="$1"
user_service_directory="$2"
shared_image_path="$3"
requested_partition="$4"
requested_cpu_count="$5"
requested_gpu_count="$6"
requested_memory_gb="$7"
requested_time_hours="$8"
requested_node="$9"
startup_timeout_seconds="${10}"
image_build_timeout_seconds="${11}"
remote_job_launcher_file="${12}"
remote_helper_file="${13}"
remote_authorized_keys_file="${14}"
managed_launcher_comment="${15}"
restart_existing_job="${16}"
environment_name="${17}"
environment_mode="${18}"
environment_build_timeout_seconds="${19}"
requested_remote_ssh_port="${20}"
vnc_geometry="${21}"
compatible_launcher_versions="17, 18"
opencodex_startup_timeout_seconds="${22:-600}"
if [[ "${requested_node}" == "__REMOTE_VNC_SCHEDULER__" ]]; then
requested_node=""
fi
[[ "${environment_name}" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || {
printf 'Invalid environment name: %s\n' "${environment_name}" >&2
exit 2
}
[[ "${environment_mode}" == "mutable" ||
"${environment_mode}" == "immutable" ]] || {
printf 'Invalid environment mode: %s\n' "${environment_mode}" >&2
exit 2
}
[[ "${environment_build_timeout_seconds}" =~ ^[1-9][0-9]*$ ]] || {
printf 'Invalid environment build timeout: %s\n' \
"${environment_build_timeout_seconds}" >&2
exit 2
}
[[ "${requested_remote_ssh_port}" =~ ^[0-9]+$ ]] &&
((requested_remote_ssh_port >= 44000 && requested_remote_ssh_port <= 44999)) || {
printf 'Invalid fixed remote SSH port: %s\n' \
"${requested_remote_ssh_port}" >&2
exit 2
}
if [[ "${vnc_geometry}" =~ ^([0-9]+)x([0-9]+)$ ]]; then
vnc_width="${BASH_REMATCH[1]}"
vnc_height="${BASH_REMATCH[2]}"
else
printf 'Invalid VNC geometry: %s\n' "${vnc_geometry}" >&2
exit 2
fi
((vnc_width >= 1024 && vnc_width <= 7680 &&
vnc_height >= 768 && vnc_height <= 4320)) || {
printf 'VNC geometry must be between 1024x768 and 7680x4320: %s\n' \
"${vnc_geometry}" >&2
exit 2
}
slurm_binary_directory="/sfw/rhel9-x86_64/slurm/24.05.0.b1/bin"
squeue_executable="${slurm_binary_directory}/squeue"
sbatch_executable="${slurm_binary_directory}/sbatch"
scontrol_executable="${slurm_binary_directory}/scontrol"
scancel_executable="${slurm_binary_directory}/scancel"
current_user="$(id -un)"
job_name="${current_user}-vnc"
legacy_gpu_job_name="${current_user}-vnc-gpu"
connection_file="${user_service_directory}/state/connection.env"
for required_file in \
"${release_directory}/start_vnc.sh" \
"${release_directory}/configure_desktop.sh" \
"${release_directory}/macos_shortcut.sh" \
"${release_directory}/bluehive-aurora.svg" \
"${release_directory}/build_vnc_image.sh" \
"${release_directory}/bh-env.sh" \
"${release_directory}/environment_common.sh" \
"${release_directory}/prepare_environment.sh" \
"${release_directory}/provision_environment.sh" \
"${release_directory}/environment-packages.txt" \
"${release_directory}/matlab-products.txt" \
"${release_directory}/ubuntu-vnc-xfce-g3_24.04.def" \
"${release_directory}/ubuntu-vnc-xfce-g3_24.04.sha256" \
"${remote_job_launcher_file}" \
"${remote_helper_file}" \
"${remote_authorized_keys_file}"; do
[[ -r "${required_file}" ]] || {
printf 'Required remote file is missing: %s\n' "${required_file}" >&2
exit 2
}
done
for slurm_executable in \
"${squeue_executable}" "${sbatch_executable}" \
"${scontrol_executable}" "${scancel_executable}"; do
[[ -x "${slurm_executable}" ]] || {
printf 'Slurm command is unavailable: %s\n' "${slurm_executable}" >&2
exit 2
}
done
read_state_value() {
local state_file="$1"
local requested_key="$2"
[[ -s "${state_file}" ]] || return 1
awk -F= -v requested_key="${requested_key}" '
$1 == requested_key {
sub(/^[^=]*=/, "")
print
exit
}
' "${state_file}"
}
managed_launcher_state_file() {
local requested_job_id="$1"
printf '%s/state/jobs/%s/managed-launcher.env\n' \
"${user_service_directory}" "${requested_job_id}"
}
launcher_version_is_compatible() {
# v18 adds startup timeout controls; its connection state matches v17.
[[ "$1" == 17 || "$1" == 18 ]]
}
job_uses_managed_launcher() {
local requested_job_id="$1"
local launcher_state_file
local job_record
local job_comment
local existing_launcher_version
launcher_state_file="$(managed_launcher_state_file "${requested_job_id}")"
existing_launcher_version="$(read_state_value "${launcher_state_file}" LAUNCHER_VERSION || true)"
if [[ -n "${existing_launcher_version}" ]]; then
launcher_version_is_compatible "${existing_launcher_version}" &&
[[ "$(read_state_value "${launcher_state_file}" JOB_ID || true)" == \
"${requested_job_id}" ]]
return
fi
job_record="$(
"${scontrol_executable}" show job -o "${requested_job_id}" 2>/dev/null || true
)"
job_comment="$(
tr ' ' '\n' <<< "${job_record}" |
awk -F= '$1 == "Comment" { print $2; exit }'
)"
[[ "${job_comment}" =~ ^remote-vnc-managed-v([0-9]+): ]] &&
launcher_version_is_compatible "${BASH_REMATCH[1]}"
}
managed_launcher_is_ready() {
local requested_job_id="$1"
local launcher_state_file
launcher_state_file="$(managed_launcher_state_file "${requested_job_id}")"
[[ "$(read_state_value "${launcher_state_file}" STATUS || true)" == "READY" ]] &&
launcher_version_is_compatible \
"$(read_state_value "${launcher_state_file}" LAUNCHER_VERSION || true)" &&
[[ "$(read_state_value "${launcher_state_file}" JOB_ID || true)" == \
"${requested_job_id}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" ENVIRONMENT_NAME || true)" == \
"${environment_name}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" ENVIRONMENT_MODE || true)" == \
"${environment_mode}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" REMOTE_SSH_PORT || true)" == \
"${requested_remote_ssh_port}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" VNC_GEOMETRY || true)" == \
"${vnc_geometry}" ]]
}
job_matches_requested_configuration() {
local requested_job_id="$1"
local launcher_state_file
local job_record
local job_comment
launcher_state_file="$(managed_launcher_state_file "${requested_job_id}")"
if [[ "$(read_state_value "${launcher_state_file}" ENVIRONMENT_NAME || true)" == \
"${environment_name}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" ENVIRONMENT_MODE || true)" == \
"${environment_mode}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" REMOTE_SSH_PORT || true)" == \
"${requested_remote_ssh_port}" ]] &&
[[ "$(read_state_value "${launcher_state_file}" VNC_GEOMETRY || true)" == \
"${vnc_geometry}" ]]; then
return 0
fi
job_record="$(
"${scontrol_executable}" show job -o "${requested_job_id}" 2>/dev/null || true
)"
job_comment="$(
tr ' ' '\n' <<< "${job_record}" |
awk -F= '$1 == "Comment" { print $2; exit }'
)"
[[ "${job_comment}" =~ ^remote-vnc-managed-v([0-9]+): ]] &&
launcher_version_is_compatible "${BASH_REMATCH[1]}" &&
[[ "${job_comment#*:}" == "${managed_launcher_comment#*:}" ]]
}
opencodex_connection_is_ready() {
if [[ "${environment_mode}" == "immutable" ]]; then
[[ "$(read_state_value "${connection_file}" OPENCODEX_STATUS || true)" == \
"DISABLED_IMMUTABLE" ]]
return
fi
[[ "$(read_state_value "${connection_file}" OPENCODEX_STATUS || true)" == \
"READY" ]] &&
[[ "$(read_state_value "${connection_file}" CONTAINER_INSTANCE_NAME || true)" =~ \
^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] &&
[[ "$(read_state_value "${connection_file}" CONTAINER_INSTANCE_PID || true)" =~ \
^[0-9]+$ ]] &&
[[ "$(read_state_value "${connection_file}" OPENCODEX_PID || true)" =~ \
^[0-9]+$ ]] &&
[[ "$(read_state_value "${connection_file}" OPENCODEX_PORT || true)" =~ \
^[0-9]+$ ]] &&
[[ "$(read_state_value "${connection_file}" CODEX_APP_SERVER_STATUS || true)" == \
"running" ]] &&
[[ "$(read_state_value "${connection_file}" CODEX_APP_SERVER_PID || true)" =~ \
^[0-9]+$ ]]
}
read_job_queue_record() {
local requested_job_id="$1"
# Finished jobs may already be purged from Slurm. Query our queue so their
# absence is an empty result, while scheduler failures still reach stderr.
"${squeue_executable}" -h -u "${current_user}" -o '%i|%T|%N|%R' |
awk -F'|' -v requested_job_id="${requested_job_id}" '
$1 == requested_job_id { print $2 "|" $3 "|" $4 }
'
}
vnc_connection_is_ready() {
local requested_job_id="$1"
local job_queue_record
local job_state
job_queue_record="$(read_job_queue_record "${requested_job_id}")" || return
job_state="${job_queue_record%%|*}"
[[ "$(read_state_value "${connection_file}" STATUS || true)" == "READY" ]] &&
[[ "$(read_state_value "${connection_file}" JOB_ID || true)" == "${requested_job_id}" ]] &&
[[ "$(read_state_value "${connection_file}" NODE || true)" =~ ^[A-Za-z0-9._-]+$ ]] &&
[[ "$(read_state_value "${connection_file}" VNC_PORT || true)" =~ ^[0-9]+$ ]] &&
[[ "$(read_state_value "${connection_file}" ENVIRONMENT_NAME || true)" == \
"${environment_name}" ]] &&
[[ "$(read_state_value "${connection_file}" ENVIRONMENT_MODE || true)" == \
"${environment_mode}" ]] &&
[[ "$(read_state_value "${connection_file}" VNC_GEOMETRY || true)" == \
"${vnc_geometry}" ]] &&
[[ "$(read_state_value "${connection_file}" VNC_CLIPBOARD || true)" == \
"ENABLED" ]] &&
opencodex_connection_is_ready &&
managed_launcher_is_ready "${requested_job_id}" &&
[[ "${job_state}" == "RUNNING" ]]
}
# Serialize discovery, cancellation, submission, and readiness across clients.
# Keep this separate from the allocation lock so --restart can stop its owner.
mkdir -p "${user_service_directory}/state"
exec 8>"${user_service_directory}/state/launch.lock"
flock -n 8 || {
printf 'Another VNC launch or restart is in progress. Wait for it to finish before retrying.\n' >&2
exit 8
}
job_id="$(read_state_value "${connection_file}" JOB_ID || true)"
if [[ "${restart_existing_job}" != "true" && "${job_id}" =~ ^[0-9]+$ ]] &&
vnc_connection_is_ready "${job_id}"; then
printf '[remote-vnc] Reusing running VNC Job %s.\n' "${job_id}" >&2
else
active_job_record="$(
"${squeue_executable}" -h -u "${USER}" -o '%i|%T|%j|%N|%R' |
awk -F'|' -v job_name="${job_name}" \
-v legacy_gpu_job_name="${legacy_gpu_job_name}" \
'$3 == job_name || $3 == legacy_gpu_job_name { print }' |
sort -t'|' -k1,1nr |
head -n 1
)"
if [[ -n "${active_job_record}" ]]; then
IFS='|' read -r job_id job_state active_job_name job_node job_reason \
<<< "${active_job_record}"
if [[ "${restart_existing_job}" == "true" ]]; then
printf '[remote-vnc] Cancelling VNC Job %s before replacement.\n' \
"${job_id}" >&2
"${scancel_executable}" "${job_id}"
cancel_deadline=$((SECONDS + 60))
while ((SECONDS < cancel_deadline)); do
active_job_record="$(read_job_queue_record "${job_id}")"
[[ -n "${active_job_record}" ]] || break
sleep 1
done
active_job_record="$(read_job_queue_record "${job_id}")"
[[ -z "${active_job_record}" ]] || {
printf 'VNC Job %s did not stop within 60 seconds.\n' \
"${job_id}" >&2
exit 7
}
active_job_record=""
elif job_uses_managed_launcher "${job_id}"; then
if job_matches_requested_configuration "${job_id}"; then
printf '[remote-vnc] Waiting for existing VNC Job %s.\n' \
"${job_id}" >&2
else
active_environment_name="$(
read_state_value \
"$(managed_launcher_state_file "${job_id}")" \
ENVIRONMENT_NAME || true
)"
active_environment_mode="$(
read_state_value \
"$(managed_launcher_state_file "${job_id}")" \
ENVIRONMENT_MODE || true
)"
active_remote_ssh_port="$(
read_state_value \
"$(managed_launcher_state_file "${job_id}")" \
REMOTE_SSH_PORT || true
)"
active_vnc_geometry="$(
read_state_value \
"$(managed_launcher_state_file "${job_id}")" \
VNC_GEOMETRY || true
)"
printf 'VNC Job %s uses environment %s (%s), SSH port %s, and geometry %s.\n' \
"${job_id}" \
"${active_environment_name:-unknown}" \
"${active_environment_mode:-unknown}" \
"${active_remote_ssh_port:-unknown}" \
"${active_vnc_geometry:-unknown}" >&2
printf 'Run remote_vnc.sh --restart with the requested configuration.\n' \
>&2
exit 7
fi
else
existing_launcher_version="$(
read_state_value \
"$(managed_launcher_state_file "${job_id}")" \
LAUNCHER_VERSION || true
)"
if [[ -n "${existing_launcher_version}" ]]; then
printf 'VNC Job %s uses launcher version %s; supported versions: %s.\n' \
"${job_id}" "${existing_launcher_version}" \
"${compatible_launcher_versions}" >&2
else
printf 'VNC Job %s was started without the current managed launcher.\n' \
"${job_id}" >&2
fi
printf 'Run remote_vnc.sh --restart with the same resource options to replace it.\n' \
>&2
exit 7
fi
fi
if [[ -z "${active_job_record}" ]]; then
partition_record="$(
"${scontrol_executable}" show partition "${requested_partition}" -o
)"
partition_qos="$(
tr ' ' '\n' <<< "${partition_record}" |
awk -F= '$1 == "QoS" { print $2; exit }'
)"
submit_options=(
--parsable
"--job-name=${job_name}"
"--comment=${managed_launcher_comment}"
"--partition=${requested_partition}"
--nodes=1
--ntasks=1
"--cpus-per-task=${requested_cpu_count}"
"--mem=${requested_memory_gb}G"
"--time=${requested_time_hours}:00:00"
"--chdir=${user_service_directory}"
"--output=${user_service_directory}/logs/%x_%j.out"
"--error=${user_service_directory}/logs/%x_%j.err"
--mail-type=FAIL
--export=NONE
--signal=B:TERM@60
)
if [[ -n "${partition_qos}" && "${partition_qos}" != "N/A" ]]; then
submit_options+=("--qos=${partition_qos}")
fi
if ((requested_gpu_count > 0)); then
submit_options+=("--gres=gpu:${requested_gpu_count}")
fi
if [[ -n "${requested_node}" ]]; then
submit_options+=("--nodelist=${requested_node}")
fi
mkdir -p "${user_service_directory}/logs"
printf -v job_wrap_command \
'exec %q %q %q %q %q %q %q %q %q %q %q %q %q %q' \
"${remote_job_launcher_file}" \
"${release_directory}" \
"${user_service_directory}" \
"${shared_image_path}" \
"${remote_helper_file}" \
"${remote_authorized_keys_file}" \
"${startup_timeout_seconds}" \
"${image_build_timeout_seconds}" \
"${environment_name}" \
"${environment_mode}" \