Skip to content

Commit d52b9ea

Browse files
committed
fix: Fix parameter validation and cloud path assignment logic
Refactored script to correctly handle script parameters, validate values, and assign appropriate cloud paths based on the selected cloud option. Improved error handling for invalid inputs.
1 parent 49972cb commit d52b9ea

30 files changed

+1361
-1414
lines changed

kvm_almalinux_810.sh

+43-45
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,47 @@ PACKER_LOG=0
1212
cloud="generic"
1313
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
1414

15-
1615
# Check for script parameters
1716
if [ "$#" -eq 1 ]; then
18-
if [[ "$1" =~ ^(oci|generic|alicloud)$ ]]; then
19-
cloud="$1"
20-
elif [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
21-
PACKER_LOG="$1"
22-
else
23-
echo -e "${RED}Invalid parameter. Please provide a valid value for PACKER_LOG (0 or 1) or cloud (oci, generic, alibabacloud).${NC}"
24-
exit 1
25-
fi
17+
if [[ "$1" =~ ^(oci|generic|alicloud)$ ]]; then
18+
cloud="$1"
19+
elif [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
20+
PACKER_LOG="$1"
21+
else
22+
echo -e "${RED}Invalid parameter. Please provide a valid value for PACKER_LOG (0 or 1) or cloud (oci, generic, alibabacloud).${NC}"
23+
exit 1
24+
fi
2625
elif [ "$#" -eq 2 ]; then
27-
if [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
28-
PACKER_LOG="$1"
29-
else
30-
echo -e "${RED}Invalid value for PACKER_LOG. Please provide 0 or 1.${NC}"
31-
exit 1
32-
fi
26+
if [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
27+
PACKER_LOG="$1"
28+
else
29+
echo -e "${RED}Invalid value for PACKER_LOG. Please provide 0 or 1.${NC}"
30+
exit 1
31+
fi
3332

34-
if [[ "$2" =~ ^(oci|generic|alicloud)$ ]]; then
35-
cloud="$2"
36-
else
37-
echo -e "${RED}Invalid cloud value. Please provide one of: oci, generic, alicloud.${NC}"
38-
exit 1
39-
fi
33+
if [[ "$2" =~ ^(oci|generic|alicloud)$ ]]; then
34+
cloud="$2"
35+
else
36+
echo -e "${RED}Invalid cloud value. Please provide one of: oci, generic, alicloud.${NC}"
37+
exit 1
38+
fi
4039
fi
4140

4241
# Set cloud_init_path based on cloud value
4342
case $cloud in
44-
oci)
45-
cloud_init_path="extra/files/cloud-init/rhel/oci/cloud.cfg"
46-
;;
47-
generic)
48-
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
49-
;;
50-
alicloud)
51-
cloud_init_path="extra/files/cloud-init/rhel/alicloud/cloud.cfg"
52-
;;
53-
*)
54-
echo -e "${RED}Invalid cloud value.${NC}"
55-
exit 1
56-
;;
43+
oci)
44+
cloud_init_path="extra/files/cloud-init/rhel/oci/cloud.cfg"
45+
;;
46+
generic)
47+
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
48+
;;
49+
alicloud)
50+
cloud_init_path="extra/files/cloud-init/rhel/alicloud/cloud.cfg"
51+
;;
52+
*)
53+
echo -e "${RED}Invalid cloud value.${NC}"
54+
exit 1
55+
;;
5756
esac
5857

5958
# Export PACKER_LOG
@@ -66,7 +65,6 @@ output_dir="packer-${version}-${cloud}-x86_64-qemu"
6665
var_file="kvm/variables_kvm_${version}.pkvars.hcl"
6766
template="kvm/kvm_${family}.pkr.hcl"
6867

69-
7068
# Display variable values
7169
echo -e "${BLUE}--- Variable Values ---${NC}"
7270
echo -e "PACKER_LOG: ${YELLOW}$PACKER_LOG${NC}"
@@ -84,26 +82,26 @@ echo -e "${YELLOW}Starting the Packer script...${NC}"
8482

8583
# Check if output directory exists
8684
if [ -d "./$output_dir" ]; then
87-
echo -e "${RED}$output_dir exists. Removing folder...${NC}"
88-
rm -rf "./$output_dir/"
85+
echo -e "${RED}$output_dir exists. Removing folder...${NC}"
86+
rm -rf "./$output_dir/"
8987
else
90-
echo -e "${GREEN}$output_dir does not exist. Proceeding...${NC}"
88+
echo -e "${GREEN}$output_dir does not exist. Proceeding...${NC}"
9189
fi
9290

9391
# Validate packer template
9492
echo -e "${YELLOW}Validating Packer template...${NC}"
95-
if ! packer validate --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
96-
echo -e "${RED}Packer validate failed - exiting now${NC}"
97-
exit 1
93+
if ! packer validate --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
94+
echo -e "${RED}Packer validate failed - exiting now${NC}"
95+
exit 1
9896
else
99-
echo -e "${GREEN}Packer template validation successful!${NC}"
97+
echo -e "${GREEN}Packer template validation successful!${NC}"
10098
fi
10199

102100
# Build packer template
103101
echo -e "${YELLOW}Building Packer template...${NC}"
104102
if ! packer build --force --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
105-
echo -e "${RED}Packer build failed - exiting now${NC}"
106-
exit 1
103+
echo -e "${RED}Packer build failed - exiting now${NC}"
104+
exit 1
107105
else
108-
echo -e "${GREEN}Packer build completed successfully!${NC}"
106+
echo -e "${GREEN}Packer build completed successfully!${NC}"
109107
fi

kvm_almalinux_87.sh

+43-45
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,47 @@ PACKER_LOG=0
1212
cloud="generic"
1313
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
1414

15-
1615
# Check for script parameters
1716
if [ "$#" -eq 1 ]; then
18-
if [[ "$1" =~ ^(oci|generic|alicloud)$ ]]; then
19-
cloud="$1"
20-
elif [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
21-
PACKER_LOG="$1"
22-
else
23-
echo -e "${RED}Invalid parameter. Please provide a valid value for PACKER_LOG (0 or 1) or cloud (oci, generic, alibabacloud).${NC}"
24-
exit 1
25-
fi
17+
if [[ "$1" =~ ^(oci|generic|alicloud)$ ]]; then
18+
cloud="$1"
19+
elif [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
20+
PACKER_LOG="$1"
21+
else
22+
echo -e "${RED}Invalid parameter. Please provide a valid value for PACKER_LOG (0 or 1) or cloud (oci, generic, alibabacloud).${NC}"
23+
exit 1
24+
fi
2625
elif [ "$#" -eq 2 ]; then
27-
if [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
28-
PACKER_LOG="$1"
29-
else
30-
echo -e "${RED}Invalid value for PACKER_LOG. Please provide 0 or 1.${NC}"
31-
exit 1
32-
fi
26+
if [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
27+
PACKER_LOG="$1"
28+
else
29+
echo -e "${RED}Invalid value for PACKER_LOG. Please provide 0 or 1.${NC}"
30+
exit 1
31+
fi
3332

34-
if [[ "$2" =~ ^(oci|generic|alicloud)$ ]]; then
35-
cloud="$2"
36-
else
37-
echo -e "${RED}Invalid cloud value. Please provide one of: oci, generic, alicloud.${NC}"
38-
exit 1
39-
fi
33+
if [[ "$2" =~ ^(oci|generic|alicloud)$ ]]; then
34+
cloud="$2"
35+
else
36+
echo -e "${RED}Invalid cloud value. Please provide one of: oci, generic, alicloud.${NC}"
37+
exit 1
38+
fi
4039
fi
4140

4241
# Set cloud_init_path based on cloud value
4342
case $cloud in
44-
oci)
45-
cloud_init_path="extra/files/cloud-init/rhel/oci/cloud.cfg"
46-
;;
47-
generic)
48-
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
49-
;;
50-
alicloud)
51-
cloud_init_path="extra/files/cloud-init/rhel/alicloud/cloud.cfg"
52-
;;
53-
*)
54-
echo -e "${RED}Invalid cloud value.${NC}"
55-
exit 1
56-
;;
43+
oci)
44+
cloud_init_path="extra/files/cloud-init/rhel/oci/cloud.cfg"
45+
;;
46+
generic)
47+
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
48+
;;
49+
alicloud)
50+
cloud_init_path="extra/files/cloud-init/rhel/alicloud/cloud.cfg"
51+
;;
52+
*)
53+
echo -e "${RED}Invalid cloud value.${NC}"
54+
exit 1
55+
;;
5756
esac
5857

5958
# Export PACKER_LOG
@@ -66,7 +65,6 @@ output_dir="packer-${version}-${cloud}-x86_64-qemu"
6665
var_file="kvm/variables_kvm_${version}.pkvars.hcl"
6766
template="kvm/kvm_${family}.pkr.hcl"
6867

69-
7068
# Display variable values
7169
echo -e "${BLUE}--- Variable Values ---${NC}"
7270
echo -e "PACKER_LOG: ${YELLOW}$PACKER_LOG${NC}"
@@ -84,26 +82,26 @@ echo -e "${YELLOW}Starting the Packer script...${NC}"
8482

8583
# Check if output directory exists
8684
if [ -d "./$output_dir" ]; then
87-
echo -e "${RED}$output_dir exists. Removing folder...${NC}"
88-
rm -rf "./$output_dir/"
85+
echo -e "${RED}$output_dir exists. Removing folder...${NC}"
86+
rm -rf "./$output_dir/"
8987
else
90-
echo -e "${GREEN}$output_dir does not exist. Proceeding...${NC}"
88+
echo -e "${GREEN}$output_dir does not exist. Proceeding...${NC}"
9189
fi
9290

9391
# Validate packer template
9492
echo -e "${YELLOW}Validating Packer template...${NC}"
95-
if ! packer validate --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
96-
echo -e "${RED}Packer validate failed - exiting now${NC}"
97-
exit 1
93+
if ! packer validate --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
94+
echo -e "${RED}Packer validate failed - exiting now${NC}"
95+
exit 1
9896
else
99-
echo -e "${GREEN}Packer template validation successful!${NC}"
97+
echo -e "${GREEN}Packer template validation successful!${NC}"
10098
fi
10199

102100
# Build packer template
103101
echo -e "${YELLOW}Building Packer template...${NC}"
104102
if ! packer build --force --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
105-
echo -e "${RED}Packer build failed - exiting now${NC}"
106-
exit 1
103+
echo -e "${RED}Packer build failed - exiting now${NC}"
104+
exit 1
107105
else
108-
echo -e "${GREEN}Packer build completed successfully!${NC}"
106+
echo -e "${GREEN}Packer build completed successfully!${NC}"
109107
fi

kvm_almalinux_88.sh

+43-45
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,47 @@ PACKER_LOG=0
1212
cloud="generic"
1313
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
1414

15-
1615
# Check for script parameters
1716
if [ "$#" -eq 1 ]; then
18-
if [[ "$1" =~ ^(oci|generic|alicloud)$ ]]; then
19-
cloud="$1"
20-
elif [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
21-
PACKER_LOG="$1"
22-
else
23-
echo -e "${RED}Invalid parameter. Please provide a valid value for PACKER_LOG (0 or 1) or cloud (oci, generic, alibabacloud).${NC}"
24-
exit 1
25-
fi
17+
if [[ "$1" =~ ^(oci|generic|alicloud)$ ]]; then
18+
cloud="$1"
19+
elif [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
20+
PACKER_LOG="$1"
21+
else
22+
echo -e "${RED}Invalid parameter. Please provide a valid value for PACKER_LOG (0 or 1) or cloud (oci, generic, alibabacloud).${NC}"
23+
exit 1
24+
fi
2625
elif [ "$#" -eq 2 ]; then
27-
if [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
28-
PACKER_LOG="$1"
29-
else
30-
echo -e "${RED}Invalid value for PACKER_LOG. Please provide 0 or 1.${NC}"
31-
exit 1
32-
fi
26+
if [ "$1" -eq 0 ] || [ "$1" -eq 1 ]; then
27+
PACKER_LOG="$1"
28+
else
29+
echo -e "${RED}Invalid value for PACKER_LOG. Please provide 0 or 1.${NC}"
30+
exit 1
31+
fi
3332

34-
if [[ "$2" =~ ^(oci|generic|alicloud)$ ]]; then
35-
cloud="$2"
36-
else
37-
echo -e "${RED}Invalid cloud value. Please provide one of: oci, generic, alicloud.${NC}"
38-
exit 1
39-
fi
33+
if [[ "$2" =~ ^(oci|generic|alicloud)$ ]]; then
34+
cloud="$2"
35+
else
36+
echo -e "${RED}Invalid cloud value. Please provide one of: oci, generic, alicloud.${NC}"
37+
exit 1
38+
fi
4039
fi
4140

4241
# Set cloud_init_path based on cloud value
4342
case $cloud in
44-
oci)
45-
cloud_init_path="extra/files/cloud-init/rhel/oci/cloud.cfg"
46-
;;
47-
generic)
48-
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
49-
;;
50-
alicloud)
51-
cloud_init_path="extra/files/cloud-init/rhel/alicloud/cloud.cfg"
52-
;;
53-
*)
54-
echo -e "${RED}Invalid cloud value.${NC}"
55-
exit 1
56-
;;
43+
oci)
44+
cloud_init_path="extra/files/cloud-init/rhel/oci/cloud.cfg"
45+
;;
46+
generic)
47+
cloud_init_path="extra/files/cloud-init/rhel/generic/cloud.cfg"
48+
;;
49+
alicloud)
50+
cloud_init_path="extra/files/cloud-init/rhel/alicloud/cloud.cfg"
51+
;;
52+
*)
53+
echo -e "${RED}Invalid cloud value.${NC}"
54+
exit 1
55+
;;
5756
esac
5857

5958
# Export PACKER_LOG
@@ -66,7 +65,6 @@ output_dir="packer-${version}-${cloud}-x86_64-qemu"
6665
var_file="kvm/variables_kvm_${version}.pkvars.hcl"
6766
template="kvm/kvm_${family}.pkr.hcl"
6867

69-
7068
# Display variable values
7169
echo -e "${BLUE}--- Variable Values ---${NC}"
7270
echo -e "PACKER_LOG: ${YELLOW}$PACKER_LOG${NC}"
@@ -84,26 +82,26 @@ echo -e "${YELLOW}Starting the Packer script...${NC}"
8482

8583
# Check if output directory exists
8684
if [ -d "./$output_dir" ]; then
87-
echo -e "${RED}$output_dir exists. Removing folder...${NC}"
88-
rm -rf "./$output_dir/"
85+
echo -e "${RED}$output_dir exists. Removing folder...${NC}"
86+
rm -rf "./$output_dir/"
8987
else
90-
echo -e "${GREEN}$output_dir does not exist. Proceeding...${NC}"
88+
echo -e "${GREEN}$output_dir does not exist. Proceeding...${NC}"
9189
fi
9290

9391
# Validate packer template
9492
echo -e "${YELLOW}Validating Packer template...${NC}"
95-
if ! packer validate --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
96-
echo -e "${RED}Packer validate failed - exiting now${NC}"
97-
exit 1
93+
if ! packer validate --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
94+
echo -e "${RED}Packer validate failed - exiting now${NC}"
95+
exit 1
9896
else
99-
echo -e "${GREEN}Packer template validation successful!${NC}"
97+
echo -e "${GREEN}Packer template validation successful!${NC}"
10098
fi
10199

102100
# Build packer template
103101
echo -e "${YELLOW}Building Packer template...${NC}"
104102
if ! packer build --force --var-file="$var_file" -var "cloud_init_path=$cloud_init_path" -var="template=$output_dir" "$template"; then
105-
echo -e "${RED}Packer build failed - exiting now${NC}"
106-
exit 1
103+
echo -e "${RED}Packer build failed - exiting now${NC}"
104+
exit 1
107105
else
108-
echo -e "${GREEN}Packer build completed successfully!${NC}"
106+
echo -e "${GREEN}Packer build completed successfully!${NC}"
109107
fi

0 commit comments

Comments
 (0)