7/28/2016

Make Mac xcode settings better

Preferences -> Text Editing -> Check numbers
Preferences -> Text Editing -> Check Page guide at column: 80
Preferences -> Text Editing -> Indent width: 2
Preferences -> Fonts and Colors -> Shift to select them all, change the font to Consolas, Size 13

Make Linux screen command settings better

==== Copy to ~/.screenrc
defscrollback 99999
altscreen on
vbell on
vbell_msg ''
termcapinfo *  vb=:

7/22/2016

Java commonly used util functions

Jave:
=====
Paths.get(a, b);  // path join
String.format("%s, haha ", a);

Dependency Injection: Guice
https://github.com/google/guice/wiki/Motivation

https://github.com/google/guice/wiki/GettingStarted

Java docs:
==========

http://www.grpc.io/grpc-java/javadoc/io/grpc/stub/StreamObserver.html

Server:
=======
Apache Struts
Apache Tiles

Jetty as the servelet container

Shell/Bash frequently/commonly used commands

Bash Commands:
==============
# Find all cpp files with debug in it.
$find . -name \*.cpp -exec grep -q "debug" '{}' \; -print

# Import Bash script tips
http://www.davidpashley.com/articles/writing-robust-shell-scripts/

if [ -z "${ZONE-}" ]  # if ZONE is null
if [ -n "${ZONE-}" ]  # if ZONE is non-zero
if [ -f "/cluster/env.sh" ] then  # if file exists
  echo "haha"
fi

RSRC=${1:-"/var/www"}  # default to /var/www if the first arg is null.

# Get group members
getent group docker


status (an init conf file in /etc/init/*.conf)

# Rename file extensions
for i in `ls --color=never`; do j=`echo $i | awk -F "." '{print $1}'`; echo $j; mv $i $j.bin; done
# Rename a file with leading 0 prefix
for i in `ls *.jpg`; do j=`echo $i | awk -F "." '{print $1}'`; k=$(printf "%06d" $j); mv $i $k.jpg; done

# To find and replace
sed -i 's/haha/xixi/g' $input
use -i to change the input file directly


for i in `grep -r "2009 Google" * -l`; do sed -i 's/2009\ Google/2010\ Google/g' $i; done
for i in `find . -name BUILD`; do echo $i; sed -i 's/\"\/\/third_party/\"@deepmap_base\/\/third_party/g' $i; done
for i in `find . -name BUILD`; do for j in common point_cloud protos third_party public anga_clients; do sed -i "s/\"\/\/$j/\"@deepmap_base\/\/$j/g" $i; rm -rf $j; done; buildifier $i; done
for i in `find . -iname "SCon*"`; do if grep -q "\\.a" $i; then echo $i; fi done

# Loop a bash array
ids=(1 2 3)

for i in ${ids[@]} do echo $i; done;

for i in {0..10}; do echo $i; done;

# Loop file content: each line
cat run.txt | while read line; do echo $line; done

# Change file name
for i in `cat files.txt`; do echo $i; j=`echo $i | awk -F hgt '{print $1}'`; mv $i ${j}.hgt.zip; done

# Change files names based on pages
j=0
for i in `ls *.pdf`; do mv $i "第${j}页.pdf"; j=$(($j+1)); done

# Sort by size
du -sh * | gsort -h

# cd to real path of a link
cd -P

# List files with full absolute path
ls -d -1 $PWD/*

# tar filter
find . -name '*.php' | tar -cvjf my.tar.bz2 --files-from -

http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O - | tar -xz
The wget option -O specifies a file to which the documents is written, and here we use -, meaning it will written to standard output and piped to tar and the tar flag -x enables extraction of archive files and -z decompresses, compressed archive files created by gzip.

# To get computer hardware info
inxi

Kubernetes frequently used commands

Kubernetes:
===========
# Start cluster
# install virtualbox. Enable VT-x and disable secure boot from BIOS if applicable. Then install minikube and kubectl
minikube start

# China commands within GFW and use Clash for Windows as the proxy

# 192.168.1.30 is the host Linux machine's physical network card ip.
# Clash for Windows needs to turn on Allow LAN
minikube start --vm-driver="docker"  --docker-env HTTP_PROXY=http://192.168.1.30:7890 --docker-env HTTPS_PROXY=http://192.168.1.30:7890 --docker-env NO_PROXY=127.0.0.1 

# Debug command: 

minikube ssh

docker@minikube: docker info

docker@minikube:ping www.google.com

docker@minikube: docker pull docker.io/kubernetesui/dashboard:v2.7.0

docker info

minikube dashboard # To access the dashboard
 
# Check namespace
minikube get namespace
# Delete a terminating namespace
NS=`kubectl get ns |grep Terminating | awk 'NR==1 {print $1}'` && kubectl get namespace "$NS" -o json   | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/"   | kubectl replace --raw /api/v1/namespaces/$NS/finalize -f -
# Get logs
minikube logs --file=logs.txt

Failed? BE CAREFUL, ALL SERVICES WILL BE GONE. Try:
$minikube stop
$minikube delete && rm -rf ~/.minikube && rm -rf ~/.kube

k cluster-info
kubectl create ns minio-dev # create namespace
k describe pod [pod name]
k get nodes --show-labels
kubectl cordon [node-name]  # Prevent a node from scheduling jobs
kubectl uncordon [node-name] # Resume scheduling jobs on node
k get pod [pod name] -o go-template={{.status.podIP}}
k get pods
k get pods -n [minio-dev]  # -n means namespace
k delete pod [pod name]
k delete pod [pod_name] -n [namespace]
k delete -n default pod [your-pod-name]
k run hello-node --image=hello:v2 --port=8088
k expose pod [pod name] --port 8088
k scale deployment hello-node --replicas=4
k edit deployment hello-node
k apply -f [?]/a.yaml
k get deployment my-nginx -o yaml
k logs [pod id]
k get nodes --show-labels
k port-forward pod/minio 9000 9090 -n minio-dev # Forward port
k port-forward svc/minio -n minio-dev 9090  # Forward port
k rollout restart deployment [deployment_name] -n [namespace]
# Get bash access
kubectl exec -it [pod name, e.g. minio-766dbb8d6d-b6fcq] -n minio-dev  -- /bin/bash

#Careful, service will be down
kubectl replace -f docs/user-guide/nginx/nginx-deployment.yaml --force

http://kubernetes.io/docs/user-guide/docker-cli-to-kubectl/

Kubernetes cluster is running.  The master is running at:

  https://52.34.246.212


The user name and password to use is located in /home/liangzou/.kube/config

7/20/2016

Docker commonly used commands

Docker:
=======
d info
d inspect [container id]
d ps -a
d images
# Fetch image
d pull "image"
d rm [container id]
d rmi [image id]
d run -it # interactive tty
      --rm # remove after exiting
      -p 8888:8080 publish container port 8080 to 8888. To access: localhost:8888
      -v /home/liangzou:/home/liangzou # mounts the host drive to container
      --mac-address 70:85:c2:53:5d:4e # Make the mac address the passed in value.
d commit [container id] # create a new image from the existing docker
# d commit a5ab749ad736 harbor.prod.deepmap.ai/dazel/core-builddeps-dazel:ubuntu1804
d history # history of an image
d run -it --rm tomcat /bin/bash  # to check the container content
d attach --sig-proxy=false [container]
d search ubuntu # search for images
d tag [image id] name:tag # d tag 80ff9c14b9e0 core-builddeps-dazel:ubuntu1804
d exec # runs a new command in a running container.
d kill [container id]
# Delete all stopped containers
d rm `d ps -aq`

# Run a docker first
d run -it -d -v /var/lib/jenkins/workspace/:/root/user --mac-address 70:85:c2:53:5d:4e  776923679937.dkr.ecr.us-west-2.amazonaws.com/buildenv:latest
d exec `d ps -f ancestor=776923679937.dkr.ecr.us-west-2.amazonaws.com/buildenv:latest -q`  bash -c "cd /root/user/DeepMap-Presubmit && bazel build --verbose_failures --spawn_strategy=standalone --genrule_strategy=standalone  -- //experimental/..."
d cp [containerid]:/file/path /host/path

# To detach the tty without exiting the shell, use the escape sequence Ctrl+p + Ctrl+q

# Why does docker attach hang?
https://stackoverflow.com/questions/35573698/why-does-docker-attach-hang

If instead of enter you would start typing a command, you would not see the extra empty prompt line. If you were to run

$ docker exec -it --user root <container-ID-or-name> bash