Advertisement
technomancing_monkey

Self Extracting Bash Scripts

Jun 20th, 2016
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. So lets look at what its going to do.
  2.  
  3. 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
  4.  
  5. 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.
  6. build.sh will then create a compressed tarball of that temp directory and CAT it to the end of RUNme.sh
  7.  
  8. 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)
  9.  
  10. 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.
  11. ================================================================================================================
  12. Directory Structure (on local machine)
  13. ================================================================================================================
  14. +ServerConfigs
  15. |--build.sh <-- Builds the self extracting Script
  16. |-+DEPLOY <-- This is where our self extracting deployment files will end up
  17. \-+Source <-- Where we will keep our scripts and files needed for deployments
  18. |-+all-in-one <-- Server type
  19. | |--httpd.conf
  20. | |--install.sh
  21. | |--iptables
  22. | |--sql.sh
  23. | \--yum.sh
  24. |
  25. |-+General <-- Used by all deployments
  26. | |--motd
  27. | |--jail.local
  28. | |-+Keys
  29. | | \--*YOUR PUBLIC KEYS FOR SSH*
  30. | |
  31. | |--logs.sh
  32. | |--ssh.sh
  33. | |-+tools
  34. | | \--*JUST SOME SCRIPTS FOR MAKING LIFE EASIER
  35. | |
  36. | \--users.sh
  37. |
  38. \--RUNme.sh <-- Magic happens here
  39.  
  40. ================================================================================================================
  41. build.sh
  42. ================================================================================================================
  43. #!/bin/bash
  44. #### BUILD SELF EXTRACTING INSTALLER PACKAGES
  45. clear
  46. function usage {
  47. echo "USAGE: build.sh [type]"
  48. echo ""
  49. echo "type What type of server will this config be used to deploy."
  50. echo " Type directly correlates to the directory that the type specific files reside in."
  51. echo " Currently the options are:"
  52. echo " all-in-one"
  53. exit 1
  54. }
  55.  
  56. if [[ $# < 1 ]]; then
  57. usage
  58. fi
  59.  
  60. if [[ $1 != "all-in-one ]]; then
  61. echo "Server type not available"
  62. fi
  63.  
  64. echo "Consolidating Configureation Resources"
  65. export TMPDIR=`mktemp -d payload.XXXXXXXX`
  66. mkdir $TMPDIR/tools
  67. #cp Source/RUNme.sh payload/
  68. cp -r Source/general/ $TMPDIR/
  69. cp $TMPDIR/keys/* $TMPDIR/
  70. cp -f Source/$1/* $TMPDIR/
  71. tar cf DEPLOY/$TMPDIR.tar $TMPDIR/*
  72. cd DEPLOY/
  73. echo "Creating Self Extracting Bash Deployable"
  74. if [ -e "$TMPDIR.tar" ]; then
  75. gzip $TMPDIR.tar
  76.  
  77. if [ -e "$TMPDIR.tar.gz" ]; then
  78. DATE=`date +%Y%m%d%H%M%S`
  79. FILE=$1_$DATE
  80. cat ../Source/RUNme.sh $TMPDIR.tar.gz > $FILE.bsx
  81. echo "... DONE!"
  82. else
  83. echo "$TMPDIR.tar.gz does not exist"
  84. exit 1
  85. fi
  86. else
  87. echo "$TMPDIR.tar does not exist"
  88. exit 1
  89. fi
  90.  
  91. echo "$FILE.bsx has been saved to the DEPLOY directory."
  92. echo "Temporary Files adn Directories have been removed"
  93. cd ..
  94. rm -rf $TMPDIR
  95. rm -f DEPLOY/$TMPDIR.tar.gz
  96. exit 0
  97. ================================================================================================================
  98. RUNme.sh
  99. ================================================================================================================
  100. #!/bin/bash
  101. #### Extract archive
  102. ## Create Temp Directory
  103. export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`
  104. echo "Temporary directory created: $TMPDIR"
  105. echo ""
  106. echo "Extracting Server Config"
  107. ARCHIVE=`awk '/^__HERE BE DRAGONS__/ {print NR + 1; exit 0; }' $0`
  108. tail -n+$ARCHIVE $0 | tar xzv -C $TMPDIR
  109. CDIR=`pwd`
  110. cd $TMPDIR
  111. cp -r payload.*/ .
  112. rm -rf payload.*/
  113. echo "Configuring file permissions"
  114. sudo chmod +x *.sh
  115. sudo chown root:wheel *
  116. echo "Running installer.sh"
  117. sudo ./installer.sh
  118. cd $CDIR
  119. echo "Cleaning up after ourselves. Removing the temporary directory"
  120. rm -rf $TMPDIR
  121. exit 0
  122. __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