22set -e
33set -o pipefail
44
5- readonly VER=2.1.5
5+ readonly VER=2.1.6
66
77# Remove the longest `*/` prefix
88readonly SCRIPT_NAME_WITH_EXT=" ${0##*/ } "
@@ -96,30 +96,30 @@ readonly HOST_USER=$1
9696
9797if ! command -v oci > /dev/null; then
9898 # shellcheck disable=SC2016
99- echo >&2 ' Please install OCI CLI'
99+ echo ' Please install OCI CLI' >&2
100100 exit 1
101101fi
102102
103103if ! command -v jq > /dev/null; then
104104 # shellcheck disable=SC2016
105- echo >&2 ' Please install `jq`'
105+ echo ' Please install `jq`' >&2
106106 exit 1
107107fi
108108
109109if ! command -v pcregrep > /dev/null; then
110110 # shellcheck disable=SC2016
111- echo >&2 ' Please install PCRE'
111+ echo ' Please install PCRE' >&2
112112 exit 1
113113fi
114114
115115if ! command -v perl > /dev/null; then
116- echo " Please install Perl"
116+ echo " Please install Perl" >&2
117117 exit 1
118118fi
119119
120120for required_env_var in ' OCI_INSTANCE' ' OCI_INSTANCE_OCID' ' OCI_BASTION_OCID' ; do
121121 if [[ ! ${! required_env_var} ]]; then
122- echo >&2 " Please define $required_env_var "
122+ echo " Please define $required_env_var " >&2
123123 exit 1
124124 fi
125125done
@@ -139,7 +139,7 @@ for key_type in 'id_rsa' 'id_dsa' 'id_ecdsa' 'id_ed25519' 'id_xmss'; do
139139 fi
140140done
141141if [[ ! $SSH_PUB_KEY ]]; then
142- echo >&2 ' No SSH public key is found'
142+ echo ' No SSH public key is found' >&2
143143 exit 1
144144fi
145145
@@ -150,19 +150,21 @@ if [[ $port ]]; then
150150 # `--ssh-public-key-file` is required
151151 # `--target-private-ip` "${OCI_INSTANCE}"
152152 # shellcheck disable=SC2086 # $PROFILE_OPT is a two-word CLI option
153- session_ocid=$( time ( # `time` prints to stderr so it does not interfere with the pipe
153+ session_ocid=$(
154+ time (# `time` prints to stderr so it does not interfere with the pipe
154155 oci bastion session create-port-forwarding $PROFILE_OPT --bastion-id " $OCI_BASTION_OCID " \
155- --target-resource-id " $OCI_INSTANCE_OCID " --target-port " $port " --session-ttl $MAX_TTL \
156- --ssh-public-key-file $SSH_PUB_KEY --wait-for-state SUCCEEDED --wait-for-state FAILED \
157- --wait-interval-seconds $CHECK_INTERVAL_SEC | jq --raw-output ' .data.resources[0].identifier' ;
158- >&2 printf " It took:"
156+ --target-resource-id " $OCI_INSTANCE_OCID " --target-port " $port " --session-ttl $MAX_TTL \
157+ --ssh-public-key-file $SSH_PUB_KEY --wait-for-state SUCCEEDED --wait-for-state FAILED \
158+ --wait-interval-seconds $CHECK_INTERVAL_SEC \
159+ | jq --raw-output ' .data.resources[0].identifier'
160+ printf " It took:" >&2
159161 )
160162 )
161163 echo " Created the bastion port forwarding session: $session_ocid "
162164
163165 # shellcheck disable=SC2086 # $PROFILE_OPT is a two-word CLI option
164- ssh_command=$( oci bastion session get $PROFILE_OPT --session-id " $session_ocid " |
165- jq --raw-output ' .data["ssh-metadata"].command' )
166+ ssh_command=$( oci bastion session get $PROFILE_OPT --session-id " $session_ocid " \
167+ | jq --raw-output ' .data["ssh-metadata"].command' )
166168 # Result: `ssh -i <privateKey> -N -L <localPort>:{HOST_IP}:5432 -p 22 [email protected] ` 167169 # Remove the placeholder
168170 ssh_command=" ${ssh_command/ -i <privateKey>/ } "
@@ -187,19 +189,21 @@ if [[ $HOST_USER ]]; then
187189 # `--wait-interval-seconds`: state check interval (defaults to 30 seconds).
188190 # `--ssh-public-key-file` is required
189191 # shellcheck disable=SC2086 # $PROFILE_OPT is a two-word CLI option
190- session_ocid=$( time ( # `time` prints to stderr so it does not interfere with the pipe
192+ session_ocid=$(
193+ time (# `time` prints to stderr so it does not interfere with the pipe
191194 oci bastion session create-managed-ssh $PROFILE_OPT --bastion-id " $OCI_BASTION_OCID " \
192- --target-resource-id " $OCI_INSTANCE_OCID " --target-os-username " $HOST_USER " --session-ttl $MAX_TTL \
193- --ssh-public-key-file $SSH_PUB_KEY --wait-for-state SUCCEEDED --wait-for-state FAILED \
194- --wait-interval-seconds $CHECK_INTERVAL_SEC | jq --raw-output ' .data.resources[0].identifier' ;
195- >&2 printf " It took:"
195+ --target-resource-id " $OCI_INSTANCE_OCID " --target-os-username " $HOST_USER " --session-ttl $MAX_TTL \
196+ --ssh-public-key-file $SSH_PUB_KEY --wait-for-state SUCCEEDED --wait-for-state FAILED \
197+ --wait-interval-seconds $CHECK_INTERVAL_SEC \
198+ | jq --raw-output ' .data.resources[0].identifier'
199+ printf " It took:" >&2
196200 )
197201 )
198202 echo " Created the bastion session: $session_ocid "
199203
200204 # shellcheck disable=SC2086 # $PROFILE_OPT is a two-word CLI option
201- ssh_command=$( oci bastion session get $PROFILE_OPT --session-id " $session_ocid " |
202- jq --raw-output ' .data["ssh-metadata"].command' )
205+ ssh_command=$( oci bastion session get $PROFILE_OPT --session-id " $session_ocid " \
206+ | jq --raw-output ' .data["ssh-metadata"].command' )
203207 # Result: `ssh -i <privateKey> -o ProxyCommand=\"ssh -i <privateKey> -W %h:%p -p 22
204208 # [email protected] \" -p 22 {HOST_USER}@{HOST_IP}` 205209 # Extract the bastion session SSH destination: the `[email protected] ` part @@ -241,4 +245,4 @@ HEREDOC
241245 set -x
242246 ssh " ${HOST_USER} @${OCI_INSTANCE} "
243247 set +x
244- fi
248+ fi
0 commit comments