-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgenerate-images.sh
executable file
·152 lines (127 loc) · 4.67 KB
/
generate-images.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
#!/bin/bash
# shellcheck disable=SC2053
# Prepare options list by searching through locations/
unset locations hosts
# Extract valid locations
mapfile -t locations < <(yq '.location' locations/*.yml | sed 's/\"//g' | sort)
# Extract valid hosts
mapfile -t hosts < <(yq '.hosts[] | select(.role == "corerouter" or .role == "gateway" or .role == "ap") | .hostname' locations/*.yml | sed 's/\"//g' | sort)
# Function to run Ansible playbook with a list of hosts and locations
generate_images() {
local limit_param=$1
echo "Generating images with limit: $limit_param"
ansible-playbook play.yml --limit "$limit_param" --tags image && echo "Location of generated images: ./tmp/images"
}
# Function to process wildcards
process_wildcard() {
local pattern=$1
local -n result_ref=$2
# Match locations
for location in "${locations[@]}"; do
if [[ "$location" == $pattern ]]; then
result_ref+=( "location_${location//-/_}" )
fi
done
# Match hosts
for host in "${hosts[@]}"; do
if [[ "$host" == $pattern ]]; then
result_ref+=( "$host" )
fi
done
}
# Check if an argument is passed
if [[ $# -gt 0 ]]; then
# Parse the argument as a comma-separated list of locations and hosts
IFS="," read -ra input_args <<< "$1"
# Validate the input against valid locations and hosts
valid_entries=()
for entry in "${input_args[@]}"; do
# Process wildcard entries (if any)
if [[ $entry == *"*"* ]]; then
# Get expanded entries from wildcard processing
expanded_entries=()
process_wildcard "$entry" expanded_entries
# Merge expanded entries with valid_entries
valid_entries+=( "${expanded_entries[@]}" )
# If it's a valid location
elif [[ " ${locations[*]} " == *" $entry "* ]]; then
valid_entries+=( "location_${entry//-/_}" )
# If it's a valid host
elif [[ " ${hosts[*]} " == *" $entry "* ]]; then
valid_entries+=( "$entry" )
else
echo "Warning: Invalid entry '$entry' ignored."
fi
done
if [[ ${#valid_entries[@]} -gt 0 ]]; then
# Combine valid entries into a comma separated list and run the playbook
IFS="," limit_param="${valid_entries[*]}"
generate_images "$limit_param"
else
echo "No valid locations or hosts provided. Exiting."
fi
exit
fi
# Show menu
echo "Usage:
This helper script allows you to build multiple locations and hosts by passing them as a
parameter and even allows using wildcards when quoted e.g.
./generate-images.sh location1,host1,host2,location2,\"host-*\",\"location*\"
or perform bbb-config related tasks via an easy menu. Either select a location by
typing the corresponding number or one of the following actions by just typing them.
Actions:
abort
return to the command line
all
generate images for all nodes and locations and return to the command line
clean
delete all temporary files generated by bbb-configs and wait for additional
commands
lint
check all configuration files by calling yamllint and ansible-lint and
return to the command line
requirements
install the requirements and wait for additional commands
" >&2
echo "The following locations were found:
" >&2
PS3=$'\nUse a location number to generate images for that location or type an action: '
# Allow the user to choose a location or action
unset location
select location in "${locations[@]}"
do
# Abort if selected
if [[ "$REPLY" == abort ]]; then break; fi
# Generate all images if selected
if [[ "$REPLY" == all ]]; then
ansible-playbook play.yml --tags image && echo "Location of generated images: ./tmp/images"
break
fi
# Delete old directories and get rid of artifacts
if [[ "$REPLY" == clean ]]; then
[ -d "./tmp/" ] && rm -r ./tmp/
echo "tmp directory was deleted..."
continue
fi
# Check all configuration files with ansible-lint
if [[ "$REPLY" == lint ]]; then
yamllint -d .github/linters/.yaml-lint.yml .
ansible-lint -c .github/linters/.ansible-lint.yml
break
fi
# Install or update requirements
if [[ "$REPLY" == requirements ]]; then
pip3 install -r requirements.txt
continue
fi
# Complain if no location was selected, and loop to ask again
if [[ "$location" == "" ]]; then
echo "'$REPLY' is not a valid selection"
continue
fi
# Generate images for a specific location
echo "Firmwares for the following location will be generated: $location"
generate_images "location_${location//-/_}"
# Break the loop
break
done