Module: Docker

Includes:
Capistrano_Karaf
Defined in:
lib/capistrano-karaf/docker.rb

Instance Method Summary collapse

Methods included from Capistrano_Karaf

#add_url, #feature_bundles, #feature_install, #feature_installed?, #feature_uninstall, #feature_uninstall_safe, #features_refreshurl, #fragment_bundle?, #headers, #install, #list_bundle_locations, #list_bundles, #list_features, #list_headers, #list_urls, #log_set, #remove_artifact_urls, #remove_otherversion_urls, #remove_url, #start, #started?, #startlevel, #startlevel_set, #stop, #uninstall, #wait_for_all_bundles, #wait_for_bundle

Instance Method Details

#block_till_everything_is_startedObject



81
82
83
84
85
86
# File 'lib/capistrano-karaf/docker.rb', line 81

def block_till_everything_is_started
  block_till_karaf_started
  puts "Sleeping for 20 seconds"
  sleep 20
  wait_for_smx_to_start      
end

#block_till_karaf_started(args = {}) ⇒ Object

block_till_karaf_started - wait till the karaf server is listening to its ssh port



71
72
73
74
75
76
77
78
79
# File 'lib/capistrano-karaf/docker.rb', line 71

def block_till_karaf_started (args={})
  args = {:timeout => 120, :sleeptime => 1}.merge(args)
  timeout = Time.now + args[:timeout]
  until (karaf_started? || timeout < Time.now) do
    sleep args[:sleeptime]
  end

  raise "Karaf didn\' start within #{args[:timeout]} seconds." unless karaf_started?
end

#force_stopObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/capistrano-karaf/docker.rb', line 25

def force_stop
  # kill all remaining karaf processes on the server
  on roles(:esb) do
    begin 
      procs = list_processes
      karaf_procs = procs.find_all { |p| p[:command].include? "karaf" }
      karaf_procs.each do |p|
        as "smx-fuse" do
          begin
            execute(:kill, p[:pid])
          rescue
          end
        end
      end
    rescue Exception => e
      puts "#{host.hostname} got exception #{e.message}"
      raise e
    end
  end
end

#karaf_started?Boolean

karaf_started? - verify if karaf is listening to its ssh port

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/capistrano-karaf/docker.rb', line 65

def karaf_started?
  n = `netstat -ao | grep 8101 | wc -l`
  n.to_i > 0
end

#list_processesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capistrano-karaf/docker.rb', line 8

def list_processes
  matches = []
  procs = capture(:ps, "ax")
  procs.each_line do |line|
    m = /(?<PID>\d+)[ ]*(?<TTY>[?\w\/\d]+)[ ]*(?<STATUS>[\w\+]+)[ ]*(?<TIME>\d+:\d{1,2}+) (?<COMMAND>.*)/.match(line)
    if m then
      matches.push ({ :pid      => m['PID'],
                      :tty      => m['TTY'],
                      :status   => m['STATUS'],
                      :time     => m['TIME'],
                      :command  => m['COMMAND']
                    })
    end
  end
  matches
end

#wait_for_smx_to_startObject

wait_for_smx_to_start - function that blocks till all bundles are active, and the last one is started



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/capistrano-karaf/docker.rb', line 47

def wait_for_smx_to_start
  # wait so we can ssh to the smx console
  on roles(:karaf) do
    # wait until all bundles are started and spring context is loaded"
    puts "Waiting till all bundles are started"
    wait_for_all_bundles(:timeout => 180, :sleeptime => 10) do 
      |b| ["Active", "Resolved", "Installed"].include? b[:status] 
    end
    wait_for_bundle(:timeout => 500, :sleeptime => 10) do |b|
      if b[:name] == "Apache CXF Bundle Jar"
        puts "Bundle status #{b}"
      end
      b[:name] == "Apache CXF Bundle Jar" and (b[:blueprint] == 'Started' or b[:blueprint] == 'Created')
    end
  end
end

#with_karafObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/capistrano-karaf/docker.rb', line 88

def with_karaf
  if not karaf_started?
    pid = Process.spawn "/app/apache-karaf/bin/start"
    Process.detach pid
    block_till_everything_is_started
    puts "Karaf is started"
  else
    puts "Karaf is already started!"
  end

  yield
end