9

I need to issue Karaf shell commands non-interactively, preferably from a script. More specifically, I need to tell Karaf to feature:install a set of features in an automated way.

# Attempt to install a feature in a way I could script
bash> bin/karaf feature:install myFeature
# Drops me into Karaf shell
karaf> feature:uninstall myFeature
Error executing command: Feature named 'myFeature' is not installed
# Feature wasn't installed

Is this possible? Is there a different way of solving this issue (automated install of a set of Karaf features) that I'm missing?

hoijui
  • 3,615
  • 2
  • 33
  • 41
dfarrell07
  • 2,872
  • 2
  • 21
  • 26

6 Answers6

5

With bin/karaf you start Karaf with a login prompt, if you want to start Karaf so you can issue commands you first need to start Karaf in server mode. For this use the bin/start shell script. Now you can use either the bin/client or the bin/shell commands to communicate with Karaf in a headless mode.

For example:

./bin/client list
START LEVEL 100 , List Threshold: 50
ID | State  | Lvl | Version | Name
----------------------------------------------------------------------------------
72 | Active |  80 | 0       | mvn_org.ops4j.pax.web.samples_war_4.1.0-SNAPSHOT_war

This should work for all versions of Karaf already (maybe not the 2.2.x line ;-) )
If the version you're using is a 3.0.x or higher you might need to add a user to the command.

./bin/client -u karaf list
John Topley
  • 113,588
  • 46
  • 195
  • 237
Achim Nierbeck
  • 5,265
  • 2
  • 14
  • 22
  • I'm seeing a `Failed to get the session` error, but that's likely a different issue. Thanks for the info! – dfarrell07 Oct 20 '14 at 15:07
  • looks like you'll need a user with the client. In that case try with -u 'user'. Don't know which user ODL is using though, but can easily be found in the users.properties file in the etc folder. – Achim Nierbeck Oct 20 '14 at 15:53
  • `karaf = karaf,_g_:admingroup` seems to be the relevant line. Still seeing `Failed to get the session.` using `./bin/client -u karaf list`. – dfarrell07 Oct 20 '14 at 19:00
  • This is a "bit" late to respond but I'm thinking that others will find this answer useful. I had the same error pop up even with "-u karaf" and what solved it for me was adding "-h localhost" – cristian.petroaca Sep 07 '15 at 14:42
  • Also a late response... just wanted to mention to be sure you are calling bin/start before connecting to the client. After that, sometimes it takes 10 seconds (or more) to start up. – Jason Capriotti Feb 25 '16 at 17:39
  • @dfarrell07 same problem, it seems that you have to wait some unpredictable amount of time after issuing each karaf command, I've started new question because of it: http://stackoverflow.com/questions/43066208/how-to-detect-if-karaf-is-ready-to-accept-commands-via-client – 9ilsdx 9rvj 0lo Mar 28 '17 at 09:54
4

To issue Karaf shell commands not-interactively, preferably from a script you can also use the Karaf client (scroll down to "Apache Karaf client"). To install features I use command like

/opt/karaf/bin/client -r 7 "feature:install http; feature:install webconsole"

The -r switch allows to retry the connection if the server is not up yet (I use it in a Docker script).

davidpodhola
  • 1,030
  • 10
  • 17
1

It's possible to issue non-interactive Karaf shell commands using sshpass if keeping the password secrete isn't important.

sshpass -p karaf ssh -tt -p 8101 -o StrictHostKeyChecking=no karaf@localhost feature:install odl-l2switch-switch-ui

Working example in OpenDaylight's Vagrant-based L2Switch Tutorial.

dfarrell07
  • 2,872
  • 2
  • 21
  • 26
1

Late to the party, but this problem can easily be solved using the Features Boot configuration, located in the etc/org.apache.karaf.features.cfg file.

According to the following link https://karaf.apache.org/manual/latest/provisioning

A boot feature is automatically installed by Apache Karaf, even if it has not been previously installed using feature:install or FeatureMBean.

There are 2 main properties of this file, the featuresRepositories and featuresBoot.

  • featuresRepositories contains a list (comma-separated) of features repositories (features XML) URLs.
  • featuresBoot contains a list (comma-separated) of features to install at boot.

Note that once you update this file, Karaf will attempt to install the features listed in the featuresBoot configuration every time it starts. So if all you are looking to automate is installing features (as per the original question), then this is a great option.

David Hall
  • 11
  • 2
0

Another option is to use Expect.

This Expect script from OpenDaylight's CI installs and verifies a Karaf feature. Here's an excerpt:

# Install feature
expect "$prompt"
send "feature:install odl-netvirt-openstack\r"
expect {{
  "Error executing command: Can't install feature" {{
    send_user "\nFailed to install test feature\n"
    exit 1
  }}
}}
dfarrell07
  • 2,872
  • 2
  • 21
  • 26
-1

So the general practice is to install the feature, then loop on a bundle:list | grep bundleName to see if the bundles you need are installed. Then you continue on with your test case.

  • this might be generally helpful info when dealing with karaf, but it is not an answer to the question (how to non interactively issue commands to karaf) – hoijui Dec 20 '17 at 11:24