Class: Deploymatic::Deployer

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/deploymatic/deployer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file_path) ⇒ Deployer

Returns a new instance of Deployer.

Raises:



22
23
24
25
26
27
28
29
30
31
# File 'lib/deploymatic/deployer.rb', line 22

def initialize(config_file_path)
  @conf = Conf.new(**YAML.load_file(config_file_path))
  @conf.check_required_fields!

  result, ok = run(:echo, '$HOME')
  raise DeploymaticError.new("Unable to retrieve the user's home directory on the remote host: #{result}") unless ok

  @home_dir = result.strip
  @conf.install_dir.gsub!('$HOME', @home_dir)
end

Instance Method Details

#deployObject

Raises:



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/deploymatic/deployer.rb', line 76

def deploy
  result, ok = run(:git, 'pull', within: @conf.install_dir)
  raise DeploymaticError.new("Cannot pull git repo '#{@conf.repo}': #{result}") unless ok

  @conf.install_commands.to_a.each do |install_command|
    result, ok = run(*install_command.split(/\s+/), within: @conf.install_dir)
    raise DeploymaticError.new("Cannot run install command '#{install_command}': #{result}") unless ok
  end

  restart
end

#installObject

Raises:



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
60
61
62
63
64
65
66
# File 'lib/deploymatic/deployer.rb', line 33

def install
  # puts systemd_unit_file_to_string
  _, ok = run(:systemd, '--version')
  raise DeploymaticError.new('systemd is not installed on the remote host') unless ok

  _, ok = run(:git, '--version')
  raise DeploymaticError.new('git is not installed on the remote host') unless ok

  result, ok = run(:ls, '/var/lib/systemd/linger/')
  raise DeploymaticError.new("User #{@conf.ssh_user} is not in the lingering list") if !ok || !result.include?(@conf.ssh_user)

  result, ok = run(:mkdir, '-p', @conf.install_dir)
  raise DeploymaticError.new("Cannot create directory '#{@conf.install_dir}': #{result}") unless ok

  result, ok = run(:git, 'clone', @conf.repo, '.', within: @conf.install_dir)
  raise DeploymaticError.new("Cannot clone git repo '#{@conf.repo}': #{result}") unless ok

  @conf.install_commands.to_a.each do |install_command|
    result, ok = run(*install_command.split(/\s+/), within: @conf.install_dir)
    raise DeploymaticError.new("Cannot run install command '#{install_command}': #{result}") unless ok
  end

  io = StringIO.new(systemd_unit_file_to_string)
  path = unit_file_path
  on(@conf.url) do
    upload!(io, path)
  end

  result, ok = run(:systemctl, '--user', 'daemon-reload')
  raise DeploymaticError.new("Cannot reload systemd daemon: #{result}") unless ok

  result, ok = run(:systemctl, '--user', 'enable', @conf.name)
  raise DeploymaticError.new("Cannot enable systemd service '#{@conf.name}': #{result}") unless ok
end

#showObject



102
103
104
# File 'lib/deploymatic/deployer.rb', line 102

def show
  puts systemd_unit_file_to_string
end

#statusObject

Raises:



95
96
97
98
99
100
# File 'lib/deploymatic/deployer.rb', line 95

def status
  result, ok = run(:systemctl, '--user', 'status', @conf.name)
  raise DeploymaticError.new("Cannot #{command} systemd service '#{@conf.name}': #{result}") unless ok

  puts result
end

#uninstallObject



68
69
70
71
72
73
74
# File 'lib/deploymatic/deployer.rb', line 68

def uninstall
  run(:systemctl, '--user', 'stop', @conf.name)
  run(:systemctl, '--user', 'disable', @conf.name)
  run(:rm, unit_file_path)
  run(:systemctl, '--user', 'daemon-reload')
  run(:rm, '-r', @conf.install_dir)
end