Module: Capistrano_Karaf

Included in:
Docker
Defined in:
lib/capistrano-karaf/core.rb,
lib/capistrano-karaf/extended.rb

Instance Method Summary collapse

Instance Method Details

#add_url(url) ⇒ Object

Add a feature url to the karaf server

url - the string containing the url

Examples

add_url "mvn:com.melexis.esb/eventstore-feature/#{eventStoreVersion}/xml/features"
# => nil

Returns nothing



35
36
37
# File 'lib/capistrano-karaf/core.rb', line 35

def add_url (url)
  execute(:features_addurl, url)
end

#feature_bundles(name, version) ⇒ Object

List all bundles provided by a feature

name - A string containing the name of the feature version - A string containing the version of the feature

Examples

feature_bundles "camel-core" "2.18"
# => [{:groupId => "org.apache.camel", :artifactId => "camel-core", :version => "2.18"}]

Returns a list containing the hashes with the bundle information



381
382
383
384
# File 'lib/capistrano-karaf/core.rb', line 381

def feature_bundles(name, version)
  data = capture(:features_info, "#{name} #{version}")
  extract_bundles_from_feature(data)
end

#feature_install(name) ⇒ Object

Install a feature on the karaf server

name - the string containing the feature name

Examples

feature_install "hello"
# => nil

Returns nothing



72
73
74
# File 'lib/capistrano-karaf/core.rb', line 72

def feature_install (name)
  execute(:features_install, name)
end

#feature_installed?(name, version = nil) ⇒ Boolean

Verify if a feature is installed

name - a string containing the name of the feature version - an optional version string

Examples

feature_installed? "camel-core"
# => true

Returns true if the feature is installed

Returns:

  • (Boolean)


139
140
141
142
# File 'lib/capistrano-karaf/extended.rb', line 139

def feature_installed? (name, version=nil)
  feature = list_features.find {|f| f[:name]==name and (version.nil? or f[:version] == version)}
  feature[:status] == 'installed' unless feature.nil?
end

#feature_uninstall(name, version = nil) ⇒ Object

Uninstall a feature on the karaf server

name - the string containing the feature name version - the optional string containing the version number

Examples

feature_uninstall "hello" "2.19.0"
# => nil

Returns nothing



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/capistrano-karaf/core.rb', line 86

def feature_uninstall(name, version=nil)
  # Keep track of the bundles that are part of the feature
  feature_bundle_urls = feature_bundles(name, version).collect {|b| b[:url]}
  puts "Bundles in feature: #{feature_bundle_urls}"

  if version.nil? then
    execute(:features_uninstall, "#{name}")
  else
    execute(:features_uninstall, "#{name}/#{version}")
  end
  
  # Verify all bundles have been uninstalled and remove the bundle if not
  list_bundle_locations.each do |installed_bundle|
    if feature_bundle_urls.include? installed_bundle[:url] then
      puts "Uninstall bundle #{installed_bundle}"
      uninstall installed_bundle[:id]
    end
  end
end

#feature_uninstall_safe(name, version = nil) ⇒ Object

Remove a feature in a safe way

This function verifies if the feature is installed before uninstalling it. This way it tries to avoid an exception on the karaf server.

name - a string containing the feature name version - an optional version string

Examples

feature_uninstall_safe "test", "1.0.0"
# => nil

Returns nothing



57
58
59
# File 'lib/capistrano-karaf/extended.rb', line 57

def feature_uninstall_safe (name, version=nil)
  feature_uninstall name, version unless !feature_installed? name, version
end

#features_refreshurlObject

Refresh the urls containing the feature repositories on the karaf server

Examples

features_refreshurl
# => nil

Returns nothing



59
60
61
# File 'lib/capistrano-karaf/core.rb', line 59

def features_refreshurl
  execute(:features_refreshurl)
end

#fragment_bundle?(bundleId) ⇒ Boolean

Verify if a bundle is a fragment bundle

bundleId - a number containing the bundleId

Examples

fragment_bundle? 101
# => false

Returns a boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/capistrano-karaf/extended.rb', line 70

def fragment_bundle? (bundleId)
  headers(bundleId).lines.any? {|l| l.match('^Fragment-Host.*')}
end

#headers(bundle) ⇒ Object

Get the headers for a bundle

bundleId - A number containing the bundle id

Examples

headers 10
# => "..."

Returns the string containing the headers information for the bundle.



361
362
363
364
# File 'lib/capistrano-karaf/core.rb', line 361

def headers (bundle)
  data = capture(:headers, bundle)
  extract_bundle_headers(data)
end

#install(uri) ⇒ Object

Install a bundle

uri - the uri for the bundle

Examples

install "mvn:blaat/blubber"

Returns nothing



177
178
179
# File 'lib/capistrano-karaf/core.rb', line 177

def install uri
  execute(:install, uri)
end

#list_bundle_locationsObject

List bundle locations

Examples

list_bundle_locations
# => [{:id => "10", :status => "INSTALLED", :blueprint => "", :context => "", :level => "60", :url => "mvn:org.springframework/spring-webmvc/3.0.5.RELEASE"}]

Returns a list of hashmaps containing the installed bundles and their url



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/capistrano-karaf/core.rb', line 335

def list_bundle_locations
  bundle_line_matcher = 
      %r{ (?<id> \d+){0}
          (?<status> \w+){0}
          (?<blueprint> \w*){0}
          (?<context> \w*){0}
          (?<level> \d+){0}
          (?<url> [\w:\.\/\-]+){0}

          ^\[\s*\g<id>\]\s\[\s*\g<status>\s*\]\s\[\s*\g<blueprint>\s*\]\s\[\s*\g<context>\s*\]\s\[\s*\g<level>\s*\]\s\g<url>
        }x
  
  data = capture(:list_all)
  matcher_to_hash(bundle_line_matcher, data)
end

#list_bundlesObject

List all bundles on the karaf server

Examples

list_bundles
# => [{:id => "10", :status => "INSTALLED", :blueprint => "", :context => "", :level => "60", :name => "camel-core", :version => "1.0.0"}]

Returns a list of hashmaps containing the installed bundles



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/capistrano-karaf/core.rb', line 243

def list_bundles
  bundle_line_matcher = 
      %r{ (?<id> \d+){0}
          (?<status> \w+){0}
          (?<blueprint> \w*){0}
          (?<context> \w*){0}
          (?<level> \d+){0}
          (?<name> [\w\s\.\-\:]+){0}
          (?<version> .+){0}

          ^\[\s*\g<id>\]\s\[\s*\g<status>\s*\]\s\[\s*\g<blueprint>\s*\]\s\[\s*\g<context>\s*\]\s\[\s*\g<level>\s*\]\s\g<name>\s\(\g<version>\)
        }x

  fragments_matcher = /\s*Fragments: ([\d\s]+)\s*/
  
  data = capture(:list)

  fragments = []
  bundles = []

  data.lines.each do |line|
    m1 = bundle_line_matcher.match line
    m2 = fragments_matcher.match line

    if m1 then
      bundles.push(m1)
    elsif m2 then
      fragment_bundles = m2[1].split /\s+/
      fragment_bundles.each {|fb| fragments.push fb}
    end
  end

  bundles1 = bundles.collect {|m| Hash[m.names.collect {|k| k.to_sym }.zip(m.captures)]}
  bundles1.collect do |b| 
    b[:fragment] = fragments.include? b[:id]
    b
  end
end

#list_featuresObject

List all features on the karaf server

Examples

list_features
# => [{:status => "Installed", :name => "camel-core", :repository => "repository", :version => "1.0.0"}]

Returns a list of hashmaps containing all available features



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/capistrano-karaf/core.rb', line 222

def list_features
  feature_line_matcher = 
      %r{ (?<status> \w+){0}
          (?<version> [\d\w\-\.]+){0}
          (?<name> [\w\-\.\:]+){0}
          (?<repository> [\w\-\:\.]+){0}

          ^\[\s*\g<status>\s*\]\s\[\s*\g<version>\s*\]\s*\g<name>\s*\g<repository>
        }x

  data = capture(:features_list)
  matcher_to_hash(feature_line_matcher, data)
end

#list_headersObject



366
367
368
369
# File 'lib/capistrano-karaf/core.rb', line 366

def list_headers
  data = capture(:headers)
  data.collect {|h| extract_bundles_from_feature(h)}
end

#list_urlsObject

List all feature urls on the karaf server

Examples

list_urls
# => [{:status => "Installed", :groupID => "repository", :artifactID => "repository", :version => "1.0.0"}]

Returns a list of hashmaps containing all feature repositories



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/capistrano-karaf/core.rb', line 201

def list_urls
  url_line_matcher = 
      %r{ (?<status> \w+){0}
          (?<groupID> [\d\w\.\-]+){0}
          (?<artifactID> [\w\.\-]+){0}
          (?<version> [\w\.\-]+){0} 

          \s*\g<status>\s*mvn\:\g<groupID>\/\g<artifactID>\/\g<version>.*
        }x

  data = capture(:features_listurl)
  matcher_to_hash(url_line_matcher, data)
end

#log_set(level) ⇒ Object

Set the log level on the karaf server

level - the string containing the level

Examples

log_set "debug"
# => nil

Returns nothing



115
116
117
# File 'lib/capistrano-karaf/core.rb', line 115

def log_set (level)
  execute(:log_set, level)
end

#remove_artifact_urls(groupID, artifactID) ⇒ Object

Remove all feature repositories with a given groupID and artifactID

groupID - a string containing the groupID artifactID - a string containing the artifactID

Examples

remove_artifact_urls("repository", "blaat")
# => nil

Returns nothing



37
38
39
40
41
42
# File 'lib/capistrano-karaf/extended.rb', line 37

def remove_artifact_urls (groupID, artifactID)
  urlsToRemove=list_urls.select {|url| url[:groupID] == groupID && url[:artifactID] == artifactID}  
  urlsToRemove.each do |url|    
    remove_url "mvn:#{url[:groupID]}/#{url[:artifactID]}/#{url[:version]}/xml/features"
  end
end

#remove_otherversion_urls(url) ⇒ Object

Remove all feature repositories with a given groupID and artifactID

url - the url to look for other versions with same url

Examples

remove_artifact_urls("mvn:com.melexis.esb/eventstore-feature/1.2/xml/features")
# => nil

Returns nothing



16
17
18
19
20
21
22
23
24
25
# File 'lib/capistrano-karaf/extended.rb', line 16

def remove_otherversion_urls (url)
  url_details_regex = %r{(?<groupID> [\w\.]+){0}
                         (?<artifactID> [\w\-]+){0}
                         ^mvn:\g<groupID>\/\g<artifactID>\/.*
                        }x
  urls_to_remove=matcher_to_hash(url_details_regex,url)
  urls_to_remove.each do |u|    
  remove_artifact_urls(u[:groupID],u[:artifactID])
  end
end

#remove_url(url) ⇒ Object

Remove a feature url from the karaf server

url - the string containing the url

Examples

remove_url "mvn:repository/eventstore-feature/1.0.0/xml/features"
# => nil

Returns nothing



48
49
50
# File 'lib/capistrano-karaf/core.rb', line 48

def remove_url (url)
  execute(:features_removeurl, url)
end

#start(bundleId) ⇒ Object

Start a bundle with the specified bundleId on the karaf server

bundleId - a number containing the id of the bundle

Examples

start 100
# => nil

Returns nothing



152
153
154
# File 'lib/capistrano-karaf/core.rb', line 152

def start (bundleId)
  execute(:start, bundleId)
end

#started?(name) ⇒ Boolean

Verify if a the bundle context is started

name - a string containing the name of the bundle

Examples

started? "camel-core"
# => true

Returns true if the bundle is started

Returns:

  • (Boolean)


153
154
155
156
# File 'lib/capistrano-karaf/extended.rb', line 153

def started? (name)
  bundle = list_bundles.find {|b| b[:name] == name}
  bundle[0][:context] == 'Started'
end

#startlevelObject



132
133
134
135
136
137
138
139
140
# File 'lib/capistrano-karaf/core.rb', line 132

def startlevel
  r = capture(:startlevel)
  m = r.match(/Level (\d+)/)
  if m then
    m[1].to_i
  else
    raise 'Invalid response from startlevel'
  end
end

#startlevel_set(level) ⇒ Object

Set the start level on the karaf server

level - the string containing the level

Examples

startlevel_set 60
# => nil

Returns nothing



128
129
130
# File 'lib/capistrano-karaf/core.rb', line 128

def startlevel_set (level)
  execute(:startlevel, level)
end

#stop(bundleId) ⇒ Object

Stop a bundle with the specified bundleId on the karaf server

bundleId - a number containing the id of the bundle

Examples

stop(100)
# => nil

Returns nothing



165
166
167
# File 'lib/capistrano-karaf/core.rb', line 165

def stop (bundleId)
  execute(:stop, bundleId)
end

#uninstall(bundleId) ⇒ Object

Uninstall a bundle with the specified bundleId from the karaf server

bundleId - a number containing the id of the bundle

Examples

start 100
# => nil

Returns nothing



190
191
192
# File 'lib/capistrano-karaf/core.rb', line 190

def uninstall (bundleId)
  execute(:uninstall, bundleId)
end

#wait_for_all_bundles(args = {}, &pred) ⇒ Object

Wait till all bundle return true for the specified predicate ( or the timeout is exceeded )

pred - a block that can be used as predicate

Optional parameters: :timeout - a number containing the timeout in seconds ( defaults to 60 ) :sleeptime - a number containing the timeout between tries in seconds ( defaults to 5 )

Examples

wait_for_all_bundles {|b| b[:context] == "STARTED"}
# => nil

Returns nothing



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

def wait_for_all_bundles (args={}, &pred)
  args = {:timeout => 60, :sleeptime => 5}.merge(args)
  timeout = Time.now + args[:timeout]
  
  bundles = list_bundles
  until Time.now > timeout or bundles.all? { |b| pred.call b} 
    failing_bundles = bundles.select { |b| !pred.call b }
    puts "Some bundles are still failing the predicate #{pred}: #{failing_bundles}"
    sleep args[:sleeptime]
    bundles = list_bundles
  end

  raise "Not all bundles pass the predicate within the timeout of #{args[:timeout]} seconds" unless list_bundles.all? { |b| pred.call b }
end

#wait_for_bundle(args = {}, &pred) ⇒ Object

Wait till the predicate passes for a bundle

pred - a block that can be used as predicate

Optional parameters: :timeout - a number containing the timeout in seconds ( defaults to 60 ) :sleeptime - a number containing the timeout between tries in seconds ( defaults to 5 )

Examples

wait_for_all_bundle {|b| b[:context] == "STARTED"}
# => nil

Returns nothing



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/capistrano-karaf/extended.rb', line 115

def wait_for_bundle (args={}, &pred)
  args = {:timeout => 60, :sleeptime => 5}.merge(args)
  timeout = Time.now + args[:timeout]
  
  while Time.now < timeout and list_bundles.none? { |b| pred.call b} 
    puts "Bundle not yet started"
    sleep args[:sleeptime]
  end
  
  if list_bundles.none? { |b| pred.call b}
    raise "Not all bundles pass the predicate within the timeout of #{args[:timeout]} seconds"
  end
end