Programming and Exciting Things

Start nodejs app on server boot

Published on 19.07.2014

I have one simple nodejs application which I want to start automatic when my VPS finish boot.
My server run Ubuntu's latest distribution which have a very decent version of upstart. Writing your own upstart scripts is very easy task, just copy and modify code bellow in

/etc/init/yourprogram.conf

description "NodeJs Server"
author      "yuks"

start on started mountall
stop on shutdown
respawn
respawn limit 99 5

script
    export HOME="/root"
    exec nodejs /projects/nodejs/public/app.js >> /var/log/node.log 2>&1
end script

Main part of this .conf file is at
 exec nodejs /projects/nodejs/public/app.js >> /var/log/node.log 2>&1 
just modify the
/projects/nodejs/public/app.js
with your nodejs app.
Re/Start of this daemon is ridiculusly easy:
start yourprogram
stop yourprogram
restart yourprogram
and of course this will start automaticly on boot time. Also we can view the status of daemon with this simple command:
user@nodejs:~# initctl status node-app
node-app start/running, process 339

More information about NodeJs can be found at http://nodejs.org/#about

Style Dialog in Android

Published on 14.06.2014

The one of the easiest ways to create custom styled Dialog in Android is to hide title and set xml layout in content. For example:

final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_select_action_new);
dialog.show();
requestWindowFeature
method is need to be set before
setContentView
And in
dialog_select_action_new.xml
file we have freedom to make any modifications, just like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f7fa"
    android:minWidth="350dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#97c7dc"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/add_event_title"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f0f7fa"
        android:orientation="vertical"
        android:padding="10dp" >

        <Button
            android:id="@+id/dialog_select_fun_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_add_fun"
            android:drawableLeft="@drawable/fun_icon"
            android:drawablePadding="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="@string/add_event_fun_btn"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/dialog_select_ill_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/btn_add_fun"
            android:drawableLeft="@drawable/baby_health_icon"
            android:drawablePadding="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="@string/add_event_fun_ill"
            android:textColor="#ffffff" />
    </LinearLayout>
</LinearLayout>

Final result is:

ps: The big plus from that is on android 2.x devices dialog looks like android 4.x version.

Autoloading Pagent keys on startup

Published on 13.06.2014

I have some private keys which I use to connect to few remote servers. In Windows managing SSH keys with Pagent is easy but loading a few keys everytime after reboot is annoying task. For luck Pagent has ability to load multiple ssh keys when it start up. The easiest method is to make shortcut in Startup program directory to Pagent with some additional params. Just like this:
Pagent shortcut
The goal is to edit "Target" field with path to our private keys, ex:

Target: D:\SSH\pageant.exe d:\SSH\private.ppk d:\SSH\deployment_keys\private.ppk
.
Also we can start Pagent from windows -> start -> run dialog with same params, ex:
D:\SSH\pageant.exe d:\SSH\private.ppk d:\SSH\0config\private.ppk

Export / Backup mongo db to cloud storage

Published on 12.06.2014

I'm using Digital Ocean for my experiments and place to host some sites, they provide very good service for ridiculous price but SSD space is limited and for backup purposes I'm using cloud service providers like pCloud. Here are a sample command for backuping Mongo databases to pCloud service.

cd /tmp/ && rm -rf mongo_dump && mkdir mongo_dump && mongodump --out /tmp/mongo_dump/ && tar -cvzf mongo_production_eureka.tar.gz mongo_dump/ && rm -r mongo_dump/ && curl -F "[email protected]" -F "password=password" -F "folderid=0" -F "filename=mongo1.tar.gz" -F "file=@/tmp/mongo_production_eureka.tar.gz" https://api.pcloud.com/uploadfile && rm mongo_production_eureka.tar.gz

For backup only:
cd /tmp/ && rm -rf mongo_dump && mkdir mongo_dump && mongodump --out /tmp/mongo_dump/ && tar -cvzf mongo_production_eureka.tar.gz mongo_dump/ && rm -r mongo_dump/

Our sys admin helped me to make this variant as bash script:
#!/bin/bash
set -e

dumpdir=mongodb.$$.$RANDOM
trap "rm -rf /tmp/$dumpdir /tmp/$dumpdir.tar.gz" exit SIGHUP SIGINT SIGTERM
cd /tmp 
rm -rf $dumpdir 
mkdir $dumpdir
mongodump --out /tmp/$dumpdir 
tar czf $dumpdir.tar.gz mongo_dump/ 
curl -F "[email protected]" -F "password=pas1" -F "folderid=0" -F "filename=mongo1.tar.gz" -F "file=@/tmp/$dumpdir.tar.gz" https://api.pcloud.com/uploadfile

More information about pCloud can be viewed in their website http://pcloud.com and Developer documentation
ps: pCloud have versioning of file and for that I'm using one name for all backups.

Nginx precompress resources for saving cpu

Published on 08.06.2014

Nginx have one great module "HttpGzipStaticModule", according to documentation on nginx website his job is to

Before serving a file from disk to a gzip-enabled client, this module will look for a precompressed file in the same location that ends in ".gz". The purpose is to avoid compressing the same file each time it is requested.

So the basic idea is to avoid CPU overhead of deflating on each request. Alson this will give us ability to use maximum compression of resources.
For using this module we must add something like that
location ~* \.(js|css|json)$ {
	gzip_static on;
}
in our config file in section
server
.
In my opinion compressing with gzip image files like (jpg/png/gif) are stupid because they already compressed. Gziping them will slow down the browser. My advice is to use "convert" from imagemagick package to stripping them on deploying.
Here are some example for gzip css/js/json files in deployment process.
find -L "/projects/***********/root/" -regextype posix-extended \( -type f -or -type l \) -and  -regex '.*\.(css|js|json)' -and -size +1k | xargs -n1 -I "{}" sh -c 'gzip -n -c -9 {} > {}.gz && touch {} {}.gz'