Skip to content

Commit 16b2764

Browse files
author
imusayev
committed
Extend Bash script for interactive kickstart script generation
1 parent 6632ae1 commit 16b2764

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

scripts/make_iso.sh

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# Name : make_iso.sh #
55
# Author : Ibrahim Musayev #
66
# Purpose : creates a new ISO image named CentOS-7-x86_64.iso and includes #
7-
# latest version of kickstart script. #
7+
# latest version of kickstart script. Also, The script provides #
8+
# an interactive menu for the user to choose between generating #
9+
# a new kickstart script or proceeding with an existing one. #
810
# History : 15.05.23 Ibrahim Musayev, creation #
911
################################################################################
1012

@@ -16,14 +18,56 @@ mnt_iso_dir="/mnt/iso"
1618
tmp_workdir="/tmp/workdir"
1719
tmp_workdir_iso="$tmp_workdir/iso"
1820
kickstart_dir="$tmp_workdir_iso/kickstart"
21+
python_replace_parameters="replace_parameters_in_ks_cfg.py"
1922
ks_cfg=$1
2023

21-
# Check if the kickstart script name is provided
22-
if [ -z "$1" ]; then
23-
read -p "Please provide the name of the kickstart script: " ks_cfg
24-
else
25-
ks_cfg=$1
26-
fi
24+
detect_python_command() {
25+
if command -v python3 &> /dev/null; then
26+
python_command="python3"
27+
elif command -v python &> /dev/null; then
28+
python_command="python"
29+
else
30+
echo "Python is not installed."
31+
exit 1
32+
fi
33+
}
34+
35+
# Function to generate new kickstart script
36+
generate_new_kickstart_script() {
37+
echo "Generating new kickstart script..."
38+
$python_command $python_replace_parameters
39+
ks_cfg=generated-ks.cfg
40+
}
41+
42+
# Function to proceed with existing kickstart script
43+
proceed_with_existing_kickstart_script() {
44+
echo "Proceeding with existing kickstart script..."
45+
# Check if the kickstart script name is provided
46+
if [ -z "$1" ]; then
47+
read -p "Please provide the name of the kickstart script: " ks_cfg
48+
else
49+
ks_cfg=$1
50+
fi
51+
}
52+
53+
detect_python_command
54+
55+
echo "Do you want to generate a new kickstart script with the new network configuration or proceed with an existing one?"
56+
select option in "Generate new kickstart script" "Proceed with existing kickstart script"; do
57+
case $option in
58+
"Generate new kickstart script")
59+
generate_new_kickstart_script
60+
break
61+
;;
62+
"Proceed with existing kickstart script")
63+
proceed_with_existing_kickstart_script
64+
break
65+
;;
66+
*)
67+
echo "Invalid option. Please try again."
68+
;;
69+
esac
70+
done
2771

2872
# Check if the ISO file exists in the Downloads folder and proceed further
2973
if [ -f "$downloads_dir/$iso_file" ]; then
@@ -34,7 +78,7 @@ if [ -f "$downloads_dir/$iso_file" ]; then
3478
sudo mkdir -p "$mnt_iso_dir"
3579
else
3680
echo "Unmount $mnt_iso_dir directory before starting..."
37-
sudo umount $mnt_iso_dir
81+
sudo umount $mnt_iso_dir 2> /dev/null
3882
fi
3983

4084
# Check if the /tmp/workdir directory exists
@@ -76,7 +120,7 @@ if [ -f "$downloads_dir/$iso_file" ]; then
76120
echo "$new_iso_name File successfully created..."
77121
sudo mv $new_iso_name $new_iso_dir
78122
echo "The new file is moved to the $new_iso_dir directory..."
79-
sudo umount "$mnt_iso_dir"
123+
sudo umount "$mnt_iso_dir" 2> /dev/null
80124
else
81125
echo "$new_iso_name File creation failed. Exiting..."
82126
exit 1
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/python3
2+
3+
def replace_parameters_in_ks_cfg(file_path="vm-ks.cfg", output_file_path="generated-ks.cfg"):
4+
5+
with open(file_path, "r") as file:
6+
content = file.read()
7+
8+
netmask = input("Enter the netmask value: ")
9+
ipaddr = input("Enter the IP address value: ")
10+
gateway = input("Enter the gateway value: ")
11+
nameserver = input("Enter the nameserver value: ")
12+
hostname = input("Enter the hostname value (FQDN): ")
13+
14+
content = content.replace("@ipaddr", ipaddr)
15+
content = content.replace("@netmask", netmask)
16+
content = content.replace("@gateway", gateway)
17+
content = content.replace("@nameserver", nameserver)
18+
content = content.replace("@hostname", hostname)
19+
20+
with open(output_file_path, "w") as file:
21+
file.write(content)
22+
file.write("\n\n# This file generated by a Python script. Don't edit directly.")
23+
24+
print(f"Updated content written to {output_file_path}")
25+
26+
replace_parameters_in_ks_cfg()

0 commit comments

Comments
 (0)