Module: Kontena::Cli::Stacks::StacksHelper

Included in:
Registry::CreateCommand, DeployCommand, Vpn::CreateCommand
Defined in:
lib/kontena/cli/stacks/stacks_helper.rb

Instance Method Summary collapse

Instance Method Details

#wait_for_deploy_to_finish(deployment, timeout = 600) ⇒ Boolean

Parameters:

  • deployment (Hash)

Returns:

  • (Boolean)


30
31
32
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
67
68
69
70
# File 'lib/kontena/cli/stacks/stacks_helper.rb', line 30

def wait_for_deploy_to_finish(deployment, timeout = 600)
  deployed = false
  states = %w(success error)
  tracked_services = []
  Timeout::timeout(timeout) do
    until deployed
      deployment = client.get("stacks/#{deployment['stack_id']}/deploys/#{deployment['id']}")
      service_deploy = deployment['service_deploys'].find{ |s| s['state'] == 'ongoing' }
      if service_deploy
        tracked_services << service_deploy['id']
        wait_for_service_deploy(service_deploy)
      end
      if states.include?(deployment['state'])
        deployed = true
        deployment['service_deploys'].select{ |s| !tracked_services.include?(s['id']) }.each do |s|
          wait_for_service_deploy(s)
        end
      else
        sleep 1
      end
    end
    if deployment['state'] == 'error'
      deployment['service_deploys'].each do |service_deploy|
        if service_deploy['state'] == 'error'
          puts "Deployment of service #{pastel.cyan(service_deploy['service_id'])} failed:"
          puts "  - #{service_deploy['reason'].strip}"
          service_deploy['instance_deploys'].each do |instance_deploy|
            if instance_deploy['state'] == 'error'
              puts "  - " + "#{instance_deploy['error'].strip} (on node #{pastel.cyan(instance_deploy['node'])})"
            end
          end
        end
      end
      abort
    end
  end

  deployed
rescue Timeout::Error
  raise 'deploy timed out'
end

#wait_for_deployment_to_start(deployment, timeout = 600) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kontena/cli/stacks/stacks_helper.rb', line 4

def wait_for_deployment_to_start(deployment, timeout = 600)
  started = false
  Timeout::timeout(timeout) do
    while deployment['state'] == 'created'
      sleep 1
      deployment = client.get("stacks/#{deployment['stack_id']}/deploys/#{deployment['id']}")
    end
    if deployment['state'] == 'error'
      puts "Stack deploy failed"
      deployment['service_deploys'].each do |service_deploy|
        if service_deploy['state'] == 'error'
          puts " - #{service_deploy['reason']}"
        end
      end

      abort
    end
  end

  started
rescue Timeout::Error
  raise 'deploy timed out'
end

#wait_for_service_deploy(service_deploy) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/kontena/cli/stacks/stacks_helper.rb', line 72

def wait_for_service_deploy(service_deploy)
  name = service_deploy['service_id'].split('/')[-1]
  spinner "Deploying service #{pastel.cyan(name)}" do |spin|
    until service_deploy['finished_at']
      sleep 1
      service_deploy = client.get("services/#{service_deploy['service_id']}/deploys/#{service_deploy['id']}")
    end
    spin.fail if service_deploy['state'] == 'error'
  end
end