29. June 2013

True/False gotcha Python vs Ruby vs PowerShell vs NodeJS vs PHP

Evaluation of conditions is very important part programming. True is ture, false is false…

Or not? It might surprise you, but not all languages evaluate numbers, empty strings or empty arrays in the same way.

Let’s examine several technologies. Python, Ruby, PowerShell, NodeJS and PHP.

Here is small code written in Python:

def test(value):
    if value:
        print "True"
    else:
        print "False"

test(0)
test(1)
test("")
test([])
test({})

Here is similar code written in Ruby:

def test(value)
    if value
        print "True"
    else
        print "False"
    end
end

test(0)
test(1)
test("")
test([])
test({})

Let’s compare it to PowerShell:

Function Test($value) {
    if ($value) {
        Write-Host "True"
    } else {
        Write-Host "False"
    }
}

Test(0)
Test(1)
Test("")
Test(@())
Test(@{})

Same logic in JavaScript for NodeJS:

function test(value) {
    if (value) {
        console.log("True");
    } else {
        console.log("False");
    }
}

test(0);
test(1);
test("");
test([]);
test({});

Code in PHP:

<?php 
function test($value) {
    if ($value) {
        echo "True";
    } else {
        echo "False";
    }
}

test(0);
test(1);
test("");
test(array());
test(array());
?>

Results are little bit surprising:

 

Python Ruby PowerShell NodeJS PHP
0 False True False  False False
1 True True True True True
“” False True False False False
[] False True True  True False
{} False True True  True False

1. June 2013

GitLab: Could not read from remote repository

When you try to push to GitLab you may end up with following error message:

fatal: Could not read from remote repository.
Please make sure you have the correct access rights
 and the repository exists.

It’s not very clear what’s real cause. You may check the permission, but it might not solve the problem.

One quick solution could be just restart of GitLab service. It still might not help.

Let’s learn some mechanics of GitLab.

You send data via ssh to GitLab server. When authentication by key is succesfull then server will invoke gitlab-shell. This will shell send request to web interface of GitLab. Yes, web interface. Then it will allow you to access git repository.

Schema:

git -> ssh -> sshd -> gitlab-shell -> gitlab web

The problem described in the beginning of this article is between gitlab-shell and gitlab-web. Most likely shell is not able to access gitlab web. URL is broken or host configuration is incorrect or port is not reachable.

Just go to gitlab-shell project and open file config.yml. You’ll see something like this:

gitlab_url: "http://localhost:80/"

Test this URL directly on server e.g. by links:

links http://localhost:80/

You may need to change hostname or port or add base directory to URL. Fix the URL so the gitlab-shell is able to access web interface. Restart gitlab service and try to push again.

Further discussion about this and similar problems is available at github.

1. June 2013

Tomcat installed as Windows service doesn’t create log files

I was chasing one very insidious bug. Tomcat installed as Windows service was not creating logs. The only log produced by Tomcat was stdout and stderr from procrun wrapper.

It was very weird. Tomcat downloaded from Apache’s website was creating logs without problem. There was no difference between directories of problematic Tomcat and working Tomcat.

Ok, let’s cut long story short. After several attempts to locate the bug I realized that Tomcat started by startup.bat was working correctly.

The only difference was in the Tomcat’s start method.

It was necessary to open Tomcat service properties (ES stands for Edit Service):

tomcat7w.exe \\ES\\tomcatweb

The tricky part here was not to check the Logging tab. This issue had nothing to do with stuff displayed in Logging tab. It was necessary to open Java tab.

When I compared working service and Tomcat service without logging I found that following lines were missing:

-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=C:\projects\apache-tomcat-7.0.40\conf\logging.properties

tomcat-edit-windows-service

Somebody who was registering the service just omitted those lines when overriding –JvmOptions. It was sufficient to add java.util.logging.manager and config.file. Restart service and Tomcat was logging without problem.

29. May 2013

Solution: KVM guest IO very slow

One problem of virtual server based on qemu KVM is that IO operations are slow in some cases.

The most common reason is that virtual disks are stored on RAID and virtual is using default HDD configuration.

It is recommended to turn off cache and set io operations to native:

<driver name='qemu' type='raw' cache='none' io='native'/>

There is very good article at serverfault. You can find there further explanations.

26. May 2013

How to activate Python virtual environment in PowerShell

It’s quite easy to set up virtual a environment with isolated packages for Python in PowerShell. There are several methods

Method 1. Pipenv

This is the youngest and most convenient method. Pipenv automatically manages the environment and can create virtualenv based on the working directory. Files with virtualenv are stored in a separate directory and the user does not need to worry about them.

Install pipenv package:

python3 -m pip install pipenv

Enter the directory where you’d like to work and type:

pipenv shell

Pipenv will bootstrap and activate the environment. It will launch subshell. If you’d like to leave the environment simply close the shell by command:

exit

Method 2. Using embedded venv

Python 3 contains package venv. It’s often confused with virtualenv which is a standalone package to manage virtual environments. The advantage of the command is that you do not need to install additional packages. Just create a virtual environment in the directory like pyenv:

python3 -m pyenv 

Activate the environment:

.\pyenv\Scripts\activate.ps1

To deactivate the environment simply call:

deactivate

Method 3. Good old virtualenv

Creating a virtual environment with virtualenv is an old method. You need to create a directory that will contain the virtual environment and then activate it. virtualenv is not part of the default Python package, it’s necessary to install it:

python3 -m pip install virtualenv

Now it’s possible to create a new virtualenv in a directory like pyenv by the following command:

virtualenv pyenv

The last step is to activate this environment in PowerShell :

.\pyenv\Scripts\activate.ps1

You should see the name of your virtual environment in the command line. The virtual environment is active.

To deactivate the environment simply call:

deactivate