Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- So lets look at what its going to do.
- install.sh is your script that will execute whatever commands you need. I have most of my program specific commands broken out into their own scripts. So install.sh will execute those scripts. It will move files like httpd.conf to the correct directory, it will copy my SSH public keys to the correct places, so on and so forth
- build.sh will create a temp directory and copy all of your general files into that temp directory. build.sh will then take all your server specific files and copy them to the temp directory, currently its simply overwriting anything thats already there, Im doing that simply because I havent had the time to refine the process.
- build.sh will then create a compressed tarball of that temp directory and CAT it to the end of RUNme.sh
- RUNme.sh is the neat bit. RUNme.sh will extract the binary data of the compressed tarball out of itself, using the token __HERE BE DRAGONS__ to tell it where the binary data starts, passing it to TAR to decompress it, and then executing your install.sh script. You could get rid of install.sh and put all your commands in RUNme.sh but I personally prefer haveing all the pieces stand on their own allowing for maximum re-usablity. Hope this helps someone. (like I said its still rough, and im sure there are things that could be done better but this was thrown together quickly to meet a need and I havent had time to go back through it)
- First we need a directory structure on our local machine. Im using a Mac, but this should work across any *nix distro as well. I use this when deploying CentOS7 servers. I have set ups for File Servers, MySQL servers, Web Servers, Management and an All In One sandbox. We are only going to look at the "all-in-one" model, the process is the same for all the others the specific contents of the self extract are different but Im sure you can figure it out.
- ================================================================================================================
- Directory Structure (on local machine)
- ================================================================================================================
- +ServerConfigs
- |--build.sh <-- Builds the self extracting Script
- |-+DEPLOY <-- This is where our self extracting deployment files will end up
- \-+Source <-- Where we will keep our scripts and files needed for deployments
- |-+all-in-one <-- Server type
- | |--httpd.conf
- | |--install.sh
- | |--iptables
- | |--sql.sh
- | \--yum.sh
- |
- |-+General <-- Used by all deployments
- | |--motd
- | |--jail.local
- | |-+Keys
- | | \--*YOUR PUBLIC KEYS FOR SSH*
- | |
- | |--logs.sh
- | |--ssh.sh
- | |-+tools
- | | \--*JUST SOME SCRIPTS FOR MAKING LIFE EASIER
- | |
- | \--users.sh
- |
- \--RUNme.sh <-- Magic happens here
- ================================================================================================================
- build.sh
- ================================================================================================================
- #!/bin/bash
- #### BUILD SELF EXTRACTING INSTALLER PACKAGES
- clear
- function usage {
- echo "USAGE: build.sh [type]"
- echo ""
- echo "type What type of server will this config be used to deploy."
- echo " Type directly correlates to the directory that the type specific files reside in."
- echo " Currently the options are:"
- echo " all-in-one"
- exit 1
- }
- if [[ $# < 1 ]]; then
- usage
- fi
- if [[ $1 != "all-in-one ]]; then
- echo "Server type not available"
- fi
- echo "Consolidating Configureation Resources"
- export TMPDIR=`mktemp -d payload.XXXXXXXX`
- mkdir $TMPDIR/tools
- #cp Source/RUNme.sh payload/
- cp -r Source/general/ $TMPDIR/
- cp $TMPDIR/keys/* $TMPDIR/
- cp -f Source/$1/* $TMPDIR/
- tar cf DEPLOY/$TMPDIR.tar $TMPDIR/*
- cd DEPLOY/
- echo "Creating Self Extracting Bash Deployable"
- if [ -e "$TMPDIR.tar" ]; then
- gzip $TMPDIR.tar
- if [ -e "$TMPDIR.tar.gz" ]; then
- DATE=`date +%Y%m%d%H%M%S`
- FILE=$1_$DATE
- cat ../Source/RUNme.sh $TMPDIR.tar.gz > $FILE.bsx
- echo "... DONE!"
- else
- echo "$TMPDIR.tar.gz does not exist"
- exit 1
- fi
- else
- echo "$TMPDIR.tar does not exist"
- exit 1
- fi
- echo "$FILE.bsx has been saved to the DEPLOY directory."
- echo "Temporary Files adn Directories have been removed"
- cd ..
- rm -rf $TMPDIR
- rm -f DEPLOY/$TMPDIR.tar.gz
- exit 0
- ================================================================================================================
- RUNme.sh
- ================================================================================================================
- #!/bin/bash
- #### Extract archive
- ## Create Temp Directory
- export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`
- echo "Temporary directory created: $TMPDIR"
- echo ""
- echo "Extracting Server Config"
- ARCHIVE=`awk '/^__HERE BE DRAGONS__/ {print NR + 1; exit 0; }' $0`
- tail -n+$ARCHIVE $0 | tar xzv -C $TMPDIR
- CDIR=`pwd`
- cd $TMPDIR
- cp -r payload.*/ .
- rm -rf payload.*/
- echo "Configuring file permissions"
- sudo chmod +x *.sh
- sudo chown root:wheel *
- echo "Running installer.sh"
- sudo ./installer.sh
- cd $CDIR
- echo "Cleaning up after ourselves. Removing the temporary directory"
- rm -rf $TMPDIR
- exit 0
- __HERE BE DRAGONS__ #IMPORTANT have a new line after token, otherwise cat will start adding binary immediately after token, and it wont work (PASTE BIN is cutting off the empty line at the end, but it MUST be there)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement