Skip to content

Commit bd792b5

Browse files
authored
Merge pull request #41 from bhumi46/testgrid
Fix RKE2 script hanging issue: remove problematic exec redirection an…
2 parents 03d7a18 + b9bdd06 commit bd792b5

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

terraform/modules/aws/rke2-cluster/main.tf

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ resource "null_resource" "rke2-primary-cluster-setup" {
9595
"sudo chmod +x /tmp/rke2-setup.sh",
9696
"echo 'Starting RKE2 setup script at $(date)...'",
9797
# Run the script with timeout to prevent hanging
98-
"timeout 900 sudo bash -x /tmp/rke2-setup.sh 2>&1 | tee /tmp/rke2-setup.log || SCRIPT_EXIT_CODE=$?",
98+
"timeout 900 sudo bash -x /tmp/rke2-setup.sh > /tmp/rke2-setup.log 2>&1",
9999
"SCRIPT_EXIT_CODE=$?",
100100
"echo 'RKE2 setup script completed at $(date) with exit code: $SCRIPT_EXIT_CODE'",
101-
"if [ $SCRIPT_EXIT_CODE -eq 124 ]; then echo 'Script timed out after 15 minutes'; tail -100 /tmp/rke2-setup.log; exit 124; fi",
102-
"if [ $SCRIPT_EXIT_CODE -ne 0 ]; then echo 'Script failed! Last 50 lines of log:'; tail -50 /tmp/rke2-setup.log; exit $SCRIPT_EXIT_CODE; fi"
101+
"if [ $SCRIPT_EXIT_CODE -eq 124 ]; then echo 'Script timed out after 15 minutes'; echo 'Last 100 lines of log:'; tail -100 /tmp/rke2-setup.log; exit 124; fi",
102+
"if [ $SCRIPT_EXIT_CODE -ne 0 ]; then echo 'Script failed! Last 50 lines of log:'; tail -50 /tmp/rke2-setup.log; exit $SCRIPT_EXIT_CODE; fi",
103+
"echo 'Script completed successfully. Last 20 lines of log:'; tail -20 /tmp/rke2-setup.log"
103104
]
104105
)
105106
}
@@ -135,10 +136,13 @@ resource "null_resource" "rke2-cluster-setup" {
135136
"echo 'INTERNAL_IP=${each.value}' | sudo tee -a /etc/environment",
136137
"sudo chmod +x /tmp/rke2-setup.sh",
137138
"echo 'Starting RKE2 setup script for ${each.key} at $(date)...'",
138-
"sudo bash -x /tmp/rke2-setup.sh 2>&1 | tee /tmp/rke2-setup.log",
139+
# Run the script with timeout to prevent hanging
140+
"timeout 900 sudo bash -x /tmp/rke2-setup.sh > /tmp/rke2-setup.log 2>&1",
139141
"SCRIPT_EXIT_CODE=$?",
140142
"echo 'RKE2 setup script for ${each.key} completed at $(date) with exit code: $SCRIPT_EXIT_CODE'",
141-
"if [ $SCRIPT_EXIT_CODE -ne 0 ]; then echo 'Script failed! Last 50 lines of log:'; tail -50 /tmp/rke2-setup.log; exit $SCRIPT_EXIT_CODE; fi"
143+
"if [ $SCRIPT_EXIT_CODE -eq 124 ]; then echo 'Script timed out after 15 minutes'; echo 'Last 100 lines of log:'; tail -100 /tmp/rke2-setup.log; exit 124; fi",
144+
"if [ $SCRIPT_EXIT_CODE -ne 0 ]; then echo 'Script failed! Last 50 lines of log:'; tail -50 /tmp/rke2-setup.log; exit $SCRIPT_EXIT_CODE; fi",
145+
"echo 'Script completed successfully. Last 20 lines of log:'; tail -20 /tmp/rke2-setup.log"
142146
]
143147
)
144148
}

terraform/modules/aws/rke2-cluster/rke2-setup.sh

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ ENV_FILE_PATH="/etc/environment"
77
source $ENV_FILE_PATH
88
env | grep -E 'K8S|RKE2|WORK|CONTROL'
99

10-
# Redirect stdout and stderr to log file
11-
exec > >(tee -a "$LOG_FILE") 2>&1
10+
# Simple logging approach - log important steps manually
11+
echo "Script started at: $(date)" | tee -a "$LOG_FILE"
1212

1313
# set commands for error handling.
1414
set -e
@@ -18,44 +18,44 @@ set -o errtrace # trace ERR through 'time command' and other functions
1818
set -o pipefail # trace ERR through pipes
1919

2020
# Add error handler
21-
trap 'echo "Error occurred at line $LINENO. Exit code: $?" >&2' ERR
21+
trap 'echo "Error occurred at line $LINENO. Exit code: $?" | tee -a "$LOG_FILE" >&2' ERR
2222

2323
# Validate required environment variables
24-
echo "Validating required environment variables..."
24+
echo "Validating required environment variables..." | tee -a "$LOG_FILE"
2525
required_vars=("WORK_DIR" "K8S_INFRA_REPO_URL" "K8S_INFRA_BRANCH" "RKE2_CONFIG_DIR" "RKE2_LOCATION" "NODE_NAME" "K8S_TOKEN" "INTERNAL_IP" "CONTROL_PLANE_NODE_1" "CLUSTER_DOMAIN")
2626

2727
for var in "${required_vars[@]}"; do
2828
if [[ -z "${!var:-}" ]]; then
29-
echo "Error: Required environment variable $var is not set"
29+
echo "Error: Required environment variable $var is not set" | tee -a "$LOG_FILE"
3030
exit 1
3131
else
32-
echo "$var is set"
32+
echo "$var is set" | tee -a "$LOG_FILE"
3333
fi
3434
done
3535

36-
echo "All required environment variables are present"
36+
echo "All required environment variables are present" | tee -a "$LOG_FILE"
3737

3838

39-
echo "Installing RKE2"
39+
echo "Installing RKE2" | tee -a "$LOG_FILE"
4040
RKE2_EXISTENCE=$( which rke2 2>/dev/null || echo "")
4141
if [[ -z "$RKE2_EXISTENCE" ]]; then
42-
echo "RKE2 not found, installing..."
42+
echo "RKE2 not found, installing..." | tee -a "$LOG_FILE"
4343
curl -sfL https://get.rke2.io | sh -
44-
echo "RKE2 installation completed"
44+
echo "RKE2 installation completed" | tee -a "$LOG_FILE"
4545
else
46-
echo "RKE2 already installed at: $RKE2_EXISTENCE"
46+
echo "RKE2 already installed at: $RKE2_EXISTENCE" | tee -a "$LOG_FILE"
4747
fi
4848

4949
cd $WORK_DIR
50-
echo "Cloning K8S infrastructure repository..."
50+
echo "Cloning K8S infrastructure repository..." | tee -a "$LOG_FILE"
5151
if [ -d "k8s-infra" ]; then
52-
echo "k8s-infra directory already exists, updating..."
52+
echo "k8s-infra directory already exists, updating..." | tee -a "$LOG_FILE"
5353
cd k8s-infra
54-
git pull origin $K8S_INFRA_BRANCH || echo "Warning: git pull failed, continuing with existing files"
54+
git pull origin $K8S_INFRA_BRANCH || echo "Warning: git pull failed, continuing with existing files" | tee -a "$LOG_FILE"
5555
cd $WORK_DIR
5656
else
5757
git clone $K8S_INFRA_REPO_URL -b $K8S_INFRA_BRANCH || {
58-
echo "Warning: git clone failed, but continuing..."
58+
echo "Warning: git clone failed, but continuing..." | tee -a "$LOG_FILE"
5959
}
6060
fi
6161

@@ -65,28 +65,27 @@ chown -R 1000:1000 $RKE2_CONFIG_DIR
6565
cd $RKE2_LOCATION
6666

6767
if [[ -f "$RKE2_CONFIG_DIR/config.yaml" ]]; then
68-
echo "RKE CONFIG file exists \"$RKE2_CONFIG_DIR/config.yaml\""
69-
echo "RKE2 setup already completed, exiting successfully"
68+
echo "RKE CONFIG file exists \"$RKE2_CONFIG_DIR/config.yaml\"" | tee -a "$LOG_FILE"
69+
echo "RKE2 setup already completed, exiting successfully" | tee -a "$LOG_FILE"
7070
exit 0
7171
fi
7272

7373
# Determine the role of the instance using pattern matching
74-
sleep 30
7574
source $ENV_FILE_PATH
76-
echo "Node name: $NODE_NAME"
77-
echo "Current directory: $(pwd)"
78-
echo "Files in current directory:"
79-
ls -la
75+
echo "Node name: $NODE_NAME" | tee -a "$LOG_FILE"
76+
echo "Current directory: $(pwd)" | tee -a "$LOG_FILE"
77+
echo "Files in current directory:" | tee -a "$LOG_FILE"
78+
ls -la | tee -a "$LOG_FILE"
8079

8180
if [[ "$NODE_NAME" == CONTROL-PLANE-NODE-1 ]]; then
82-
echo "PRIMARY CONTROL PLANE NODE"
81+
echo "PRIMARY CONTROL PLANE NODE" | tee -a "$LOG_FILE"
8382
RKE2_SERVICE="rke2-server"
84-
echo "Looking for primary control plane config template..."
85-
ls -la rke2-server-control-plane-primary.conf.template 2>/dev/null || echo "Template file not found!"
83+
echo "Looking for primary control plane config template..." | tee -a "$LOG_FILE"
84+
ls -la rke2-server-control-plane-primary.conf.template 2>/dev/null | tee -a "$LOG_FILE" || echo "Template file not found!" | tee -a "$LOG_FILE"
8685
if cp rke2-server-control-plane-primary.conf.template $RKE2_CONFIG_DIR/config.yaml; then
87-
echo "Successfully copied primary control plane config template"
86+
echo "Successfully copied primary control plane config template" | tee -a "$LOG_FILE"
8887
else
89-
echo "Error: Failed to copy primary control plane config template"
88+
echo "Error: Failed to copy primary control plane config template" | tee -a "$LOG_FILE"
9089
exit 1
9190
fi
9291
export RKE2_SERVICE="rke2-server" | sudo tee -a $ENV_FILE_PATH

0 commit comments

Comments
 (0)