Class: GitDeploy

Inherits:
Thor
  • Object
show all
Includes:
Configuration, SSHMethods
Defined in:
lib/git_deploy.rb,
lib/git_deploy/changelog.rb,
lib/git_deploy/remote_path.rb,
lib/git_deploy/ssh_methods.rb,
lib/git_deploy/configuration.rb

Defined Under Namespace

Modules: Changelog, Configuration, RemotePath, SSHMethods Classes: Generator

Constant Summary collapse

LOCAL_DIR =
File.expand_path('..', __FILE__)

Instance Method Summary collapse

Instance Method Details

#download(*files) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/git_deploy.rb', line 133

def download(*files)
  require_remote!
  abort "Error: Specify at least one file to download" if files.empty?

  scp_download files.inject({}) { |all, file|
    file = file.strip
    all[File.join(deploy_to, file)] = file
    all
  }
end

#hooksObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/git_deploy.rb', line 63

def hooks
  require_remote!

  remote_hook = "#{deploy_to}/.git/hooks/post-receive"
  if run_test("[ -f #{remote_hook} ]") && !options[:force]
    abort "Error: Remote already has a post-receive hook. Use --force to overwrite."
  end

  hooks_dir = File.join(LOCAL_DIR, 'hooks')
  remote_dir = "#{deploy_to}/.git/hooks"

  scp_upload "#{hooks_dir}/post-receive.sh" => "#{remote_dir}/post-receive"
  run "chmod +x #{remote_dir}/post-receive"
end

#initObject



20
21
22
23
# File 'lib/git_deploy.rb', line 20

def init
  require 'git_deploy/generator'
  Generator.start(['--template', options[:template]])
end

#log(n = nil) ⇒ Object



114
115
116
117
118
# File 'lib/git_deploy.rb', line 114

def log(n = nil)
  require_remote!
  tail_args = options.tail? ? '-f' : "-n#{n || options.lines}"
  run "tail #{tail_args} #{deploy_to}/log/deploy.log"
end

#rerunObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/git_deploy.rb', line 85

def rerun
  require_remote!
  run <<-BASH, :echo => false
    bash -e -c '
      cd '#{deploy_to}'
      declare -a revs=( $(git rev-parse HEAD@{1} HEAD) )
      deploy/after_push ${revs[@]} 2>&1 | tee -a log/deploy.log
    '
  BASH
end

#restartObject



79
80
81
82
# File 'lib/git_deploy.rb', line 79

def restart
  require_remote!
  run "cd #{deploy_to} && deploy/restart 2>&1 | tee -a log/deploy.log"
end

#rollbackObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/git_deploy.rb', line 97

def rollback
  require_remote!
  run <<-BASH, :echo => false
    bash -e -c '
      cd '#{deploy_to}'
      declare -a revs=( $(git rev-parse HEAD HEAD@{1}) )
      git reset --hard ${revs[1]}
      callback=after_push
      [ -x deploy/rollback ] && callback=rollback
      deploy/$callback ${revs[@]} 2>&1 | tee -a log/deploy.log
    '
  BASH
end

#setupObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/git_deploy.rb', line 29

def setup
  require_remote!

  remote_hook = "#{deploy_to}/.git/hooks/post-receive"
  if run_test("[ -f #{remote_hook} ]") && !options[:force]
    abort "Error: Remote already has a post-receive hook. Use --force to overwrite."
  end

  sudo = options.sudo? ? "#{sudo_cmd} " : ''

  unless run_test("test -x #{deploy_to}")
    run ["#{sudo}mkdir -p #{deploy_to}"] do |cmd|
      cmd << "#{sudo}chown $USER #{deploy_to}" if options.sudo?
      cmd
    end
  end

  run [] do |cmd|
    cmd << "chmod g+ws #{deploy_to}" if options.shared?
    cmd << "cd #{deploy_to}"
    unless run_test("[ -d #{deploy_to}/.git ]")
      cmd << "git init #{options.shared? ? '--shared' : ''}"
      cmd << "sed -i'' -e 's/master/#{branch}/' .git/HEAD" unless branch == 'master'
    end
    cmd << "git config --bool receive.denyNonFastForwards false" if options.shared?
    cmd << "git config receive.denyCurrentBranch ignore"
  end

  # Thor replaces inherited options when an explicit hash is passed — include :remote.
  invoke :hooks, [], remote: options[:remote], force: options[:force]
end

#upload(*files) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/git_deploy.rb', line 121

def upload(*files)
  require_remote!
  files = files.map { |f| Dir[f.strip] }.flatten
  abort "Error: Specify at least one file to upload" if files.empty?

  scp_upload files.inject({}) { |all, file|
    all[file] = File.join(deploy_to, file)
    all
  }
end