Monday, June 26, 2017

Java DOM from XML is not namespace aware by default

Something which costed me a couple of headaches using getElementsByTagNameNS with a DocumentBuilderFactory from the java.xml.parsers package.

You find in the class properties:

    private boolean namespaceAware = false;

and I thought XML namespaces where the bread and butter of XML ...

So when building a DOM from an XML document you need to change the defaults of the factory like:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();

Friday, June 23, 2017

Shell looping vs xargs

For a very long time I was using the following construct to execute a command on foo, bar, and baz (all typed into an interactive shell prompt)

for F in foo bar baz; do command $F ; done

Now I finally got the -n option of xargs (or --max-args if you're using GNU) which limits to number of parameters to pass to the next command. So i can replace the previous command with:

echo foo bar baz | xargs -n 1 command

Here the -n option tells xargs to give at max one parameter to proceed, and create a new command with the next parameter.

As a bonus, instead of constructing an array of numbers with

seq 1 10

don't forget bash and zsh brace expansion


Friday, June 16, 2017

Tomcat7 not starting after Jessie->Stretch ugrade

I was wondering why the tomcat7 debian package would not start after the upgrade.

systemctl status tomcat7 output would look quite OK

systemctl status tomcat7
● tomcat7.service - LSB: Start Tomcat.
   Loaded: loaded (/etc/init.d/tomcat7; generated; vendor preset: enabled)
   Active: active (exited) since Fri 2017-06-16 10:10:34 CEST; 29min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 536 ExecStart=/etc/init.d/tomcat7 start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/tomcat7.service

Jun 16 10:10:34 new-ada systemd[1]: Starting LSB: Start Tomcat....
Jun 16 10:10:34 new-ada systemd[1]: Started LSB: Start Tomcat..
Jun 16 10:10:34 new-ada tomcat7[536]: Starting Tomcat servlet engine: tomcat7start-stop-dae…tory)
Jun 16 10:10:34 new-ada tomcat7[536]:  (already running).
Hint: Some lines were ellipsized, use -l to show in full.

But not java process was created

pgrep -lf java
echo $?

so something was obviously wrong.

But the devil is in the details, can you see this truncated line ?

Jun 16 10:10:34 new-ada tomcat7[536]: Starting Tomcat servlet engine: tomcat7start-stop-dae…tory)

If you run it in full with systemctl status -l tomcat7 it expends to

Jun 16 10:10:34 new-ada tomcat7[536]: Starting Tomcat servlet engine: tomcat7start-stop-daemon: unable to stat /usr/lib/jvm/java-7-openjdk-amd64//bin/java (No such file or directory)

Now I get it, the upgrade removed the openjdk-7-jre-headless package.Time to switch to openjdk8 ! The package openjdk-8-jre-headless was already installed, so I justed needed to set JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ in /etc/default/tomcat7

Note for the future:

  • Why was this package removed ?
  • Should I configure the systemctl status to show ellipsized lined by default ?

Thursday, June 15, 2017

Render HTML from standard input with w3m

From a very long time, I was using the links browser as my default browser. It allows quick testing of a web site with a simple
links -dump
Alas links does not support rendering html from standard input, so I finally switched to w3m, handles that well. So I could do:
cat bla.html | w3m -T text/html -dump
Using curl and any member of the Unix Toolkit gang, you see which possibilities it opens. Oh and UTF-8 works well too !
w3m -T text/html -dump | grep свободная
Debian — это свободная операционная система (ОС) для вашего компьютера.
свободная that’s Russian for Free.

Testing upgrades on a backup system

Like many Debian users, I am planning very soon to upgrade to my own personal server ada to Debian Stretch.
Since I do a full rsync backup of the server to a different location, I was wondering if it was possible to use this backup root fs in a container to test the upgrade. Turns is out it works very well with systemd-nspawn

Copy the backup to a separate dir in /var/lib/machines
# /var/lib/machines is where machinectl look for images
# see for rsync flags detial
rsync -axHAX --info=progress2 --numeric-ids --whole-file /home/manu/Projects/backups/ada_rootfs/ /var/lib/machines/ada/
For networking this container, I installed libvirtd, which comes with a DHCP server serving private IPs on the virbr0 bridge.
systemctl status libvirtd
libvirtd.service - Virtualization daemon
   Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-06-08 23:15:33 CEST; 6 days ago
     Docs: man:libvirtd(8)
 Main PID: 2609 (libvirtd)
    Tasks: 18 (limit: 4915)
   Memory: 53.0M
      CPU: 2.448s
   CGroup: /system.slice/libvirtd.service
           ├─2609 /usr/sbin/libvirtd
           ├─4868 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile
           └─4869 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile

juin 09 11:34:34 django dnsmasq-dhcp[4868]: not giving name ada to the DHCP lease of 192.168.122.
juin 09 12:04:24 django dnsmasq-dhcp[4868]: DHCPREQUEST(virbr0) 06:91:4f:92:3b:a8
juin 09 12:04:24 django dnsmasq-dhcp[4868]: DHCPACK(virbr0) 06:91:4f:92:3b:a8 ada
Start the container, giving a a Virtual Ethernet Link connected to virbr0
systemd-nspawn --directory /var/lib/machines/ada --machine ada --network-bridge=virbr0 --boot --network-veth
(observe a nice booting sequence, up to a login prompt)
Login inside the container and configure the network
# snippet for /etc/network/interfaces
# the Virtual Ethernet Link appears as host0 inside the container
auto host0
iface host0 inet dhcp
# restart the network
systemctl restart networking
Find out from the host side which IP was assigned
machinectl list
ada     container systemd-nspawn debian 8

1 machines listed.

Inspect from the host system if the Apache virtual hosts are running
curl --silent --header 'Host:' | w3m -T text/html -dump
Happy Upgrades !