Class: Deploy::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy/runner.rb

Constant Summary collapse

BLACK =
"1"
BLUE =
"38;5;27;1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ Runner

Returns a new instance of Runner.



14
15
16
# File 'lib/deploy/runner.rb', line 14

def initialize(spec)
  @spec = spec
end

Class Method Details

.run(spec) ⇒ Object



10
11
12
# File 'lib/deploy/runner.rb', line 10

def self.run(spec)
  self.new(spec).deploy
end

Instance Method Details

#deployObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/deploy/runner.rb', line 18

def deploy
  @before  = @spec["before"]
  @after   = @spec["after"]
  @cleanup = @spec["cleanup"]

  ok = true
  tasks "BEFORE", @before

  cmd "ssh-add #{@spec[:key]} 2>/dev/null" if @spec[:key]

  @spec["servers"].each do |server|
    log "[Server #{server}]", BLUE

    ok = ok && justdoit(server)

    @commit = ssh(server, "git log -1 --pretty=%s")
    @author = ssh(server, "git log -1 --pretty=%an")
    @hash   = ssh(server, "git log -1 --pretty=%h")
    @date   = ssh(server, "git log -1 --pretty=%ad")

  end

  tasks "AFTER", @after if ok
  tasks "CLEANUP", @cleanup

  notify
end

#justdoit(server) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/deploy/runner.rb', line 64

def justdoit(server)
  log "CURRENT STATUS"
  status = ssh(server, "git status")
  puts status
  puts
  # return unless status.include? "working directory clean"

  log "CURRENT RELEASE"
  puts ssh(server, "git log -1")
  puts

  # TODO Figure this out
  log "PULLING FROM ORIGIN"
  fetched = ssh(server, "git fetch #{@spec["remote"]}")
  puts fetched == "" ? "(none)" : fetched
  puts
  # return unless fetched.size > 0

  log "REBASING"
  rebased = ssh(server, "git rebase #{@spec["remote"]}/#{@spec["branch"]}")
  puts rebased
  puts
  # return if rebased.include? "is up to date"

  log "NEW STATUS"
  status = ssh(server, "git status")
  puts status
  puts

  log "NEW RELEASE"
  puts ssh(server, "git log -1")
  puts

  true
end

#notifyObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/deploy/runner.rb', line 100

def notify
  return unless @spec["notifications"]
  return unless @spec["notifications"]["slack"]

  site    = @spec["site"]
  service = @spec["notifications"]["slack"]["service"]
  channel = @spec["notifications"]["slack"]["channel"]
  url     = "https://hooks.slack.com/services/#{service}"

  payload = {
    "channel"     => channel,
    "username"    => "Deployment #{DateTime.now}",
    "text"        => "New deployment to <#{site}>",
    "icon_emoji"  => ":dotser:",
    "attachments" => [
      {
        # "fallback" => "[fallback] Required text summary of the attachment that is shown by clients that understand attachments but choose not to show them.",
        # "pretext"  => "[pretext] Optional text that should appear above the formatted data",
        # "text"     => "[text] Optional text that should appear within the attachment",
        "_color"    => "warning",
        "fields"   => [
          {
            "title" => "Commit",
            "value" => @commit,
            "short" => true,
          },
          {
            "title" => "Date",
            "value" => @date,
            "short" => true,
          },
          {
            "title" => "Author",
            "value" => @author,
            "short" => true,
          },
          {
            "title" => "Hash",
            "value" => @hash,
            "short" => true,
          },
          {
            "title" => "Site",
            "value" => site,
            "short" => true,
          },
          {
            "title" => "Branch",
            "value" => @spec["branch"],
            "short" => true,
          },
        ]
      }
    ]
  }.to_json

  `curl -s -X POST --data-urlencode 'payload=#{payload}' #{url}`
end

#task(cmd) ⇒ Object



58
59
60
61
62
# File 'lib/deploy/runner.rb', line 58

def task(cmd)
  puts "$ #{cmd}"
  system(cmd)
  puts
end

#tasks(type, tasks) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/deploy/runner.rb', line 46

def tasks(type, tasks)
  return unless tasks
  log "#{type} ACTIONS"
  if tasks.kind_of? Array
    tasks.each do |t|
      task t
    end
  else
    task tasks
  end
end