Posts

Showing posts with the label Level 3

[ Level 3 ] Test your Python code.

Ref: https://python-guide.readthedocs.org/en/latest/writing/tests.html Mock: http://www.voidspace.org.uk/python/mock/getting-started.html# Wish this helps. regards, Stanley Huang

[ Level 3 ] How to build MySQL client tool - mysql

First, you have to download MySQL source and use the following command to build it. $ ./configure --without-server --enable-thread-safe-client --with-client-ldflags=-all-static --prefix=/usr/local/mysql --with-machine-type=powerpc --with-zlib-dir=/usr/local/zlib --without-debug --without-docs --with-big-tables If you don't want to build it, there is a way to get it. (The sample is for Ubuntu only.) $ apt-get install libmysqlclient15-dev Wish this helps. regards, Stanley Huang

[ Level 3 ] The script for creating virtualbox envrionment (Ubuntu 12.04) with lauch new OS image.

In my company, I need to test it when I build a new image. Therefore I have to setup my testing environment for integration test. I wrote a script to automatically setup the environment from new install Ubuntu. The steps are: 1. Download and setup tftp server. 2. Download and setup dhcp server with netboot option. 3. Install virtualbox. After that you could create a VM then choose netboot. (the first interface must connect to tap0 to get boot server information) #!/bin/bash ######## utilities ######## _getTasks() { indent="$1" cat $0 | grep '()' | grep -v "^_" | grep "^[^ ]" | cut -d'(' -f1 | sed -e "s/^/$indent/" } _showUsage() { indent=" " cat <<EOF Usage: $0 task Ex. $0 `_getTasks | head -1` tasks: ${indent}[ all ] `_getTasks "$indent"` EOF } _die() { echo "$1" exit ${2:-1} } _checkID() { uid=`id | cut -d'(' -f1 | cut -d'=' -f2` [ $uid -ne ...

[ Level 3 ] The script for setup the Ubuntu for Juniper VPN.

There is an paper told us how to setup Ubuntu 12.04 for Juniper VPN. Then I follow the guide and also write a script for installation/setup. The steps as the following: 1. Download ans install ia32-libs to support 32 bit libraries. 2. Download and install x64 JRE, jre-7u21-linux-x64.tar.gz. 3. Create plugin link for firefox/google chromium. 4. Update alternative Java. 5. Download and install i586 JRE, jre-7u21-linux-i586.tar.gz. After that, you could login to your VPN. #!/bin/bash ## url link for 64 bit JRE. jre_x64_source_url=http://javadl.sun.com/webapps/download/AutoDL?BundleId=76853 ## url link for 32 bit JRE jre_i586_source_url=http://javadl.sun.com/webapps/download/AutoDL?BundleId=76851 die() { echo "$1" exit ${2:-1} } checkRootID() { uid=`id | cut -d'(' -f1 | cut -d'=' -f2` [ $uid -ne 0 ] && die "Need root to execute this command, exit!" } checkRootID pek2c() { bDebug && read -p "Press enter key t...

[Level 3] Network Simulator

Memo it: 1. openWNS: www.openwns.org/Wiki 2. ns-3: www.nsnam.org , ns-3 provides python API to use. Wish this helps. regards, Stanley Huang

[Level 3] How to setup Juniper VPN in Ubuntu 64 bit environment.

A good sample to demo how to setup Juniper VPN in Ubuntu 64 bit environment. http://wireless.siu.edu/install-ubuntu-64.htm Wish this helps. regards, Stanley Huang

[ Level 3 ] Python MRO.

Python MRO introduction. http://docs.python-guide.org/en/latest/writing/style/ Wish this helps. regards, Stanley Huang

[ Level 3 ] Python coding style.

Python coding style: http://docs.python-guide.org/en/latest/writing/style/ Wish this helps. regards, Stanley Huang

[Level 3] Advanced vim settings

There is a good sample for vim settings: http://amix.dk/vim/vimrc.html Wish this helps. regards, Stanley Huang

[ Level 3 ] Python config factory with __subclasses__.

It's a sample code for use __subclasses__ to validate the type. I write a config factory utility #!/bin/env python import os, sys if './' not in sys.path: sys.path.append('./') if './lib' not in sys.path: sys.path.append('./lib') if '../lib' not in sys.path: sys.path.append('../lib') if os.getcwd()+'/lib' not in sys.path: sys.path.append(os.getcwd()+'/lib') import yaml, json from libproperty import * from StringIO import StringIO from abc import * ## Property Abstract Class ## Subclass naming rule: xxxProperty. (e.g. JavaProperty, YamlProperty) class CommonProperty(object): __metaclass__ = ABCMeta def __init__(self, FILE=None): self._prop_file_output = self._prop_file = FILE self._prop = None pass @staticmethod def isValidType(type): return '%sProperty' % type in [ c.__name__ for c in CommonProperty.__subclasses__() ] @abstractmethod def save(self, FI...

[Level 3] Cassandra stress tool from git in Ubuntu.

If you want to do the stress test for Cassandra. You could try an open source project call cassandra-stress of zznate. First you must to have git client and maven. Please reference here: git installation , maven installation Clone the src from git. # git clone https://github.com/zznate/cassandra-stress.git Then you would find a folder named "cassandra-stress", cd it and build the binary. # cd ./cassandra-stress # mvn install and mvn would create a sub folder "target", cd it and run the stress script. # cd ./target/appassembler/ # sh ./bin/stress -o insert -b 1000 -n 2000000 localhost:9160 Wish this helps. regards, Stanley Huang

[Level 3] Run python in shell script.

If you want to wrap python script into shell script. You could use the following sample code. #!/bin/sh # -*- mode: Python -*- """:" # bash code here; finds a suitable python interpreter and execs this file. # prefer unqualified "python" if suitable: python -c 'import sys; sys.exit(sys.hexversion < 0x020500b0)' 2>/dev/null \ && exec python "$0" "$@" for pyver in 2.6 2.7 2.5; do which python$pyver > /dev/null 2>&1 && exec python$pyver "$0" "$@" done echo "No appropriate python interpreter found." >&2 exit 1 ":""" import os, sys print os.path.dirname('/Hello/World/c.txt') $ ./test.sh /Hello/World Wish this helps. regards, Stanley Huang

[Level 3] Add row count in select of MySQL.

mysql> set @row_count; mysql> select *, @row_count := @row_count + 1 as row_count from test; +------+---------+-----------+ | id | product | row_count | +------+---------+-----------+ | 1 | cloth | 1 | | 2 | shoes | 2 | | 3 | paints | 3 | +------+---------+-----------+ 10 rows in set (0.00 sec) Wish this helps. regards, Stanley Huang

[Level 3] rw_ether_atob, rw_ether_btoa stored function in MySQL.

delimiter // drop function if exists rw_ether_atob// create function rw_ether_atob(sAscii char(17)) returns bit(48) deterministic begin declare bReturn bit(48); ##set bReturn=conv(replace(sAscii,':',''),16,2); ## string output not bit ##set bReturn=conv(replace(sAscii,':',''),16,10); ## not work ##set bReturn=cast(replace(sAscii,':','') as bit(48)); ## syntax error ##set bReturn=bin(replace(sAscii,':','')); ## syntax error set bReturn=unhex(replace(sAscii,':','')); return bReturn; end// drop function if exists rw_ether_btoa// create function rw_ether_btoa(sBit bit(48)) returns char(17) deterministic begin declare sReturn char(17); set sReturn=lpad(hex(sBit),12,'0'); set sReturn=concat_ws(':', substr(sReturn,1,2), substr(sReturn,3,2), substr(sReturn,5,2), substr(sReturn,7,2), substr(sReturn,9,2), substr(sReturn,11,2)); return sReturn; end// delimiter ; /* mysql> c...

[Level 3] Benchmark in MySQL

#!/usr/bin/bash #mysqlslap -a -c100 i10 -u root -p --engine=InnoDB,MyISAM,MEMORY,ARCHIVE mysqlslap -a --only-print ## ref.@@pseudo_thread_id to get different values of thread. # mysqlslap --create="CREATE TABLE A (a int);INSERT INTO A values (@@pseudo_thread_id)" # --query="SELECT * FROM A" --concurrency=50 --iterations=200 Wish this helps. regards, Stanley Huang

[Level 3] Transaction in MySQL.

#!/usr/bin/bash -vx MYSQL_HOME=/opt/mysql/mysql mysql -uroot -proot_pwd <<EOF create database if not exists test; use test; create table if not exists test (id int) engine=InnoDB; alter table test engine=Innodb; truncate table test; insert into test values (1),(2),(3); system cp $MYSQL_HOME/data/test/test.ibd $MYSQL_HOME/data/test/test.ibd.bak start transaction; insert into test values (4); system cp $MYSQL_HOME/data/test/test.ibd $MYSQL_HOME/data/test/test.ibd.tran1; rollback; system cp $MYSQL_HOME/data/test/test.ibd $MYSQL_HOME/data/test/test.ibd.rollback; system diff $MYSQL_HOME/data/test/test.ibd.tran1 $MYSQL_HOME/data/test/test.ibd.rollback > ./diff_tran1_rollback; start transaction; insert into test values (4); system cp $MYSQL_HOME/data/test/test.ibd $MYSQL_HOME/data/test/test.ibd.tran2 commit; system cp $MYSQL_HOME/data/test/test.ibd $MYSQL_HOME/data/test/test.ibd.commit system diff $MYSQL_HOME/data/test/test.ibd.tran2 $MYSQL_HOME/data/test/test.ibd.commit > ./di...

[Level 3] XML in MySQL.

#!/usr/bin/bash createTable() { mysql -ujoseph <<EOF CREATE TABLE persons ( id int auto_increment primary key, data text ); EOF } #createTable createUsers(){ mysql -ujoseph <<EOF INSERT INTO persons(data) values (' <person> <name>stanley</name> <sex>m</sex> <addr>taipei</addr> <tels> <tel>12340001</tel> <tel>12340002</tel> <tel>12340003</tel> </tels> </person> '), (' <person> <name>joseph</name> <sex>m</sex> <addr>taipei</addr> <tels> <tel>12350001</tel> <tel>12350002</tel> <tel>12350003</tel> </tels> </person> ') ; EOF } #createUsers queryUserTels() { mysql -ujoseph <<EOF select ExtractValue(data,'//person/tels/tel[1]'), ExtractValue(data,'/person/tels/tel[2]'), ExtractValue(data,'person/tel...

[Level 3] mysqldump/mysqlimport notes for MySQL.

#!/usr/bin/bash #dData=`dirname $0`/data dData=/tmp/data mkdir -p $dData && cd $dData mysqldump -uroot test > $dData/test.sql mysqldump -uroot test t > $dData/test.t.sql #mysql -uroot < $dData/test.sql #mysql -uroot < $dData/test.t.sql exit exit exit --routines --triggers #!/usr/bin/bash #dData=`dirname $0`/data dData=/tmp/data mkdir -p $dData cd $dData mysql -uroot <<EOF use test; truncate table t; select * from t; EOF mysqlimport --fields-terminated-by=, --fields-enclosed-by='"' \ --lines-terminated-by="\n", --ignore \ test $dData/t.txt ## '--ignore/--replace' contain unique key values already in the table, 'test' is database name, 't' is the tablename. Wish this helps. regards, Stanley Huang

[Level 3] Notes for MySQL replication

#################################################### master server set server_id=0; ## master server_id must be 0 grant replication slave on *.* to 'slave'@'%' identified by 'slave'; show master status; # +-----------------------+----------+--------------+------------------+ # | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | # +-----------------------+----------+--------------+------------------+ # | Stanley-NB-bin.000103 | 377 | | | # +-----------------------+----------+--------------+------------------+ ## in my.cnf [mysqld] server-id=0 #################################################### slaver server set server_id=1; # shell> mysqldump -uroot -p test t1 > /tmp/t.sql # mysql> create database if not exist test; # mysql> use test; # mysql> source /tmp/t.sql change master to master_host='192.168.1.100', master_user='slave', master_password='slave', master_...

[Level 3] Multithread in Python

#!/bin/env python import os, sys import threading import random class ThreadClass (threading.Thread): def __init__(self, function, seconds, threads): self.function = function self.seconds = seconds threading.Thread.__init__(self) threads.append(self) def run(self): print 'run %s...' % self.seconds self.function(self.seconds) print 'run %s done!' % self.seconds class TestMultiThread(object): def __init__(self): self.max_thread = 5 self.threads = [] pass def task(self, seconds): os.popen('sleep %s' % seconds).read() def run(self): for i in range(0, self.max_thread): seconds = int(random.random() * 10) ThreadClass(self.task, seconds, self.threads).start() for t in self.threads: t.join() print 'Jobs done!' if __name__ == '__main__': tmt = TestMultiThread() tmt.ru...