Commit 046fe52

mo khan <mo@mokhan.ca>
2017-01-30 16:56:02
create s3 plugin to deploy tarball via awscli.
1 parent d35023f
Changed files (4)
lib/capistrano/s3tarball/tasks/s3.rake
@@ -0,0 +1,42 @@
+strategy = self
+
+namespace :s3 do
+  desc 'Check that the repository is reachable'
+  task :check do
+    on roles(:all) do
+      strategy.check
+    end
+  end
+
+  desc 'Clone the repo to the cache'
+  task :clone do
+    on roles(:all) do
+      if strategy.test
+        info t(:mirror_exists, at: repo_path)
+      else
+        within deploy_path do
+          strategy.clone
+        end
+      end
+    end
+  end
+
+  desc 'Update the repo mirror to reflect the origin state'
+  task :update => 's3:clone' do
+    on roles(:all) do
+      within repo_path do
+        strategy.update
+      end
+    end
+  end
+
+  desc 'Copy repo to releases'
+  task :create_release => 's3:update' do
+    on roles(:all) do
+      within repo_path do
+        execute :mkdir, '-p', release_path
+        strategy.release
+      end
+    end
+  end
+end
lib/capistrano/s3tarball/plugin.rb
@@ -0,0 +1,58 @@
+require "capistrano/scm/plugin"
+
+module Capistrano
+  class S3Plugin < ::Capistrano::SCM::Plugin
+    def set_defaults
+      set :current_revision, ENV.fetch('BUILD_VERSION', `git ls-remote #{fetch(:git_repo)} #{fetch(:git_branch)}`)[0...7]
+      set :git_repo, ''
+      set :aws_profile, 'default'
+      set :git_branch, ENV['BRANCH']
+      set :bucket_name, ''
+    end
+
+    def define_tasks
+      eval_rakefile File.expand_path("../tasks/s3.rake", __FILE__)
+    end
+
+    def register_hooks
+      after "deploy:new_release_path", "s3:create_release"
+      before "deploy:check", "s3:check"
+    end
+
+    def test
+      backend.test " [ -f #{repo_path}/#{tarball} ] "
+    end
+
+    def check
+      s3 "ls s3://#{fetch(:bucket_name)}/#{fetch(:application)}/#{tarball}"
+    end
+
+    def clone
+      backend.execute(:mkdir, '-p', repo_path)
+    end
+
+    def update
+      source = "s3://#{fetch(:bucket_name)}/#{fetch(:application)}/#{tarball}"
+      destination = "#{repo_path}/#{tarball}"
+      s3 "cp #{source} #{destination}"
+    end
+
+    def release
+      path = "#{repo_path}/#{tarball}"
+      strip = "--strip-components=1"
+      backend.execute(:tar, "-xvzf", path, strip, "-C", release_path)
+    end
+
+    def s3(*args)
+      args.unshift "--profile #{fetch(:aws_profile)}"
+      args.unshift :s3
+      args.unshift :aws
+      backend.execute(*args)
+    end
+
+    def tarball
+      "#{fetch(:application)}-#{fetch(:current_revision)}.tar.gz"
+    end
+  end
+end
+
lib/capistrano/s3tarball.rb
@@ -1,7 +1,7 @@
 require "capistrano/s3tarball/version"
+require "capistrano/s3tarball/plugin"
 
 module Capistrano
   module S3tarball
-    # Your code goes here...
   end
 end
capistrano-s3tarball.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
   spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
   spec.require_paths = ["lib"]
 
+  spec.add_dependency "capistrano", "~> 3.7"
   spec.add_development_dependency "bundler", "~> 1.13"
   spec.add_development_dependency "rake", "~> 10.0"
   spec.add_development_dependency "rspec", "~> 3.0"