atuin

atuin 可以把指令歷史改放到 sqlite db 裡,提供了 fancy 的畫面跟功能,還蠻酷的。它在雲端有提供 sync 伺服器,可以把指令同步上去,我本來在想這會有安全疑慮吧,畢竟有些時候指令就會帶密碼。不過,atuin 已經有做加密了,放上去是安全的。

atuin sync 伺服器也可以自行架設,除了可以同步之外, sync 伺服器還有提供一個類似 github graph 的功能,可以看到活動圖,蠻有趣的。

安裝

bash <(curl https://raw.githubusercontent.com/ellie/atuin/main/install.sh)

如果想試試預設的 sync 伺服器,就先用 atuin register 來註冊

atuin register -u <USERNAME> -e <EMAIL> -p <PASSWORD>

若不需要,就匯入目前的 history

atuin import auto

設定

如果是使用 bash ,就用以下指令,把設定放到 .bashrc

curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
echo 'eval "$(atuin init bash)"' >> ~/.bashrc

若是 zsh ,就改用以下指令,把設定放到 .zshrc 裡

echo 'eval "$(atuin init zsh)"' >> ~/.zshrc

設定好,就關掉終端機,開新的終端機,或者是登出,重新再登入,設定就生效了。

使用

使用很簡單,按下 ctrl+r 或是按上,就會看到 atuin 的畫面,輸入字母,上方就會篩選出相關的指令。

那也可以打指令去找,例如找昨天下午3點以前的 ls 指令

atuin search --exit 0 --after "yesterday 3pm" ls

相當簡單。

架設 sync 伺服器的部份有空再來試好了,目前就先都以本機使用為主。

想法

其實就現在來說,使用 ctrl+r 就已經很方便,而且也不需要安裝,直接就能使用。未來會有需要集中管理指令歷史功能的強烈需求嗎?我想可能是沒有,就先用一陣子試試看好了,一周以後再來評估是否要繼續使用。

HAProxy

去年因為需要建置 OpenShift 容器平台而接觸到了 HAProxy,才知道為什麼 AWS 上會有分 Application Load balancer 等不同類型的負載平衡器。

簡單的說,HAProxy 的負載平衡模式分為兩種,一種是 HTTP mode,一種是 TCP mode;也就是 Layer 7 跟 Layer 4。建置 OpenShift 容器平台時,是使用 TCP mode ,Layer 4 的模式,當 HAProxy 收到連線請求時,是直接看哪一個 backend node 可以用,就直接左手給右手,連線到該台 backend node。那 HAProxy 也可以設定為 HTTP mode ,在這種模式下,就像是 nginx/apache 的 reverse proxy ,可以在 frontend 加上憑證跟做 VirtualHost。

安裝很簡單,Ubuntu 跟 RHEL 都有提供

sudo apt install -y haproxy
sudo yum install -y haproxy

安裝以後,設定檔位置是在 /etc/haproxy.cfg

主要需要調整的是 frontend 跟 backend

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 10s
        timeout client  60s
        timeout server  60s
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend http_bind
        bind *:80
        mode tcp
        option tcplog
        default_backend router_http


frontend https_bind
        bind *:443
        mode tcp
        option tcplog
        default_backend router_https

backend router_http
        mode tcp
        server router_server router:80

backend router_https
        mode tcp
        server router_server router:443

frontend 決定 haproxy 要 listen 哪一個 port,backend 決定是由哪個 node 來處理連線,很容易理解。(聽同事說,這跟 F5 的設定也很接近。)

在 RHEL 會比較容易遇到 HAProxy 有問題,主因是 SELinux ,在啟動 HAProxy 前,需要先使用 setsebool 來設定:

getsebool -a | grep haproxy_connect_any
setsebool haproxy_connect_any on

跟 HAProxy 相關的 SELinux 設定可以用 man 8 haproxy_selinux 來查詢。

參考資料