Module: Deploy

Extended by:
Deploy
Included in:
Deploy
Defined in:
lib/deploy.rb

Constant Summary collapse

MOVED_DIRS =
[
  'log',
  'tmp'
].freeze
SHARED_DIRS =
[
  'public/assets',
  'public/fonts',
  'vendor/bundle',
  'acme'
].freeze
INIT_DIRS =
[
  'tmp/sockets',
  'tmp/pids',
  'config/credentials'
].freeze
SHARED_FILES =
[
  'config/credentials/production.key'
].freeze

Instance Method Summary collapse

Instance Method Details

#exec_cmd(cmd) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/deploy.rb', line 80

def exec_cmd(cmd)
  Open3.popen2e(cmd) do |_, output, thread|
    logger.info "\e[35m  #{cmd} (PID: #{thread.pid})  \e[0m"
    output.each_line do |line|
      logger.info "  #{line.chomp}"
    end
    puts "\n"
  end
end

#exec_cmds(env = 'production', restart_cmds: ['bundle exec pumactl phased-restart'], added_cmds: []) ⇒ Object



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

def exec_cmds(env = 'production', restart_cmds: ['bundle exec pumactl phased-restart'], added_cmds: [])
  start_at = Time.now
  logger.info "Deploy started at #{start_at}"
  cmds = [
    'whoami',
    'git pull',#  --recurse-submodules
    'bundle install'
  ]
  cmds << "RAILS_ENV=#{env} bundle exec rake db:migrate"
  cmds += restart_cmds
  cmds += Array(added_cmds)
  cmds.each do |cmd|
    exec_cmd(cmd)
  end
  finish_at = Time.now
  logger.info "Deploy finished at #{finish_at}, used: #{(finish_at - start_at).round(2)}s"
end

#github_hmac(data) ⇒ Object



33
34
35
# File 'lib/deploy.rb', line 33

def github_hmac(data)
  OpenSSL::HMAC.hexdigest('sha1', RailsCom.config.github_hmac_key, data)
end

#init(root: Pathname.pwd, shared: Pathname.pwd.join('../shared')) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/deploy.rb', line 37

def init(root: Pathname.pwd, shared: Pathname.pwd.join('../shared'))
  dirs = (MOVED_DIRS + SHARED_DIRS + INIT_DIRS).map { |dir| shared.join(dir) }
  FileUtils.mkdir_p dirs, verbose: true

  files = SHARED_FILES.map { |file| shared.join(file) }
  FileUtils.touch files, verbose: true

  cmds = []
  cmds << 'bundle config set --local deployment true'
  cmds << 'bundle config set --local path vendor/bundle'
  cmds << 'bundle config set --local without development test'
  cmds += MOVED_DIRS.map do |path|
    "rm -rf #{root.join(path)}"
  end
  cmds += (MOVED_DIRS + SHARED_DIRS).map do |path|
    "ln -Tsfv #{shared.join(path)} #{root.join(path)}"
  end
  cmds += SHARED_FILES.map do |path|
    "ln -Tsfv #{shared.join(path)} #{root.join(path)}"
  end
  cmds.each do |cmd|
    exec_cmd(cmd)
  end
end

#loggerObject



90
91
92
93
94
95
96
# File 'lib/deploy.rb', line 90

def logger
  if defined? Rails
    Rails.logger
  else
    Logger.new $stdout
  end
end

#restartObject



29
30
31
# File 'lib/deploy.rb', line 29

def restart
  exec_cmd('bundle exec pumactl restart')
end