Class: Shippy::Remote::App

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/shippy/remote/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, app_name) ⇒ App

Returns a new instance of App.



9
10
11
12
# File 'lib/shippy/remote/app.rb', line 9

def initialize(host, app_name)
  @host = host
  @app_name = app_name
end

Instance Method Details

#backup_current_release ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shippy/remote/app.rb', line 32

def backup_current_release
  ts = Time.now.to_i.to_s
  timestamp_dir = backups_path.join(ts)
  current = current_path

  on(@host) do
    if test("[ -d #{current} ]")
      execute :mkdir, "-p", timestamp_dir
      execute :mv, current, timestamp_dir
    end
  end

  timestamp_dir.join(@app_name)
end

#backups_path ⇒ Object



100
101
102
# File 'lib/shippy/remote/app.rb', line 100

def backups_path
  SHIPPY.deploy_path.join("backups", @app_name)
end

#current_path ⇒ Object



96
97
98
# File 'lib/shippy/remote/app.rb', line 96

def current_path
  SHIPPY.deploy_path.join("apps", @app_name)
end

#list_backups ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shippy/remote/app.rb', line 66

def list_backups
  base = backups_path
  data = []

  on(@host) do
    if test("[ -d #{base} ]")
      data = capture(:ls, "-1r", base).split
    end
  end

  data
end

#prepare_directories ⇒ Object



14
15
16
17
# File 'lib/shippy/remote/app.rb', line 14

def prepare_directories
  path = current_path
  on(@host) { execute :mkdir, "-p", path }
end

#prune_old_releases(keep: 5) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/shippy/remote/app.rb', line 47

def prune_old_releases(keep: 5)
  base = backups_path

  on(@host) do
    if test("[ -d #{base} ]")
      releases = capture(:ls, "-1", base).split

      if releases.count > keep
        to_delete = releases - releases.last(keep)

        if to_delete.any?
          paths = to_delete.map { |r| base.join(r).to_s }
          execute :rm, "-rf", *paths
        end
      end
    end
  end
end

#restore_backup(target_ts) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shippy/remote/app.rb', line 79

def restore_backup(target_ts)
  backup_source = backups_path.join(target_ts, @app_name)
  target = current_path

  on(@host) do
    unless test("[ -d #{backup_source} ]")
      raise "Backup directory #{backup_source} does not exist"
    end
  end

  backup_current_release

  on(@host) do
    execute :mv, backup_source, target
  end
end

#upload_release(local_archive_path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shippy/remote/app.rb', line 19

def upload_release(local_archive_path)
  source_dir = current_path.join("..")

  on(@host) do
    remote_tar = capture("mktemp").strip
    upload! local_archive_path, remote_tar

    execute :tar, "-xzpf", remote_tar, "-C", source_dir

    execute :rm, remote_tar
  end
end