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'
].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



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

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

#exec_cmds(env = 'production', added_cmds: []) ⇒ Object



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

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

#github_hmac(data) ⇒ Object



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

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



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/deploy.rb', line 36

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



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

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

#restartObject



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

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