|
| 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