Lively Web

by robertkrahn
1 deployments · 1 still active · last rev. 2 years ago

Compatible with: Ubuntu 12.04 LTS
						#! /bin/bash
#
# Installs a node.js stack backed by PostgreSQL and fronted by nginx.
# <UDF name="user_username" Label="Main user name" />
# <UDF name="user_password" Label="Main user password" />
# <UDF name="user_ssh_key"  Label="Main user RSA SSH key" />
# <UDF name="postgres_root_password" Label="PostgreSQL root password" />
# <UDF name="nodejs_version" Label="Node.js version" default="0.10" />
#
# If not deploying to Linode, set these environment variables. All required:
#  - USER_USERNAME: the username you want for your main Linux user
#  - USER_PASSWORD: the password for this ^ user
#  - USER_SSH_KEY: the contents of an SSH public key. SSH will be locked out of password auth.
#  - NODEJS_VERSION: the version of node.js to install (e.g. "0.6.14")

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# export USER_USERNAME=lively
# export USER_PASSWORD=lively01
# export USER_SSH_KEY=
# read -d '' USER_SSH_KEY << EOF
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkSv9an9wr+A5fdLjAXjJrIlVaqFYs/kjoJYHBlBjQIvBrZADdJwIBnYLqM7X2DVJ+0XaFKHxhMGEDrtbybEp+ok0mhVAPcObAUyYWsYAeV8W7GX3P3Pa/hNvd3z7ko1/mSDIsd2WiqvSb/jsr9P4TxIx8/3L4y1WoBzYQoj9q3+XdoH/zknCIOr7BQW3O5lxKeL9w/IDoKjyzONZlijACBQXO+uKB9OQoh6F3GoPaygvgviRgOSKIUhBL8V/EXyLXviwi1KNQkiOJUr/7OMWO23raOdoLsJWvHLi/K0aIxZQcYCp5yD73eHl2hxG/hB/qKneEKUm6xe+6FHqw7qXF robert@SilverAir.local
# EOF
# export NODEJS_VERSION=0.10
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

# update
apt-get update
apt-get upgrade -y
apt-get -y install build-essential libssl-dev curl git bzip2 unzip

LOGFILE=~/stackscript.log

USER_HOMEDIR=/home/$USER_USERNAME

# set default shell to bash
sed -i '' -e "s|SHELL=/bin/sh|SHELL=/bin/bash|g" /etc/default/useradd;

# setup user
echo "Creating account username $USER_USERNAME" >> $LOGFILE
mkdir -p $USER_HOMEDIR;
useradd -p `openssl passwd -1 $USER_PASSWORD` -d $USER_HOMEDIR $USER_USERNAME
echo "User $USER_USERNAME created" >> $LOGFILE
usermod $USER_USERNAME -G admin
echo "User $USER_USERNAME added to group admin" >> $LOGFILE
echo "$USER_USERNAME	ALL=(ALL) ALL" >> /etc/sudoers
chmod 400 /etc/sudoers
echo "User $USER_USERNAME added to sudoers" >> $LOGFILE

# install SSH key
mkdir -p $USER_HOMEDIR/.ssh;
echo "$USER_SSH_KEY" > $USER_HOMEDIR/.ssh/authorized_keys
sed -i '' -e "s/\#PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config
sed -i '' -e "s/PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config
chown -R 700 $USER_HOMEDIR/.ssh;
chown 644 $USER_HOMEDIR/.ssh/authorized_keys;


# install git, build tools, OpenSSL
apt-get install -y git-core build-essential autoconf libssl-dev

# nvm & node
pushd $USER_HOMEDIR;
echo "Pushed into $PWD" >> $LOGFILE;
git clone git://github.com/creationix/nvm.git $USER_HOMEDIR/nvm;
touch $USER_HOMEDIR/.bash_profile
echo ". ~/nvm/nvm.sh" >> $USER_HOMEDIR/.bash_profile
echo "nvm use 0.10" >> $USER_HOMEDIR/.bash_profile
echo "Installed nvm"
. nvm/nvm.sh;
nvm install $NODEJS_VERSION;
nvm use $NODEJS_VERSION;
echo "Installed nodejs"
chown -R $USER_USERNAME:$USER_USERNAME nvm;
popd;

# node stuff
pushd $USER_HOMEDIR;
echo "Pushed into $PWD" >> $LOGFILE;
su lively -s /bin/bash -l -c "npm install -g forever";
popd;

# emacs
apt-get install -y python-software-properties python
sudo add-apt-repository ppa:cassou/emacs
sudo apt-get update
sudo apt-get install -y emacs24 emacs24-el emacs24-common-non-dfsg

# install nginx
sudo apt-get install -y software-properties-common;
sudo add-apt-repository -y ppa:nginx/stable;
sudo apt-get update;
sudo apt-get install -y nginx;
service nginx stop;
# docker
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
echo "deb http://get.docker.io/ubuntu docker main" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update
sudo apt-get install -y lxc-docker

# docker as non-root
sudo groupadd docker
sudo gpasswd -a ${$USER_USERNAME} docker
sudo service docker restart

# supervisor
apt-get install -y supervisor

supervisor_conf="/etc/supervisor/conf.d/supervisord.conf"
sudo tee $supervisor_conf <<"EOF"
[program:lively]
command=/home/lively/lively-docker/start.sh
autorestart=true
autostart=true
redirect_stderr=true
stdout_logfile=/home/lively/lively-docker/logs/lively.log
stdout_logfile_maxbytes=15MB
stdout_logfile_backups=10
EOF

service supervisord restart

# restart SSH
service ssh restart

#cleanup
chown -R $USER_USERNAME:$USER_USERNAME $USER_HOMEDIR;

echo "Done!"  >> $LOGFILE