ラベル シェルスクリプト の投稿を表示しています。 すべての投稿を表示
ラベル シェルスクリプト の投稿を表示しています。 すべての投稿を表示

2019年6月28日金曜日

kubectl コマンド実行時に対象となるクラスターを表示し、実行を保留して誤爆を防ぐ kubectl ラッパーを書いた

kubectl コマンドで破壊的オペレーションを実行する際、実行時の current-context を出力することで、ユーザーに実行対象の Kubernetes クラスターを確認させ、かつユーザーの入力があるまで実行を保留し誤爆を防いでくれるやつです。

# Run the `source` command to load this file into the current shell like follow:
#   $ source kubectl-wrapper.sh

# kubect-wrapper displays the current-context and prompts for user confirmation
# when trying to execute kubectl's subcommands that can update the cluster's state.
kubectl() { (
  # if has 1 argument || has -h --help --dry-run option || Sub-commands that are not dangerous if executed without confirmation; then
  if [ $# -le 1 ] || { echo " $* " | grep -Eq -- ' -h | --help | --dry-run '; } \
    || { echo " $* " | grep -Eq ' api-resources | api-version | cluster-info | completion | config | describe | diff | explain | get | logs | top | version | wait '; }; then
    command kubectl "$@"
    return $?
  fi
  printf "\e[01;33m%s\n%s\e[0m" "# CurrentContext: " "#   "
  command kubectl config current-context
  printf "\e[01;33m%s\e[0m" "# Press Enter key to continue... "
  read -r
  command kubectl "$@"
)}

GKE に限らずですが、複数の k8s クラスターを使い分けてると、 context を誤爆しそうで怖かった。

gcloud container clusters get-credentials --zone asia-east1-a --project MY_PROJECT MY_KUBERNETES_CLUSTER_NAME

動作確認は bash 入りの自機でしかしていないので悪しからず…。

2019年6月16日日曜日

WSL1 上で Docker をカジュアルに利用する

2019-06-18 ここから追記。

元々書いていた方法だと、 build image 時に以下の問題が有ると怒られたので、おとなしく DOCKER_HOST 環境変数を使うようにしました…。

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have ‘-rwxr-xr-x’ permissions. It is recommended to double check and reset permissions for sensitive files and directories.

以下、その手順。

1. タスクトレイアイコンの Docker を右クリック → Settings から設定を開く

2. □ Expose daemon on tcp://localhost:2375 without TLS にチェックを入れる

3. DOCKER_HOST 環境変数を Docker for Windows に向ける

export DOCKER_HOST=tcp://localhost:2375

~/.bashrc は dotfiles リポジトリで一括管理しているので、
Mac でも Windows でも同じ ~/.bashrc を使いたい。
ので、以下のようにいい感じに追記した。

# If exist docker.exe in $PATH, set env:DOCKER_HOST.
command -v docker.exe && export DOCKER_HOST=tcp://localhost:2375

4. docker run hello-world

$ curl -LRsS https://get.docker.com/ | bash
   :
   :
$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

負けた。


2019-06-18 ここまで追記。

以下、元の記事を残しますが、非推奨です。


「Ubuntu いくつと Docker いくつ、特定のバージョン同士だと動作する!」とか「DOCKER_HOST=tcp://localhost:2375」みたいなのに疲れた人用。

1. (インストール済みの場合) WSL 上でインストールした Docker を削除する

最終的に以下な状態になれば OK 。

$ docker
docker: command not found

お手元の環境に合わせて適切に削除してください。

apt purge docker.io
apt purge docker-ce

など。

2. Docker for Windows をインストールする

こちら

コマンドプロンプト上から docker.exe が実行できることを確認して下さい。

C:\Users\djeeno>docker.exe

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

...

実行できない場合は PATH が通ってないです。
Docker for Windows インストール後に一度も再起動していない場合は、再起動して OS 環境変数の PATH を読み直し。

3. WSL の PATH の通ったディレクトリ配下に以下のファイルを設置する。

こちら

手っ取り早く /usr/local/bin/docker にインストールしたい場合は以下を実行してください。

sudo curl -LRsS https://raw.githubusercontent.com/djeeno/windows/master/docker -o /usr/local/bin/docker
sudo chmod +x /usr/local/bin/docker

以下、ファイルの中身を転記。

#!/bin/sh

# This script executes a command with the same file name as the executable file itself,
# or a file name with ".exe" added after that.
# It's just like BusyBox.

command_name=$(basename "$0")

if command -v "${command_name}.exe" >/dev/null; then
  exec "${command_name}.exe" "$@"
else
  exec "${command_name}" "$@"
fi

ざっくり言うと、このファイルを docker という名前で実行すると、 PATH 配下にある docker.exe を実行してくれます。

おわり

$ type docker
docker is /usr/local/bin/docker

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

シンプル。

2019年3月13日水曜日

diff コマンドに色をつけたいけど colordiff みたいな別のコマンドはインストールしたくない

色付き diff が見たいたいけど colordiff みたいな別のコマンドはインストールしたくないシーンがあったので、 プレーンな diff コマンドに bash で、というか主に sed で、無理やり色をつけました。
ssh先 ( 最近だと kubectl exec ですかね ) のサーバーで色付き diff が欲しい時に重宝します。

difff() {(R=$(printf '\e[31m');G=$(printf '\e[32m');B=$(printf '\e[36m');W=$(printf '\e[1m');N=$(printf '\e[0m');diff -u "$@"|sed $(uname -s|grep -q '^Darwin'&&printf -- -E||printf -- -r) "s/^(@@.+@@|@@.+@@)$/$B\1$N/g;s/^(\+.*)$/$G\1$N/g;s/^(\-.*)$/$R\1$N/g;s/^[^\+\-]*((\+{3}|-{3}) [^ ].*)/$W\1/g;")}

実行例はこちら。

$ difff A.txt B.txt

git diff で見慣れた感じで出すようにしています。

Ubuntu 16.04, 同 18.04, CentOS 7, Alpine Linux (2019-03-12 時点で latest で降ってきたやつ) 辺りで動作確認済み。

-r オプションないし -E オプションがある sed + POSIX な環境であれば動くんじゃないかなと思います。

production 環境で apt install colordiff とか yum install colordiff とか apk --add colordiff なんて罪深いことはやりたくないです。

2018年2月6日火曜日

【1分かからずにできる】Linuxから(curlで)LINEに通知メッセージを送る - LINE Notify

めちゃくそ簡単でした。

以下のURLにアクセスします。

https://notify-bot.line.me/my/

「トークンを発行する」をクリックします。

「トークン名」を入力し、

通知の送信先を選択し、トークンを発行します。

※自身もしくはグループのみが選択できます。
 画像は自身への通知(1:1でLINE Notifyから通知を受け取る)を選択しています。

「発行したトークン」をコピーします。

以下を実行します。

curl https://notify-api.line.me/api/notify -X POST -H 'Authorization: Bearer <発行したトークン>' -F 'message=帰りたい'

おわり。

以下、レスポンス。

{"status":200,"message":"ok"}

2018年1月28日日曜日

CIDR表記のネットワークアドレスを計算する/指定したネットワークに属するIPアドレス一覧を出力する/そのIPアドレス群のPing応答を確認するスクリプト

CIDR表記のネットワークのネットワークアドレスを出力したり、
ブロードキャストアドレス以外のIPアドレスを一覧出力したり、
またそのIPアドレス群のPing応答を確認したりするbashスクリプトを、
個人的に欲しかったので、作りました。

https://gist.github.com/djeeno/fcd09b666a8e7bd6fc356b5f410f0ff5

CentOS7、Ubuntu16.04、Ubuntu16.04(WSL)、Debian9、macOS High Sierraで動作を確認しています。

使い方を以下に例示。

# IPアドレスを一覧出力: netaddr
$ ip.sh netaddr 192.168.100.50/29
192.168.100.48

# IPアドレスを一覧出力: list
$ ip.sh list 192.168.100.0/29
192.168.100.49
192.168.100.50
192.168.100.51
192.168.100.52
192.168.100.53
192.168.100.54

# IPアドレスに対してPingを飛ばし応答のあったアドレスを出力: inuse
$ ip.sh ping 192.168.100.1/29
192.168.100.49  ttl=64 Linu   time=7.43ms
192.168.100.50  ttl=128 Windows time=8.88ms
192.168.100.54  ttl=64   Linux    time=5.69ms

Ping応答のttlからOSを(ある程度)判別できるので、
「これが何のOSなのか」も横に出力するようにしました。

(と言っても、Linux or UNIX or Windowsの3択ですが…)


適当に立てたマシンがDHCPからどのIPアドレスを振られているか調べる時とか、非常に便利です。