KernelCoffee

Another sip of code ?

Reminder - Javascript - Random Hexadecimal Color Function

| Comments

Sometime you need some random values for testing pieces of code for exemple, in my case some placeholders for a QML component.

I quickly wrote a javascript function to generate random HEX colors, it was so ugly I’m not going to show it here.
A friend of mine needed a similar function, so I send it to him. I guess he was shocked by how ugly that code was, that he sent me back a way more elegant one.

1
2
3
function randomColor() {
    return "#" + (0x1000000 + parseInt(Math.random() * 0x1000000)).toString(16).substr(-6);
}

Thanks Arnaud Sellier.

Auto-Herbergement : Mozilla Sync Server Avec Nginx Et Gunicorn Sur Centos

| Comments

Mozilla Sync : Qu’est ce que c’est ?

Le service Sync de Mozilla permet de synchroniser et sauvegarder l’ensemble des favoris, cookies, onglets… de Firefox sur un serveur distant.

Qu’est ce que cela signifie ?

Et bien cela signifie qu’en cas de perte de données/crash/vol de l’ordi les données ne sont pas perdu mais sauvegardé a distance. Un des avantages de cette technologie est qu’il est possible de synchroniser plusieurs machines avec un même compte, comme par exemple : un ordinateur chez soi, un ordinateur portable et celui du bureau.

Objectif

Le but de ce tutoriel est d’installer un service Sync sur un serveur personnel. Ce service est hébergé derrière un serveur web Nginx et utilisera MySql et Gunicorn.

Installation

J’assume que si vous êtes ici, c’est que vous savez vous débrouiller avec un terminal et que votre serveur est déjà configuré avec :

  • MySql-server
  • Nginx ( + certificat ssl)

Mozilla a publie les instructions pour Nginx dans sa documentation, mais je la trouve incomplète et est faite pour un serveur sous Debian.
Ce qui suis est donc plus ou moins une version sous CentOS avec plus de details sur comment lancer le service avec gunicorn et nginx.

Avant de commencer la procédure nous avons besoin :

  • D’un serveur personnel sous CentOS (réalisé sous CentOS 6.x).
  • python-devel
  • mercurial
  • mysql
  • python-virtualenv On peut installer le tout avec la commande suivante
1
$ sudo yum install python-devel mercurial sqlite python-virtualenv mysql-devel

Par habitude je fais toujours tourner chaque service avec un utilisateur différent.

1
$ sudo adduser mozilla

Puis on se connecte a ce compte via la commande “su”.

1
$ sudo su mozilla

N’oublier pas de revenir dans le dossier de l’utilisateur mozilla via la commande “cd”.
L’ensemble des instructions suivante sont réalisées en tant qu’utilisateur mozilla.

On récupéré la dernière version de Sync server et on la compile

1
2
3
4
mozilla$ cd ~
mozilla$ hg clone https://hg.mozilla.org/services/server-full
mozilla$ cd server-full
mozilla$ make build

On en profite pour installer mysql et gunicorn via easy_install fourni avec le serveur sync

1
2
3
mozilla$ cd ~/server-full
mozilla$ bin/easy_install Mysql-Python
mozilla$ bin/easy_install gunicorn

Configuration

Maintenant que Sync est installé, configurons le pour qu’il soit pleinement exploitable.

Commençons par créer la base mysql et son utilisateur. Personnellement j’utilise ce script les créer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh

DB_NAME=<database_name>
DB_USER=$DB_NAME
DB_PASSWORD=<password>

echo "
CREATE DATABASE '$DB_NAME';
CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost' WITH GRANT OPTION;
" > request.sql

mysql -u root -p < request.sql

rm request.sql

n’oubliez pas de personnaliser DB_NAME, DB_USER et DB_PASSWORD.

Ensuite dans le fichier server-full/developpement.ini modifiez :

1
2
3
4
5
6
[server:main]
use = egg:Gunicorn #modification pour dire a Sync qu'on utilise Gunicorn
host = 0.0.0.0
port = 5000
use_threadpool = True
threadpool_workers = 60

Ensuite dans le fichier server-full/etc/sync.conf modifiez les blocks :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[storage]
backend = syncstorage.storage.sql.SQLStorage
sqluri = mysql://<user>:<password>@localhost/<database> #modification pour utiliser MySql
standard_collections = false
use_quota = true
quota_size = 51200 #augmentation du quota a 50Mo au lieu de 5Mo
pool_size = 100
pool_recycle = 3600
reset_on_return = true
display_config = true
create_tables = true

[auth]
backend = services.user.sql.SQLUser
sqluri = mysql://<user>:<password>@localhost/<database> #mofication pour utiliser Mysql
pool_size = 100
pool_recycle = 3600
create_tables = true
# Uncomment the next line to disable creation of new user accounts.
#allow_new_users = false

[nodes]
fallback_node = https://www.yourserver.net #modification 

Gunicorn et Nginx

Mon fichier de configuration Nginx pour Sync

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
    listen      80;
    server_name sync.kernelcoffee.org;
    rewrite     ^   https://$server_name$request_uri? permanent;
}

server {
    listen  443 ssl;
    server_name sync.kernelcoffee.org;

    ssl_certificate /etc/ssl/certs/ssl.crt;
    ssl_certificate_key /etc/ssl/certs/ssl.key;

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:5000/;
    }
}

Nginx va recevoir les requêtes http(s), rediriger si besoin en https et assurer la communication avec Gunicorn en interne.
C’est Gunicorn qui va faire tourner le service Sync, Nginx assure la passerelle avec l’extérieur.

Fichier init

In progress
il me reste a stopper proprement le service / This script doesn’t stop the service gracefully yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# /etc/init.d/sync
# version 0.1 2013-03-12 (YYYY-MM-DD)

### BEGIN INIT INFO
# Provides:   sync
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Mozilla Sync server
# Description:    Starts the mozilla sync server
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

prog=sync
SYNC_USER=mozilla
SYNC_HOME=/home/mozilla/server-full
CPU_COUNT=2
pidfile=/var/run/mozilla/sync.pid
lockfile=/var/run/mozilla/sync.lock
conffile=${SYNC_HOME}/development.ini
GUNICORN=${SYNC_HOME}/bin/gunicorn_paster
GUNICORN_ARGS="--daemon --pid $pidfile --workers $CPU_COUNT"
INVOCATION="$GUNICORN $GUNICORN_ARGS $conffile"

start () {
    echo -n "Starting $prog"
    daemon --user ${SYNC_USER} --pidfile  ${pidfile} $INVOCATION
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo "Stopping $prog"
    killproc -p ${PIDFILE} ${prog}
    RETVAL=$?
    echo
    [ RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|help}"
        RETVAL=2
esac

exit $RETVAL

Pour utiliser ce script il faut également créer le dossier /var/run/mozilla ou seront stocker le fichier pid et lock

1
2
$ sudo mkdir /var/run/mozilla
$ sudo chown mozilla:mozilla /var/run/mozilla

on peut également lancer le service via la commande :

1
$ server-full/bin/gunicorn_paster server-full/development.ini

Maintenant que tout est en place, démarrons le serveur et assurons nous qu’il se lance au démarrage de la machine.

1
2
$ /etc/init.d/sync start
$ chkconfig sync on

NOTICE

Si comme moi vous utilisez des certificat SSL auto-généré, pensez a aller une 1ère fois sur l’adresse de votre serveur Sync afin de les accepter sinon Firefox refusera de s’y synchroniser.


Source: Mozilla Documentation run-sync howto and nginx

Reminder - CentOS - Screen : No More PTYs

| Comments

Recently I switched server (yes, the one running this website), and I am using the command ‘screen’ to run a few services.

But here’s the drawback, unless I was logged as root, I couldn’t have a screen session running without the error :

1
No more PTYs, Sorry, could not find a PTY

Which after a while started to annoy me quite a bit (Typically my init script would just die without explanation since I redirect the output in /dev/null).

Most of solutions across the web recommend to change the ownership of the /dev/tty* (tty used by the user) to the current user or add the user to the group tty. Not only this is a solution that doesn’t work, but it is a dangerous one (if the user is hacked and it belongs to the tty group, it can listen to other ttys).

but thanks to the website Fooninja.net, I was able to solve this issue by running the command :

1
mount -t devpts /dev/ptmx /dev/pts

Of course you can make it permanent by adding this line to the file /etc/fstab

1
/dev/ptmx       /dev/pts        devpts  defaults        0       0

After a little more research looking for why this actually works, It looks like I can’t read a man and I’m unable to connect the dots :

When a process opens /dev/ptmx, it gets a file descriptor for a pseudoterminal master (PTM), and a pseudoterminal slave (PTS) device is created in the /dev/pts directory. Each file descriptor obtained by opening /dev/ptmx is an independent PTM with its own associated PTS, whose path can be found by passing the descriptor to ptsname(3).

And in my case /dev/pts/ didn’t exist / wasn’t mounted so ptmx wasn’t able to create slaves.

Mystery solved.

Source: Fooninja.net, man ptmx

Reminder - Fedora - AMD/NVIDIA Drivers Version Not in Sync With Kernel Version

| Comments

My current Workstation is equipped with a AMD card (5000 series) where I use the non-free video drivers, from time to time there is a few days latency between the moment where the linux kernel is updated and the AMD catalyst drivers from the fusion repositories are updated to the same version of the kernel.

Over the past weeks there has been no release of the AMD drivers and the Fedora update remove the latest kernel version enabled with the AMD video drivers.

After some research, I found out that the AMD Catalyst drivers are stuck in the testing repo, but how to tell yum to retrieve the drivers from the Fusion non-free update-testing repo ?

Well you have to specify it to do so using this kind of command :

lang: bash
1
$ yum --enablerepo rpmfusion-nonfree-updates-testing install kmod-catalyst

And you should have the latest AMD Catalyst driver to correspond to your Linux kernel.

EDIT:
This method also works with the Nvidia drivers

lang: bash
1
$ yum --enablerepo rpmfusion-nonfree-updates-testing install kmod-nvidia

source : Fedora forums

Reminder - OS X - the Command Line Tools for Xcode Don’t Appear to Be Installed

| Comments

I just got my mac mini back from France, now that I live in Boston. After a whole lot of updates and upgrades, I wanted to install some tools that I need with MacPorts, but sometimes things don’t go the way you want.
Port tries to install/compile and fails with a whole lot of errors, but within the error log is hidden this line :

1
The Command Line Tools for Xcode don't appear to be installed

I don’t understand, Xcode is installed and in the past the command line tools were installed at the same time.
Apparently with the two last revision of OS X Lion (OS X.7) and Mountain Lion (OS X.8), it is not the case anymore.
So, to resolve this situation, the salvation in inside Xcode in the preferences panel, section download.

Click on the install button for “Command Line Tools” and when the installation is finished you will be able to use the command lines again.

Source: MacObserver

Hardware - Linux - Saitek Cyborg R.A.T 7/9 Compatibility

| Comments

I like games, which is why I keep a Window partition on my laptop (labeled ‘Wintendo’), and good games deserve a good mouse to run them. And so I bought the Cybork R.A.T 7 from Saitek, looks weird at first glance but is fully customizable to feel awesome when you use it. How disappointed I was when I discovered that I wasn’t behaving properly when I plugged it in my Fedora partition.

So here’s the way to resolve this matter.

If like me you don’t have a Xorg.conf file (Fedora generates it automatically at startup), you can create a file name “20-cyborgrat7.conf” (or “20-cyborgrat9.conf” if you have a R.A.T 9) in the “xorg.cong.d” folder.

So to summarize :
At the path “/etc/X11/xorg.conf.d/”, create a file name “20-cyborgrat7.conf” or “20-cyborgrat9.conf” containing the following :

1
2
3
4
5
6
7
8
9
Section "InputClass"
        Identifier "R.A.T."
        MatchProduct "R.A.T.7|R.A.T.9"
        MatchDevicePath "/dev/input/event*"
        Option "Buttons” “17"
        Option "ButtonMapping" "1 2 3 4 5 0 0 8 9 7 6 12 0 0 0 16 17"
        Option "AutoReleaseButtons" "13 14 15"
        Option "ZAxisMapping" "4 5 6 7"
EndSection

And reboot the X server, so far it works pretty well, I haven’t tested all the buttons yet, but vertical and horizontal scroll works.

EDIT:
The config file is also working with the R.A.T.9 so I specified it in the article and the title.

Source : baudet.me

Reminder - SSH - Send Command Without Login

| Comments

Over the past few days I had to manage a few remote devices, and send commands to them. I was like “I wish I could just send the command without having to login, that would save me a lot of time.”

Well after a google search I found it !

1
$ ssh <user>@<ip server> '<command>'

It’s so simple I almost feel dummer.

here is an example to reboot a remote device.

1
$ ssh root@192.168.0.42 'reboot'

it’s almost too simple…

source : SystemsBoy

Reminder - Git/diff - Add Some Colors to Diff

| Comments

I like diff. It’s a very usefull tool. But it comes by default only in black and white and it’s quite difficult to rapidly see the differences in the code.

Don’t worry the solution is very easy and is called colordiff !

First step, let’s install color diff : Since I am using Fedora, I use Yum to install, but colordiff should be in the repositories of you favorite linux distro.

1
sudo yum install colordiff

you can now use colordiff instead of diff to add some colors to your life.

Now to some even better stuff, let’s enable this into git !

For that you can use the easy and fast way with this command :

1
git config --global color.ui true

there is also another and more complicated way, by opening the .gitconfig file in your home directory (so ~/.gitconfig) and adding these lines :

1
2
3
4
5
[color]
        branch = auto
        diff = auto
        interactive = auto
        status = auto

And voilà, git now use colordiff instead of diff when you use the command git diff and that make your life more easy and I like when my life is more easy.

These two method are very similar, the first one enable the default automatic way to add color to git diff, the second one open a whole new level of customization where examples can be found on the git Cheat Sheet (see sources).

Sources: Git CookBook Git Cheat Sheet

Reminder - Ssh - Terminate an Unresponsive Session

| Comments

A colleague of mine, just taught me something very useful (thanks Bryan !), how to terminate an unresponsive ssh session.

Let’s take this situation : You are connected to another computer (server, device, toaster, etc.), then suddenly your ssh session dies (usually when the network is down or the computer on the other side got disconnected) and the terminal is stuck waiting for the ssh session to timeout and give back control to the user.

Well there is a way to terminate this unresponsiveness with this shortcut :

1
~.

Now, I don’t have to kill the terminal anymore !

I found a similar explanation and more shortcuts at the Lone Sysadmin (see source).

Source: The Lone Sysadmin

Experiment - Video - Test UI QML OpenGl

| Comments

Here is a small video showing a little experiment of a QML based UI destined to be used with a touchscreen (still a work in progress for that though).

The idea is to have a very simple interface to control different values using simple or multi touch screen. The glowing blob slowly pulsates, I tried to match a heart beat so it feels natural, and have it hidden, only to appear when the screen is touched.

The final source code will be on my github but not just yet.

to be continued…