Skip to content

Instantly share code, notes, and snippets.

@jufemaiz
Last active January 24, 2017 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jufemaiz/eba1ee62c3709d2db90cc16eb2c5c10b to your computer and use it in GitHub Desktop.
Save jufemaiz/eba1ee62c3709d2db90cc16eb2c5c10b to your computer and use it in GitHub Desktop.
Rails + GIT Gems

In order to deal with GIT based gems, you'll need to make add some additional .ebextensions/*.config files.

  1. Support GIT in your EC2 instance;
  2. Backup the existing bundle command
  3. Bootstrap your rails project to add in your vendor directory to the shared environment
  4. Replace your bundle_install with a better bundle_install_deployment approach

Please note that these are placed in your Rails applications .ebextensions folder.

files:
# Make the appropriate directories
"/opt/elasticbeanstalk/hooks/appdeploy/pre/020_bootstrap.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
# /opt/elasticbeanstalk/hooks/appdeploy/pre/01a_bootstrap.sh
. $(/opt/elasticbeanstalk/bin/get-config container -k support_dir)/envvars
# LOCAL VARIABLES
export EB_CONFIG_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
export EB_CONFIG_APP_BASE=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)/../
export EB_CONFIG_APP_ONDECK=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
export EB_CONFIG_APP_SUPPORT=$(/opt/elasticbeanstalk/bin/get-config container -k app_asset_dir)/../
# Create directories in the application ondeck directory
mkdir -p $EB_CONFIG_APP_ONDECK/vendor $EB_CONFIG_APP_ONDECK/public $EB_CONFIG_APP_ONDECK/tmp
# Create directories in the support directory
mkdir -p $EB_CONFIG_APP_SUPPORT/vendor_bundle $EB_CONFIG_APP_SUPPORT/assets
# Remove the log directory to use support directory
rm -rf $EB_CONFIG_APP_ONDECK/log
# Set the vendor directory properly
chmod go-w $EB_CONFIG_APP_ONDECK/vendor
# Set the tmp directory properly
chmod 777 $EB_CONFIG_APP_ONDECK/tmp
# Simlink those directories
ln -s $EB_CONFIG_APP_SUPPORT/vendor_bundle $EB_CONFIG_APP_ONDECK/vendor/bundle
ln -sf $EB_CONFIG_APP_SUPPORT/assets $EB_CONFIG_APP_ONDECK/public
ln -sf $EB_CONFIG_APP_SUPPORT/pids $EB_CONFIG_APP_ONDECK/tmp/pids
ln -sf $EB_CONFIG_APP_SUPPORT/logs $EB_CONFIG_APP_ONDECK/log
# Add log files
touch $EB_CONFIG_APP_ONDECK/log/$RACK_ENV.log
touch $EB_CONFIG_APP_ONDECK/log/cron_log.log
touch $EB_CONFIG_APP_ONDECK/log/cron_error.log
# Change ownership of the Application Base Directory
chown -h $EB_CONFIG_APP_USER:$EB_CONFIG_APP_USER -R $EB_CONFIG_APP_BASE
true
encoding: plain
# Bundle install using deployment
"/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install_deployment.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. $(/opt/elasticbeanstalk/bin/get-config container -k support_dir)/envvars
. $(/opt/elasticbeanstalk/bin/get-config container -k script_dir)/use-app-ruby.sh
export EB_CONFIG_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
export EB_CONFIG_APP_ONDECK=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
cd $EB_CONFIG_APP_ONDECK
if [ -f Gemfile ]; then
echo "running 'bundle install --deployment' with Gemfile:"
cat Gemfile
if [ -d $EB_CONFIG_APP_ONDECK/vendor/cache ]; then
bundle install --local --deployment
chown -h -R $EB_CONFIG_APP_USER:$EB_CONFIG_APP_USER $EB_CONFIG_APP_ONDECK/vendor/cache
# Incase there is a gem that is missing from the cache
bundle install --deployment
else
bundle install --deployment
fi
if [ $? != 0 ]; then
echo "ERROR: bundle install failed!"
exit 1
else
echo "bundle install succeeded"
fi
else
echo "no Gemfile found! Skipping bundle install stage!"
fi
if [ -f Gemfile.lock ]; then
echo "encountered a Gemfile.lock, setting proper permissions"
chown -h $EB_CONFIG_APP_USER:$EB_CONFIG_APP_USER Gemfile.lock
else
echo "no Gemfile.lock file found, so no permissions to set on it"
fi
true
encoding: plain
commands:
# Backups the existing bundle command
01_backup_existing_bundle_command:
test: test -f /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh
cwd: /opt/elasticbeanstalk/hooks/appdeploy/pre
command: mv 10_bundle_install.sh 10_bundle_install.sh.bak
packages:
yum:
git: []
commands:
01_install_git:
command: "yum install git"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment