Tuesday, October 8, 2013

Setting environment variables when running command via SSH in Ubuntu

There are less environment variables available when running command directly by SSH then interactively. This is because for some reason, Ubuntu does a little less for non-interactive shells and stops executing .bashrc after this line:

[ -z "$PS1" ] && return

So if you wish to run anything, it should be done before, like:

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_21/
[ -z "$PS1" ] && return

Thursday, September 26, 2013

Convert color console output into HTML with Java

Some command line tools can print colored text, which looks nice in console itself (if it is supported), but not so nice when you need to show it in HTML:

[47;32mhello world

There is small console tool "aha" that can pipe such input and transform it directly in command line, but sometimes you have to do it from code. For such cases there is Java library Jansi which prints ANSI escape sequences into Windows consoles. Their less advertised feature is library for direct conversion between ANSI and HTML, which it does just fine:

@Grapes(
    @Grab(group='org.fusesource.jansi', module='jansi', version='1.11')
)

import org.fusesource.jansi.*

private String colorize(String text) throws IOException {
  new ByteArrayOutputStream().with {
    new HtmlAnsiOutputStream(it).with {
      write(text.getBytes("UTF-8"))
      close()
    }
    return new String(it.toByteArray(), "UTF-8");
  }
}

println colorize(" [47;32mhello world")

Thursday, September 5, 2013

Adding error messages to domain object in Grails

Sometimes it is more convenient to do some validation outside of the domain object, on the other side it is still nice to use standard error reporting facilities. For these cases, you can inject errors into domain objects like:

    if (someError) {
      domainobject.errors.rejectValue('param', "hasErrors")
    }
    ...
    if (!domainobject.hasErrors() && domainobject.save(flush: true)) {
    ...


Wednesday, August 28, 2013

Jenkins kills Tomcat or other Java process after job ends

Recently, I was adding new application deployment to Tomcat server that was started by Jenkins. Process was starting nicely, but was stopping at the end of the job. The only clue was log record:

Service exit with a return value of 143

This issue can be easily solved by adding -Xrs to JAVA_OPTS. Option description can be found at http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html.


Thursday, August 8, 2013

Running Selenium with Groovy on PhantomJS

There is script that runs and takes snapshot of PhantomJS with Selenium:


@Grab("com.github.detro.ghostdriver:phantomjsdriver:1.0.1")

import org.openqa.selenium.*
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.openqa.selenium.remote.DesiredCapabilities

new PhantomJSDriver(new DesiredCapabilities()).with {
    manage().window().setSize(new Dimension(1028, 768))
 get("http://www.google.com")
 getScreenshotAs(OutputType.FILE).renameTo("screenshot.png")
 quit()
}



Running Selenium with Groovy on Firefox

There is small script to run and take screenshot in Firefox with Selenium from Groovy:


@Grab("org.seleniumhq.selenium:selenium-firefox-driver:2.34.0")

import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.*
 
new FirefoxDriver().with {
 get("http://www.google.com")
 getScreenshotAs(OutputType.FILE).renameTo("screenshot.png")
 quit()
}


Wednesday, July 17, 2013

Setting PATH for Jenkins slave agent

There is a lot of variables and settings available for Jenkins slave agents, but for some reason nothing worked for PATH in my case. Finally, the only configuration that did the job, was to set PATH in user's home .bashrc (.profile, profile, .bash_profile, etc. was not working) on slave node, like:

  PATH=$PATH:/opt/phantomjs-1.9.1-linux-x86_64/bin/