Friday, December 12, 2014

Using Git With Multiple SSH Keys and Accounts

Generating SSH keys for GitHub

Here are github account and work account.

  • GitHub:
    SSH Key: github_id_rsa
    Account: oopsmonk

  • Work:
    SSH Key: work_id_rsa
    Account: SamChen

Add SSH config File

Modify ~/.ssh/config

# Default github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_id_rsa

# Work git server
Host work.gitserver.com
    HostName work.gitserver.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/work_id_rsa

Git Repository Configuation

  • GitHub Project:

    $ git clone https://github.com/abc/projectA.git  
    $ cd projectA
    #github account
    $ git config user.name "oopsmonk"
    $ git config user.email "oopsmonk@example.com.tw"
  • Working Project:

    $ git clone https://work.com.tw/repo/projectW.git  
    $ cd projectW
    #working account
    $ git config user.name "SamChen"
    $ git config user.email "SamChen@example.com.tw"

Now you can deal with git repositories using different accounts.

(Additional) Using ssh-agent to access GitHub without password

Check key list

# No ssh-agent running 
$ ssh-add -l
Could not open a connection to your authentication agent.
$  

Enable ssh-agent (Ubuntu12.04):

#enable ssh-agent
$ eval `ssh-agent -s`

#add keys to agent
$ cd ~/.ssh
$ ssh-add work_id_rsa github_id_rsa
Identity added: work_id_rsa (work_id_rsa)
Identity added: github_id_rsa (github_id_rsa)

#list added keys
$ ssh-add -L

#delete keys in agent  
$ ssh-add -D

Now you can commit modifications to git repository without password.

Reference:
Understanding ssh-agent and ssh-add
How can I run ssh-add automatically, without password prompt?

Wednesday, December 10, 2014

pyenv Quick Start (Utunbu 14.04)

installation

Requirements

$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm

install pyenv

$ cd ~
$ git clone git://github.com/yyuu/pyenv.git .pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc

install pyenv-virtualenv

$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv 
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
$ exec $SHELL

Common Usage (Command Reference)

List all available versions

$ pyenv version -l  

Install python2.7.8 to pyenv

$ pyenv install 2.7.8  

List installed versions

$ pyenv versions
* system (set by /home/oopsmonk/.pyenv/version)
  2.7.8
  3.4.2

Creating a virtualenv

$ pyenv virtualenv 2.7.8 mypy-2.7.8

List virtualenvs

$ pyenv virtualenvs
  mypy-2.7.8 (created from /home/oopsmonk/.pyenv/versions/2.7.8)
  mypy-3.4.2 (created from /home/oopsmonk/.pyenv/versions/3.4.2)

# current versions 
$ pyenv versions
  * system (set by /home/oopsmonk/.pyenv/version)
  2.7.8
  3.4.2
  mypy-2.7.8
  mypy-3.4.2

Use python via virtualenv

# show current version
oopsmonk@VBox:~/markdown-note$ python --version
Python 2.7.6

# Change to other version
oopsmonk@VBox:~/markdown-note$ pyenv activate mypy-2.7.8
(mypy-2.7.8)oopsmonk@VBox:~/markdown-note$ python --version
Python 2.7.8

# Deactivate
(mypy-2.7.8)oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv deactivate
oopsmonk@oopsmonk-VBox:~/markdown-note$  python --version
Python 2.7.8
# Why current version is 2.7.8? it's supposed to 2.7.6.

oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv activate mypy-3.4.2
(mypy-3.4.2) oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv deactivate
oopsmonk@oopsmonk-VBox:~/markdown-note$ python --version
Python 3.4.2

oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv versions
  system
  2.7.8
  3.4.2
  mypy-2.7.8
* mypy-3.4.2 (set by PYENV_VERSION environment variable)

# delete a virtualenv   
$ pyenv uninstall mypy-2.7.8

# Go back to original system version  
$ alias pyenv_deactivate='pyenv deactivate && unset PYENV_VERSION'
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv activate mypy-3.4.2
(mypy-3.4.2) oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv_deactivate
oopsmonk@oopsmonk-VBox:~/markdown-note$ python --version
Python 2.7.6

Use python via pyenv

# Global python version
$ pyenv global 

# python version in current folder
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv local mypy-3.4.2
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv version
mypy-3.4.2 (set by /home/oopsmonk/markdown-note/.python-version)
oopsmonk@oopsmonk-VBox:~/markdown-note$ cd ..
oopsmonk@oopsmonk-VBox:~$ pyenv version
system (set by /home/oopsmonk/.pyenv/version)
oopsmonk@oopsmonk-VBox:~$ cd -
/home/oopsmonk/markdown-note
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv version
mypy-3.4.2 (set by /home/oopsmonk/markdown-note/.python-version)
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv local --unset
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv version
system (set by /home/oopsmonk/.pyenv/version)

# python version in current shell 
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv shell mypy-3.4.2
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv version
mypy-3.4.2 (set by PYENV_VERSION environment variable)
oopsmonk@oopsmonk-VBox:~/markdown-note$ cd -
/home/oopsmonk
oopsmonk@oopsmonk-VBox:~$ pyenv version
mypy-3.4.2 (set by PYENV_VERSION environment variable)
oopsmonk@oopsmonk-VBox:~$ pyenv shell --unset
oopsmonk@oopsmonk-VBox:~$ pyenv version
system (set by /home/oopsmonk/.pyenv/version)
oopsmonk@oopsmonk-VBox:~$ cd -
/home/oopsmonk/markdown-note
oopsmonk@oopsmonk-VBox:~/markdown-note$ pyenv version
system (set by /home/oopsmonk/.pyenv/version)

Monday, July 21, 2014

Raspberry Pi Monitor

Use RRDTool monitor Raspberry Pi, include CPU temperture, Memory usage, Disk I/O, Network I/O...

Install

install packages

sudo apt-get install libcairo2-dev libpango1.0-dev libglib2.0-dev libxml2-dev librrd-dev python2.7-dev rrdtool python-rrdtool
wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.1.tar.gz
tar xf psutil-2.1.1.tar.gz
cd psutil-2.1.1
sudo python setup.py install

Download or clone rpi-monitor on github
https://github.com/oopsmonk/rpi-monitor

Setup Crontab

By defualt, the cron.log is disabled in Raspbian. To enable it:

sudo vi /etc/rsyslog.conf

find the line and uncomment it.

# cron.*                          /var/log/cron.log

Restart rsyslog via:

sudo /etc/init.d/rsyslog restart

Modify crontab

crontab -e

Add schedule as below

#data collection every 5 minutes
*/5 * * * * /path/to/rpi-monitor/rpi_monitor.py
#generate daily graph report at 00:01
1 0 * * * /path/to/rpi-monitor/graphReport.py -1d
#generate weekly graph report at 00:03 on Monday
3 0 * * 1 /path/to/rpi-monitor/graphReport.py -1w  

Report example

Raspberry Pi Hardware

CPU Temperture

The temperture drop to 44 because I add a fan on CPU.

CPU Used Percentage

PID Count

Memory Usage

Mount point Usage

Mount Point Percentage

HDD I/O

eth0 I/O

eth1 I/O