forked from Normation/rudder-tools
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Inventory generation | ||
|
||
This folder contains script to generate data to be used to generate inventories: uuid, hostname, ip, mac, certificates, OS name, agent version | ||
q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
#!/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 | ||
|
||
# detect if any option has been passed to the script | ||
ANY_OPTION_DEFINED=false | ||
|
||
|
||
# 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 | ||
|
||
} | ||
|
||
|
||
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 | ||
|
||
echo "RUDDER_TEST_${i}" > ${DEST_PATH}/${UUID}/hostname | ||
|
||
|
||
IP4=$((IP4+1)) | ||
validate_ip | ||
|
||
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 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:g:wt: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 | ||
|