Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to install (or upgrade) and run Count.ly Server on macOS #1329

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Full and "correct" installation on macOS.
  • Loading branch information
GlennAustin committed May 7, 2020
commit 821924c97281e6cd2007a90084c90663f43066d9
48 changes: 27 additions & 21 deletions bin/commands/countly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -229,31 +229,37 @@ countly_upgrade (){
}

countly_mark_version (){
countly_root ;
if [ "$1" == "fs" ] || [ "$1" == "db" ]
then
UPGRADE=$(nodejs "$DIR/../scripts/version_marks.js" write_"$1" "$2");
elif [ "$1" == "help" ]
then
echo "countly mark_version usage:"
echo " countly mark_version fs <version> # upgrades fs version";
echo " countly mark_version db <version> # upgrades db version";
echo " countly mark_version help # this command";
if [[ $EUID -ne 0 ]]; then
sudo countly mark_version "$@"
else
if [ "$1" == "fs" ] || [ "$1" == "db" ]
then
UPGRADE=$(nodejs "$DIR/../scripts/version_marks.js" write_"$1" "$2");
elif [ "$1" == "help" ]
then
echo "countly mark_version usage:"
echo " countly mark_version fs <version> # upgrades fs version";
echo " countly mark_version db <version> # upgrades db version";
echo " countly mark_version help # this command";
fi
fi
}

countly_compare_version (){
countly_root ;
if [ "$1" == "fs" ] || [ "$1" == "db" ]
then
UPGRADE=$(nodejs "$DIR/../scripts/version_marks.js" compare_"$1" "$2");
echo "$UPGRADE";
elif [ "$1" == "help" ]
then
echo "countly compare_version usage:"
echo " countly compare_version fs <version> # compares fs version";
echo " countly compare_version db <version> # compares db version";
echo " countly compare_version help # this command";
if [[ $EUID -ne 0 ]]; then
sudo countly compare_version_version "$@"
else
if [ "$1" == "fs" ] || [ "$1" == "db" ]
then
UPGRADE=$(nodejs "$DIR/../scripts/version_marks.js" compare_"$1" "$2");
echo "$UPGRADE";
elif [ "$1" == "help" ]
then
echo "countly compare_version usage:"
echo " countly compare_version fs <version> # compares fs version";
echo " countly compare_version db <version> # compares db version";
echo " countly compare_version help # this command";
fi
fi
}

Expand Down
4 changes: 2 additions & 2 deletions bin/commands/launchd/com.countly.api.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/countly-current/.nodeenv/bin/shim</string>
<string>/usr/local/countly-current/api/api.js</string>
<string>/usr/local/countly/.nodeenv/bin/shim</string>
<string>/usr/local/countly/api/api.js</string>
</array>
<key>Label</key>
<string>com.countly.api</string>
Expand Down
4 changes: 2 additions & 2 deletions bin/commands/launchd/com.countly.dashboard.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/countly-current/.nodeenv/bin/shim</string>
<string>/usr/local/countly-current/frontend/express/app.js</string>
<string>/usr/local/countly/.nodeenv/bin/shim</string>
<string>/usr/local/countly/frontend/express/app.js</string>
</array>
<key>Label</key>
<string>com.countly.dashboard</string>
Expand Down
75 changes: 40 additions & 35 deletions bin/commands/launchd/install.sh
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COUNTLY_DIR="$( cd "$DIR"/../../.. && pwd )"

# Make sure that brew is installed - we'll need it.
if [ ! -x /usr/local/bin/brew ]
then
if [ $EUID == 0 ]
then
echo "Brew can't be installed as root"
exit 1
fi
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
if [ $EUID == 0 ]
then
# Can't install brew as root, use SUDO_USER
sudo -u $SUDO_USER /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
doBrew () {
sudo -u $SUDO_USER brew "$@"
}
else
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
doBrew() {
brew "$@"
}
fi
else
if [ $EUID == 0 ]
then
doBrew () {
sudo -u $SUDO_USER brew "$@"
}
else
doBrew() {
brew "$@"
}
fi
fi

# Make sure that mongodb is installed
MONGODB=mongodb-community@3.6
if [ -z "$(sudo -u $SUDO_USER brew list | grep "$MONGODB")" ]
if [ -z "$(doBrew list | grep "$MONGODB")" ]
then
# mongodb is now on its own brew tap. make sure we have it tapped.
if [ $EUID == 0 ]
then
echo "Brew can't be run as root"
exit 1
fi
if [ -z "$(brew tap | grep mongodb/brew)" ]
then
brew tap mongodb/brew
fi
brew install "$MONGODB"
# mongodb is now on its own brew tap. make sure we have it tapped.
if [ -z "$(doBrew tap | grep mongodb/brew)" ]
then
brew tap mongodb/brew
fi
doBrew install "$MONGODB"
fi

# Make sure that mongodb is running
MONGO_RUNNING="$(sudo brew services list | grep "$MONGODB")"
if [ -z "$MONGO_RUNNING" -o "$(echo "$MONGO_RUNNING" | sed -E -e 's/^[^[:space:]]+[[:space:]]([[:alpha:]]+)[[:space:]].+$/\1/')" = "stopped" ]
then
sudo brew services start $mongodb
sudo brew services start mongodb/brew/mongodb-community@3.6
fi

# Make sure that nodeenv is installed under brew
if [ -z "$(sudo -u $SUDO_USER brew list | grep nodeenv)" ]
then
if [ $EUID == 0 ]
then
echo "Brew can't be run as root"
exit 1
fi
brew install nodeenv
fi

# Make sure that nodeenv is set up for countly
COUNTLY_DIR="$( cd "$DIR"/../../.. && pwd )"
if [ ! -d "$COUNTLY_DIR/.nodeenv" ]
if [ -z "$(brew list | grep nodeenv)" ]
then
( cd "$COUNTLY_DIR" && nodeenv -n 10.19.0 .nodeenv )
doBrew install nodeenv
fi

# Create a symbolic link to this countly in /usr/local/countly-current
rm -f /usr/local/countly-current
ln -s "$COUNTLY_DIR" /usr/local/countly-current
sudo rm -f /usr/local/countly-current
sudo ln -s "$COUNTLY_DIR" /usr/local/countly-current

if [ -f /Library/LaunchDaemons/com.countly.dashboard.plist ];
then
Expand All @@ -70,6 +72,9 @@ fi

sudo cp $DIR/com.countly.api.plist /Library/LaunchDaemons/
sudo cp $DIR/com.countly.dashboard.plist /Library/LaunchDaemons/
# Point to our installation
sudo sed -E -i '' -e 's,/usr/local/countly/,'"${COUNTLY_DIR}"'/,g' /Library/LaunchDaemons/com.countly.api.plist
sudo sed -E -i '' -e 's,/usr/local/countly/,'"${COUNTLY_DIR}"'/,g' /Library/LaunchDaemons/com.countly.dashboard.plist

sudo launchctl load /Library/LaunchDaemons/com.countly.api.plist
sudo launchctl load /Library/LaunchDaemons/com.countly.dashboard.plist
22 changes: 17 additions & 5 deletions bin/countly.install.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DATE=$(date +%Y-%m-%d:%H:%M:%S)
totalm=$(free -m | awk '/^Mem:/{print $2}')
CAN_MAKE_SWAP=N
if [ ! -z "$(which free)" ]
then
totalm=$(free -m | awk '/^Mem:/{print $2}')
CAN_MAKE_SWAP=Y
elif [ ! -z "$(which sysctl)" -a ! -z "$(which numfmt)" ]
then
totalm=$(sysctl hw.memsize | sed -E -e 's,^[^[:digit:]]*([[:digit:]])[^[:digit:]]*,\1,' | numfmt --to-unit=1048576)
else
totalm=0
fi
if [ "$totalm" -lt "1800" ]; then
echo "Countly requires at least 2Gb of RAM"
if [ "$COUNTLY_OVERWRITE_MEM_REQUIREMENT" != "1" ]; then
exit 1
else
if [ "$COUNTLY_OVERWRITE_MEM_REQUIREMENT" == "1" -a $CAN_MAKE_SWAP = Y ]; then
echo "COUNTLY_OVERWRITE_MEM_REQUIREMENT set, running make.swap.sh"
bash "$DIR/scripts/make.swap.sh" 2>&1 | tee "$DIR/../log/countly-install-$DATE.log"
else
exit 1
fi
fi
if [ -f "$DIR/offline_installer.sh" ]; then
if [ -d "/Library" -a -d "/Library/LaunchDaemons" ]; then
bash "$DIR/countly.install_macos.sh" 2>&1 | tee "$DIR/../log/countly-install-$DATE.log"
elif [ -f "$DIR/offline_installer.sh" ]; then
bash "$DIR/offline_installer.sh" 2>&1 | tee "$DIR/../log/countly-install-$DATE.log"
elif [ -f /etc/lsb-release ]; then
bash "$DIR/countly.install_ubuntu.sh" 2>&1 | tee "$DIR/../log/countly-install-$DATE.log"
Expand Down
132 changes: 132 additions & 0 deletions bin/countly.install_macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash
set -e

if [ $EUID = 0 -a ! -z "$SUDO_USER" ]; then
# OK, we have to go back to non-superuser because brew installations don't work as root.
sudo -u $SUDO_USER bash "$0" "$@"
elif [ $EUID != 0 -a "$SUDO_USER" != "root" ]; then
echo "Please execute Countly installation script with a superuser..." 1>&2
exit 1
else
INSTALL_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

bash "$INSTALL_DIR/scripts/logo.sh";

#install brew
#Xcode or the Xcode command-line tools were installed when we installed brew.
if [ ! -x /usr/local/bin/brew ]
then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
fi

#install and start logrotate
brew install logrotate
sudo brew services start logrotate

#install wget (since Count.ly uses that instead of curl)
brew install wget

#install nginx
brew install nginx
if [ -z "$(sudo -u www-data echo 'exists' 2>/dev/null)" ]; then
sudo sysadminctl -addUser www-data -fullName 'WWW-data'
sudo dscl . -create /Users/www-data IsHidden 1
#sudo dscl . -delete "/SharePoints/WWW-data's Public Folder"
fi

#install nodeenv and nodejs
brew install nodeenv
LATESTVERSION="$(nodeenv -l 2>&1 | tr -s "\t" "\n" | egrep '^10\..+' | tail -1)"
sudo rm -rf "$INSTALL_DIR/../.nodeenv"
nodeenv -n "$LATESTVERSION" "$INSTALL_DIR/../.nodeenv"
source "$INSTALL_DIR/../.nodeenv/bin/activate"

set +e
NODE_JS_CMD=$(which nodejs)
set -e

#install grunt & npm modules
( cd "$INSTALL_DIR/.." ; npm install npm@6.4.1 -g; npm --version; npm install -g grunt-cli --unsafe-perm ; npm install --unsafe-perm )

( cd "$INSTALL_DIR/.." && npm install argon2 --build-from-source )

#install mongodb
set -x
bash -x "$INSTALL_DIR/scripts/mongodb.install.sh" /usr/local/etc/mongod.conf

cp "$INSTALL_DIR/../frontend/express/public/javascripts/countly/countly.config.sample.js" "$INSTALL_DIR/../frontend/express/public/javascripts/countly/countly.config.js"

bash -x "$INSTALL_DIR/scripts/detect.init.sh"

#configure and start nginx
set +e
if [ ! -d /usr/local/etc/nginx/conf.d ]; then
mkdir /usr/local/etc/nginx/conf.d
fi
if [ -f /usr/local/etc/nginx/conf.d/default.conf ]; then
countly save /usr/local/etc/nginx/conf.d/default.conf "$INSTALL_DIR/config/nginx"
else
touch "$INSTALL_DIR/config/nginx/default.conf"
fi
countly save /usr/local/etc/nginx/nginx.conf "$INSTALL_DIR/config/nginx"
cp "$INSTALL_DIR/config/nginx.server.conf" /usr/local/etc/nginx/conf.d/default.conf
cat "$INSTALL_DIR/config/nginx.conf" \
| sed -E -e 's,user www-data;,#user www-data;,' \
| sed -E -e 's,/var/run/nginx.pid,/var/run/nginx/nginx.pid,' \
| sed -E -e 's, /var/, /usr/local/var/,' \
| sed -E -e 's, /etc/, /usr/local/etc/,' \
| sed -E -e 's,^([[:space:]]+)(include /usr/local/etc/nginx/sites-enabled/\*;)$,\1\2~\1include /usr/local/etc/nginx/servers/\*;,' \
| tr '~' '\n' \
>/usr/local/etc/nginx/nginx.conf
sudo brew services restart nginx
#sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
#sudo defaults write /Library/LaunchDaemons/homebrew.mxcl.nginx.plist UserName 'www-data'
#sudo chmod a+r /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
#sudo chown -R www-data /usr/local/var/log/nginx /usr/local/var/run/nginx
#sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
set -e

#create configuration files from samples
if [ ! -f "$INSTALL_DIR/../api/config.js" ]; then
cp "$INSTALL_DIR/../api/config.sample.js" "$INSTALL_DIR/../api/config.js"
fi

if [ ! -f "$INSTALL_DIR/../frontend/express/config.js" ]; then
cp "$INSTALL_DIR/../frontend/express/config.sample.js" "$INSTALL_DIR/../frontend/express/config.js"
fi

if [ ! -f "$INSTALL_DIR/../plugins/plugins.json" ]; then
cp "$INSTALL_DIR/../plugins/plugins.default.json" "$INSTALL_DIR/../plugins/plugins.json"
fi

#install nghttp2
brew install nghttp2
npm install --unsafe-perm node-gyp

#install plugins
node "$INSTALL_DIR/scripts/install_plugins"

#get web sdk
countly update sdk-web

# close google services for China area
if ping -c 1 google.com >> /dev/null 2>&1; then
echo "Pinging Google successful. Enabling Google services."
countly plugin disable EChartMap
else
echo "Cannot reach Google. Disabling Google services. You can enable this from Configurations later."
countly config "frontend.use_google" false
countly plugin enable EChartMap
fi

#compile scripts for production
cd "$INSTALL_DIR/.." && grunt dist-all

# after install call
sudo countly check after install

#finally start countly api and dashboard
countly start

bash "$INSTALL_DIR/scripts/done.sh";
fi
Loading