2014年11月20日 星期四

[Linux] Docker 使用筆記 - 透過 Supervisor 開機執行多項服務(SSH, Apache Web Server, MySQL) @ Ubuntu 14.04

前陣子把玩 Docker 時,粗略知道運作是著重落在一隻 process 上,在 Dockerfile 中雖然可以執行多個 CMD,但擺上多隻會共同運行指令仍是無效的。解法的原理就是執行一隻程式,由那支程式管理其他程式。在 Docker 官方文件上就是使用 Supervisor 啦:Using Supervisor with Docker

Dockefile:

FROM ubuntu:14.04
RUN apt-get update
RUN sudo -E "DEBIAN_FRONTEND=noninteractive" apt-get install -y telnet curl mysql-server-5.6 apache2 php5 php5-mysql openssh-server supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/mysqld /var/run/sshd /var/log/supervisor

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN mkdir -p /root/.ssh/ && chmod 700 /root/.ssh/
COPY id_rsa.pub /root/.ssh/authorized_keys
RUN chmod 600 /root/.ssh/authorized_keys

EXPOSE 22 80 3306
CMD ["/usr/bin/supervisord"]


supervisord.conf:

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"

[program:mysql]
command=/usr/bin/pidproxy /var/run/mysqld/mysqld.pid /usr/sbin/mysqld
autorestart=true


id_rsa / id_rsa.pub:

$ ssh-keygen -t rsa -f id_rsa -P''

上述檔案都在同一層目錄中,分別有 Dockefile, supervisord.conf, id_rsa 和 id_rsa.pub ,共四個檔案。

接著建立 Image:

$ cd /path/dir
$ sudo docker build -t dev .


運行,將本機端的 10022, 10080, 13306 分別對應到 Docker Container 的 22, 80, 3306:

$ sudo docker run -t -p 10022:22 -p 10080:80 -p 13306:3306 dev
/usr/lib/python2.7/dist-packages/supervisor/options.py:295: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '
CRIT Supervisor running as root (no user in config file)
WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
INFO RPC interface 'supervisor' initialized
CRIT Server 'unix_http_server' running without any HTTP authentication checking
INFO supervisord started with pid 1
INFO spawned: 'sshd' with pid 9
INFO spawned: 'mysql' with pid 10
INFO spawned: 'apache2' with pid 11
INFO success: sshd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
INFO success: mysql entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
INFO success: apache2 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
...


也可以按 ctrl+c 離開,但 VM 還會繼續跑

遠端登入:

$ ssh -i /path/dir/id_rsa root@localhost:10022

若有需求想要 console 端登入:

$ sudo docker ps -a
$ sudo docker inspect --format '{{.State.Pid}}' CONTAINER_ID
$ sudo nsenter --target PID_NUMBER --mount --uts --ipc --net --pid


收工!

2014年11月13日 星期四

[SQL] 從 Table 中取出 Column 1 取資料並塞進 Column 2 方法 @ MySQL 5.6

有一張 table 儲存 field1, field2 為原始資料,由於資料尚未驗證,想要透過 REGEXP 驗證資料結果,這時就想到每次找都要驗證一次有點煩,所以就乾脆建立 field3,將驗證過的資料儲存起來。

至於解法方面,就單純把撈出來的資料再 INSERT 回去 :P 假設有一張 Table 名為 mTable,其中 key 為 field1, field2,而 field3 預設可以為空,目標則是將 field1 驗證過的資料儲存在 field3,未來使用時就可以透過 field3 來當作資料驗證。

新增資料用法:

INSERT INTO (field1, field2) VALUES ... ;

驗證 field1 資料,把正確格式擺進 field3:

INSERT mTable (field1, field2, field3)

SELECT field1, field2, field1 AS field3 FROM mTable WHERE field3 IS NULL AND field1 REGEXP '^[0-9A-Za-z]+$'

ON DUPLICATE KEY UPDATE

field3 = VALUES(field3)


如此一來,就完成驗證 :P

接著應該也可以嘗試 MySQL trigger 用法?不過,驗證資料這件事應該不要叫 DB 做才對 XD 只是...環境所限啊。

2014年11月12日 星期三

[Linux] 使用 awk, sort, uniq, grep, dig 批次驗證 email address @ Ubuntu 14.04

工作時中,偶爾會對 UNIX 系統感動! 雖然有時很簡單的任務會龜毛到硬要連續指令來處理,明明寫一小段 code 就好了啊 Orz

取處已知清單中的 email domain:

$ mysql -e "select email from db.user;" > email.log;
$ sort email.log | uniq | awk -F'@' '{print $2}' | sort | uniq | grep "." > email.domain.log


最後的 grep "." 則是要避開 hostname 不合法的項目(如 localhost 等)。

驗證 mx:

$ dig -t mx test.com +short | wc -l
$ test `dig -t mx test.com +short | wc -l` -ne 0 && echo "ok"


批次驗證:

$ awk '{ system("test `dig -t mx "$1" +short | wc -l` -ne 0 && echo "$1)}' email.domain.log > email.validated.log

2014年11月11日 星期二

[Linux] c++: internal compiler error: Killed (program cc1plus) @ Ubuntu 14.04

最近 編譯 C++ 的 source code 時,開始會蹦出奇怪的狀態:

c++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.

See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions.


一開始碰過一兩次後,重開機真的又沒事,但些會開始又常碰到,接著猜想是不是 memory 不夠了,除了用 free -m 觀察外,也把一些 daemon 關掉,真的猜中了 :P

大概是因為我租的機器只有 512MB RAM 啦 Orz

2014年11月8日 星期六

[Linux] 在 UTF8 Terminal 環境下,找尋 Big5 檔案內的關鍵字 @ Ubuntu 14.04

最近想要研究一些老檔案,都是 Big5 編碼,接著就想著到底該怎樣叫 grep 幫你查資料,因為 pattern 要用 Big5 編碼,但 Terminal 是 UTF-8 環境。

我想到的是先把 pattern 轉成 big5 後,透過 xxd 轉乘 hex code,然後一樣將 input 透過 xxd 輸出後用 grep 去找,結果堪用,但不是很方便。

問一下高手,高手獻技:

$ grep -r `echo -ne "大學" | piconv -f utf8 -t big5` target_files

還差一點,因為 Terminal 環境編碼關係,解法:

$ LC_ALL=C grep -r `echo -ne "大學" | piconv -f utf8 -t big5` target_files

此時 Terminal 輸出會亂掉,所以再把 output 從 Big5 轉成 UTF-8:

$ LC_ALL=C grep -r `echo -ne "大學" | piconv -f utf8 -t big5` target_files | piconv -f big5 -t utf8

若想要有 grep highlight ,那就再叫 grep 做一次吧 XD

$ LC_ALL=C grep -r `echo -ne "大學" | piconv -f utf8 -t big5` target_files | piconv -f big5 -t utf8 | grep "大學"

收工!