Bash script to add environment and date in Symfony appversion.yml file

Problem

You are using CI/CD to deploy a Symfony application that has the appversion.yml configuration file, and you want to add the environment and a timestamp to it after deployment.

Solution

You can achieve this by creating and running a bash script like the following and passing the environment as a variable (ie script_name env) :

#!/bin/bash
# Script for adding the environment and time in deployment
# Needs the environment as a parameter
# DATE_CREATED=2017.06.07
# DATE_UPDATED=2017.06.08
# VERSION=1.01

CONF_PATH=/var/www/analyse/app/config

sed '/app.version/s/\(.*$\)/\1'" (${1}) $(date +%Y%m%d-%H%M)"'/g' $CONF_PATH/appversion.yml > $CONF_PATH/appversion_new.yml
mv $CONF_PATH/appversion.yml $CONF_PATH/appversion_bak.yml
mv $CONF_PATH/appversion_new.yml $CONF_PATH/appversion.yml

Creating a simple rails docker image for testing in cloud deployment

Trying to deploy a rails application in a cloud provider ie dreamhost, that uses OpenStack these are the steps needed:

  1. Install docker in your development machine and your cloud provider by following the installation instructions from here
  2. Create an account in Docker Hub, that will be needed later on to push your docker image with the application
  3. Pull the official rails docker image to your development environment:
    sudo docker pull rails:latest
    
  4. Create a new simple rails application:
    rails new docker_test
  5. Change to the application directory and add a Dockerfile in the root directory containing the following:
    FROM rails:onbuild
  6. Build your new image by using:
    sudo docker build -t rails_docker_test .
  7. Check that your image was build by using:
    sudo docker images
  8. Start the container with:
    sudo docker run --name rails_test -p 0.0.0.0:3000:3000 -d rails_docker_test
  9. Make sure that you can see the initial rails page by using your browser to go to http://127.0.0.1:3000
  10. Push your image to your Docker Hub account by first logging in to it from the command line:
    sudo docker login --username=yourhubusername --email=youremail@company.com

    , and then when you get ‘Login Succeeded’, push your image to your account:

    sudo docker push yourhubusername/rails_docker_test
  11. TBC

fatal: cannot exec ‘/tmp/…/git-ssh.sh’: Permission denied – Capistrano, Dreamhost, permission denied for git-ssh.sh

Problem

When trying to use the new Capistrano 3.x to set up your rails project in a dreamhost account, you get the following error complaining that the git-ssh.sh script copied to your account by capistrano cannot be executed as the permission is denied:

fatal: cannot exec '/tmp/example.com/git-ssh.sh': Permission denied

Solution

It seems that Dreamhost, and quite possibly other hosting providers are not allowing executables from the /tmp directory, which is where Capistrano places the git-ssh.sh script. So in order to be able to execute the script you can change the directory where the script is copied in the first place and put it in your home directory. You can do that by adding the following to the config/deploy.rb file:

set :tmp_dir, "/home/dh_user_name/tmp"

Keeping uploaded files between deployments

Problem
You are using file_column plugin (or maybe another plugin?), to upload files in your ruby on rails application. Because the files are big you don’t want to have a different copy stored in your subversion repository for each different deployment version. You want to keep a common folder with all your uploaded files, and use it with every different deployment version.

Solution
For the example, we will have a numbers table that has two file_column uploadable columns (intro,voice_mail), with the following migration:

class CreateNumbers < ActiveRecord::Migration
  def self.up
    create_table :numbers do |t|
      t.column :customer_id, :int, :null => false
      t.column :phone_no, :string, :null => false
      t.column :intro, :string
      t.column :vmail, :string
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end

  def self.down
    drop_table :numbers
  end
end
  1. If you already have used cap deploy or cap setup you should have a shared folder in your deployment server.
    You should copy the intro and vmail folders that should be located on your public/number folder on your local development client, on a folder called number in your development server in your shared folder.
  2. Create a file in your local pc in lib/cap_recipes.rb:
    Capistrano.configuration(:must_exist).load do
    
      desc "Keep generated uploaded files between deployments"
        task :after_symlink do
           run "rm -drf #{deploy_to}/#{current_dir}/public/number"
           sudo "ln -nfs #{shared_path}/number #{deploy_to}/#{current_dir}/public"
        end
      end

    Be careful with the naming of the task as (at least for capistrano 1.4 that I’m using), it must have a special name as after_symlink.
    Also be careful that if you try to use before_symlink, it won’t work as the current symlink won’t be setup.

  3. In your config/deploy.rb file add at the top the following:
    require 'lib/cap_recipes'
  4. Now you should be ready to deploy your new version so:
    svn -m "added customised capistrano recipes" commit

    make sure that you check in your version in subversion, and then:

    cap deploy

    you should be able to see a link in your current/public folder called number that points to the shared/number folder that hold all the uploaded files.