Class: MarathonDeploy::Deployment

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

Constant Summary collapse

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

Returns a new instance of Deployment.

Raises:

  • (ArgumentError)


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

def initialize(url, application)
  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.



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

def application
  @application
end

#deploymentIdObject (readonly)

Returns the value of attribute deploymentId.



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

def deploymentId
  @deploymentId
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#applicationExists?Boolean

Returns:

  • (Boolean)


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

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

#cancel(deploymentId, force = false) ⇒ Object

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
120
# File 'lib/marathon_deploy/deployment.rb', line 113

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



130
131
132
133
134
# File 'lib/marathon_deploy/deployment.rb', line 130

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)


154
155
156
157
158
# File 'lib/marathon_deploy/deployment.rb', line 154

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

#healthcheck_timeoutObject



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

def healthcheck_timeout
  return HEALTHY_WAIT_TIMEOUT
end

#rolling_restartObject



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

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



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

def timeout
  return DEPLOYMENT_TIMEOUT
end

#update_app(force = false) ⇒ Object



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

def update_app(force=false)
  url = @url + MarathonDefaults::MARATHON_APPS_REST_PATH + @application.id
  url += force ? '?force=true' : ''
  $LOG.debug("Updating app #{@application.id}  #{url}")
  response = HttpUtil.put(url,@application.json)    
  @deploymentId = Utils.response_body(response)[:deploymentId]
  return response
  
end

#versionsObject



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/marathon_deploy/deployment.rb', line 66

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_deployment_id(message = "Deployment with deploymentId #{@deploymentId} in progress") ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/marathon_deploy/deployment.rb', line 41

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/marathon_deploy/deployment.rb', line 87

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 in #{HEALTHY_WAIT_RECHECK_INTERVAL}s")
        end
      end      
      sleep(HEALTHY_WAIT_RECHECK_INTERVAL)
    end                         
  end  
end