Skip to content

Commit f4775e8

Browse files
committed
Fix additional Sonar warnings and shellcheck errors.
1 parent d5893e5 commit f4775e8

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

grafana/non-prod/docker/build_push_to_ecr.sh

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
# Set variables
44

5-
dirname=$(dirname "$0")
6-
DOCKERFILE_DIR=$(realpath "$dirname")
7-
echo "DOCKERFILE_DIR: $DOCKERFILE_DIR"
5+
dirname=$(dirname "${0}")
6+
DOCKERFILE_DIR=$(realpath "${dirname}")
7+
echo "DOCKERFILE_DIR: ${DOCKERFILE_DIR}"
88

99
# if parameter not passed, prompt for the environment.
1010
# Do not accept response if it is not one of the following: prod, int, ref, internal-dev
1111
# loop until valid response is received
12-
if [ -z "$1" ]; then
12+
if [[ -z "${1}" ]]; then
1313
while true; do
14-
read -p "Enter the environment (prod, int, ref, internal-dev): " ENVIRONMENT
15-
case "$ENVIRONMENT" in
14+
read -r -p "Enter the environment (prod, int, ref, internal-dev): " ENVIRONMENT
15+
case "${ENVIRONMENT}" in
1616
prod|int|ref|internal-dev)
1717
break
1818
;;
@@ -22,10 +22,10 @@ if [ -z "$1" ]; then
2222
esac
2323
done
2424
else
25-
ENVIRONMENT="$1"
25+
ENVIRONMENT="${1}"
2626
fi
2727
# Check if the environment is valid
28-
if [[ ! "$ENVIRONMENT" =~ ^(prod|int|ref|internal-dev)$ ]]; then
28+
if [[ ! "${ENVIRONMENT}" =~ ^(prod|int|ref|internal-dev)$ ]]; then
2929
echo "Invalid environment. Please enter one of: prod, int, ref, internal-dev."
3030
exit 1
3131
fi
@@ -36,57 +36,57 @@ AWS_REGION="eu-west-2"
3636
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
3737
REPOSITORY_NAME="${PREFIX}-grafana-app"
3838
IMAGE_TAG="11.0.0-22.04_stable"
39-
LOCAL_IMAGE_NAME="$REPOSITORY_NAME:$IMAGE_TAG"
40-
IMAGE_NAME="$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$LOCAL_IMAGE_NAME"
39+
LOCAL_IMAGE_NAME="${REPOSITORY_NAME}:${IMAGE_TAG}"
40+
IMAGE_NAME="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${LOCAL_IMAGE_NAME}"
4141
TAGS='[
4242
{"Key": "Environment", "Value": "non-prod"},
4343
{"Key": "Project", "Value": "immunisation-fhir-api-grafana"},
44-
{"Key": "Environment", "Value": "'"$ENVIRONMENT"'"}
44+
{"Key": "Environment", "Value": "'"${ENVIRONMENT}"'"}
4545
]'
4646
LIFECYCLE_POLICY_FILE="lifecycle-policy.json"
4747

4848
# Change to the directory containing the Dockerfile
49-
if ! cd "$DOCKERFILE_DIR"; then
49+
if ! cd "${DOCKERFILE_DIR}"; then
5050
echo "DOCKERFILE_DIR not found."
5151
exit 1
5252
fi
5353

5454
# Check if Dockerfile exists
55-
if [ ! -f Dockerfile ]; then
55+
if [[ ! -f Dockerfile ]]; then
5656
echo "Dockerfile not found in DOCKERFILE_DIR."
5757
exit 1
5858
fi
5959

6060
# Create ECR repository if it does not exist
61-
if ! aws ecr describe-repositories --repository-names "$REPOSITORY_NAME" --region "$AWS_REGION" > /dev/null 2>&1; then
62-
echo "Creating ECR repository: $REPOSITORY_NAME"
63-
aws ecr create-repository --repository-name "$REPOSITORY_NAME" --region "$AWS_REGION"
61+
if ! aws ecr describe-repositories --repository-names "${REPOSITORY_NAME}" --region "${AWS_REGION}" > /dev/null 2>&1; then
62+
echo "Creating ECR repository: ${REPOSITORY_NAME}"
63+
aws ecr create-repository --repository-name "${REPOSITORY_NAME}" --region "${AWS_REGION}"
6464
# Add tags to the repository
65-
aws ecr tag-resource --resource-arn "arn:aws:ecr:$AWS_REGION:$ACCOUNT_ID:repository/$REPOSITORY_NAME" --tags "$TAGS"
65+
aws ecr tag-resource --resource-arn "arn:aws:ecr:${AWS_REGION}:${ACCOUNT_ID}:repository/${REPOSITORY_NAME}" --tags "${TAGS}"
6666
fi
6767

6868
# Apply lifecycle policy to the ECR repository
69-
aws ecr put-lifecycle-policy --repository-name "$REPOSITORY_NAME" --lifecycle-policy-text "file://$LIFECYCLE_POLICY_FILE" --region "$AWS_REGION"
69+
aws ecr put-lifecycle-policy --repository-name "${REPOSITORY_NAME}" --lifecycle-policy-text "file://${LIFECYCLE_POLICY_FILE}" --region "${AWS_REGION}"
7070

7171
printf "Building and pushing Docker image to ECR...\n"
7272
# Authenticate Docker to ECR
73-
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com"
73+
aws ecr get-login-password --region "${AWS_REGION}" | docker login --username AWS --password-stdin "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
7474

7575
printf "Building Docker image...\n"
7676
# Remove existing Docker image if it exists
77-
docker rmi "$IMAGE_NAME" --force
77+
docker rmi "${IMAGE_NAME}" --force
7878

7979
# Pull the base image for linux/amd64 architecture
8080
docker pull --platform linux/amd64 grafana/grafana:latest
8181

8282
# Build Docker image for linux/amd64 architecture and push to ECR
8383
docker buildx create --use
84-
if ! docker buildx build --platform linux/amd64 -t "$IMAGE_NAME" --push .; then
84+
if ! docker buildx build --platform linux/amd64 -t "${IMAGE_NAME}" --push .; then
8585
echo "Docker build failed."
8686
exit 1
8787
fi
8888

8989
# Inspect the built image
90-
echo "Image: $LOCAL_IMAGE_NAME"
90+
echo "Image: ${LOCAL_IMAGE_NAME}"
9191

9292
echo "Docker image built and pushed to ECR successfully."

grafana/non-prod/docker/run.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
#!/bin/sh
1+
#!/bin/bash
2+
23
echo "Grafana Docker container startup..."
34

45
GRAFANA_SERVER_PATH=$(which grafana-server)
5-
if [ -z "$GRAFANA_SERVER_PATH" ]; then
6+
if [[ -z "${GRAFANA_SERVER_PATH}" ]]; then
67
echo "grafana-server not found. Exiting."
78
exit 1
89
else
9-
echo "grafana-server found at $GRAFANA_SERVER_PATH"
10+
echo "grafana-server found at ${GRAFANA_SERVER_PATH}"
1011
fi
1112

1213
echo "Starting Grafana with environment variables:"
1314
env | grep GF_
1415

1516
# Check if Grafana configuration exists
16-
if [ ! -f /etc/grafana/grafana.ini ]; then
17+
if [[ ! -f /etc/grafana/grafana.ini ]]; then
1718
echo "Grafana configuration not found. Exiting."
1819
exit 1
1920
fi
2021

2122
# Start Grafana in the foreground
2223
echo "Starting Grafana server..."
23-
exec "$GF_PATHS_HOME/bin/grafana-server" \
24-
--homepath="$GF_PATHS_HOME" \
24+
exec "${GF_PATHS_HOME}/bin/grafana-server" \
25+
--homepath="${GF_PATHS_HOME}" \
2526
--config=/etc/grafana/grafana.ini \
2627
--packaging=docker \
2728
cfg:default.paths.data=/var/lib/grafana \

0 commit comments

Comments
 (0)