Skip to content

Commit 2db3623

Browse files
committed
init commit
0 parents  commit 2db3623

8 files changed

Lines changed: 272 additions & 0 deletions

File tree

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in eb_fast_deploy.gemspec
4+
gemspec

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Nicola Brisotto
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# EbFastDeploy
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'eb_fast_deploy'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install eb_fast_deploy
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"

eb_fast_deploy.gemspec

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'eb_fast_deploy/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "eb_fast_deploy"
8+
spec.version = EbFastDeploy::VERSION
9+
spec.authors = ["Nicola Brisotto"]
10+
spec.email = ["nicolabrisotto@gmail.com"]
11+
spec.description = %q{TODO: Write a gem description}
12+
spec.summary = %q{TODO: Write a gem summary}
13+
spec.homepage = ""
14+
spec.license = "MIT"
15+
16+
spec.files = `git ls-files`.split($/)
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ["lib"]
20+
21+
spec.add_development_dependency "bundler", "~> 1.3"
22+
spec.add_development_dependency "rake"
23+
end

lib/eb_fast_deploy/version.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module EbFastDeploy
2+
VERSION = "0.0.1"
3+
end

lib/tasks/eb_fast_deploy.rake

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
require "../eb_fast_deploy/version"
2+
3+
def do_cmd(cmd)
4+
print "- - - cmd: #{cmd}\n"
5+
result = `#{cmd}`
6+
print "#{result}\n"
7+
end
8+
9+
def set_vars
10+
ENV['EB_TARGET'] = (ENV['EB_TARGET'] || "STAGE").upcase
11+
@eb_environment = ENV["EB_#{ENV['EB_TARGET']}_ENVIRONMENT"]
12+
@application_name = ENV["EB_#{ENV['EB_TARGET']}_APPLICATION_NAME"]
13+
ENV['AWS_ACCESS_KEY_ID']= ENV["EB_#{ENV['EB_TARGET']}_AWS_ACCESS_KEY_ID"]
14+
ENV['AWS_SECRET_ACCESS_KEY'] = ENV["EB_#{ENV['EB_TARGET']}_AWS_SECRET_ACCESS_KEY"]
15+
ENV['EB_ELASTICBEANSTALK_URL'] = ENV["EB_#{ENV['EB_TARGET']}_ELASTICBEANSTALK_URL"]
16+
ENV['FOG_DIRECTORY'] = ENV["EB_#{ENV['EB_TARGET']}_FOG_DIRECTORY"]
17+
ENV['FOG_DEPLOY_DIRECTORY'] = ENV["EB_#{ENV['EB_TARGET']}_FOG_DEPLOY_DIRECTORY"]
18+
@deploy_tmp_dir = "/home/vagrant/tmp/holden-fandom_for_deploy"
19+
@deploy_zip_file_path = "/home/vagrant/tmp/holden-fandom_for_deploy.zip"
20+
@aws_credential_file = "#{Rails.root}/tmp/aws_credential_file"
21+
22+
print "--- setting ENV ---\n"
23+
print "ENV['EB_TARGET']: #{ENV['EB_TARGET']}\n"
24+
print "ENV['AWS_ACCESS_KEY_ID']=#{ENV['AWS_ACCESS_KEY_ID']}\n"
25+
print "ENV['AWS_SECRET_ACCESS_KEY']=#{ENV['AWS_SECRET_ACCESS_KEY']}\n"
26+
print "ENV['FOG_DIRECTORY']=#{ENV['FOG_DIRECTORY']}\n"
27+
print "ENV['FOG_DEPLOY_DIRECTORY']=#{ENV['FOG_DEPLOY_DIRECTORY']}\n"
28+
print "@eb_environment: #{@eb_environment}\n"
29+
print "@aws_credential_file: #{@aws_credential_file}\n"
30+
print
31+
end
32+
33+
namespace :eb do
34+
desc "assets compile and upload to S3"
35+
task :assets do
36+
set_vars unless @eb_environment
37+
38+
do_cmd "rake assets:precompile FOG_DIRECTORY=#{ENV['FOG_DIRECTORY']} AWS_ACCESS_KEY_ID=#{ENV['AWS_ACCESS_KEY_ID']} AWS_SECRET_ACCESS_KEY=#{ENV['AWS_SECRET_ACCESS_KEY']}"
39+
do_cmd "mv public/assets/manifest.yml tmp/"
40+
do_cmd "rm public/assets/* -Rf"
41+
do_cmd "mv tmp/manifest.yml public/assets/"
42+
end
43+
44+
desc "bundle pack"
45+
task :bundle_pack do
46+
set_vars unless @eb_environment
47+
48+
do_cmd "rm vendor/cache -Rf"
49+
do_cmd "bundle install"
50+
51+
#gems
52+
#call "bundle pack --all" from shell before this rake
53+
#because it doesn't pach git gems
54+
do_cmd "bundle pack --all"
55+
56+
end
57+
58+
desc "upload project to s3"
59+
task :upload do
60+
print "=== upload project to s3 ===\n"
61+
print "\n"
62+
63+
set_vars unless @eb_environment
64+
65+
do_cmd "mkdir #{@deploy_tmp_dir}"
66+
do_cmd "rm #{@deploy_zip_file_path}"
67+
do_cmd "rsync --exclude='.git' --exclude='.bundle' --exclude='tmp/*' --exclude='log/*' . #{@deploy_tmp_dir} -r --delete-excluded --delete"
68+
69+
current_dir = `pwd`
70+
do_cmd "cd #{@deploy_tmp_dir} && zip -r #{@deploy_zip_file_path} . && cd #{pwd}"
71+
72+
print "storage = Fog::Storage.new provider: #{ENV['FOG_PROVIDER']}, aws_access_key_id: #{ENV['AWS_ACCESS_KEY_ID']}, aws_secret_access_key: #{ENV['AWS_SECRET_ACCESS_KEY']}\n"
73+
storage = Fog::Storage.new({
74+
provider: ENV['FOG_PROVIDER'],
75+
region: ENV['FOG_REGION'],
76+
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
77+
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
78+
})
79+
print "bucket = storage.directories.get(#{ENV['FOG_DEPLOY_DIRECTORY']})\n"
80+
bucket = storage.directories.get(ENV['FOG_DEPLOY_DIRECTORY'])
81+
82+
if File.exist? @deploy_zip_file_path
83+
print "creating new remote file..."
84+
deploy_zip_file = File.open @deploy_zip_file_path, "r"
85+
remote_file = bucket.files.new key: "prj.zip", body: deploy_zip_file
86+
result = remote_file.save
87+
print "remote_file.save: #{result}\n"
88+
else
89+
print "file #{@deploy_zip_file_path} not found"
90+
end
91+
92+
end
93+
94+
desc "disable memcache"
95+
task :disable_memcache do
96+
#assets precompile run in production env and search for memcache server
97+
do_cmd "sed -i 's/^ elasticache/# elasticache/' #{Rails.root}/config/environments/production.rb"
98+
do_cmd "sed -i 's/^ config.cache_store/# config.cache_store/' #{Rails.root}/config/environments/production.rb"
99+
end
100+
101+
desc "elastic beanstalk config"
102+
task :cfg do
103+
set_vars unless @eb_environment
104+
105+
#create temporary aws_credentials file
106+
f = File.new(@aws_credential_file, "w+")
107+
108+
content = ""
109+
content << "AWSAccessKeyId=#{ENV['AWS_ACCESS_KEY_ID']}\n"
110+
content << "AWSSecretKey=#{ENV['AWS_SECRET_ACCESS_KEY']}\n"
111+
f.write content
112+
f.close
113+
114+
#eb cfg
115+
eb_cfg_path = "#{Rails.root}/.elasticbeanstalk/config"
116+
f = File.new(eb_cfg_path, "a+")
117+
118+
f.write "ApplicationName=#{@application_name}\n"
119+
f.close
120+
end
121+
122+
desc "publish to elastic beanstalk"
123+
task :publish do
124+
AWS.config(
125+
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
126+
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
127+
region: ENV['FOG_REGION']
128+
)
129+
version_label = "#{Time.now}"
130+
aws_app_opt = {
131+
application_name: @application_name,
132+
description: "deploy",
133+
source_bundle: {
134+
s3_bucket: ENV['FOG_DEPLOY_DIRECTORY'],
135+
s3_key: "prj.zip"
136+
},
137+
version_label: version_label
138+
}
139+
140+
aws_env_opt = {
141+
environment_name: @eb_environment,
142+
version_label: version_label
143+
}
144+
145+
eb = AWS.elastic_beanstalk
146+
eb.client.create_application_version aws_app_opt
147+
eb.client.update_environment aws_env_opt
148+
end
149+
150+
desc "clean modified files"
151+
task :cleanup do
152+
File.delete(@aws_credential_file) if File.exist?(@aws_credential_file)
153+
do_cmd "git checkout .elasticbeanstalk/config"
154+
do_cmd "git checkout config/environments/production.rb"
155+
# do_cmd "rm vendor/cache -Rf"
156+
end
157+
158+
desc "deploy to elastic beanstalk"
159+
task :deploy do
160+
set_vars
161+
162+
print "=== deploy to elastic beanstalk ===\n\n"
163+
164+
# Rake::Task['eb:cfg'].invoke
165+
Rake::Task['eb:assets'].invoke
166+
Rake::Task['eb:bundle_pack'].invoke
167+
Rake::Task['eb:upload'].invoke
168+
Rake::Task['eb:publish'].invoke
169+
Rake::Task['eb:cleanup'].invoke
170+
171+
end
172+
end
173+

0 commit comments

Comments
 (0)