Windows Subsystem for Linux

ref:
Windows Subsystem for Linuxをインストールしてみよう!

Windows Subsystem for Linux Installation Guide for Windows 10

https://qiita.com/kikuchi_kentaro/items/9dc441f64c17a38aad96

WSLとHyperを利用した環境を整える

導入

管理者権限でPowerShellを開いて以下を実行する。

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

その後、PCを再起動する。

C:\\Windows\\System32\\bash.exeを実行すると対話型shellが起動する。

最初にパッケージをアップデートする。

sudo apt update
sudo apt upgrade

PATH設定

デフォルトでWindowsのパスを自動で読み込む。

# export JAVA_HOME='/mnt/c/tools/jdk1.8.0_181'
export PATH=''
export PATH=$PATH:'/usr/local/sbin'
export PATH=$PATH:'/usr/local/bin'
export PATH=$PATH:'/usr/sbin'
export PATH=$PATH:'/usr/bin'
export PATH=$PATH:'/sbin'
export PATH=$PATH:'/bin'

jdkを入れる

# jdk10
sudo apt install default-jre

# jdk8
sudo apt install openjdk-8-jre-headless

.bashrcにjavaのパスを追記する。

# これで /usr/bin/java を見に行く
$JAVA_HOME='/usr'

jq

sudo apt install jq

Docker

Docker for Windowsを使う場合はエイリアスの設定のみ。

alias docker='docker.exe'

ref:
How to completely uninstall docker

使い方

Windowsのファイルにアクセス

/mnt/c

WSL用の.bashrc

#!/bin/sh

bashrc_dir=`dirname ${0}`

# PATH
export JAVA_HOME='/usr' # -> /user/bin/java
echo $JAVA_HOME
export PATH=$PATH:'/usr/local/sbin'
export PATH=$PATH:'/usr/local/bin'
export PATH=$PATH:'/usr/sbin'
export PATH=$PATH:'/usr/bin'
export PATH=$PATH:'/sbin'
export PATH=$PATH:'/bin'

# .bashrc
touch ~/.bashrc2
source ~/.bashrc2

# download
download() {
    save_path='~/downloads/__bashtemp'
    mkdir -p ${save_path}
    (cd ${save_path} && curl -LO ${1})
}

# Proxy
export no_proxy=*.example.com,127.0.0.1,localhost

# .bashrc
alias bashrc='source ~/.bashrc'

# confirm
alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'

# kafka
kafka-reset-topic() {
    kafka-topics.sh --zookeeper localhost:2181 --delete --topic $1;
    kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic $1;
}
# kafka-send my-topic a.json
kafka-send() {
    cat $2 | jq -c . | kafka-console-producer.sh --broker-list localhost:9092 --topic $1
}

# git
alias gadd='git add .'
alias gstatus='git status'
alias gcommit='git commit -a -m'
alias gpush='git push origin'
alias gchmodx="git update-index --add --chmod=+x"

# .exe
alias docker='docker.exe'
alias kubectl='kubectl.exe'
alias docker-compose='docker-compose.exe'

# docker
alias dps="docker ps --format '{{.Names}}\n  -> {{.Image}}\n'"
alias dpss="docker ps --format '{{.Names}}\n  -> {{.Image}}\n  -> {{.Ports}}\n'"
alias dpsss="docker ps --format '{{.Names}}\n  -> {{.Image}}\n  -> {{.Ports}}\n  -> {{.Status}}\n  -> {{.Command}}\n'"
alias drun='winpty docker run -it --rm'
alias dimages="docker images --format 'table {{.ID}}\t{{.Repository}}\t{{.Tag}}' | awk '! /k8s.gcr.io/'"
alias dlog='docker logs -f'
alias dclean='docker rm `docker ps --filter "status=exited" -q`'
alias dprune='docker system prune && docker volume prune'
alias drmi="docker rmi --force $(docker images -q) "
alias drm='docker rm --force $(docker ps -a -q)'
# drm() { docker rm "$(docker ps -a -q)" ; }
dsh() { docker exec -it $1 bash ; }
dbuild() { docker build -t $1 . ; }

# kubectl
alias kpods='kubectl get pods'
alias kservices='kubectl get services'
alias kdeployments='kubectl get deployments'
alias knamespaces='kubectl get namespaces'
alias kapply='kubectl apply -f'
alias kdelete='kubectl delete deployment'
alias kprune='kubectl delete deployments --all; kubectl delete services --all; kubectl delete configmaps --all; kubectl delete namespaces --all;'
alias kswitch='kubectl config set-context $(kubectl config current-context) --namespace'
kpod() { kubectl get  pods --selector name=$1 -o jsonpath="{.items[0].metadata.name}" ; }
klog() { 
    [ -z $2  ] && c="" || c="-c $2";
    kubectl logs $c -f `kubectl get pods --selector name=$1 -o jsonpath="{.items[0].metadata.name}"`;
}
ksh()  {
    [ -z $2  ] && c="" || c="-c $2";
    kubectl exec $c -it `kubectl get pods --selector name=$1 -o jsonpath="{.items[0].metadata.name}"` sh ;
}
alias kdebug="winpty kubectl run -it --rm tmp-pod --image=ellerbrock/alpine-bash-curl-ssl bash"



# helm
happly() { helm del $1 --purge; helm install . --name $1 ; }
hdel() { helm delete $1; helm del $1 --purge ; }

# mysql-monitor [query] -P [port]
# mysql-monitor "select * from db.table"
mysql-monitor() { 
    [ -z $2  ] && P="" || P="-P $2";
    while :; do mysql ${P} -u root -ppassword -e "${1}"; sleep 0.4; done 
}

# Add Environment paths:
# /c/tools/***/bin
for FILE in $(find /mnt/c/tools -maxdepth 2 -type d -name "bin"); do
    echo "Add env path: $FILE"
    export PATH=$PATH:$FILE
done