Skip to content

Commit 35c7b4b

Browse files
authored
style(shell): declare runtime env via stub for ShellCheck (SC1091/SC2154/SC2312) (#653)
## Summary Follow-up to #649. Resolves the remaining ShellCheck warnings in the three `packages/standard` scripts that source `/root/cloud-variables` at runtime: `ami-configure.sh`, `backup.sh`, and `restore.sh`. `/root/cloud-variables` is written by CloudFormation UserData at instance boot and is not tracked in the repo, so ShellCheck reports SC1091 (not followable) and SC2154 (variables not assigned) for every variable read from it. ## Changes - **New `packages/standard/cloud-variables.stub`** declaring the variables CFN injects (`DVOL`, `S3`, `KMS`, `RECOVERYS3`, `RECOVERY_NEWRDS`) with no-op default assignments. The stub is a static declaration only — it is never sourced at runtime; the real values still come from CFN. - **`# shellcheck source=packages/standard/cloud-variables.stub`** directive added above each `source /root/cloud-variables` line. The path is resolved from the repo root, matching how CI invokes shellcheck. Clears SC1091 and all SC2154. - **SC2312 hoists**: the remaining `return value masked in pipeline` warnings in `ami-configure.sh` and `backup.sh` are fixed by capturing the producing command's output into a variable, then feeding it to the consumer via herestring. No runtime behavior change. No `# shellcheck disable` directives are added; the stub plus `source=` directive is the proper fix. ## Verification ``` $ shellcheck --check-sourced --external-sources \ packages/standard/ami/ami-configure.sh \ packages/standard/scripts/backup.sh \ packages/standard/scripts/restore.sh $ echo $? 0 ``` `bash -n` passes on all three scripts. ## Test plan - [x] `bash -n` clean on all three scripts - [x] `shellcheck --check-sourced --external-sources` produces no output for the three files - [ ] CI ShellCheck workflow passes on this PR
1 parent 3f8ffb2 commit 35c7b4b

4 files changed

Lines changed: 18 additions & 2 deletions

File tree

packages/standard/ami/ami-configure.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ exec > /var/log/openemr-configure.log 2>&1
55
cd /root/openemr-devops/packages/standard
66

77
# pick up cloud settings
8+
# shellcheck source=packages/standard/cloud-variables.stub
89
source /root/cloud-variables
910

1011
# prepare the encrypted volume CFN just added
1112
# this used to be in a weird ready-loop but that doesn't make any sense to me
1213
DVOL_SERIAL=${DVOL/-/}
13-
DVOL_DEVICE=/dev/$(lsblk -no NAME,SERIAL | awk -v s="${DVOL_SERIAL}" '$2 == s {print $1}')
14+
# Split across statements so ShellCheck can see lsblk's exit status (SC2312).
15+
LSBLK_OUTPUT=$(lsblk -no NAME,SERIAL)
16+
DVOL_DEVICE=/dev/$(awk -v s="${DVOL_SERIAL}" '$2 == s {print $1}' <<< "${LSBLK_OUTPUT}")
1417
mkfs -t ext4 "${DVOL_DEVICE}"
1518
echo "${DVOL_DEVICE}" /mnt/docker ext4 defaults,nofail 0 0 >> /etc/fstab
1619
mkdir /mnt/docker
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# shellcheck shell=sh
2+
# Declares the variables written into /root/cloud-variables by CloudFormation
3+
# UserData at instance boot, so ShellCheck can resolve them statically.
4+
# This file is NOT used at runtime; the real values come from CFN.
5+
: "${DVOL:=}" # encrypted EBS volume serial
6+
: "${S3:=}" # backup bucket for this instance
7+
: "${KMS:=}" # KMS key ID for SSE
8+
: "${RECOVERYS3:=}" # source bucket when restoring from another stack
9+
: "${RECOVERY_NEWRDS:=}" # replacement RDS hostname when restoring
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/bin/bash
22

3+
# shellcheck source=packages/standard/cloud-variables.stub
34
source /root/cloud-variables
45
PASSPHRASE=$(aws s3 cp "s3://${S3}/Backup/passphrase.txt" - --sse aws:kms --sse-kms-key-id "${KMS}")
56
export PASSPHRASE
6-
duplicity --full-if-older-than 7D --include "$(docker volume inspect standard_sitevolume | jq -r ".[0].Mountpoint")" --exclude '**' / "boto3+s3://${S3}/Backup"
7+
VOLUME_INFO=$(docker volume inspect standard_sitevolume)
8+
MOUNTPOINT=$(jq -r ".[0].Mountpoint" <<< "${VOLUME_INFO}")
9+
duplicity --full-if-older-than 7D --include "${MOUNTPOINT}" --exclude '**' / "boto3+s3://${S3}/Backup"
710
duplicity remove-all-but-n-full 2 --force "boto3+s3://${S3}/Backup"

packages/standard/scripts/restore.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ while getopts "r:" opt; do
1616
esac
1717
done
1818

19+
# shellcheck source=packages/standard/cloud-variables.stub
1920
source /root/cloud-variables
2021

2122
case ${RECOVERYMODE} in

0 commit comments

Comments
 (0)