hSATAC

>> ('cat' + 'ash').reverse #=> "hsatac"

Ssh Vm With Virsh Console

最近在玩 kvm,當 guest vm 的 network 環境不確定的時候,可以用 virt-viewer vnc 進去作設定,但總有沒有 X 或不適合使用 GUI 的狀況。這時可以利用 virsh console 這個指令進行連線。不過在 guest vm 要先修改一下設定。

首先修改 guest vm 的 /etc/grub.conf ,把 kernel 那行最後加上 console=tty0 console=ttyS0,1152200

例如:

1
2
3
4
kernel /vmlinuz-2.6.32-220.13.1.el6.centos.plus.x86_64 ro root=/dev/mappp
er/vg_w2vm001-lv_root rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rr
d_LVM_LV=vg_w2vm001/lv_swap rd_NO_MD quiet rd_LVM_LV=vg_w2vm001/lv_root rhgb craa
shkernel=auto SYSFONT=latarcyrheb-sun16 rd_NO_DM console=tty0 console=ttyS0,1152200

重開 guest vm 之後,在 host 就可以用 virsh console <domain> 直接連到 ssh terminal 啦。

在 CentOS 5 上安裝 Python26, Hg, Mercurial-server

筆記一下在 CentOS 5 上設定 Python 2.6, hg 以及 mercurial-server。

CentOS 套件庫裡面有 hg ,不過版本很舊,而且 mercurial-server 的 hook 部分會需要 python 2.5+ 的特性,而官方套件庫裡的 hg 相依於 python 2.4,因此套件庫裡面的 hg 就不合用了。

安裝 Python 2.6 + hg

  • sudo yum install python26 python26-devel 從 epel 安裝 python 2.6
  • 抓下最新的 hg source wget http://mercurial.selenic.com/release/mercurial-2.1.2.tar.gz 解壓後不要急著 make,先修改一下 Makefile
  • line 9 的 PYTHON 改成 PYTHON=python26 讓他抓 python 2.6
  • doc 的部份應該是缺 docutils 所以會出錯,不需要 doc 直接砍掉:line 33 改為 all: build, line 53 改為 install: install-bin

  • 接著就可以 make all && su -c "make install" && hg version

安裝 mercurial-server

  • 抓下最新 mercurial-server 原始碼解壓
  • 一樣先修改 Makefile, doc 的部份一樣移除: line 53 改為 installfiles: install pythoninstall
  • CentOS 的 useradd 不支援 --system,把 --system 改成 -r。如果 repositories 的 path 想改可以順便修改 --home 的值。
  • 修改完後 sudo make setup-useradd 就安裝好了

設定 mercurial-server

  • 先把自己的 public key 放到 /etc/mercurial-server/keys/root/ 下,接著要改用 hgadmin 這個 repo 來設定
  • check out hgadmin 這個 repo hg clone ssh://hg@localhost/hgadmin
  • /etc/mercurial-server/keysetc/mercurial-server/access.conf 複製過來。
  • add, commit 後 push,以後就可以用這個 repo 來管理使用者和權限了。
  • 原本的 /etc/mercurial-server/access.conf/etc/mercurial-server/keys 就可以刪除了。

Redmine + hg

順便講一下 Redmine 和 hg 搭配,如果設定好後在 repositories 分頁一直 404,看 log 顯示 hg: error during getting info: hg exited with non-zero status: 255 的話,多半是檔案權限問題。

可以修改 redmine/config/environment.rb 打開 config.log_level = :debug 看更詳細的 log

應該可以看到實際執行的 hg 指令,用 redmine user 去執行看看就能抓出問題所在。

把 Git 中的目錄搬到另一個 Git 並保留 Commit

今天在 refactor 公司的 git repository 時,有個需求,是要把原本 A repository 的其中一個目錄抽出來,獨立成 B repository。

原本以為這個需求無法達成,不過做了點研究以後發現是可行的,甚至 B repository 是已存在的 repository 也可以做到!

先說獨立出新的 repository 這個狀況,很簡單,先 git clone 出一個乾淨的 A repository 然後 git remote rm origin 不要 track remote。

接著在 git 根目錄下 git filter-branch --subdirectory-filter <目錄> -- --all 你就會看到這個目錄以外的東西都不見了,而且相關的 commit log 還在。

如果是要獨立出一個新的 repository 做到這邊就可以結束了。

接著講要把檔案和 commit log 匯到已存在的 B repository:接續上一步,用 mkdir <你要的目錄>; mv * <你要的目錄> 把抽出來的檔案都移到你預想要放的目錄 git add .; git commitcd .. 再用 git clone 把 B repository clone 出來,切到 B repository 的目錄,用 add local repository as remote 的方式 git remote add repoA ../<A repo 的目錄> 然後 git pull repoA master 就完成了。

參考:Moving Files from one Git Repository to Another, Preserving History

Xcode 4.3 Publish Ipa for Testflight

這兩天在研究 testflight 碰到了一點 Xcode 4.3 的小雷,記錄一下。

按照 testflight 的教學上傳 ipa 檔,一直出現 mismatched-ubiquitykvstore-identifier-value 的錯誤,但是憑證已經確認多次,肯定沒有問題。

翻了一下應該是 APP ID enable iCloud 的問題,但是不能 disable 掉的狀況,只好自己去 entitlement 補上需要的參數。

Xcode 4.3 的 entitlement 換地方了。

  • 請到 target 的 summary tab 拉到最底下找到 entitlement 區塊,勾選 enable Entitlement
  • iCloud key-value Store 這個欄位填上 .*
  • iCloud Container 這個部分自己加一個值 .*

存檔後你的專案就會多一個 專案名.entitlements 的檔案,打開確認一下內容是否有

1
2
3
4
5
6
<key>com.apple.developer.ubiquity-container-identifiers</key>
 <array>
     <string>$(TeamIdentifierPrefix).*</string>
 </array>
 <key>com.apple.developer.ubiquity-kvstore-identifier</key>
 <string>$(TeamIdentifierPrefix).*</string>

再做 Archive, Share 成 ipa 檔上傳就可以了。

2012/04/42 補充

用 .* 的 key 送審 AppStore 時會被 reject,請設成跟你的 bundle identifier 一樣即可。

也就是:

1
2
3
4
5
6
<key>com.apple.developer.ubiquity-container-identifiers</key>
 <array>
     <string>$(TeamIdentifierPrefix)com.yourcompany.coolapp</string>
 </array>
 <key>com.apple.developer.ubiquity-kvstore-identifier</key>
 <string>$(TeamIdentifierPrefix)com.yourcompany.coolapp</string>

PHP-Resque Auto Scale Workers

PHP-Resque is an amazing PHP port of Resque. After playing it for a while, an idea crossed my mind: It’s a total waste to create numbers of workers when there’s not many jobs to do. How about auto scale it? With the EventListener design of PHP-Resque, we could achieve it by writing some simple hooks.

Also, it solved issue #32 of PHP-Resque.

Here’s my code: PHP-Resque Auto Scale

Introduction

This is a project trying to build an auto scale architecture of PHP-Resque.

Youtube 加入我的最愛後自動抓檔上傳至 Google Music

這個標題很冗長,不過正是 Youtube 加入我的最愛後自動抓檔上傳到 Google Music 。

這個需求是這樣來的,我常常在 Youtube 聽到喜歡的歌,習慣性按加入最愛,隨時可以拿出來重複播放。不過在最近都用 Google Music 來管理我的音樂庫,行進時也可以用 Android 上的 Google Music App 聆聽音樂。

但這樣一來,就要透過 Youtube Downloader 等網站或軟體抓下影片檔後再轉檔、上傳到 Google Music ,這樣實在是太麻煩了。

有沒有什麼方法可以把這個過程自動化呢?第一個想到的是利用 ifttt 。不過 ifttt 要達成這個功能需要繞許多彎路,最後決定自己寫一個。

Redmine Migration From Trac 0.12

This article will demostrate a near perfect redmine migration from trac 0.12 step by step.

1
2
3
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11
PassengerRuby /usr/bin/ruby
* /etc/httpd/conf.d/redmine.conf
1
2
3
4
5
6
7
8
   <VirtualHost *:80>
      ServerName redmine.miiicasa.com 
      DocumentRoot /home/m/share/htdocs/redmine/public
      <Directory /home/m/share/htdocs/redmine/public>
         AllowOverride all    
         Options -MultiViews    
      </Directory>
   </VirtualHost>

PHP-Resque 簡介

Resque 是 Github 基於 Redis 開發的 background job 系統。相較其他肥大的 queue 系統, Resque 的設計真的非常單純簡潔,充分利用 Redis 的特性。更多介紹可以看原作者的 Blog

PHP-Resque 是把 Resque porting 到 PHP 的專案。使用和 原本 Resque 一樣的概念和設計。甚至連 Redis 的 key 命名都一樣,因此也可以使用 Ruby 版本的 resque-web 來監控 PHP-Resque 的運行狀況。

設計

Resque 的設計有兩個角色: Job 和 Worker。 每個 Job 都是定義成類別,新增 Job 的時候會將 Job 的類別和相關參數 json_encode 後儲存到不同的 queue 裡面,而 Worker(s) 則會依序從 redis 讀取 Job 出來執行。

執行的時候並不是這個 Worker 本身去執行,而是會 fork 一個 process 來執行。這樣設計是為了避免時間一長, Worker 的記憶體管理不良導致卡死的狀況。

讀取 queue 時會依據你啟動 worker 的時候給的 queue 順序來讀取,因此優先權較高的 queue 要設定在前面。 Redis 可以是單機或 RedisCluster。而許多不同伺服器上可以按需求部屬執行不同 queue 的 worker。

Resque Job 執行失敗並不會自動重試,而是把它丟到 fail 的 queue 裡面。如果你有重試的需求可能要自己處理。若是有特別重要的 Job 需要監控執行狀態的,可以參考 README 中的 Tracking Job Statuses 一節。

以下先來介紹如何使用 PHP-Resque:

安裝 PHP-Resque

安裝非常容易,只要 git clone https://github.com/chrisboulton/php-resque.git 下來,放到你想要的地方,由於 Resque 沒有 config 檔的設計,設定都是寫在環境變數中再執行就可以了。

在 CentOs 5 上升級 Redis 2.4.5

CentOS 5 上的 Redis 套件只有到 2.0 最近用一套 PHP-Resque 需求 Redis 2.2 以上,只好手動升級了。

首先抓下最新穩定版解壓

1
2
3
4
wget http://redis.googlecode.com/files/redis-2.4.5.tar.gz
tar zxvf redis-2.4.5.tar.gz
cd redis-2.4.5
make

跟原本的 redis 2.0 裝在同一個目錄

1
sudo make PREFIX=/usr install

再把新的 config 檔蓋過去

1
sudo cp redis.conf /etc/

為了讓原本的 init script 正常運作 redis.conf 要稍微修改

1
2
3
daemonize yes
...
pidfile /var/run/redis/redis.pid

原本 redis-server 是裝在 /usr/sbin 新的是裝在 /usr/bin 把 /usr/sbin/redis-server 覆蓋過去

1
sudo mv /usr/bin/redis-server /usr/sbin/

大功告成。

用 Scrpit 指令來紀錄輸出及輸入

假設我們現在要執行一個重要的動作,想要紀錄 terminal 下面每一個輸出和輸入,當然我們可以在每個指令後面用 cmd > filename 把輸出導向 log 檔,但如果是一連串的動作時就很不方便了。

unix 系列內建一個指令叫 script ,他可以很方便的紀錄你每一個輸出和輸入。詳細用法可以參考 man script

在這邊講兩個常用的方法,一個是 script log.txt 會把紀錄存到 log.txt, 用 script -a log.txt 就可以把新的 log append 到檔案後,不會蓋掉原本檔案。當你動作結束,想停止紀錄時,只要打 exit 就可以停止紀錄。

另一個是 script -C "ls -al" log.txt 他會把你指定的指令存到 log.txt 中,這個用法的不需輸入 exit

如果你的 .bash_profile 裏面有用到一些自訂的函數例如 git-autocompletion 之類,那你在使用 script 的時候可能會發現每下一個指令都告訴你 command not found。正確的解法是把你的 function 移到 .bashrc 中,在 .bash_profile 裏面去 source .bashrc 即可。

最後說明一下 log 檔的觀看方式,因為 script 會把所有東西都紀錄下來,包括控制碼這些,所以可以用 less -r log.txtmore log.txt 來觀看。

如果需要把 log 的控制碼去掉,可以參考下面這個 script:

1
2
3
4
5
6
7
8
9
#!/usr/bin/env perl
while (<>) {
    s/ \e[ #%()*+\-.\/]. |
       (?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
       (?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
       (?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
       \e.|[\x80-\x9f] //xg;
    print;
}