Balaji Vajjala's Blog

A DevOps Blog from Trenches

04.07

  • 4.7 Script a deployment
    • Learning Objectives
    • EXERCISE: Modify Capistrano script.
    • EXERCISE: Modify Capistrano Script (OPTIONAL: Ruby)

4.7 Script a deployment

Learning Objectives

By the end of this lesson you will be able to:

  • Write a deployment script in Capistrano.

EXERCISE: Modify Capistrano script.

  1. Go to the deployment/config directory.

    cd /home/ec2-user/continuous_integration_example/software/deployment/config

  2. Create a new file called mydeploy.rb

    “` $ sudo vim mydeploy.rb

    “`

  3. Insert the contents of the code example below into the mydeploy.rb. Be sure to replace the 29 in cd29 with your userid.

    “` require ‘rubygems’ require ‘aws-sdk’ load ‘/usr/share/tomcat6/scripts/config/aws.config’

    sdb = AWS::SimpleDB.new

    set :sdb_domain, “cd29”

    set :domain do item = sdb.domains[“#{sdb_domain}”].items[‘parameters’] item.attributes[‘ip_address’].values[0].to_s end

    set :artifact_bucket do item = sdb.domains[“#{sdb_domain}”].items[‘parameters’] item.attributes[‘artifact_bucket’].values[0].to_s end

    set :ip_address do item = sdb.domains[“#{sdb_domain}”].items[‘parameters’] item.attributes[‘ip_address’].values[0].to_s end

    set :user, “ec2-user” set :use_sudo, false set :deploy_to, “/usr/share/tomcat6/webapps” set :artifact, “brewery.war” set :artifact_url, “https://s3.amazonaws.com/#{artifact_bucket}/#{artifact}” set :ssh_options, { :forward_agent => true }

    set :application, domain

    role :web, domain role :app, domain role :db, domain, :primary => true

    set :deploy_via, :remote_cache

    def get_binding binding # So that everything can be used in templates generated for the servers end

    def from_template(file) require ‘erb’ template = File.read(File.join(File.dirname(FILE), “..”, file)) result = ERB.new(template).result(self.get_binding) end

    namespace :deploy do

    task :setup do run “sudo chown -R tomcat:tomcat #{deploy_to}” run “sudo service tomcat6 stop” end

    task :deploy do run “cd #{deploy_to} && sudo rm -rf brewery* && sudo wget #{artifact_url}” end

    desc “Restart Application” task :restart, :roles => :app do run “sudo service tomcat6 restart” end

    after “deploy:setup”, “deploy:deploy” after “deploy:deploy”, “deploy:restart” end “`

    Save and close the mydeploy.rb file

  4. Add the file in Git

    “` $ git add .

    “`

  5. Commit the file to Git

    “` $ git commit -m “Created deployment script”

    “`

  6. Push it to the Git master.

    “` $ git push

    “`

  7. Modify your Jenkins Deploy job to run your version: mydeploy.rb. Run a Build job, Environment job and the Deploy job. Verify the application is deployed to the environment.

EXERCISE: Modify Capistrano Script (OPTIONAL: Ruby)

  1. From the AWSCLI instance, change directory to devopsinthecloud.
  2. Open the file as shown below.

    “` $ sudo vim deploy.rb

    “`

  3. A list of some of the deploy tasks is provided below.

    “` deploy:setup deploy:deploy deploy:bundle_install deploy:db_migrate deploy:restart

    “`

  4. Capify is run in the jenkins.template

  5. You can also find these files by going to https://raw.github.com/stelligent/continuous_integration_example/master/deployment/config/deploy.rb.
  6. Review the Deployment Jenkins job.
  7. Make a modification to the contents of the file and save it.
  8. Add the file in Git

    “` $ git add .

    “`

  9. Commit the file to Git

    “` $ git commit -m “Modified deployment script”

    “`

  10. Push it to the Git master.

    “` $ git push

    “`