forked from Normation/rudder-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory-generation
executable file
·241 lines (181 loc) · 4.89 KB
/
inventory-generation
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
#!/bin/bash
# @description Generate inventories for Rudder
# @man This command allows to generate the data to create inventories
# @man and to use these data to create inventories
# @man +
# @man *Options*:
# @man +
# @man *-d --data*: generate the given number of data for inventories
# @man +
# @man *-g --generate*: generate inventories from the data
# @man +
# @man *-w --wipe*: wipe all the generated data
# @man +
# @man *-t --template*: use the given template for inventory generation
# @man +
# @man *-p --path*: use the given path to for the data generated
set -x
DATA=false
NUMBER=0
GENERATE=false
WIPE=false
TEMPLATE=tml.ocs
DEST_PATH=data
DEST_INVENTORIES=inventories
# detect if any option has been passed to the script
ANY_OPTION_DEFINED=false
#Definining list of inventory (short & long)
DEF_LONG_OS_NAME=("Microsoft Windows Server 2019 Standard" "Debian GNU/Linux 9.1 (stretch)" "Debian GNU/Linux 9.2 (stretch)" "Debian GNU/Linux 8.6 (Jessie)" "CentOS Linux release 7.6.1810 (Core)" "CentOS Linux release 7.4.1810 (Core)" "CentOS Linux release 6.10 (Final)")
DEF_SHORT_OS_NAME=(Windows Debian Debian Debian Centos Centos Centos)
MAX_SOFT_VERSION=100
# Output usage
function usage()
{
echo "todo"
}
# Ensure that all numbers of an IP is smaller than 254
function validate_ip {
if [ ${IP4} -gt 254 ]
then
IP4=1
IP3=$((IP3+1))
fi
if [ ${IP3} -gt 254 ]
then
IP3=1
IP2=$((IP2+1))
fi
if [ ${IP2} -gt 254 ]
then
IP2=1
IP1=$((IP1+1))
fi
}
# Generate data in DEST_PATH, one folder per node, named by its uuid
function generate_data {
mkdir ${DEST_PATH}
IP1=192
IP2=168
IP3=111
IP4=1
for (( i=1; i<=${NUMBER}; i++ ))
do
UUID=$(uuidgen)
mkdir ${DEST_PATH}/${UUID}
openssl genrsa -des3 -out ${DEST_PATH}/${UUID}/localhost.priv -passout "pass:Cfengine passphrase" 4096
openssl rsa -in ${DEST_PATH}/${UUID}/localhost.priv -passin "pass:Cfengine passphrase" -RSAPublicKey_out -out ${DEST_PATH}/${UUID}/localhost.pub
openssl req -new -sha256 -key ${DEST_PATH}/${UUID}/localhost.priv -out ${DEST_PATH}/${UUID}/agent.cert -passin "pass:Cfengine passphrase" -x509 -days 3650 -extensions agent_cert -config openssl-agent.cnf -subj "/UID=${UUID}"
echo "RUDDER_TEST_${i}" > ${DEST_PATH}/${UUID}/hostname
IP4=$((IP4+1))
validate_ip
echo "${UUID}" > ${DEST_PATH}/${UUID}/uuid
echo "${IP1}.${IP2}.${IP3}.${IP4}" > ${DEST_PATH}/${UUID}/ip
echo "d0:ab:d5:e5:90:e8" > ${DEST_PATH}/${UUID}/mac
echo "CentOS" > ${DEST_PATH}/${UUID}/osname
echo "CentOS release 6.6 (Final)" > ${DEST_PATH}/${UUID}/fullosname
echo "5.1.0-beta" > ${DEST_PATH}/${UUID}/agent_version
done
}
function create_inventory {
mkdir ${INVENTORIES}
echo "Creating inventories based on the folders in ${DEST_PATH}"
# compute numer of OSs
os_list_size=${#DEF_LONG_OS_NAME[@]}
echo $os_list_size
# List all data
for d in ${DEST_PATH}/*; do
if [ -d "$d" ]; then
# read variables
UUID=$(<$d/uuid)
IP=$(<$d/ip)
MAC=$(<$d/mac)
OS_NAME=$(<$d/osname)
FULL_OS_NAME=$(<$d/fullosname)
AGENT_VERSION=$(<$d/agent_version)
#Random value
RAND_OS_VAL=$((RANDOM%$os_list_size))
RAND_OS_NAME=${DEF_SHORT_OS_NAME[$RAND_OS_VAL]}
RAND_FULL_OS_NAME=${DEF_LONG_OS_NAME[$RAND_OS_VAL]}
RAND_SOFT_VERSION=$(($RANDOM%$MAX_SOFT_VERSION))
fi
done
}
function wipe {
echo "Wipping folder ${DEST_PATH}"
echo "Type ctrl-c to abort now and return to continue."
read a
rm -rf ${DEST_PATH}
}
# Defines available options
OPTIONS=d:gwt:p:
#LONGOPTS=data:,generate,wipe,template:,DEST_PATH:
LONGOPTS=data:
# Use ! to avoid failing with set -e
# Use $PIPESTATUS to get the right return code even with !
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# Wrong arguments
echo "Wrong parameters for command inventory-generation"
usage
exit 1
fi
eval set -- "$PARSED"
while true; do
case "$1" in
-d|--data)
NUMBER="$2"
DATA=true
ANY_OPTION_DEFINED=true
shift 2
;;
-g |--generate)
GENERATE=true
ANY_OPTION_DEFINED=true
shift
;;
-w |--wipe)
WIPE=true
ANY_OPTION_DEFINED=true
shift
;;
-t |--template)
TEMPLATE="$2"
ANY_OPTION_DEFINED=true
shift
;;
-p |--path)
DEST_PATH="$2"
ANY_OPTION_DEFINED=true
shift
;;
--)
shift
break
;;
*)
echo "Programming error - option not correctly handled"
exit 2
;;
esac
done
# No option given, need to show usage
if [ "${ANY_OPTION_DEFINED}" = "false" ]
then
usage
exit 1
fi
# Wipe existing data ?
if [ "${WIPE}" = "true" ]
then
wipe
fi
# Generate the data
if [ "${DATA}" = "true" ]
then
generate_data
fi
# Generate the data
if [ "${GENERATE}" = "true" ]
then
create_inventory
fi