gv.vim / committia.vim

之前看到有人介紹 gv.vim 跟 committia.vim,試了一下,真的挺不錯的。

gv.vim 是 git commit browser,文件上只有提 vim-plug 的安裝,但我試的結果,Vundle 也可以安裝。
裝了以後,用 :GV 就可以開啟 commit browser,挺方便的。我之前是都到 terminal 用 tig 這個指令來看,現在有 gv.vim ,就不需要特別離開 vim ,到 terminal 了。

committia.vim 是可以在 git commit 時,可以直接看到檔案變更 (diff) 的 plugin 。之前是開另外一個 terminal ,輸入 git diff 來看 ,或是在 git commit 前先 git diff 來看。

WordPress 升級以後無法登入後台

Embed from Getty Images

才想說趕緊要把電影流水帳寫一寫,結果就遇到 WordPress 無法登入後台,查了 php-fpm slow log,發現是 media-deduper plugin 問題,使用 wp-cli 去 deactivate 以後,就可以登入了。

wp plugin deactivate media-deduper –path=’/var/www/wp’ –network

有人回報這問題了:https://wordpress.org/support/topic/504-bad-gateway-3/

但是登入以後,編輯器進不去… 見鬼了,後來發現是 MySQL的 CPU使用率超高,用 show processlist 去看,有 Waiting for table level lock 的情況。
查了以後才知道是因為 table 是 MyISAM 格式的關係。 (可以用這指令看 SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = ” AND ENGINE IS NOT NULL; ),而主因則是 media-deduper 所導致的。

要改可以用 alter table 來改 engine,可以先用下面這組指令一次產生出所有需要改格式的 alter table 敘述,再用 source 來執行。

 SET @DATABASE_NAME = 'name_of_your_db';
SELECT CONCAT('ALTER TABLE ', table_name, ' ENGINE=InnoDB;') AS sql_statements
FROM information_schema.tables AS tb
WHERE table_schema = @DATABASE_NAME
AND ENGINE = 'MyISAM'
AND TABLE_TYPE = 'BASE TABLE'
ORDER BY table_name DESC;

不過修改以後,media-deduper 還是會引起 MySQL 的 CPU 使用率超高,再去 media-deduper 的論壇找,找到這篇:Urgent: New Update Release (of 3 hours ago) broke my Website! 裏面講的,其實跟前面有人回報的問題 (504 bad gateway) 一樣,只是最後作者說有修正了,並且建議先移除 plugin 之後,再重新裝即可。於是我照著做就解決問題了。

Ansible提速

參考資料

上面的文章幾乎都是說要調整 ansible.cfg,Speeding up Ansible Playbook runs 這篇仔細說明每個參數加了之後有提速多少,很有參考價值。accelerate mode 則是加在 playbook 裡。

ansible.cfg 主要關鍵設定:

  1. pipeline = True
  2. control_path = /tmp/ansible-ssh-%%h-%%p-%%r
  3. ssh_args = -o ControlMaster=auto -o ControlPersist=60s
  4. poll_interval = 5
  5. forks = 25
  6. fact_caching = jsonfile
  7. fact_caching_connection = /tmp/.ansible_fact_cache

補充 ssh_args 的說明,這兩個設定主要是在會一直頻繁使用時,可以重複使用連線並避免太快斷線。

也可以加上 UseDNS = no,避免使用 DNS 反查。

s3cmd

想要把某個 bucket 裡的檔案備份到另外一個 bucket 去,找了一下,在 StackOverflow 上找到這篇:How do I back up an AWS S3 Bucket without versioning the source bucket [closed]
裏面提到一個 s3cmd 的指令,只要簡單的下 s3cmd –recursive cp s3://bucket_a s3://bucket_b 就可以了。
在 Ubuntu 14.04 裡只要使用 sudo apt install s3cmd 就可以安裝了,使用前需要使用 s3cmd –configure 來設定 AWS Access key id 跟 secret access key。
想要安裝最新版本,可以使用 pip 來安裝。

Bash 4 的 hash table

Python 的 dict 很方便,寫 bash 時,自然會想 Bash 到底有沒有這個呢?

在 StackOverflow 上找到這篇 How to define hash tables in Bash? ,裏面就介紹了用法:

# Python 語法:animals = {'moo': 'cow', 'woof': 'dog'}
# 宣告1
declare -A animals
animals=( ["moo"]="cow" ["woof"]="dog")
# 宣告2
declare -A animals=( ["moo"]="cow" ["woof"]="dog")
# 取某個鍵對應的內容,跟 Python 的 animals['moo'] 一樣。
echo "${animals[moo]}"
# Iterate
for sound in "${!animals[@]}"; do echo "$sound - ${animals[$sound]}"; done

棄用 keepass2,改用 keepassxc

最近用 keepass2 時,碰到 plugin 不能用的錯誤訊息,更新 plugin 以後也無法解決。在找解決方法時,意外看到 keepassxc ,一樣是跨平台,再加上瀏覽器的整合也不錯,乾脆就跳槽了。

使用方法不難:

我先用網站提供的 PPA 來安裝,執行 keepassxc ,可以開啟 keepass2 的資料檔案,但設定上遇到問題。我碰到的問題是在 Preferences 裡找不到 Browser integration 這選項,利用 Google 搜索以後 (詳見 keepassxc FAQ這個 issue),才知道是因為 Ubuntu trusy 的 QT 版本問題,所以無法提供 Browser integration,得使用網站提供的 AppImage 檔案才行。

所以就下載 AppImage 檔案,用 chmod +x 為檔案增加執行權限再執行,就可以看見,並且 Browser 的 extension/addon 也可以順利的跟 keepassxc 連接。

Ansible 需要 python

如果目標主機上沒有 Python 時,Ansible 可是會抗議的。

所以必須要先幫目標主機裝上 Python 才行。首先要將 gather_facts 設定為 no,再利用 raw 模組來進行安裝,不使用 gather_facts 跟其他模組的原因是這些都會使用到 Python。

---
- hosts: all
  become: yes
  gather_facts: no
  tasks:
    - name: install python in Ubuntu
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)

上面用的是 apt,表示適用於 Debian/Ubuntu 等 debian-like 的 distro,至於其他的 distro ,就看套件管理程式是什麼囉。

加速 SSH X11 forward 的速度

今天用 SSH 連回家裡電腦,開 thunderbird ,發現慢的要命,心裡想,照理來說,不傳圖片應該會比較快,難道是傳輸的內文太多嗎? 就利用 Google 找了一下,結果說是加密速度問題,改用較快的加密協定就可以了。

文章裡說改用 arcfour,blowfish-cbc ,但因為安全性的關係,新的 OpenSSH server 都沒有啟用,所以退而求其次,改用 aes128-ctr, aes192-ctr ,的確速度快了很多。

ssh -YC4c aes128-ctr,aes192-ctr user@server

 

另外附上測試 SSH 加密速度的腳本網址:https://gist.github.com/graysky2/0e265604bfd4856a2596

 

參考資料:

Pro Vagrant

今天翻了 Pro Vagrant 這本書,這本書是 2015 年出版的,到今天書裡已經有些資訊是過時的了。不過裏面介紹了如何使用 vagrant 、如何打包 box 以及使用上的觀念與建議作法,還蠻實用的。

書本的原始碼都在 https://github.com/pro-vagrant

裏面提到的 atlas.hashicorp.com ,現在 Hashicorp 已經不再繼續,並且在 2018/6/27 之後,hashicorp 把 vagrant boxes 都搬到  https://app.vagrantup.com 了,Ubuntu 的 box 都在 https://app.vagrantup.com/ubuntu

作者的網站 http://boxes.gajdaw.pl 也已經消失了。

幾個有用的 plugin:

另外就是知道有 vagrant share 跟 vagrant connect 這兩個子指令,vagrant share 可以讓 VM 有個對外的網址,對方或伙伴就可以用 vagrant connect 連上 VM 提供的服務,說真的,這兩個指令蠻讓我驚訝的。

chronograf 使用 gitlab oauth2 認証

去年11月在試 chronograf 1.3 使用 gitlab oauth2 來認証登入時,沒試出來,就去 chronograf github repository 那邊發了 issue ,後來4月左右出了 1.4,開發者有回應,請我再試試看,昨天終於試出來了,下面紀錄一下過程與設定。

  1. 首先去 gitlab admin 頁面 (我用的是 gitlab 8.13.6) 新增 application,在 callback url 填入”http://your_chronograf_server:8888/oauth/generic/callback”。新增以後,把 “applicatoin id” and “secret” 記下來,後面會拿來填入 chronograf 的 “–generic-client-id ” / “–generic-client-secret “。
  2. 在啟動 chronograf 時,帶入以下參數 (gitlab_server 與 chronograf_server 請自行替換):
    • –generic-auth-url=http://gitlab_server/oauth/authorize?redirect_uri=http%3A%2F%2Fchronograf_server%3A8888%2Foauth%2Fgeneric%2Fcallback&response_type=code
    • –generic-token-url=http://gitlab_server/oauth/token?redirect_uri=http%3A%2F%2Fchronograf_server%3A8888%2Foauth%2Fgeneric%2Fcallback&grant_type=authorization_code
    • –generic-scopes=api
    • –token-secret=mysupersecret
    • –generic-api-url=http://gitlab_server/api/v3/user
  3. 到 chronograf 頁面時,就會看到 “Login with Generic”,點選按鈕,瀏覽器會帶到 gitlab 的登入頁面,在登入以後,會再帶回到 chronograf purgatory 頁面。

到這邊就算是成功了。但是我還沒去試怎麼去給予權限,讓使用者不要只能停留在 purgatory 頁面。