Class: MarathonDeploy::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/marathon_deploy/deployment.rb

Constant Summary collapse

WAIT_FOR_DEPLOYMENT_TIMEOUT =
MarathonDefaults::WAIT_FOR_DEPLOYMENT_TIMEOUT
DEPLOYMENT_RECHECK_INTERVAL =
MarathonDefaults::DEPLOYMENT_RECHECK_INTERVAL
DEPLOYMENT_TIMEOUT =
MarathonDefaults::DEPLOYMENT_TIMEOUT
HEALTHY_WAIT_TIMEOUT =
MarathonDefaults::HEALTHY_WAIT_TIMEOUT
HEALTHY_WAIT_RECHECK_INTERVAL =
MarathonDefaults::HEALTHY_WAIT_RECHECK_INTERVAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, application, deployment_timeout = DEPLOYMENT_TIMEOUT) ⇒ Deployment

Returns a new instance of Deployment.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/marathon_deploy/deployment.rb', line 17

def initialize(url, application, deployment_timeout = DEPLOYMENT_TIMEOUT)
  raise ArgumentError, "second argument to deployment object must be an Application", caller unless (!application.nil? && application.class == Application)
  raise Error::BadURLError, "invalid url => #{url}", caller if (!HttpUtil.valid_url(url))
  @url = HttpUtil.clean_url(url)
  @application = application
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



15
16
17
# File 'lib/marathon_deploy/deployment.rb', line 15

def application
  @application
end

#deploymentIdObject (readonly)

Returns the value of attribute deploymentId.



15
16
17
# File 'lib/marathon_deploy/deployment.rb', line 15

def deploymentId
  @deploymentId
end

#urlObject (readonly)

Returns the value of attribute url.



15
16
17
# File 'lib/marathon_deploy/deployment.rb', line 15

def url
  @url
end

Instance Method Details

#applicationExists?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# File 'lib/marathon_deploy/deployment.rb', line 132

def applicationExists?
  response = list_app
  if (response.code.to_i == 200)
    return true
  end
    return false
end

#cancel(deploymentId, force = false) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
# File 'lib/marathon_deploy/deployment.rb', line 123

def cancel(deploymentId,force=false)
  raise ArgumentError, "deploymentId must be specified to cancel deployment", caller if (deploymentId.empty?)
  if (running_for_deployment_id?)
    response = HttpUtil.delete(@url + MarathonDefaults::MARATHON_DEPLOYMENT_REST_PATH + deploymentId + "?force=#{force}")
    $LOG.debug("Cancellation response [#{response.code}] => " + JSON.pretty_generate(JSON.parse(response.body)))
  end
  return response
end

#create_appObject



140
141
142
143
144
# File 'lib/marathon_deploy/deployment.rb', line 140

def create_app
  response = HttpUtil.post(@url + MarathonDefaults::MARATHON_APPS_REST_PATH,@application.json)
  @deploymentId = get_deployment_id
  return response
end

#health_checks_defined?Boolean

Returns:

  • (Boolean)


165
166
167
168
169
# File 'lib/marathon_deploy/deployment.rb', line 165

def health_checks_defined?
  health_checks = @application.health_checks
  return true unless health_checks.nil? or health_checks.empty?
  return false
end

#healthcheck_timeoutObject



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

def healthcheck_timeout
  return HEALTHY_WAIT_TIMEOUT
end

#rolling_restartObject



157
158
159
160
161
162
163
# File 'lib/marathon_deploy/deployment.rb', line 157

def rolling_restart
  url = @url + MarathonDefaults::MARATHON_APPS_REST_PATH + @application.id + '/restart'
  $LOG.debug("Calling marathon api with url: #{url}")
  response = HttpUtil.post(url,{})
  $LOG.info("Restart of #{@application.id} returned status code: #{response.code}")
  $LOG.info(JSON.pretty_generate(JSON.parse(response.body)))
end

#timeoutObject



24
25
26
# File 'lib/marathon_deploy/deployment.rb', line 24

def timeout
  return DEPLOYMENT_TIMEOUT
end

#update_appObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/marathon_deploy/deployment.rb', line 146

def update_app
  $LOG.debug("Updating app #{@application.id} #{url}")
  response = Utils.putJSON(@url,@application)
  begin
    @deploymentId = Utils.response_body(response)[:deploymentId]
  rescue Exception=>e
    $LOG.error "EXCEPTION: #{e}"
  end
  return response
end

#versionsObject



32
33
34
35
36
37
38
39
40
# File 'lib/marathon_deploy/deployment.rb', line 32

def versions
  if (!applicationExists?)
    response = HttpUtil.get(@url + MarathonDefaults::MARATHON_APPS_REST_PATH + @application.id + '/versions')
    response_body = Utils.response_body(response)
    return response_body[:versions]
  else
    return Array.new
  end
end

#wait_for_application(message = "Deployment of application #{@application.id} in progress") ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/marathon_deploy/deployment.rb', line 76

def wait_for_application(message = "Deployment of application #{@application.id} in progress")
    deployment_seen = false
    Timeout::timeout(DEPLOYMENT_TIMEOUT) do
      while running_for_application_id?
        deployment_seen = true
        #response = list_all
        #STDOUT.print "." if ( $LOG.level == 1 )
        $LOG.info(message)
        deployments_for_application_id.each do |item|
          $LOG.debug(deployment_string(item))
        end
        #$LOG.debug(JSON.pretty_generate(JSON.parse(response.body)))
        sleep(DEPLOYMENT_RECHECK_INTERVAL)
      end
      #STDOUT.puts "" if ( $LOG.level == 1 )
      if (deployment_seen)
        $LOG.info("Deployment of application #{@application.id} ended")
      end
    end
end

#wait_for_deploymentObject



42
43
44
45
46
47
48
49
50
# File 'lib/marathon_deploy/deployment.rb', line 42

def wait_for_deployment()
    startTime = Time.now
    Timeout::timeout(WAIT_FOR_DEPLOYMENT_TIMEOUT) do
        while !deployment_running?
          sleep(DEPLOYMENT_RECHECK_INTERVAL)
          $LOG.info("Waiting for Marathon to start deployment")
        end
    end
end

#wait_for_deployment_id(message = "Deployment with deploymentId #{@deploymentId} in progress") ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/marathon_deploy/deployment.rb', line 52

def wait_for_deployment_id(message = "Deployment with deploymentId #{@deploymentId} in progress")
    startTime = Time.now
    deployment_seen = false
    Timeout::timeout(DEPLOYMENT_TIMEOUT) do
      while running_for_deployment_id?
        deployment_seen = true
        #response = list_all
        #STDOUT.print "." if ( $LOG.level == 1 )
        elapsedTime = '%.2f' % (Time.now - startTime)
        $LOG.info(message + " (elapsed time #{elapsedTime}s)")
        deployments = deployments_for_deployment_id
        deployments.each do |item|
          $LOG.debug(deployment_string(item))
        end
        sleep(DEPLOYMENT_RECHECK_INTERVAL)
      end
      #STDOUT.puts "" if ( $LOG.level == 1 )
      if (deployment_seen)
        elapsedTime = '%.2f' % (Time.now - startTime)
        $LOG.info("Deployment with deploymentId #{@deploymentId} ended (Total deployment time #{elapsedTime}s)")
      end
    end
end

#wait_until_healthyObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/marathon_deploy/deployment.rb', line 97

def wait_until_healthy
  startTime = Time.now
  Timeout::timeout(HEALTHY_WAIT_TIMEOUT) do
    loop do
      break if (!health_checks_defined?)
      sick = get_alive(false)
      elapsedTime = '%.2f' % (Time.now - startTime)
      if (!sick.empty?)
        $LOG.info("#{sick.size}/#{@application.instances} instances are not healthy, retrying in #{HEALTHY_WAIT_RECHECK_INTERVAL}s (elapsed time #{elapsedTime}s)")
        $LOG.debug("Sick instances: " + sick.join(','))
      else
        healthy = get_alive(true)
        if (healthy.size == @application.instances)
          elapsedTime = '%.2f' % (Time.now - startTime)
          $LOG.info("#{healthy.size} of #{@application.instances} expected instances are healthy (Total health-check time #{elapsedTime}s).")
          $LOG.debug("Healthy instances running: " + healthy.join(','))
          break
        else
          $LOG.info("#{healthy.size} healthy instances seen, #{@application.instances} healthy instances expected, retrying in #{HEALTHY_WAIT_RECHECK_INTERVAL}s")
        end
      end
      sleep(HEALTHY_WAIT_RECHECK_INTERVAL)
    end
  end
end