forked from Juniper/junos-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateFiles.sh
executable file
·340 lines (282 loc) · 10.3 KB
/
generateFiles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
set -e
# Function to check if a command is available
command_exists() {
command -v "$1" >/dev/null 2>&1
}
retry_script() {
while true; do
./generateFiles.sh
# Check the exit status of the script
if [ $? -eq 0 ]; then
# Script ran successfully, exit the loop
break
else
# Script failed, display an error message
echo "Script failed. Retrying in 5 seconds..."
sleep 5
fi
done
}
# caputres the user's home directory
home_dir="$PWD"
# Check if Python is installed
if ! command_exists python3; then
echo "Python is not installed. Please Install 'Python' before running the script"
fi
# Check if Python is installed
if ! command_exists go; then
echo "Go is not installed. Please Install 'Go' before running the script."
fi
# Prompt the user for their choice
echo "Do you want to:"
echo "1. Build a provider from scratch"
echo "2. Provide a configuration"
read -p "Enter your choice (1/2): " choice
# Check the user's choice
if [ "$choice" == "1" ]; then
echo "You chose to build a provider from scratch."
# Create config.toml file
if [ ! -f "config.toml" ]; then
echo "Creating config.toml file..."
cat << EOF > config.toml
yangDir = "$(pwd)/yang_files"
providerDir = "$(pwd)/terraform_providers"
xpathPath = "$(pwd)/xpath_inputs.xml"
providerName = "vsrx"
fileType = "both"
EOF
fi
# Check if yang_files folder exists
if [ -d "yang_files" ]; then
# Find at least one .yang file in yang_files folder
yang_files=$(find yang_files -name "*.yang")
# If YANG files are found, generate YIN and Xpath Files
if [ -n "$yang_files" ]; then
echo "Found .yang files in yang_files folder."
# Change directory to /cmd/processYang
cd $home_dir/cmd/processYang || exit 1
# Activate venv
python3 -m venv venv
source venv/bin/activate
# Check and install pyang if needed
if ! command_exists pyang; then
echo "pyang is not installed. Installing pyang..."
pip install pyang
pyang -v
fi
# Run go build command and generate YIN and Xpath Files
go build
./processYang -config $home_dir/config.toml
deactivate
else
echo "No .yang files found in yang_files folder. Add files and re-run script"
fi
else
echo "yang_files folder does not exist. Create and add necessary files"
fi
elif [ "$choice" == "2" ]; then
echo "You chose to provide a configuration."
script_directory="$home_dir/user_config_files"
# Create the directory if it doesn't exist
mkdir -p "$script_directory"
# Initialize the config_file variable
config_file=""
echo ""
echo ""
# Prompt the user for the configuration file name and validate it
while [ ! -f "$config_file" ]; do
read -p "Enter the configuration file name: " config_file
full_path="$script_directory/$config_file"
if [ -f "$full_path" ]; then
config_file="$full_path"
else
echo "The provided file does not exist in your script's directory."
config_file=""
fi
done
# Ask user what device they are working on
valid_options=("vsrx" "vmx" "vqfx" "vptx")
user_input=""
while [[ ! " ${valid_options[*]} " =~ " $user_input " ]]; do
read -p "Enter a valid device option (vsrx, vmx, vqfx, vptx): " user_input
if [[ ! " ${valid_options[*]} " =~ " $user_input " ]]; then
echo "Invalid input. Please enter one of the following options: vsrx, vmx, vqfx, vptx."
fi
done
# Prompt the user for the Junos version
read -p "Enter the Junos version: " junos_version
echo ""
echo ""
# Display the user's selections
echo "Configuration file path: $config_file"
echo "Device name: $user_input"
echo "Junos version: $junos_version"
# Ask for confirmation
while true; do
read -p "Are these selections correct? (yes/no): " confirmation
case "$confirmation" in
[Yy]* ) break;;
[Nn]* )
# If the user says no, allow them to start over
exec "$0";;
* ) echo "Please answer 'yes' or 'no'.";;
esac
done
echo ""
echo ""
# Create or overwrite config.toml file
echo "Creating or overwriting config.toml file..."
cat > config.toml << EOF
yangDir = "$(pwd)/yang_files"
providerDir = "$(pwd)/terraform_providers"
xpathPath = "$(pwd)/xpath_inputs.xml"
providerName = "$user_input"
fileType = "both"
EOF
echo ""
echo ""
junos_version_combined="${junos_version}R1"
device_names=("vmx" "vptx" "vsrx" "vqfx")
device_mappings=("junos" "junos" "junos-es" "junos-qfx")
# Find the index of the input device name in the device_names array
index=-1
for ((i=0; i<${#device_names[@]}; i++)); do
if [ "${device_names[$i]}" = "$user_input" ]; then
index=$i
break
fi
done
# Output the corresponding mapping if found
if [ $index -ne -1 ]; then
supported_devices="${device_mappings[$index]}"
else
echo "Device mapping not found."
fi
# Use the user's input to look up the supported devices
supported_devices="${device_mappings[${user_input}]}"
# Check if the device name exists in the mapping
if [ -z "$supported_devices" ]; then
echo "Device name not recognized."
exit 1
fi
# # Output the supported devices
# echo "Supported devices for $selected_device: $supported_devices"
common_path="yang/$junos_version/$junos_version_combined/common/[email protected]"
path_to="yang/$junos_version/$junos_version_combined/$supported_devices/conf/"
# Define the target directory in the home directory
target_dir="$home_dir/yang_files"
# Check if the target directory already exists, and create it if not
if [ ! -d "$target_dir" ]; then
echo "yang_files folder does not exist. Creating and adding necessary files"
mkdir -p "$target_dir"
target_dir="$home_dir/yang"
# Check if the target directory already exists, and create it if not
if [ ! -d "$target_dir" ]; then
mkdir -p "$target_dir"
# Git clone the Juniper YANG repository into the target directory
echo "Cloning the Juniper YANG repository for Junos $junos_version into $target_dir..."
git clone https://github.com/Juniper/yang.git "$target_dir"
fi
# Check if the git clone was successful
if [ $? -eq 0 ]; then
echo "Cloning successful. YANG files are in $target_dir."
else
echo "Folder already created or clone failed. If clone failed, please check your internet connection or repository URL."
fi
target_dir="$home_dir/yang_files"
# Copy all files from the source directory to the target directory
cp -r "$path_to"* "$target_dir"
# Copy the common file to the target directory
cp "$common_path" "$target_dir"
# Check if the copy operation was successful
if [ $? -eq 0 ]; then
echo "Files copied successfully to $target_dir."
else
echo "Copy operation failed."
fi
target_dir="$home_dir/yang"
rm -rf $target_dir
fi
# Find at least one .yang file in yang_files folder
yang_files=$(find yang_files -name "*.yang")
# If YANG files are found, generate YIN and Xpath Files
if [ -n "$yang_files" ]; then
echo "Found .yang files in yang_files folder."
# Change directory to /cmd/processYang
cd $home_dir/cmd/processYang || exit 1
# Activate venv
python3 -m venv venv
source venv/bin/activate
# Check and install pyang if needed
if ! command_exists pyang; then
echo "pyang is not installed. Installing pyang..."
pip install pyang
pyang -v
fi
# Run go build command and generate YIN and Xpath Files
go build
./processYang -config $home_dir/config.toml
deactivate
# go run $home_dir/Internal/processYang/createXpathInputs.go
else
echo "No .yang files found in yang_files folder. Add files and re-run script"
fi
cd "$home_dir"
go run createXpathInputs.go
echo ""
echo ""
# Search for XML files containing "xpath" in their filenames in the home directory
xml_files=$(find "$PWD" -type f -name '*xpath*.xml')
# Define the folder name
folderName="TFtemplates"
# Check if the folder already exists
if [ ! -d "$folderName" ]; then
# Create the folder if it doesn't exist
mkdir "$folderName"
echo "Folder '$folderName' created successfully. Folder stores tf templates for testing"
else
echo "Folder '$folderName' already exists to store .tf templates"
fi
# Check if any matching XML files were found
if [ -n "$xml_files" ]; then
# Now Build the provider
cd $home_dir/cmd/processProviders
go build
./processProviders -config $home_dir/config.toml
cd $home_dir/terraform_providers
go mod tidy -go=1.16 && go mod tidy -go=1.17
go build
else
echo "No XML xpath file found. Try renaming the xpath file to include 'xpath' in the name."
fi
# Define the target directory in the home directory
target_dir="$home_dir/testbed"
# Check if the target directory already exists
if [ ! -d "$target_dir" ]; then
# If it doesn't exist, create the directory
mkdir -p "$target_dir"
echo "Created target directory: $target_dir"
fi
# Check if main.tf does not exist in the testbed directory
if [ ! -f "$target_dir/main.tf" ]; then
# Create main.tf
touch "$target_dir/main.tf"
echo "Created main.tf in the testbed directory"
fi
# Create a folder named after device_name appended with "_1"
device_folder="$target_dir/${user_input}_1"
if [ ! -d "$device_folder" ]; then
# If it doesn't exist, create the folder
mkdir -p "$device_folder"
echo "Created folder: $device_folder"
fi
# Check if main.tf does not exist in the device folder
if [ ! -f "$device_folder/main.tf" ]; then
# Create main.tf in the device folder
touch "$device_folder/main.tf"
echo "Created main.tf in the device folder: $device_folder"
fi
else
echo "Invalid choice. Please enter 1 or 2."
fi