class GoDeploy::Deploy

App go-deploy

Constants

TMP_DIR

Attributes

config[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/go_deploy/deploy.rb, line 15
def initialize
  super
  begin
    error_message = 'Please specify an order e.g.go-deploy production deploy or go-deploy production logs'
    @file_name = ARGV[0]
    @action = ARGV[1]
    puts error_message.red and exit if @file_name.nil? && @action.nil?

    yaml_file = File.join(Dir.pwd, "#{@file_name}.yaml")

    self.config = YAML.load_file(yaml_file)
    @console = Console.new(config)
    @service = config['service']
  rescue StandardError => e
    puts e.message.red
    exit
  end
end

Public Instance Methods

run() click to toggle source
# File lib/go_deploy/deploy.rb, line 34
def run
  case @action.upcase
  when 'DEPLOY'
    deploy_go
  when 'LOGS'
    logs
  end
end

Private Instance Methods

build_go() click to toggle source
# File lib/go_deploy/deploy.rb, line 127
def build_go
  @service_name = @service['name']
  puts 'Step 6 build:go'.green
  @is_restart = @service['is_restart']

  if @is_restart
    @console.sudo_exec(ssh: ssh, command: "sudo systemctl stop #{@service_name}.service")
    puts "\tStop #{@service_name} service".green
  end

  @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && go build -o #{@service_name}")
end
copy_files() click to toggle source
# File lib/go_deploy/deploy.rb, line 140
def copy_files
  puts 'Step 7 copy files'.green
  @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{@deploy_env_file} #{@deploy_to}")

  @service['copy_files'].each do |file_name|
    @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{file_name} #{@deploy_to}")
  end

  @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{@service_name} #{@deploy_to}")
end
deploy_go() click to toggle source
# File lib/go_deploy/deploy.rb, line 59
def deploy_go
  # Step 1
  wrapper
  # Step 2
  git_check
  # Step 3
  git_clone
  # Step 4
  git_update
  # Step 5
  set_env
  # Step 6
  build_go
  # Stop 7
  copy_files
  # Stop 8
  remove_files
  # Stop 9
  start_go_service if @is_restart
end
git_check() click to toggle source
# File lib/go_deploy/deploy.rb, line 97
def git_check
  @git_repo_url = @service['git_repo_url']
  puts 'Step 2 git:check'.green
  @console.exec(ssh: ssh, command: "git ls-remote #{@git_repo_url} HEAD")
end
git_clone() click to toggle source
# File lib/go_deploy/deploy.rb, line 103
def git_clone
  @deploy_to = @service['deploy_to']
  puts 'Step 3 git:clone'.green
  @console.exec(ssh: ssh, command: "mkdir -p #{@deploy_to}")
  @console.exec(ssh: ssh, command: "git clone #{@git_repo_url} #{@deploy_to}/repo")
  # clean up files
  @service['copy_files'].each do |file_name|
    @console.exec(ssh: ssh, command: "rm -rf #{@deploy_to}/#{file_name}")
  end
end
git_update() click to toggle source
# File lib/go_deploy/deploy.rb, line 114
def git_update
  puts 'Step 4 git:clone'.green
  @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && git remote set-url origin #{@git_repo_url}")
  @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && git remote update --prune")
end
logs() click to toggle source
# File lib/go_deploy/deploy.rb, line 80
def logs
  @service_name = @service['name']
  @console.sudo_exec(ssh: ssh, command: "sudo journalctl -u #{@service_name}.service -f", is_show_color: false)
end
remove_files() click to toggle source
# File lib/go_deploy/deploy.rb, line 151
def remove_files
  puts 'Step 8 removed:files'.green
  @console.exec(ssh: ssh, command: "cd #{@deploy_to}/repo && mv #{@service_name} #{@deploy_to}")
  @console.exec(ssh: ssh, command: "rm -rf #{@deploy_to}/repo")
  @console.exec(ssh: ssh, command: "rm -rf #{@git_wrapper_path}")
  @console.exec(ssh: ssh, command: 'rm -rf /tmp/git-ssh*')
end
set_env() click to toggle source
# File lib/go_deploy/deploy.rb, line 120
def set_env
  @deploy_env_file = "#{@deploy_to}/repo/.env"
  @env_file = @service['env_file']
  puts 'Step 5 set:env'.green
  @console.log(command: "Uploading #{@deploy_env_file}") if ssh.scp.upload!(@env_file, @deploy_env_file)
end
ssh() click to toggle source
# File lib/go_deploy/deploy.rb, line 45
def ssh
  return @ssh if defined? @ssh

  @ssh = Net::SSH.start(config['host'], config['user'], ssh_options)
end
ssh_options() click to toggle source
# File lib/go_deploy/deploy.rb, line 51
def ssh_options
  {
    keys: [config['passphrase']],
    forward_agent: true,
    paranoid: true
  }
end
start_go_service() click to toggle source
# File lib/go_deploy/deploy.rb, line 159
def start_go_service
  puts "Step 9 systemctl:start:#{@service_name}".green
  @console.sudo_exec(ssh: ssh, command: "sudo systemctl start #{@service_name}.service")
  puts "\tStart #{@service_name} service".green
end
wrapper() click to toggle source
# File lib/go_deploy/deploy.rb, line 85
def wrapper
  puts 'Step 1 git:wrapper'.green
  @git_ssh_name = "git-ssh-#{SecureRandom.hex(10)}.sh"
  @git_wrapper_path = "#{TMP_DIR}/#{@git_ssh_name}"

  @console.exec(ssh: ssh, command: "mkdir -p #{File.dirname(@git_wrapper_path).shellescape}")
  file = StringIO.new("#!/bin/sh -e\nexec /usr/bin/env ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no \"$@\"\n")
  @console.log(command: "Uploading #{@git_wrapper_path}") if @ssh.scp.upload!(file, @git_wrapper_path)

  @console.exec(ssh: ssh, command: "chmod 700 #{@git_wrapper_path.shellescape}")
end