Skip to content

Commit

Permalink
automate code deploys with sql dumps and tars, ui and script ready, n…
Browse files Browse the repository at this point in the history
…eeds runner ref #78
  • Loading branch information
seeReadCode committed Mar 23, 2017
1 parent 42e9a04 commit 26afc29
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 8 deletions.
9 changes: 7 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#per site in .env
REPO_NAME=coyote
REPO_URL=https://github.com/coyote-team/coyote.git
SUPPORT_EMAIL="[email protected]"
SUPPORT_EMAIL="[email protected]"
SUPPORT_PASSWORD=""
ASSET_SYNC_GZIP_COMPRESSION=true
ASSET_SYNC_MANIFEST=true
ROLLBAR_ACCESS_TOKEN=
Expand All @@ -10,4 +11,8 @@ FLOWDOCK_API_TOKEN=""
SERVER_USER=""
PRODUCTION_IP=""
STAGING_IP=""
BOOKMARKLET="true"
BOOKMARKLET="false"
WEBSITE_URL=""
WEBSITE_TITLE=""


8 changes: 8 additions & 0 deletions app/assets/javascripts/main.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ $ ->
data = {}
data[bulk] = sets

#turbolinks transitions
$('#main').addClass('fadeOut')
$.ajax
url: url
Expand All @@ -108,3 +109,10 @@ $ ->
error: (jqXHR, textStatus, errorThrown) ->
alert textStatus, errorThrown
$('#main').addClass('fadeIn').removeClass('fadeOut')


#deploy status
$(document).on "ajax:success", "form.deploy", (evt, data, status, xhr) ->
console.log "deployed!"


30 changes: 30 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class HomeController < ApplicationController
before_action :get_users
before_action :admin, only: [:deploy]

def index
if current_user
Expand All @@ -12,6 +13,7 @@ def index
@description_count = Description.all.count
@approved_count = Description.approved.count
if current_user.admin?
check_commit
@review_count = Description.ready_to_review.count
#@unapproved_count = Description.not_approved.count
@open_assignment_count = Image.assigned_undescribed.count
Expand Down Expand Up @@ -52,4 +54,32 @@ def index
end
@minimum_password_length = User.password_length.min
end

def deploy
@deployed_at = Rails.cache.fetch("deployed_at", expires_in: 1.hour) do
Time.now
end
Rails.logger.info "Deploying"
render nothing: true
end

protected
def check_commit
@current_commit = Rails.cache.fetch("current_commit", expires_in: 5.minutes) do
`git rev-parse --short HEAD`.chomp
end
@latest_commit = Rails.cache.fetch("latest_commit", expires_in: 5.minutes) do
require 'open-uri'
builds = JSON.load(open("https://api.travis-ci.org/repos/coyote-team/coyote/builds.json"))
builds = builds.map{|b| b if b["result"]==0}.compact
builds[0]["commit"].first(7)
end
@deployed_at = Rails.cache.read("deployed_at")
#TODO if deployed at has value and has passed 5 minutes
# if current commit now equal latest commit
# reset deployed at to nil
# else
# problem with deploy
end

end
1 change: 1 addition & 0 deletions app/views/application/_nav.haml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@
= link_to user_session_path do
%i.fa.fa-fw.fa-sign-in{"aria-hidden" => "true"}
Log in
51 changes: 45 additions & 6 deletions app/views/home/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
-if admin?
.row
.col-md-6
%h2>
Export spreadsheet of image records
%small>
%code .csv
= button_tag export_images_path(format: "csv"), class: 'btn btn-primary' do
Export
= form_tag import_images_path, multipart: true, class: 'form-horizontal' do |f|
%h2>
Import images
Expand All @@ -136,9 +142,42 @@
= button_tag type: "submit", class: "btn btn-primary disabled", data: {disaabled: true} do
Upload
.col-md-6
%h2>
Export spreadsheet of image records
%small>
%code .csv
= button_tag export_images_path(format: "csv"), class: 'btn btn-primary' do
Export
%h2 Deploy Status
- if @latest_commit
- if @latest_commit != @current_commit
- deploy_works = false
- if deploy_works #TODO
- if @deployed_at.nil?
-disable_with = "<i id='disable-me' class='fa fa-spinner fa-spin fa-fw'></i>"
- klass= 'btn btn-primary'
- form_klass = "inline-form"
- disabled = false
= button_to deploy_path, params: {},
form_class: form_klass, class: klass, disabled: disabled, method: :post, remote: true,
data: {disable_with: disable_with} do
Update
- else
- if @deployed_at + 5.minutes >= Time.now
Deploying...
- else
Redeploy run at:
= @deployed_at
-else
%p
Latest commit:
= link_to "https://github.com/coyote-team/coyote/commit/" + @latest_commit, target: "_blank" do
=@latest_commit
%p
Current commit:
= link_to "https://github.com/coyote-team/coyote/commit/" + @current_commit, target: "_blank" do
=@current_commit
- else
%p
Up to date at:
= link_to "https://github.com/coyote-team/coyote/commit/" + @current_commit, target: "_blank" do
=@current_commit
- else
%p
Current commit:
= link_to "https://github.com/coyote-team/coyote/commit/" + @current_commit, target: "_blank" do
=@current_commit
18 changes: 18 additions & 0 deletions bin/deploy_self.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
#usage: deploy_self production

cd ~/code/coyote

DATE=$(date +"%Y%m%d%H%M")
DIR=~/backups/coyote_$DATE

mkdir -p $DIR
rsync -az --copy-links ~/data/coyote/current/* $DIR
RAILS_ENV=$1 bundle exec rake db:backup
mv ../coyote*.sql $DIR

tar -zcvf ~/backups/$DIR.tar.gz ~/backups
rm -rf $DIR

#deploy
git pull; bundle exec cap production deploy
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
devise_for :users, :controllers => { registrations: 'registrations' }
scope "/admin" do
resources :users
post :deploy, :to => "home#deploy"
end
get '/login', to: redirect('/users/sign_in')
get '/logout', to: redirect('/users/sign_out')
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/backup.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace :backup do
desc "Backup database"
task :db do
app_root = File.join(File.dirname(__FILE__), "..", "..")
output_file = File.join(app_root, "..", "#{ENV['DATABASE_NAME']}-#{Time.now.strftime('%Y%m%d')}.sql")
if ENV['DATABASE_PASSWORD'] and !ENV['DATABASE_PASSWORD'].blank?
command = "/usr/bin/env mysqldump -h #{ENV['DATABASE_HOST']} -u #{ENV['DATABASE_USERNAME']} -p#{ENV['DATABASE_PASSWORD']} #{ENV['DATABASE_NAME']} > #{output_file}"
else
command = "/usr/bin/env mysqldump -h #{ENV['DATABASE_HOST']} -u #{ENV['DATABASE_USERNAME']} #{ENV['DATABASE_NAME']} > #{output_file}"
end
puts command
system(command)
end
end

0 comments on commit 26afc29

Please sign in to comment.