Advertisement
opexxx

pow.sh

Mar 7th, 2014
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.91 KB | None | 0 0
  1. #!/bin/sh
  2. #                       W
  3. #                      R RW        W.
  4. #                    RW::::RW    DR::R
  5. #         :RRRRRWWWWRt:::::::RRR::::::E        jR
  6. #          R.::::::::::::::::::::::::::Ri  jiR:::R
  7. #           R:::::::.RERRRRWWRERR,::::::Efi:::::::R             GjRRR Rj
  8. #            R::::::.R             R:::::::::::::;G    RRj    WWR    RjRRRRj
  9. #            Rt::::WR      RRWR     R::::::::::::::::fWR::R;  WRW    RW    R
  10. #        WWWWRR:::EWR     E::W     WRRW:::EWRRR::::::::: RRED WR    RRW   RR
  11. #        'R:::::::RRR            RR     DWW   R::::::::RW   LRRR    WR    R
  12. #          RL:::::WRR       GRWRR        RR   R::WRiGRWW    RRR    RRR   R
  13. #            Ri:::WWD    RWRRRWW   WWR   LR   R W    RR    RRRR    RR    R
  14. #   RRRWWWWRE;,:::WW     R:::RW   RR:W   RR   ERE    RR    RRR    RRR    R
  15. #    RR:::::::::::RR    tR:::WR   Wf:R   RW    R     R     RRR    RR    R
  16. #      WR::::::::tRR    WR::RW   ER.R   RRR       R       RRRR    RR    R
  17. #         WE:::::RR     R:::RR   :RW   E RR      RW;     GRRR    RR    R
  18. #         R.::::,WR     R:::GRW       E::RR     WiWW     RRWR   LRRWWRR
  19. #       WR::::::RRRRWRG::::::RREWDWRj::::RW  ,WR::WR    iRWWWWWRWW    R
  20. #     LR:::::::::::::::::::::::::::::::::EWRR::::::RRRDi:::W    RR   R
  21. #    R:::::::::::::::::::::::::::::::::::::::::::::::::::tRW   RRRWWWW
  22. #  RRRRRRRRRRR::::::::::::::::::::::::::::::::::::,:::DE RRWRWW,
  23. #            R::::::::::::: RW::::::::R::::::::::RRWRRR
  24. #            R::::::::::WR.  ;R::::;R  RWi:::::ER
  25. #            R::::::.RR       Ri:iR       RR:,R
  26. #            E::: RE           RW           Y
  27. #            ERRR
  28. #            G       Zero-configuration Rack server for Mac OS X
  29. #                    http://pow.cx/
  30. #
  31. #     This is the installation script for Pow.
  32. #     See the full annotated source: http://pow.cx/docs/
  33. #
  34. #     Install Pow by running this command:
  35. #     curl get.pow.cx | sh
  36. #
  37. #     Uninstall Pow: :'(
  38. #     curl get.pow.cx/uninstall.sh | sh
  39.  
  40.  
  41. # Set up the environment. Respect $VERSION if it's set.
  42.  
  43.       set -e
  44.       POW_ROOT="$HOME/Library/Application Support/Pow"
  45.       NODE_BIN="$POW_ROOT/Current/bin/node"
  46.       POW_BIN="$POW_ROOT/Current/bin/pow"
  47.       [[ -z "$VERSION" ]] && VERSION=0.4.1
  48.  
  49.  
  50. # Fail fast if we're not on OS X >= 10.6.0.
  51.  
  52.       if [ "$(uname -s)" != "Darwin" ]; then
  53.         echo "Sorry, Pow requires Mac OS X to run." >&2
  54.         exit 1
  55.       elif [ "$(expr "$(sw_vers -productVersion | cut -f 2 -d .)" \>= 6)" = 0 ]; then
  56.        echo "Pow requires Mac OS X 10.6 or later." >&2
  57.        exit 1
  58.      fi
  59.  
  60.      echo "*** Installing Pow $VERSION..."
  61.  
  62.  
  63. # Create the Pow directory structure if it doesn't already exist.
  64.  
  65.      mkdir -p "$POW_ROOT/Hosts" "$POW_ROOT/Versions"
  66.  
  67.  
  68. # If the requested version of Pow is already installed, remove it first.
  69.  
  70.      cd "$POW_ROOT/Versions"
  71.      rm -rf "$POW_ROOT/Versions/$VERSION"
  72.  
  73.  
  74. # Download the requested version of Pow and unpack it.
  75.  
  76.      curl -s http://get.pow.cx/versions/$VERSION.tar.gz | tar xzf -
  77.  
  78.  
  79. # Update the Current symlink to point to the new version.
  80.  
  81.      cd "$POW_ROOT"
  82.      rm -f Current
  83.      ln -s Versions/$VERSION Current
  84.  
  85.  
  86. # Create the ~/.pow symlink if it doesn't exist.
  87.  
  88.      cd "$HOME"
  89.      [[ -a .pow ]] || ln -s "$POW_ROOT/Hosts" .pow
  90.  
  91.  
  92. # Install local configuration files.
  93.  
  94.      echo "*** Installing local configuration files..."
  95.      "$NODE_BIN" "$POW_BIN" --install-local
  96.  
  97.  
  98. # Check to see whether we need root privileges.
  99.  
  100.      "$NODE_BIN" "$POW_BIN" --install-system --dry-run >/dev/null && NEEDS_ROOT=0 || NEEDS_ROOT=1
  101.  
  102.  
  103. # Install system configuration files, if necessary. (Avoid sudo otherwise.)
  104.  
  105.      if [ $NEEDS_ROOT -eq 1 ]; then
  106.        echo "*** Installing system configuration files as root..."
  107.        sudo "$NODE_BIN" "$POW_BIN" --install-system
  108.        sudo launchctl load -Fw /Library/LaunchDaemons/cx.pow.firewall.plist 2>/dev/null
  109.      fi
  110.  
  111.  
  112. # Start (or restart) Pow.
  113.  
  114.      echo "*** Starting the Pow server..."
  115.      launchctl unload "$HOME/Library/LaunchAgents/cx.pow.powd.plist" 2>/dev/null || true
  116.      launchctl load -Fw "$HOME/Library/LaunchAgents/cx.pow.powd.plist" 2>/dev/null
  117.  
  118.  
  119. # Show a message about where to go for help.
  120.  
  121.      function print_troubleshooting_instructions() {
  122.        echo
  123.        echo "For troubleshooting instructions, please see the Pow wiki:"
  124.        echo "https://github.com/37signals/pow/wiki/Troubleshooting"
  125.        echo
  126.        echo "To uninstall Pow, \`curl get.pow.cx/uninstall.sh | sh\`"
  127.      }
  128.  
  129.  
  130. # Check to see if the server is running properly.
  131.  
  132.      # If this version of Pow supports the --print-config option,
  133.      # source the configuration and use it to run a self-test.
  134.      CONFIG=$("$NODE_BIN" "$POW_BIN" --print-config 2>/dev/null || true)
  135.  
  136.      if [[ -n "$CONFIG" ]]; then
  137.        eval "$CONFIG"
  138.        echo "*** Performing self-test..."
  139.  
  140.        # Check to see if the server is running at all.
  141.        function check_status() {
  142.          sleep 1
  143.          curl -sH host:pow "localhost:$POW_HTTP_PORT/status.json" | grep -c "$VERSION" >/dev/null
  144.        }
  145.  
  146.        # Attempt to connect to Pow via each configured domain. If a
  147.        # domain is inaccessible, try to force a reload of OS X's
  148.        # network configuration.
  149.        function check_domains() {
  150.          for domain in ${POW_DOMAINS//,/$IFS}; do
  151.            echo | nc "${domain}." "$POW_DST_PORT" 2>/dev/null || return 1
  152.          done
  153.        }
  154.  
  155.        # Use networksetup(8) to create a temporary network location,
  156.        # switch to it, switch back to the original location, then
  157.        # delete the temporary location. This forces reloading of the
  158.        # system network configuration.
  159.        function reload_network_configuration() {
  160.          echo "*** Reloading system network configuration..."
  161.          local location=$(networksetup -getcurrentlocation)
  162.          networksetup -createlocation "pow$$" >/dev/null 2>&1
  163.          networksetup -switchtolocation "pow$$" >/dev/null 2>&1
  164.          networksetup -switchtolocation "$location" >/dev/null 2>&1
  165.          networksetup -deletelocation "pow$$" >/dev/null 2>&1
  166.        }
  167.  
  168.        # Try twice to connect to Pow. Bail if it doesn't work.
  169.        check_status || check_status || {
  170.          echo "!!! Couldn't find a running Pow server on port $POW_HTTP_PORT"
  171.          print_troubleshooting_instructions
  172.          exit 1
  173.        }
  174.  
  175.        # Try resolving and connecting to each configured domain. If
  176.        # it doesn't work, reload the network configuration and try
  177.         # again. Bail if it fails the second time.
  178.         check_domains || {
  179.           { reload_network_configuration && check_domains; } || {
  180.             echo "!!! Couldn't resolve configured domains ($POW_DOMAINS)"
  181.             print_troubleshooting_instructions
  182.             exit 1
  183.           }
  184.         }
  185.       fi
  186.  
  187.  
  188. # All done!
  189.  
  190.       echo "*** Installed"
  191.       print_troubleshooting_instructions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement