SHARE
TWEET

imongodb.sh

h8rt3rmin8r May 25th, 2020 (edited) 1,534 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env bash
  2. #>------------------------------------------------------------------------------
  3. #>
  4. #> [ imongodb ]
  5. #>
  6. #>    This unofficial script will install MongoDB on Ubuntu
  7. #>
  8. #>    NOTE: This script is intended for Ubuntu versions 18.04 or later
  9. #>
  10. #>    Created on 20200525 by h8rt3rmin8r - the source code of this script may
  11. #>    be found at the following location: https://pastebin.com/MBQNgvUc
  12. #>
  13. #> USAGE:
  14. #>
  15. #>    imongodb <OPTION>
  16. #>
  17. #>    where "OPTION" is one of the following:
  18. #>
  19. #>                   |
  20. #>    -c, --check    | Check if MongoDB is currently installed on the local
  21. #>                   | system; outputs are either "TRUE" or "FALSE"
  22. #>                   |
  23. #>    -h, --help     | Print this help text to the terminal
  24. #>                   |
  25. #>    -i, --install  | Install MongoDB; this operation will fail if MongoDB is
  26. #>                   | already installed
  27. #>                   |
  28. #>
  29. #> REFERENCE:
  30. #>
  31. #>    MongoDB installation instructions (OFFICIAL)
  32. #>    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
  33. #>
  34. #>    How to install and Use MongoDB on Ubutnu 20.04 (howtoforge.com)
  35. #>    https://www.howtoforge.com/how-to-install-and-use-mongodb-on-ubuntu-2004/
  36. #>
  37. #>    UNIX ulimit Settings
  38. #>    https://docs.mongodb.com/manual/reference/ulimit/
  39. #>
  40. #>------------------------------------------------------------------------------
  41.  
  42. # declare sub-functions
  43.  
  44. function imongodb_addkey() {
  45.     ## add the repository key for MongoDB
  46.     wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc \
  47.         | sudo apt-key add - 2>/dev/null
  48.     return $?
  49. }
  50.  
  51. function imongodb_check() {
  52.     ## check for the presence of MongoDB on the local system
  53.     imongodb_vbs "Checking the local system for MongoDB"
  54.    
  55.     local check_test=$(which mongo &>/dev/null; echo $?)
  56.    
  57.     if [[ "${check_test}" -eq 0 ]]; then
  58.         imongodb_vbs "Outcome: TRUE"
  59.         return 0
  60.     else
  61.         imongodb_vbs "Outcome: FALSE"
  62.         return 1
  63.     fi
  64. }
  65.  
  66. function imongodb_depends() {
  67.     ## check for the presence of required software packages
  68.     declare -a depends=( "gpg:gnupg" "wget" )
  69.     local e_c="0"
  70.     for i in "${depends[@]}"; do
  71.         if [[ "${i}" =~ ':' ]]; then
  72.             local cycle_target="${i//[:]*}"
  73.             local cycle_name="${i//*[:]}"
  74.         else
  75.             local cycle_target="${i}"
  76.             local cycle_name="${i}"
  77.         fi
  78.         local cycle_check=$(which "${cycle_target}" &>/dev/null; echo $?)
  79.         if [[ "${cycle_check}" -ne 0 ]]; then
  80.             local e_c="1"
  81.             imongodb_vbs "ERROR: Missing required package: ${cycle_name}"
  82.             imongodb_vbs "Install with: sudo apt-get install ${cycle_name}"
  83.         fi
  84.     done
  85.     return "${e_c}"
  86. }
  87.  
  88. function imongodb_help() {
  89.     ## general help text function
  90.     cat "${0}" \
  91.         | grep -E '^[#][>]' \
  92.         | sed 's/^..//'
  93.     return $?
  94. }
  95.  
  96. function imongodb_list() {
  97.     ## write the sources list file for MongoDB
  98.     sudo touch "${list_file}" 2>/dev/null
  99.    
  100.     echo "${src_x}" \
  101.         | sudo tee "${list_file}" 2>/dev/null
  102.    
  103.     return $?
  104. }
  105.  
  106. function imongodb_sudo() {
  107.     ## attempt to gain access to 'sudo'
  108.     sudo echo -n &> /dev/null
  109.     local e_c="$?"
  110.    
  111.     if [[ "${e_c}" -eq 0 ]]; then
  112.         imongodb_vbs "Outcome: TRUE"
  113.         return 0
  114.     else
  115.         imongodb_vbs "Outcome: FALSE"
  116.         return 1
  117.     fi
  118. }
  119.  
  120. function imongodb_sysinstall() {
  121.     ## run the MongoDB package installation via apt
  122.     sudo apt-get install mongodb-org -y
  123.     return $?
  124. }
  125.  
  126. function imongodb_sysstart() {
  127.     ## start and enable the MongoDB service
  128.     sudo systemctl start mongod
  129.     local e_c_a="$?"
  130.    
  131.     if [[ "${e_c_a}" -ne 0 ]]; then
  132.         imongodb_vbs "ERROR: Encountered an issue while starting the MongoDB background service"
  133.         imongodb_vbs "Attempting alternative init process for the background service"
  134.        
  135.         sudo systemctl daemon-reload
  136.         sudo systemctl start mongod
  137.        
  138.         e_c_a="$?"
  139.     fi
  140.    
  141.     if [[ "${e_c_a}" -ne 0 ]]; then
  142.         return 1
  143.     else
  144.         sudo systemctl enable mongod
  145.         local e_c_b="$?"
  146.     fi
  147.    
  148.     if [[ "${e_c_a}${e_c_b}" =~ 1 ]]; then
  149.         return 1
  150.     else
  151.         return 0
  152.     fi
  153. }
  154.  
  155. function imongodb_sysupdate() {
  156.     ## perform a full system package list update
  157.     sudo apt-get update -y
  158.     return $?
  159. }
  160.  
  161. function imongodb_vbs() {
  162.     ## verbosity printing function
  163.     local d_t="$(date '+%s.%N')"
  164.     local i_n="$@"
  165.     local output="${d_t}|${r_t}|${s_h}|${i_n}"
  166.     echo "${output}" &>/dev/stderr
  167.     return $?
  168. }
  169.  
  170. # declare variables
  171.  
  172. r_t="$(date '+%s.%N')"
  173. s_h="imongodb"
  174. src_a='deb [ arch=amd64,arm64 ]'
  175. src_b='https://repo.mongodb.org/apt/ubuntu'
  176. src_c='bionic/mongodb-org/4.2'
  177. src_d='multiverse'
  178. src_x="${src_a} ${src_b} ${src_c} ${src_d}"
  179. list_file="/etc/apt/sources.list.d/mongodb-org-4.2.list"
  180.  
  181. # parse user inputs and execute operations
  182.  
  183. case "${1//[-]}" in
  184.     c|C|check)
  185.         imongodb_check
  186.         e_c="$?"
  187.         if [[ "${e_c}" -eq 0 ]]; then
  188.             imongodb_vbs "MongoDB was FOUND on the local system"
  189.         else
  190.             imongodb_vbs "MongoDB was NOT FOUND on the local system"
  191.         fi
  192.         exit ${e_c}
  193.         ;;
  194.     h|H|help)
  195.         imongodb_help
  196.         exit $?
  197.         ;;
  198.     i|I|install)
  199.         imongodb_check
  200.         e_c="$?"
  201.        
  202.         if [[ "${e_c}" -eq 0 ]]; then
  203.             imongodb_vbs "MongoDB is already installed on the local system"
  204.             imongodb_vbs "Aborting all installation operations"
  205.             imongodb_vbs "Use '--help' for more information"
  206.             exit 1
  207.         else
  208.             imongodb_vbs "MongoDB was NOT found on the local system"
  209.             imongodb_vbs "Continuing the installation process ..."
  210.             sleep 0.2
  211.         fi
  212.        
  213.         imongodb_vbs "Confirming access to 'sudo'"
  214.         imongodb_sudo
  215.         e_c="$?"
  216.        
  217.         if [[ "${e_c}" -ne 0 ]]; then
  218.             imongodb_vbs "ERROR: Access to 'sudo' could not be established"
  219.             imongodb_vbs "Terminating all installation operations"
  220.             imongodb_vbs "Use '--help' for more information"
  221.             exit 1
  222.         else
  223.             imongodb_vbs "Access to 'sudo' was successful"
  224.             imongodb_vbs "Continuing the installation process ..."
  225.             sleep 0.2
  226.         fi
  227.        
  228.         imongodb_vbs "Checking for required software packages"
  229.         imongodb_depends
  230.         e_c="$?"
  231.        
  232.         if [[ "${e_c}" -ne 0 ]]; then
  233.             imongodb_vbs "Install the missing packages and try again"
  234.             exit 1
  235.         else
  236.             imongodb_vbs "All required software was found"
  237.             imongodb_vbs "Continuing the installation process ..."
  238.             sleep 0.2
  239.         fi
  240.        
  241.         imongodb_vbs "Installing the MongoDB package repository key"
  242.         imongodb_addkey
  243.         e_c="$?"
  244.        
  245.         if [[ "${e_c}" -ne 0 ]]; then
  246.             imongodb_vbs "ERROR: Encountered an unknown error while adding the MongoDB repository key"
  247.             imongodb_vbs "Aborting all installation operations"
  248.             exit 1
  249.         else
  250.             imongodb_vbs "Package repository key was successfully added to the system"
  251.             imongodb_vbs "Continuing the installation process ..."
  252.             sleep 0.2
  253.         fi
  254.        
  255.         imongodb_vbs "Writing the sources list file (${list_file})"
  256.         imongodb_list
  257.         e_c="$?"
  258.        
  259.         if [[ "${e_c}" -ne 0 ]]; then
  260.             imongodb_vbs "ERROR: Encountered an unknown error while writing the sources list"
  261.             imongodb_vbs "Aborting all installation operations"
  262.             exit 1
  263.         else
  264.             imongodb_vbs "Sources list constructed successfully"
  265.             imongodb_vbs "Continuing the installation process ..."
  266.             sleep 0.2
  267.         fi
  268.        
  269.         imongodb_vbs "Updating package lists and installing the MongoDB package"
  270.         sleep 0.5
  271.         imongodb_sysupdate
  272.         e_c_a="$?"
  273.         imongodb_sysinstall
  274.         e_c_b="$?"
  275.        
  276.         e_c_x="${e_c_a}${e_c_b}"
  277.         e_c_x="${e_c_x//0}"
  278.        
  279.         if [[ ! "x${e_c_x}" == "x" ]]; then
  280.             imongodb_vbs "ERROR: Encountered an unknown error while updating the system packages"
  281.             imongodb_vbs "Aborting all installation operations"
  282.             exit 1
  283.         else
  284.             imongodb_vbs "Updates and installations completed successfully"
  285.             imongodb_vbs "Continuing the installation process ..."
  286.             sleep 0.2
  287.         fi
  288.  
  289.         imongodb_vbs "Enabling and activating the 'mongod' background service"
  290.         imongodb_sysstart
  291.         e_c="$?"
  292.        
  293.         if [[ "${e_c}" -ne 0 ]]; then
  294.             imongodb_vbs "ERROR: Encountered an unknown error while activating MongoDB with 'systemctl'"
  295.             imongodb_vbs "Aborting final MongoDB activation process"
  296.             exit 1
  297.         else
  298.             imongodb_vbs "MongoDB service activation completed successfully"
  299.             imongodb_vbs "MongoDB is now fully installed on the local system"
  300.             imongodb_vbs "Begin using MongoDB with the command: 'mongo'"
  301.             exit 0
  302.         fi
  303.         ;;
  304. esac
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top