http://blog.glidenote.com
に移転しました

Archive for the "mac" Category

tmuxの外観をカスタムして、運用と開発を加速させる

ターミナルマルチプレクサ Advent Calendar 2011の16日目です。
15日目はdodaさんのリモートの tmux でコピーした内容のローカルのクリップボードへの反映でした。

tmuxを利用し、

  • sshでリモートサーバに接続するたびに、ホスト別にwindowを生成
  • エディタ用、コマンド操作用、負荷監視用など用途別にwindowを生成し、paneを増やし画面分割

などをしていると、今は「どのwindow」の「どのpane」で作業をしているのか分からなくなることがあります。

これは意図せず他のサーバで作業をしてしまい、事故を引き起こしてしてしまう可能性があるため、私はtmuxの外観をカスタムすることで、「どのwindow」の「どのpane」にいるか瞬時に判断出来るようにしています。

デフォルトのtmux

.tmux.confをいじらずに利用している場合

俺のtmux

俺のtmuxステータスバー

俺のこだわり

  • ステータスの色を変更 ( status-fg status-bg )
  • 画面左下にtmuxを起動しているマシンのホスト名を表示 ( status-left )
  • 画面右下に時計を表示( statu-right )
  • 今作業をしているアクティブなwindowだけ色をつける ( window-status-current-fg window-status-current-bg window-status-current-attr)
  • 今作業をしているアクティブなpaneの外枠に色をつける ( pane-active-border-fg pane-active-border-bg )
  • 長時間利用するので目に優しい配色

.tmux.confをまとめて書くと下記のような感じです

# status
set -g status-fg cyan
set -g status-bg black
 
set -g status-left-length 30
set -g status-left '#[fg=white,bg=black]#H#[fg=white]:#[fg=white][#S#[fg=white]][#[default]'
set -g status-right '#[fg=black,bg=cyan,bold] [%Y-%m-%d(%a) %H:%M]#[default]'
 
# window-status-current
setw -g window-status-current-fg black
setw -g window-status-current-bg cyan
setw -g window-status-current-attr bold#,underscore
 
# pane-active-border
set -g pane-active-border-fg black
set -g pane-active-border-bg cyan
view raw .tmux.conf This Gist brought to you by GitHub.

余談ですが、.tmux.confに記載する際に利用するset-option(セッションの設定)とset-window-option(ウィンドウの設定)は

set-option => set
set-window-option => setw

と略して書くことが出来ます。見やすい設定のために書式を統一しておきましょう。

サンプルから記載方法を学ぶ

.tmux.confのサンプルは私の利用するRHEL系OSの場合

/usr/share/doc/tmux-1.4/examples
/usr/share/doc/tmux-1.5/examples

などの配下にあるので、それを参考にすると良いかと思います。

外観を修正する際にしておくと便利な設定

外観をカスタムする際に、.tmux.confの再読みをキーバインドで割り当てておくと便利です。
設定を修正し、再読み込みをさせるですぐに結果が分かります。
私は下記のようにキーバインドを割り当てています

# prefix + r で設定ファイルを再読み込み
unbind r
bind r source-file ~/.tmux.conf

より良いターミナル、tmux環境へ

最近、社内で@banyanさんと一緒に「ターミナルマルチプレクサ勉強会」を
開催した際に、 @banyanさんのプレゼンの言葉に共感したのでそれを書いて締めたいと思います。

tmux では、空気を吸うように
画面分割をする。
動作を意識せず、直感で行う。

良いターミナル、tmux環境は、運用と開発を加速させるので、みんなでより良いものを目指していきましょう!
それでは!

ウィンドウ管理ツールWindow TidyをLaunchdで常時起動にした

windowのレイアウトを自在に変更出来るWindow Tidyを最近使い始めて、めちゃくちゃ便利なんですが、いつの間にか落ちていることが結構あったので、Launchdで常時起動させるようにしました。
動作環境はLionです。

~/Library/LaunchAgents/windowtidy.plist を用意

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>windowtidy</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Window Tidy.app/Contents/MacOS/Window Tidy</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

設定の読み込み

/bin/launchctl load ~/Library/LaunchAgents/windowtidy.plist

これでkillしても自動で起動してきて、常時起動するようになりました。Launchdで起動を制御しているので、window tidy側の設定「Launch Window Tidy at login」のチェックは外しておきましょう。

参考サイト

スーパーサーバーSupervisorの導入手順メモ

daemontoolsの代替として最近使い出したスーパーサーバーSupervisorの導入方法をいつも忘れるのでメモ。ほとんど@hibomaが社内Wikiにまとめてた内容のパクリですが、自宅サーバ構築とかで外から見ようと思っても見えないので。いつもありがとう、ひろやん!!

導入環境はScientific Linux 6.1です。(Upstart経由での起動の部分を、init経由での起動にすればCentOS 5系でも動作するかと思います)

Supervisorの導入

easy_installでも良いんですが、pipで導入

sudo yum -y install python-setuptools
sudo easy_install pip
sudo pip install supervisor


log用ディレクトリと、conf用ディレクトリを用意

デフォルトだと/tmp配下にログが吐かれるので、log用ディレクトリの作成

sudo mkdir /var/log/supervisor/

監視するデーモンごとに設定を分けるので、confディレクトリを用意

sudo mkdir /etc/supervisord.d


Supervisorの設定を用意

conf生成用のコマンドで作成

sudo echo_supervisord_conf > /etc/supervisord.conf

zshだとエラー(zsh: permission denied: /etc/supervisord.conf)が出てので、下記コマンドで生成。

sudo su - root -c "echo_supervisord_conf > /etc/supervisord.conf"

下記のように修正をしてます。

--- /etc/supervisord.conf.dist 2011-11-24 15:19:15.940609281 +0900
+++ /etc/supervisord.conf 2011-11-24 15:23:12.655620601 +0900
@@ -13,11 +13,11 @@
 ;password=123 ; (default is no password (open server))

 [supervisord]
-logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
+logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
 logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
 logfile_backups=10 ; (num of main logfile rotation backups;default 10)
 loglevel=info ; (log level;default info; others: debug,warn,trace)
-pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
+pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
 nodaemon=false ; (start in foreground if true;default false)
 minfds=1024 ; (min. avail startup file descriptors;default 1024)
 minprocs=200 ; (min. avail process descriptors;default 200)
@@ -125,5 +125,5 @@
 ; interpreted as relative to this file. Included files *cannot*
 ; include files themselves.

-;[include]
-;files = relative/directory/*.ini
+[include]
+files = /etc/supervisord.d/*.ini

監視するデーモンの設定は

/etc/supervisord.d/hogehoge.ini

みたいな感じで用意します。

Hesoを監視するようにした時の設定の/etc/supervisord.d/heso.iniは下記のような感じです。

[program:heso]
command=/var/www/heso/bin/server
stdout_logfile_maxbytes=1MB
stderr_logfile_maxbytes=1MB
stdout_logfile=/var/log/%(program_name)s.log
stderr_logfile=/var/log/%(program_name)s.log
autorestart=true
view raw heso.ini This Gist brought to you by GitHub.


Upstart経由でSupervisorを起動

Supervisor自体が落ちたら意味がないので、Upstartで経由で起動。Supervisorが落ちたらUpstartが自動で起動。
/etc/init/supervisord.conf を用意

description "supervisord"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec /usr/bin/supervisord -n

下記コマンドで起動

sudo initctl start supervisord

psなどでプロセスを確認して起動していればOK。supervisordをkillして自動で起動してくるかも確認。

CentOS 5とかならinittabに下記のような設定をして、init経由で起動させれば良いかと。

svd:2345:respawn:/usr/bin/supervisord -n
view raw etc_inittab This Gist brought to you by GitHub.


Supervisorの各種コマンド

supervisorctl
    add             -- Activates any updates in config for process/group
    avail           -- Display all configured processes
    clear           -- Clear process/multiple-process/all-process log files
    fg              -- Connect to a process in foreground mode
    help            -- Show help
    maintail        -- tail of supervisor main log file
    open            -- Connect to a remote supervisord process. (for UNIX domain socket, use unix:///socket/path)
    pid             -- Get the PID of supervisord.
    quit      exit  -- Exit the supervisor shell.
    reload          -- Restart the remote supervisord.
    remove          -- Removes process/group from active config
    reread          -- Reload the daemon's configuration files
    restart         -- Restart process or group.
    shutdown        -- Shut the remote supervisord down.
    start           -- Start process or groups.
    status          -- Get process status info.
    stop            -- Stop process or group.
    tail            -- tail of process stdout
    update          -- Reload config and add/remove as necessary
    version         -- Show the version of the remote supervisord process

たとえばHeso用の設定を変更した場合は下記みたいな感じで設定を再読込

sudo supervisorctl reread heso


supervisorctlをzshで補完

supervisorctlコマンドをzshで補完出来るように。
functionsの下に置くと、自動で読み込まれるようになります。

sudo yum -y install hg
mkdir ~/tmp
cd ~/tmp
hg clone https://bitbucket.org/hhatto/zshcompfunc4supervisor
cd zshcompfunc4supervisor
sudo cp _supervisor* /usr/share/zsh/4.3.10/functions/


参考サイト

Trac WikiをVim上で編集する

社内で利用しているTracをVim上から編集出来たら楽だなーと思ってプラグインを探していたら、
VimTracという、そこそこ良いものがありました。

利用環境はTrac 0.12.2.ja1、Vim7.3です。

Trac側のXML-RPCプラグインを利用するので、もし未導入ならtrac xml-rpc – Google 検索などを参考にして導入してください。

VimTracの導入

VimTrac – A vim client for the Trac wiki and ticketing system : vim online

プラグインの管理にはVundleを利用しているので、.vimrcか.vimrc.localに下記のような設定を書いてBundleInstall。Trac-wikiマークアップ用プラグインも一緒に導入。

Bundle 'Trac-Wikimarkup'
Bundle 'VimTrac'
let g:tracServerList = {}
let g:tracServerList['strac'] = 'http://maeda:***********@192.168.25.37/trac/server/login/xmlrpc'
let g:tracServerList['ectrac'] = 'http://maeda:***********@192.168.25.37/trac/ec/login/xmlrpc'
let g:tracServerList['hostrac'] = 'http://maeda:***********@192.168.25.37/trac/hosting/login/xmlrpc'
let g:tracServerList['sharetrac'] = 'http://maeda:***********@192.168.25.37/trac/share/login/xmlrpc'

みたいな感じで設定。***********は自分のtracログイン時のアカウントです。設定の書式は

let g:tracServerList['(Server Name)'] = 'http://(user):(password)@(trac serverpath)/login/xmlrpc'

といった形式。社内Tracは部署ごとに分かれているので、

  • strac
  • ectrac
  • sharetrac

などと私はtracServerListを設定してます。接続するときに利用します。

trac.vimの修正

Vundle経由で導入すると、trac.pyのpathを見つけられないので、~/.vim/bundle/VimTrac/plugin/trac.pyを下記のように修正

@@ -132,10 +132,10 @@
     finish
 endif

-if filereadable($VIMRUNTIME."/plugin/trac.py")
- pyfile $VIMRUNTIME/plugin/trac.py
-elseif filereadable($HOME."/.vim/plugin/trac.py")
- pyfile $HOME/.vim/plugin/trac.py
+if filereadable($VIMRUNTIME."/bundle/VimTrac/plugin/trac.py")
+ pyfile $VIMRUNTIME/bundle/VimTrac/plugin/trac.py
+elseif filereadable($HOME."/.vim/bundle/VimTrac/plugin/trac.py")
+ pyfile $HOME/.vim/bundle/VimTrac/plugin/trac.py
 else
   call confirm('trac.vim: Unable to find trac.py. Place it in either your home vim directory or in the Vim runtime directory.', 'OK')
   finish
view raw trac.vim.diff This Gist brought to you by GitHub.

trac.vimの利用方法

Tracサーバに接続

tracServerListで設定した名前で接続

:TWServer strac
:TWServer ectrac


新規wikiページの作成

:TWCreate Common/Server/Memo/maeda


wikiページの保存

:TWSave "Comment"


既存wikiページを開く

:TWOpen Common/Server/Memo/maeda

Wikiページの一覧リストからも選択可能です。

ticket一覧を表示

tracServerListで設定した名前で接続

:TTServer strac
:TTServer ectrac

ticket一覧取得は、最初利用出来なかったのですが、XML-RPCのプラグインをXmlRpcPlugin – Trac Hacksの最新のものにして利用できるようになりました。


社内tracなので当たり障りのないページを開いた感じ

社内SNSでmizzyさんから

VimTracって、編集中に別な人がページ更新した場合に、マージできるのかな?

と聞かれたので、検証をしてみたところ何の警告もなく、上書きをしてしまいました!!
doc/trac.txt at master from mjbrownie/Trac.vim – GitHubを見ても特にconflictとかmergeとかは書いてないので、機能的にないぽいです。複数人で同時に編集するようなページで利用するでは要注意です。
公式ページhttp://www.ascetinteractive.com.au/vimtracも現在はリンク切れなので、情報はかなり少なめです。

今までブラウザからのWiki編集に慣れてしまっていたので、まだ違和感がありますが、Vimで完結出来るので使い勝手良いです。

BundleInstallすると

E122: Function LoadWikiCommands already exists, add ! to replace it

とかのエラーが出るけど、ちゃんとプラグインのインストールは出来てて、実害は無さそうなのでとりあえず放置で。

About

@glidenote

このブログは個人的なものです。ここで述べられている内容は私の個人的な意見に基づくものであり、私の雇用者に一切の関係はありません