Tuesday, August 20, 2013

Setting env variables in linux just for single command

Use this pattern:
FOOO=bar bash -c 'echo $FOOO'

Some working and non-working examples:

$ FOOO=bar bash -c 'echo $FOOO'
$ echo $FOOO

$ FOOO=bar echo $FOOO

Thursday, August 15, 2013

Jenkins + Multiple SCMs + git + hooks/triggers = mission Possible

Multiple SCMs plugin version 0.2 - in its documentation is remarked two controversial statements:
  • "Post-commit type triggers don't currently work".
  • "Allow polling to work with multiple instances" - in version 0.2
So is pooling possible? There are peaople on the internet stating the two opposite points. Yes, it is possible but some tricks are needed.
  • for every GIT repo, defined in Multiple SCM plugin config, an "Unique SCM name (optional)" is needed. 
  • check the version of your GIT plugin (Multiple SCM plugin uses GIT plugin). When my git plugin version was 1.1.x, the pooling did not work, and updating to git plugin 1.4 solved my issue.

Additional data:

Friday, August 9, 2013

linux: demonizing a process and killing it when you want

This is very useful for processes which do not write an PID file. This is extreme stupidness... but it could be work-arounded.

For example I want to start ant kill later the jasmine server, which is started this way:
bundle exec rake jasmine:server --trace

The whole batch script will be:
[ -f tmp/jasmine.pid ] && kill -9 `cat tmp/jasmine.pid` && rm tmp/jasmine.pid
bundle exec rake jasmine:server --trace &
echo $! > tmp/jasmine.pid
sleep 20
bundle exec rake jasmine:run
kill -9 `cat tmp/jasmine.pid`

rm tmp/jasmine.pid

My strings:
tmp/jasmine.pid - my pid file
bundle exec rake jasmine:server --trace - command to be deamonized
sleep 20 - this ensures time for the server to start
bundle exec rake jasmine:run - my task to be executed on the demonised process

Other information:
[-f file] && command - executes command if file exists
"echo $!" prints the pid of the last command in current console.

magic apostrophes in linux = Back Quotes for Command Substitution

They are replaced by the output of the command between them:

kill -9 `echo 9999`

is equivalent to:

kill -9 9999

killing app, listening on a defined port in single line

In Linux this will kill the process, listening on port 8888:

ps aux | grep port=8888 | awk '{print $2}' | tail -n 1 | xargs kill -9

Tuesday, July 23, 2013

Appending HTML file as e-mail in Jenkins/Hudson

You need "Jenkins Email Extension Plugin", which could be installed from Jenkins/Hudson available plugins. Restart is needed.
 You need to fill "Project Recipient List, "Pre-send Script" and correct triggers.

"Pre-send Script"have to be something like this:

def reportPath = build.getWorkspace().child("test.html")
msg.setContent(reportPath.readToString(), "text/html");

Thursday, June 27, 2013

Propagating java system properties to maven project code - issue solved

Issue description: I have got Hudson/Jenkins project. It is build via maven. Maven "Goals and options" is set to
test -DxxxApiConfigFile=config-xxxApi-client-jenkins

Starting console command "mvn test -DxxxApiConfigFile=config-xxxApi-client-jenkins" behaves correctly - the system property is set. But running

Solution: Maven field "Goals and options" has to be set to:
-DargLine="-DxxxApiConfigFile=config-xxxApi-client-jenkins" clean test

or even better:
-DxxxApiConfigFile=config-xxxApi-client-jenkins -DargLine="-DxxxApiConfigFile=config-xxxApi-client-jenkins" test


Monday, June 24, 2013

Redmine wiki - how-to

Here is a list of tags used in RedMine Wiki.

Source for most of this: http://www.redmine.org/projects/redmine/wiki/RedmineTextFormatting

<notextile>**</notextile> - do not process the string inside

h1. Heading

h2. Subheading

h3. Subheading

---- Horizontal rule

Tuesday, June 18, 2013

Examples of permutations in Ruby

Permutations in arrays:
["1","2","3"].permutation.map { |a| a.join }
 => ["123", "132", "213", "231", "312", "321"] 

Permutations in stripped string:
"1 2 3".split(" ").permutation.map { |a| a.join }
 => ["123", "132", "213", "231", "312", "321"]

Permutations in hashtables:

x = {"2"=>"twenty","3"=>"thirty","4"=>"forty"}
y = {"1"=>"one", "2"=>"two",  "3"=>"tree"}
z = Hash.new

x.each_pair do |key1,value1|
  y.each_pair do |key2,value2|
    z[ (key1 + key2) ] = (value1+" "+value2)

z.map{|k,v| "#{k}=#{v}"}.join(';  ')

 => "33=thirty tree;  22=twenty two;  23=twenty tree;  41=forty one;  42=forty two;  31=thirty one;  43=forty tree;  32=thirty two;  21=twenty one" 

Monday, June 3, 2013

Качване на нови карти на китайски GPS със софтуер iGO / Windows Mobile

Oот главното меню превърташ иконите надясно и избираш USB Option
Трябва да е активирано Mass Storage. Ако не е го правиш и даваш опция Запиши която е горе вдясно.
Това са ти картите:
Четеш инструкциите и следваш каквото пише там.

Wednesday, May 29, 2013

ruby: skipping elements in iteration cycle

Lets we have this situation:

[7, 2, 3, "a", "b", "c"].sort
ArgumentError: comparison of Fixnum with String failed
from (irb):1:in `sort'
from (irb):1

We want to sort only integer values.

The solution is using reject statement:

[7, 2, 3, "a", "b", "c"].reject{ |x| x.to_i == 0 }.sort
 => [2, 3, 7]

Sunday, May 26, 2013

respond_to? and global methods in Ruby

Lets have an method defined outside of any class:
def my_method 
    "my Method is executed" 

Checking its persistence with respond_to? seems wrong:
respond_to? :my_method => false

The solution to that issue is that the syntax of respond_to? for global methods is different (and absolutely no logical to me). Global methods are presented as private methods of Object class. So the correct verification is:

Object.respond_to?( :my_method, true )

Monday, May 20, 2013

'resque <Exception>' syntax not working in ruby - solved

ensure keyword does, but resque not.

The solution: The correct keyword is rescue not resque. In this case the exception is thrown before go through  the "undefined method" resque.

Monday, May 13, 2013

Java application as a daemon - initd start/stop template file

Your Java application is a daemon and you want to stop it more gracefully than "killall java".

The example below is what you need:

# init script for a Java application

# Check the application status
# This function checks if the application is running
check_status() {

  # Running ps with some arguments to check if the PID exists
  # -C : specifies the command name
  # -o : determines how columns must be displayed
  # h : hides the data header
  s=`ps -C 'java -jar /path/to/application.jar' -o pid h`

  # If somethig was returned by the ps command, this function returns the PID
  if [ $s ] ; then
    return $s

  # In any another case, return 0
  return 0


# Starts the application
start() {

Monday, April 29, 2013

GIT: Setting Up a Shared Repository

Shell commands for creating new git shared project:
$ mkdir /pub/my-repo.git
$ cd /pub/my-repo.git
$ git --bare init --shared
$ git --bare fetch /home/alice/myproject master:master
$ chgrp -R $group /pub/my-repo.git

Wednesday, April 24, 2013

Importing constants & methods from other java class

It a nice syntax feature:

Importing static variables:

import static com.xxxx.yyy.className.variableName;

Importing static methods:

import static org.junit.Assert.*;

Tuesday, April 23, 2013

Simulate user browser behaviour in java - forms

There are two major approaches:

  • use a browser library. There you define all actions you want to do.
  • parse the browser response manually and fill the forms by on low-level.

Saturday, April 20, 2013

Windcards in certificate "Common Name"

Nice feature of X.509 certificates are wind cards. For example:

Common name = *.example.com

Friday, April 19, 2013

How to become a Certified Ethical Hacker


TODO: add ethical hacker tools used

Installing root and intermediate certificates in java keystore

Issue faced:
java software could not validate that the server certificate is correct. This exception is thrown:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target

Issue in deep:
Default java validation mechanism:

Friday, April 12, 2013

HTTP Basic Authentication in Java - two different ways

We will see two different ways of authentication with java and . The connection is implemented with standard Sun/Oracle class java.net.HttpURLConnection.

Solution 1: the more popular way, with Authenticator

            Authenticator.setDefault (new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    log.info( String.format("Getting PasswordAuthentication with username %s and pass with lenght = %d", 

                    return new PasswordAuthentication ( 

Solution 2: the direct authentication injection, according http-basic standard:

import javax.xml.bind.DatatypeConverter; // standard class for Java 1.6, no additional jar needed

            String encryptedUserPass = DatatypeConverter.printBase64Binary( 
                        ( userStr +":" + passStr ).getBytes()
            connection.setRequestProperty  ("Authorization", "Basic " + encryptedUserPass );

Why the popular way is not fine?

  • it uses same authenticator for all connection. In multitheaded and multi-connectioned applications it could cause problems - same authentication for different servers. I could not found a way of setting an Authenticatotor just for my connection.
  • two requests for each my request. The first is initialised with no authentication, and when server says that it needs, the java library sends the second one, with user+pass; For debugging purposes it is not good.

Certificates with multiple valid hosts

The standard SSL certificate applies only to server in its CN (common name) field.

There is an extension Subject Alternative Name (, which allows the certificate to be valid for more than one host. It cold be very helpful in such complex server environments.

Note that the client software have to be aware of the Subject Alternative Name (, otherwise it would identify it as invalid. New browsers seem to be compatible.

Wednesday, April 10, 2013

Some git workflow

There are local commits.

git clone
Gets a Git repository from a remote source (makes a copy), so you could pull and push later.

Example with clones the repo with submodules:
git clone --recursive git://github.com/foo/bar.git
Example, alternatively cloning submodules later this way:
git clone git://github.com/foo/bar.git
cd bar
git submodule init
git submodule update
git clone git@github.com:user/test.git

git identity - user name and email:
git config --global user.name "Alexander Tomov"
git config --global user.email tomov@example.com
git show url
git config --get remote.origin.url 

git log origin/<branch>..HEAD
 - shows local commits

git diff origin/<branch>..HEAD
 - shows diff of local commits

git push

error: failed to push some refs to 'x'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.

git pull --rebase

git status
Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Example output:
# On branch xxxx
# Your branch is ahead of 'origin/xxxx' by 2 commits.

git push

git status
# On branch xxxx
# Changes not staged for commit:

Some important commands:

git branch -a
#    list remote branches

git pull --recurse-submodules
#    update all submodules (newer git clients)

git submodule foreach pull
#    update all submodules (for older git clients)

git add
Adds files changes in your working directory to your index.

Example: git add .

git rm
Removes files from your index and your working directory so they will not be tracked.

Example: git rm filename

git commit
Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit.

Examples: git commit -m ‘committing added changes’
git commit -a -m ‘committing all changes, equals to git add and git commit’

git commit --amend
It does a fast-fix for your last commit. In other words it joins your current commit with your last commit.

git merge
Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.

Example: git merge newbranchversion

git reset
Resets your index and working directory to the state of your last commit.

Example: git reset --hard HEAD

git stash
Temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.

git stash
Saved working directory and index state "WIP on master: 84f241e first commit"
HEAD is now at 84f241e first commit
(To restore them type "git stash apply")

git stash apply
Revert the changes made by "git stash"

git fetch
Fetches all the objects from the remote repository that are not present in the local one.

Example: git fetch origin

git log
Shows a listing of commits on a branch including the corresponding details.

Example: git log
commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
Author: User <user@domain.com>
Date:   Mon May 3 09:24:05 2010 +0300

    first commit

git show
git show <commit_id>
Shows information about a git object.

Example: git show
commit 84e341e8a0d768fb37ff7ad40e294b61a99a0abe
Author: User <user@domain.com>
Date:   Mon May 3 09:24:05 2010 +0300

    first commit

diff --git a/README b/README
new file mode 100644
index 0000000..e69de29

Example: git show 84e341e8a0d768fb37ff7ad40e294b61a99a0abeq
Applies diff on the commit

git grep
Lets you search through your trees of content for words and phrases.

Example: git grep "www.siteground.com" -- *.php

git diff
Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.

Example: git diff
Example: git diff path/to/file.txt

git config -l
Show git config, my identity information (name, email) and remote repository info (url)

git checkout
Discard changes

Example: git checkout file.txt

To reset one file in your working directory to its committed state:

Example: git reset --hard
To reset every file in your working directory to its committed state:

git mv old_name.txt new_name.txt

Rename a file

git update-index --assume-unchanged

To temporarily ignore changes in a certain file, run:
git update-index --assume-unchanged <file>

Then when you want to track changes again:
git update-index --no-assume-unchanged <file>

Reverting to previous commit
git checkout 0923d7fc32...

Where HEX id is the hash value from git status. Additional info and workflows could be found in that thread.

Switching to another branch

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master

$ git checkout -b branch_to_switch remotes/origin/branch_to_switch

git checkout -b develop remotes/origin/develop

Showing only file names in commits
git log --name-only


Delete a Git tag
There are separate commands for deleting remote and local tags:
git push --delete origin tag_name
git branch -d the_local_branch

Show tags
git tag

Branches 'develop' and 'origin/develop' have diverged.
And branch 'develop' may be fast-forwarded.
git rebase origin/develop
Async-ed branch:
Symptom of async-ed develop branch:
# git remote show origin

  Local branch configured for 'git pull':
    master merges with remote master
(no develop here)
  Local refs configured for 'git push':

git branch --set-upstream develop origin/develop

git status
# On branch develop
# Your branch is ahead of 'origin/develop' by 1 commit.

# git remote show origin
  Local branches configured for 'git pull':
    develop merges with remote develop
    master  merges with remote master
# git checkout develop

Nice links:

Example workflow:

On undoing, fixing, or removing commits in git:

Tuesday, April 9, 2013

Linux command "find" in examples

find . -name *.rb
- search for all files with extension rb from current folder on.

Friday, April 5, 2013

Parsing XSD with floating elements

There is a way to create valid XSD definition with different order of the elements. This is important when the other party pulls text with different tag order or there are variantions of the received content.

The solution is <xsd:all> tag, described here. StackOverflow gives more practical problem, being solved by <xs:all>.

Monday, March 25, 2013

Useful maven targets

Kickstart of maven commands.

mvn compile
compiles the project

mvn clean compile
cleans and compiles the project. List of targets is supported as arguments to maven

mvn jar:jar
makes jar file in target folder

mvn assembly:assembly
builds the project, run project tests and makes a jar with dependancies. Assembly module is needed.

mvn assembly:single
builds the project, skip project tests and makes a jar with dependanciesAssembly module is needed.

mvn tests
run project tests

mvn test -Dtest=SaleTest
run specific test. No need path to be specified. No need extension to be specified (SaleTest, not SaleTest.java)

mvn install
installs current project in the repository, so other projects could depend on it

mvn install -DskipTests

installs current project in the repository, so other projects could depend on it; skip executing tests, which could prevent installation of project (the artefact)

mvn jetty:run
Runs the project in jetty web server - for web applications


Sunday, January 27, 2013

Disable message "Windows detected a hard disk problem"

It happens in Windows 7. The current PC configuration has HDD S.M.A.R.T. problem but has worked with it for more that year. Windows XP has not such disgnostics, nut Windows 7 detects the broken state and notifies me on a few hours. This is very irritating.

I found the solution of the problem:
1. Please open gpedit.msc. Navigate to Computer Configuration\Administrative Templates\System\Troubleshooting and Diagnostics\Disk Diagnostic\
2. Double Click on “Disk diagnostic: configure execution level”, set it to “disabled”. “Not configured” is enabled by default.

The full text of the error message is:
Backup your files immediately to prevent information loss, and then contact the computer manufacturer to determine if you need to repair or replace the disk.

Tuesday, January 1, 2013

maven - Uncompilable source code error

Caused by: java.lang.RuntimeException: Uncompilable source code - package xxxxxxx does not exist.

Solved: The error went away after mvn clean. Perfect time killer.

I encounter the error only in test maven environment.