Class: SunRaise::Deployer

Inherits:
Object
  • Object
show all
Defined in:
lib/deployer.rb

Overview

working horse

Instance Method Summary collapse

Instance Method Details

#go!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/deployer.rb', line 13

def go!
  conf[:shared_dirs] ||= {}
  return if conf[:help]
  return destroy_existen! if conf[:destroy]
  if !conf[:remake] && initiated?
    update
  else
    init_deploy
  end

  callbacks :after

  puts "SSH OUT: \n" + @ssh_out.join("\n") if conf[:verbose]
end

#init_deployObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/deployer.rb', line 28

def init_deploy
  destroy_existen! if conf[:remake]
  log_ok "initial deploy.."
  ssh_exec [
    "mkdir -p #{deploy_path}",
    "cd #{deploy_path}",
    "mkdir -p log shared tmp",
    "git clone #{conf[:git_url]} current"
  ]
  log_ok "Created dir and cloned repo"
  
  test_rails_app! 'current' if conf[:test_rails_app]
  auto_migrate! 'current' if conf[:auto_migrate]

  log_ok "Made new dirs and cloned repo"
  make_links 'current'
end

#release!Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/deployer.rb', line 88

def release!
  ssh_exec [
    "cd #{deploy_path}", # folder magic :) old current => previous, pre_release => current
    "mv current current2",
    "mv #{@new_dir} current",
    "if [ -d previous ]; then mv previous previous2 -f; fi",
    "mv current2 previous"
  ]
  log_ok "Released!"
end

#updateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/deployer.rb', line 46

def update
  
  last_commit = ssh_exec ["cd #{current_path}", "git log -1 --pretty=format:\"%H\""]
  last_repo_commit = (`cd #{conf[:local_project_path]} && git ls-remote #{conf[:git_url]}`).split("\t").first

  if !conf[:force] && last_commit.strip == last_repo_commit.strip
    log_ok "Nothing to update"
    return
  end

  new_commits = (`cd #{conf[:local_project_path]} && git log #{last_commit}..HEAD --pretty=format:"%s"`).split("\n")

  log_ok "New commits: \n    #{new_commits.join "\n    "}"
  @new_dir = "pre_release"
  
  ssh_exec [
    "cd #{deploy_path}",
    "rm #{@new_dir} .git_temp previous2 current2 -rf", # if previous deploy was crashed
    "mv current/.git .git_temp", # moving repo dir
    "cp current #{@new_dir} -r", # clone sitedir without .git
    "mv .git_temp #{@new_dir}/.git", # move git in pre_release folder
  ]

  ssh_exec (conf[:shared_dirs].keys + conf[:linked_dirs]).map {|dir| "rm #{deploy_path}/#{@new_dir}/#{dir}" }

  ssh_exec [
    "cd #{deploy_path}",
    "cd #{@new_dir}",
    "git reset HEAD --hard",
    "git pull origin master"
  ]

  log_ok "Forked, git updated"

  make_links @new_dir

  test_rails_app! @new_dir if conf[:test_rails_app]
  auto_migrate! @new_dir if conf[:auto_migrate]
  
  release! if conf[:release]
end