Added demo ruby project and added readme.md example of using a dir instead of script.

rewrite
hak8or 5 years ago
parent 9dc3988c3c
commit a39c5e18c6
  1. 7
      readme.md
  2. 4
      ruby_server/Gemfile
  3. 7
      ruby_server/config.ru
  4. 94
      ruby_server/ruby_server.sh
  5. 13
      ruby_server/ruby_website.service
  6. 12
      ruby_server/website.rb

@ -13,14 +13,17 @@ Note you must update the ```PROXMOX_IP_ADDR``` variable at the top of the script
deploy.sh # Outputs a container ID when complete.
# Create an Arch Linux based container which then runs a script to init gogs.
deploy.sh gogs.sh # Outputs a container ID when complete.
deploy.sh gogs.sh
# Create a snapshot of a container and then restore to said snapshot.
deploy.sh -ID 101 -snapshot foo # Snapshot called foo was created.
deploy.sh -ID 101 -snapshot foo # Rolling back to snapshot foo.
# Just run a script to init gogs on an already existing container.
deploy.sh -ID 101 gogs.sh # Returns nothing.
deploy.sh -ID 101 gogs.sh
# Copy the contents of a directory and run {directory}/{directory.sh}
deploy.sh -ID 101 ruby_server
# Get a comma seperated IPv4 and IPv6 address of a container.
deploy.sh -ID 101 # Outputs an IPv4 and IPv6 address seperated by a comma.

@ -0,0 +1,4 @@
source 'https://rubygems.org'
gem 'sinatra'
gem 'puma'

@ -0,0 +1,7 @@
require 'rubygems'
require 'bundler'
Bundler.require
require './website'
run Sinatra::Application

@ -0,0 +1,94 @@
#!/usr/bin/env bash
##########################
# Script to install a small ruby based server.
# - Creates a webserver user and group for running ruby in
# - Uses bundler to handle dependancies
# - Puma since webrick doesn't doesn't bind to both IPv4 and IPv6 at the
# same time due to a bug.
# - Creates systemd unit file for puma based ruby server.
# - Starts puma process when complete on port 9463
# - PATH put in .bash_profile so ruby works in non interactive login shell.
#
# To start the webserver.
# systemctl start ruby_website
#
# To stop the webserver.
# systemctl stop ruby_website
#
# To run bundle as website user on project.
# su -l website -c 'cd /var/www && bundle install'
#
# To just run the ruby project manually without systemd
# su -l website -c 'cd /var/www && bundle exec rackup -s puma -p 9463 -o [::]'
#
# You can also just change to the website user and do all your work in there.
# su -l website
##########################
# Header for this script
TITLE="Ruby_Server_Setup"
LOGFILE=/tmp/$TITLE.log
DEPTH=2
if [[ $DEPTH == 0 ]]; then
TAGSTR="-->"
elif [[ $DEPTH == 1 ]]; then
TAGSTR="--->"
elif [[ $DEPTH == 2 ]]; then
TAGSTR="----->"
elif [[ $DEPTH == 3 ]]; then
TAGSTR="------>"
fi
echo "$TAGSTR ====== $TITLE (Logged to $LOGFILE) ======"
# Get Ruby
echo "$TAGSTR Installing Ruby"
yaourt -S ruby --noconfirm > $LOGFILE 2>&1
# Change to website user since that's where all our gems and whatnot will exist.
groupadd website
useradd -m website -g website
# Create the /var/www directory which will hold our project.
mkdir /var/www
# Copy contents of source dir into better dir.
echo "$TAGSTR Copying from old dir into proper dir"
cp -r $PWD/* /var/www
chown -R website:website /var/www
# Copy over the systemd unit file of the website.
cp $PWD/ruby_website.service /lib/systemd/system
# Have Ruby in path by modfying bash sourced script.
BASHSCRIPT="/home/website/.bash_profile"
if [[ -e $BASHSCRIPT ]]; then
# Append ruby path string to bash if such a line wasn't found.
echo "$TAGSTR Appending to end of found bash_profile."
grep -q -F "PATH=\"\$(ruby -e 'print Gem.user_dir')/bin:\$PATH\"" $BASHSCRIPT || echo "PATH=\"\$(ruby -e 'print Gem.user_dir')/bin:\$PATH\"" >> $BASHSCRIPT
grep -q -F "PATH=\$PATH:\$HOME/.gem/bin" $BASHSCRIPT || echo "PATH=\$PATH:\$HOME/.gem/bin" >> $BASHSCRIPT
grep -q -F "export GEM_HOME=\$HOME/.gem" $BASHSCRIPT || echo "export GEM_HOME=\$HOME/.gem" >> $BASHSCRIPT
else
# File doesn't exist, so create it and add the ruby path.
echo "$TAGSTR Creating new bash_profile."
echo "PATH=\"\$(ruby -e 'print Gem.user_dir')/bin:\$PATH\"" > $BASHSCRIPT
echo "PATH=\$PATH:\$HOME/.gem/bin" >> $BASHSCRIPT
echo "export GEM_HOME=\$HOME/.gem" >> $BASHSCRIPT
fi
# Get bundler gem without documentation.
echo "$TAGSTR Fetching Bundler"
su -l website -c 'gem update --no-rdoc --no-ri' > $LOGFILE 2>&1
su -l website -c 'gem install bundler --no-rdoc --no-ri' > $LOGFILE 2>&1
# Install the website dependancies
echo "$TAGSTR Running bundle install to get all gems."
su -l website -c 'cd /var/www && bundle install' > $LOGFILE 2>&1
# Start up the website.
systemctl enable ruby_website > $LOGFILE 2>&1
systemctl start ruby_website > $LOGFILE 2>&1
# Lastly, say we are done.
echo "$TAGSTR Completed $TITLE"

@ -0,0 +1,13 @@
[Unit]
Description=Example Ruby website
Requires=network.target
[Service]
Type=simple
User=website
Group=website
WorkingDirectory=/var/www
ExecStart=/usr/bin/bash -lc 'bundle exec rackup -s puma -p 9463 -o [::]'
[Install]
WantedBy=multi-user.target

@ -0,0 +1,12 @@
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
get '/' do
'Hello world! :D'
end
get '/:text' do
"Hello there, I see you are accessing #{params['text']}"
end
Loading…
Cancel
Save