SaltStack HowTo: Installing, Setting up and Provisioning

The problem and the solution

I work at a company that creates web sites and applications. I joined them in 2008 when there were 2 front-end developers and 3 back-end, including me. Company slowly grew to 7 back-end and 6 front-end developers. The following problem started to get in our way more and more: we were working on one single development server, which means that from time to time we would step on each other toes. What happened is, one guy would open a file, the other guy would open the same file and whoever saves and closes first, is a loser. Also, that server had to have every possible service, program and PHP extension imaginable because it needed to support a lot of diverse projects.

Continue reading “SaltStack HowTo: Installing, Setting up and Provisioning” »

Using Google API to get Google Analytics data with PHP

You might have a need to display data from Google Analytics on your website like number of product page views in your webshop CMS. To achieve that, you would request the page views and maybe sessions (unique visits) for a certain URL from Google Analytics. If you’re using PHP, Google created Google API PHP client to help you do that. In this tutorial we’re going to use Server to Server Application authentication mechanism called two-legged OAuth.

Continue reading “Using Google API to get Google Analytics data with PHP” »

Importing Amazon Kindle Paperwhite to Croatia

Every year I try to surprise my wife with some extraordinary birthday gift. Last year I bought her a nice looking designer dress, an e-book and ordered the Kindle so she can read the Ender’s Game once more. I opted for the newest Kindle, the Paperwhite one. So, ordering this kind of stuff to Croatia is not that easy. We did enter the EU, but for some reason I couldn’t have ordered it from UK, Germany or Italy. Go figure.

As I was a bit late with organizing the delivery, I didn’t have the time to go through 4 people to deliver Kindle like it is a reactor-grade plutonium. So it had to be the States. I placed the order on Wednesday, got charged $160 + $20 shipping. Amazon said that it would take five days to deliver. Two days later, I got an email from local DHL that the package arrived and that I should reply to them with my EORI number.

What is an EORI number?

An EORI number is a unique number throughout the European Community, assigned by a Customs Authority in a Member State to operators (businesses) or persons Economic Operator (EO). By registering for Customs Purposes in one Member State, an EO is able to obtain an EORI number which is valid throughout the European Community. The EO will then use this number in all communications with any EC Customs Authorities for appropriate customs declarations.

Ok, time to find a customs office and request the number. Things in Croatia work a bit different than in more western parts of the world. Namely, to request the EORI number you have to fill a form. And send it via mail. Snail mail. The mail that travels three days from the post office to its destination 6 km away. The mail that my Kindle, which is freezing in some cargo area, derides, having traveled from Nevada over Frankfurt to Zagreb in only two days.

Customs receive my request next Wednesday, and I wait a day to get my EORI number. Needless to say, it is Thursday, my wifes birthday, and all she got was an e-book she can’t read :). Now, EORI requests usually return the same way, via snail mail, but I decide to be assertive and hurry the procedure. Later on I realize that the EORI number is just your PIN prefixed with the country code, and all they needed to do is an INSERT statement to The Great Database Of People That Import Stuff™. Great. Now to pay the cargo “handling”.

DHL, after receiving my EORI number, charges me with $57 (ouch) and delivers Kindle on Friday.

Reading Kindle is great, wife and I share it, buying books is fun and less expensive (money- and space-wise) and I don’t regret one buck spent! Just, what sould I do with this EORI now :)

Quick File Opening in NetBeans

NetBeans is an irreplaceable tool for my every day use. Yes, it’s written in boring, slow Java, but the feature set is great and it helps me to get my tasks done without getting in my way. There are some things that could be better or more responsive. One of those things are code scanning and opening files. I like how Sublime Text 2 does the file open (Go to Anything), and this is how to achieve this in NetBeans.

First, without any plugins, you can type ALT + SHIFT + O which brings up the Go to File dialogue. It is ok, but it doesn’t provide the fuzzy search (you can emulate it by putting * between letters). To achieve fuzzy searching, we need the help of a plug-in called Open File Fast. Plug-in is last reported to run with NetBeans 6.9, but I’m running it with latest, 7.2. To install and configure it, follow these steps:

  • Go to http://plugins.netbeans.org/plugin/16495/open-file-fast and download for the latest version
  • In NetBeans, go to Tools → Plugins → Downloaded → Add Plugins, select the downloaded file and click Install (http://wiki.netbeans.org/FaqPluginInstall)
  • Restart NetBeans (although it is not needed to run the plug-in, I found that I couldn’t set key short cut for Open File Fast)
  • Now for the short cut. Go to Tools → Options → Keymap, search for “open file fast” → Assign short cut
I’ve assigned it CTRL + P. It still isn’t near as fast as Sublime Text, but it gets the job done.

Doctrine2 CLI under Silex application

I got it running with Doctrine 2.3.1-DEV. Get doctrine provider using composer:

{
    "minimum-stability": "dev",
    "require": {
        "silex/silex": "1.0.*",
        "taluu/doctrine-orm-provider" : "*",
    },
 
    "autoload": {
        "psr-0": { "Entity": "app/" }
    }
}

Create doctrine.php and put the following content inside. I like to put it in bin/doctrine.php:

#!/usr/bin/env php5
<?php
// Load your bootstrap or instantiate application and setup your config here
 
require_once APP_ROOT .'/vendor/autoload.php';
 
$app        = new Silex\Application();
 
// Doctrine DBAL
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => $config['db']
));
 
// Doctrine ORM, I like the defaults, so I've only modified the proxies dir and entities path / namespace
$app->register(new Nutwerk\Provider\DoctrineORMServiceProvider(), array(
    'db.orm.entities'              => array(array(
        'type'      => 'annotation',
        'path'      => APP_PATH .'/Entity',
        'namespace' => 'Entity',
    )),
    'db.orm.proxies_dir'           => APP_ROOT .'/var/cache/doctrine/Proxy',
));
 
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Symfony\Component\Console\Helper\HelperSet;
 
$helperSet = new HelperSet(array(
    'db' => new ConnectionHelper($app['db.orm.em']->getConnection()),
    'em' => new EntityManagerHelper($app['db.orm.em'])
));
 
ConsoleRunner::run($helperSet);

And that’s it :)

Pretty HTML5 multiple file upload with Bootstrap, jQuery, Twig and Silex

There are a number of ways to achieve multiple file upload functionality, but I like HTML5 way of doing it, and it will be supported across all major browsers when IE10 ships. Also, Twitters’ Bootstrap helped me achieve the look without problems. I used a bit of jQuery for help with events. Alongside vanilla html, I will put Twig form syntax to achieve this, together with Symfony2 Form component, for server side.

Continue reading “Pretty HTML5 multiple file upload with Bootstrap, jQuery, Twig and Silex” »

Linux and accessing the Windows shares

I had the problem of accessing the windows shares on my network, I’m using Ubuntu and getting inside the workgroup directory didn’t show any machines. So, I’ve found the solution which is pretty simple:

1. Samba config

# edit /etc/samba/smb.cnf
# set the name of your work group (although, I have 2 different work 
# groups in my network, and I can access them both after this configs)
workgroup = WORKGROUP
 
# setup the netbios name. Use whatever name your machine is called. 
# From samba.org: This sets the NetBIOS name by which a Samba server is known. 
# By default it is the same as the first component of the host's DNS name. 
# If a machine is a browse server or logon server this name (or the first 
# component of the hosts DNS name) will be the name that these services are 
# advertised under.
netbios name = ubuntu
 
# Reorder the naming services
name resolve order = lmhosts wins bcast host6

2. Add wins to Name Service Switch config

# edit /etc/nsswitch.conf or wherever NSS stores its config
 
# Add wins to hosts:
hosts:          files mdns4_minimal [NOTFOUND=return] wins dns mdns4

3. Restart Samba

# /etc/init.d/smbd restart

And that’s it, you should be able to access all of the shares across all of the workgroups.

Development environment

This is a quick post on how to set up your own DNS server with custom TLDso you can easily and more quickly get started on your next project. I am doing my programming on the Linux machine (Ubuntu to be Precise :)). The idea behind this set-up is to evade the need to ever modify your /etc/hosts file. Also, there is a possibility to even skip the creating of Apache VirtualHost directive and restarting the web server. Onward with the How-To.

Disclaimers:

  • I use Ubuntu, so substitute apt-get with yum or what ever you use
  • Anywhere you see the IP 192.168.1.253, replace with your own
  • I haven’t set up any forwarders in named.conf.options

Install and configure DNS (BIND9)

sudo apt-get install bind9

Edit these files

/etc/bind/named.conf.local:

zone "dev" {
    type master;
    file "/etc/bind/db.dev";
};

zone "1.168.192.in-addr.arpa" {
    type master;
    file "/etc/bind/db.192.168.1";
};

/etc/bind/db.dev

$TTL	604800
@		IN		SOA		dev. root.dev. (
	             2012042301		; Serial
			 604800		; Refresh
			  86400		; Retry
			2419200		; Expire
			 604800 )	; Negative Cache TTL
;
@		IN		NS	dev.
@		IN		A	192.168.1.253
*.dev.	14400 	IN 		A	192.168.1.253

/etc/bind/db.192.168.1:

$TTL	604800
@		IN		SOA		dev. root.dev. (
	     2012042301		; Serial
			 604800		; Refresh
			  86400		; Retry
			2419200		; Expire
			 604800 )	; Negative Cache TTL
;
@		IN		NS		dev.
253		IN 		PTR		dev.

Be careful to replace 253 in your files for your own last IP octet. Also, the filename should reflect your IP.

DNS servers setup…

Ok, now that we got this set up, we need to tell our system to use the local DNS server before going for the ISP and beyond. To achieve this, use Networking manager in Ubuntu, here’s how mine looks like. The final goal is for the /etc/resolv.conf too read: nameserver 127.0.0.1.

…and finishing up

Now that everything is set up, restart bind:

sudo /etc/init.d/bind9 restart

Test your setup by pinging anything.dev. If you get the response from your server, all is working great.

Apache Virtual Document Root

If your projects have similar / identical directory structure (i.e. public directory for publicly available files) than you can go a step further and setup the Apache Virtual Document Root. In doing so, you will be able to create a new directory in your projects root and have it magically turned up by calling http://newdirectory.dev.

<IfModule vhost_alias_module>
    <VirtualHost *>
        UseCanonicalName Off
        VirtualDocumentRoot "/path/to/projects/%1/public"

        ServerName projects.dev
        ServerAlias *.dev

        SetEnv APPLICATION_ENV development
    </VirtualHost>
</IfModule>

# Enable mod_vhost_alias apache module
sudo a2enmod vhost_alias
# Restart server
sudo /etc/init.d/apache2 restart

I don’t have this enabled for myself, but it does work, although not well tested. For further info on this topic, check the following links:

P.S. Yes, I got carried away while creating the featured image :)