External tool definitions for mRemoteNG

Here are some external tool definitions I have defined in mRemoteNG.

WinSCP

Display name

WinSCP (sftp)

Filename

C:\Program Files (x86)\WinSCP\WinSCP.exe

Arguments

sftp://%username%:%password%@%hostname%:%port%/

Ping

Display name

Ping

Filename

cmd

Arguments

/c ping -t %HostName%

Traceroute

Display name

Ping

Filename

cmd

Arguments

/c set /P = | tracert %HostName%

More can be found at the mRemoteNG forum.

If you haven’t tried mRemoteNG, you should. It’s very handy if you’re on Windows and need to connect to various servers through for example SSH and RDP. Sure beats plain PuTTY at least…

MediaWiki: Promote user to sysop when sysops are gone

Working for a client which has documentation on a MediaWiki installation which was set up before we got there. Was able to create users for ourselves through the regular site, but certain things like deleting pages can only be done by sysops. None of our newly registered users are of course sysops and the only existing sysop user is from whoever installed the wiki to begin with.

Here’s how to promote a user to sysop the hackish way, directly in the database.

Find database details

Note: This can of course be skipped if you already know how to access your mediawiki database, but I didn’t :)
  1. SSH into the system where MediaWiki is installed and find its location.
    Was /var/www/html/foowiki in our case.

  2. Open LocalSettings.php and everything should be under the heading ## Database settings

Promote user

  1. Connect with a mysql client.
    mysql -h localhost -u wikiuser -p wikidb
  2. Find the user id.
    SELECT user_id, CONVERT(user_name USING utf8) FROM `user`;
  3. Add user n to sysop (and bureaucrat) group.
    INSERT INTO `user_groups` VALUES (n, 'sysop'),(n, 'bureaucrat');

The user should now be sysop, which can be double checked on the Special:ListUsers wiki page.

Simple automated website deployment via GitHub

Here is a very simple way to add automatic deployment via GitHub. This assumes

  • You have shell access to your web host
  • Your website is in a repository on GitHub
  • CGI scripts are enabled for your website
  • That git (and optionally composer if you use that) is installed

Initial pull

Log on to your web host and do the initial pull.

$ cd path/to/website
$ git clone https://github.com/<you>/<website>.git .

Deployment script

Put the following script in a file named for example deploy.cgi in your website root.

#!/bin/bash
echo Content-type: text/plain
echo

export PATH=~/bin/:$PATH

git pull
composer.phar install
echo DONE
I’m on Dreamhost and the export line is so the cgi script will use the composer.phar and php symlink located in ~/bin. See this post for more info.

Add execute rights to the script.

$ chmod u+x deploy.cgi

You should now be able to visit http://<your-website>/deploy.cgi in your browser and see output like this:

Already up-to-date.
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating autoload files
DONE

Set up GitHub service hook

Go to https://github.com/<you>/<website>/settings/hooks, click on WebHook URLs and enter the full URL to your script, http://<your-website>/deploy.cgi

Test it out

With this set up you should be able to make a change at your dev machine, commit it and push it to GitHub. The script should then be executed shortly after and your website should be updated automatically. Pretty cool :)

This is of course ultra simple, and if this is a critical site you should probably add some security of some sort. A simple version could be to use a cryptic name like a guid for the script and of course not have this script checked in at GitHub. You might also perhaps want to only pull from a certain branch or things like that, but for a small simple site this works pretty well as long as you remember to only push to GitHub when you have a working set of commits ;)

Java: Simple check to see if a server is listening on a port

Here is a simple method to check if a server is listening on a certain port. I used it to ignore certain non-critical SFTP related integration tests in a project when I hadn’t bothered starting the SFTP server.

public static boolean serverListening(String host, int port)
{
    Socket s = null;
    try
    {
        s = new Socket(host, port);
        return true;
    }
    catch (Exception e)
    {
        return false;
    }
    finally
    {
        if(s != null)
            try {s.close();}
            catch(Exception e){}
    }
}

To ignore a test (assuming JUnit) you can then do the following:

@Test
public void some_test()
{
    assumeTrue(serverListening("localhost", 22));

    // Rest of test...
}

How to install PHPUnit on Windows

Note to self in case I need to do this again…

Commands assumes the PHP install is in C:\wamp\bin\php\php5.3.13 and that this path is already added to the PATH environment variable.

Install PEAR

  1. Download go-pear.phar and Save it as C:\wamp\bin\php\php5.3.13\pear\go-pear.phar.
  2. Open an elevated command prompt.
  3. Go to the PHP directory.
    > cd \wamp\bin\php\php5.3.13
  4. Install PEAR, pressing enter to accept defaults and Y to alter php.ini.
    > php .\pear\go-pear.phar
  5. Add environment variables.
    > start PEAR_ENV.reg

Install PhpUnit

  1. Make sure pear is fully up to date (should be if we just installed it)
    > pear upgrade-all
  2. Add pear channels.
    > pear channel-discover components.ez.no
    > pear channel-discover pear.phpunit.de
    > pear channel-discover pear.symfony.com
  3. Install PHPUnit with all dependencies.
    > pear install --alldeps phpunit/PHPUnit
  4. Verify installation by checking version.
    > phpunit --version

Quick’ish way to swap between Java versions on Windows

Here’s a sort of quick way to swap between Java versions on Windows without having to change any environment variables and restarting your console window and such. The short version is to create symlinks to your java versions and then a single symlink called for example ‘active’ which points to the one you want. In your environment variables java_home and path you’d then point to this ‘active’ symlink instead of the actual installation. You can then fairly quickly swap out the target of the ‘active’ symlink and, tadaa’ish, you have a different version.

Setup

  1. Open up an elevated command prompt.
  2. Go/Make somewhere you want this.
    > c:
    > mkdir \dev\java
    > cd \dev\java
  3. Set up the symlinks
    > mklink /d 1.5 "c:\Program Files\Java\jdk1.5.0_22"
    > mklink /d 1.7 "c:\Program Files\Java\jdk1.7.0"
    > mklink /d 1.6 "c:\Program Files\Java\jdk1.6.0_27"
    > mklink /d active 1.7
  4. Set your JAVA_HOME and PATH environment variables. *
    setx JAVA_HOME "C:\dev\java\active" /m
    setx PATH "%JAVA_HOME%\bin;%PATH%" /m
* The second command assumes you don’t have Java in your PATH already. If you do, you should edit it the usual way instead. Also note that if JAVA_HOME is already set, it will be expanded in the second command.

Now open up a new regular command prompt and run the following.

> java -version
> mvn --version

Both (skip mvn if you don’t have maven installed) should report Java version 1.7.

Swapping

So, let’s say we want to change to java 1.5, we just need to run the following in an elevated command prompt.

> cd \dev\java
> rmdir active && mklink /d active 1.5

If you now repeat the version checks we did in our regular command prompt they should both report version 1.5 instead of 1.7. And we didn’t even have to restart the command prompt.

Shortcut

To prevent us from having to do this manually we could also create a simple bat file. I made one I called swap.bat which I put in the same directory as the symlinks with the following contents.

@echo off
cd \dev\java && rmdir active && mklink /d active %~1

You could then create a short cut to for example c:\dev\java\swap.bat 1.5, set it to run as administrator, and you’d have a two click solution to change the Java version to 1.5. I created one shortcut for each version.

If you have a better way, please leave a comment though. Always on the lookout for things and techniques that can make my developer life simpler :)

How to install software on SunOS

Solaris is a major pain and unfortunately several servers I have to deal with runs on it. Why is it a pain? Because for some reason it seems all Solaris machines are set up differently. Different sets of installed binaries, different places things are configured, and so on. Probably all caused by lazy sysadmins and/or developers.

Anyways, today I found out that curl wasn’t installed in a server where i needed to test a SOAP request. After some hours of digging I finally managed to get a working curl installed, and figured I should document the procedure here in case I need it again, which I probably will…

Continue reading