Saltstack script testing using docker

There are literally thousands of ways for a saltstack  script to go awry! One simple way to test these scripts is to create a single salt-master many salt-minions autmated testing rig.

First one needs a build script


 

#!/bin/bash
#
# build the docker images and start the test environment
# angelos@unix.gr
#
echo "Cleaning Up"
docker rm -f salt-master
for minion in 1 2 3
do
 docker rm -f salt-minion${minion}
done
echo "Building Master Image"
echo "Copying my ssh credentials to be used for git"
mkdir -p creds 2> /dev/null
cp $HOME/.ssh/id_rsa creds/
cp $HOME/.ssh/known_hosts creds/
cp $HOME/.gitconfig creds/gitconfig
docker build . 2>&1 | tee master.log
id=`grep 'Successfully built' master.log | cut -d" " -f 3`
if [ "X${id}" == "X" ]
then
 echo "Salt Master build failed"
 exit 1
fi
docker run -d \
 -h salt-master --name salt-master \
 -v $PWD/bath:/srv/salt \
 -v $PWD/pillar:/srv/pillar \
 --memory-swappiness=1 $id
docker tag -f ${id} salt-master
echo "Building Minion image"
docker build -f Dockerfile.minion . 2>&1 | tee minion.log
id=`grep 'Successfully built' minion.log | cut -d" " -f 3`
if [ "X${id}" == "X" ]
then
 echo "Salt Minion build failed"
 exit 1
fi
for minion in 1 2 3
do
 hash=`docker run -d \
 -h salt-minion${minion} --name=salt-minion${minion} \
 --link salt-master \
 --memory-swappiness=1 $id`
 docker tag -f ${id} salt-minion${minion}
done
docker ps

The big trick now is not so much the Dockerfiles that create the relevant docker images as much as each image’s startup script

Here is the salt-master’s docker CMD script


~/saltstack (master *% u=)$ cat master-start.sh 
#!/bin/bash

service rsyslog start
service salt-minion start
service salt-master start
service sshd start
echo "Sleeping a bit: 15 secs"
sleep 15
Echo "Auto-accepting All minion keys"
salt-key -A -y
echo "Going into infinity"
sleep infinity;true

Here is the salt-minions’ docker CMD script


 

~/saltstack (master *% u=)$ cat minion-start.sh 
#!/bin/bash
service rsyslog start
salt-minion --daemon --log-level debug
echo "Going into infinity"
sleep infinity;true

 

The above couple of almost brain dead scripts  will allow you to create an automated test platform for saltstack scripts.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: