#!/bin/bash

yum install -y yum-plugin-versionlock
yum --enablerepo=extras install -y epel-release

cat <<EOF > /etc/yum.repos.d/bigstep-tools.repo
[bigstep-tools] 
name=Bigstep Tools Software Repository 
baseurl=http://bsiuser:NUn30no2b8sjh02h77jsigu272829@internalrepo.bigstepcloud.com/bigstep-tools/centos/\$releasever/\$basearch 
enabled=1 
gpgcheck=0 
protect=1
EOF

yum clean all
yum versionlock flannel-0.7.0-1.el7.x86_64
yum versionlock etcd-3.1.7-1.el7.x86_64
yum versionlock docker-1.12.6-28.git1398f24.el7.centos.x86_64
yum versionlock kubernetes-client-1.6.3-1.fc27.x86_64
yum versionlock kubernetes-node-1.6.3-1.fc27.x86_64
yum versionlock kubernetes-master-1.6.3-1.fc27.x86_64
# yum versionlock list

setenforce 0
# systemctl disable firewalld
# systemctl stop firewalld

yum install -y wget htop iftop
yum install -y docker etcd3 flannel device-mapper-libs device-mapper-event-libs
yum install -y kubernetes-client kubernetes-node kubernetes-master



export ifname=$(route -n | grep "UG" | grep ens | tr -s " " | cut -f 8 -d " ")
# export eth_wan=192.168.137.130
export eth_wan=$(ip addr show $ifname | grep inet | tr -s " " | cut -f3 -d " "| cut -f1 -d "/" | head -n1)

read -r -d '' ETCDCONF << EOM
# [member]
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_LISTEN_PEER_URLS="http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://$eth_wan:2379"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#
#[cluster]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
#ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://$eth_wan:2379"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#ETCD_STRICT_RECONFIG_CHECK="false"
#ETCD_AUTO_COMPACTION_RETENTION="0"
#
#[proxy]
#ETCD_PROXY="off"
#ETCD_PROXY_FAILURE_WAIT="5000"
#ETCD_PROXY_REFRESH_INTERVAL="30000"
#ETCD_PROXY_DIAL_TIMEOUT="1000"
#ETCD_PROXY_WRITE_TIMEOUT="5000"
#ETCD_PROXY_READ_TIMEOUT="0"
#
#[security]
#ETCD_CERT_FILE=""
#ETCD_KEY_FILE=""
#ETCD_CLIENT_CERT_AUTH="false"
#ETCD_TRUSTED_CA_FILE=""
#ETCD_AUTO_TLS="false"
#ETCD_PEER_CERT_FILE=""
#ETCD_PEER_KEY_FILE=""
#ETCD_PEER_CLIENT_CERT_AUTH="false"
#ETCD_PEER_TRUSTED_CA_FILE=""
#ETCD_PEER_AUTO_TLS="false"
#
#[logging]
#ETCD_DEBUG="false"
# examples for -log-package-levels etcdserver=WARNING,security=DEBUG
#ETCD_LOG_PACKAGE_LEVELS=""
EOM

echo "$ETCDCONF" > /etc/etcd/etcd.conf

systemctl enable etcd
systemctl start etcd

echo '{"Network": "172.17.0.0/16", "SubnetLen": 24, "Backend": {"Type": "vxlan", "VNI": 1}}' | etcdctl --endpoints http://$eth_wan:2379 set /atomic.io/network/config

# curl -s http://$eth_wan:2379/v2/keys/atomic.io/network/subnets | python -m json.tool
# curl -s http://$eth_wan:2379/v2/keys/atomic.io/network/config | python -m json.tool

read -r -d '' FLANNELD_TMP << EOM
# Flanneld configuration options

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://$eth_wan:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/atomic.io/network"

# Any additional options that you want to pass
#FLANNEL_OPTIONS=""
EOM

echo "$FLANNELD_TMP" > /etc/sysconfig/flanneld

mkdir -p /etc/kubernetes/manifests

export docker_disk=$(lsblk -l | grep sd | cut -f1 -d " " | cut  -c 1-3 | uniq -c | sort | head -n 1 | tr -s " " | cut -f3 -d " ")

pvcreate /dev/$docker_disk
vgcreate docker /dev/$docker_disk
lvcreate --wipesignatures y -n thinpool docker -l 95%VG
lvcreate --wipesignatures y -n thinpoolmeta docker -l 1%VG
lvconvert -y \
--zero n \
-c 512K \
--thinpool docker/thinpool \
--poolmetadata docker/thinpoolmeta

cat <<EOF > /etc/lvm/profile/docker-thinpool.profile
activation {
  thin_pool_autoextend_threshold=80
  thin_pool_autoextend_percent=20
}
EOF

lvchange --metadataprofile docker-thinpool docker/thinpool

lvs -o+seg_monitor

cat <<EOF > /etc/docker/daemon.json
{
    "storage-driver": "devicemapper",
    "storage-opts": [
    "dm.thinpooldev=/dev/mapper/docker-thinpool",
    "dm.use_deferred_removal=true",
    "dm.use_deferred_deletion=true"
    ]
}
EOF

systemctl enable flanneld
systemctl start flanneld

systemctl enable docker
systemctl start docker

read -r -d '' K8S_CONFIG << EOM
# kubernetes system config
#
# The following values are used to configure various aspects of all
# kubernetes services, including
#
#   kube-apiserver.service
#   kube-controller-manager.service
#   kube-scheduler.service
#   kubelet.service
#   kube-proxy.service
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"

# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"

# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=false"

# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://$eth_wan:8080"
EOM

echo "$K8S_CONFIG" > /etc/kubernetes/config


read -r -d '' K8S_APISERVER << EOM
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#

# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"

# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"

# Port minions listen on
KUBELET_PORT="--kubelet-port=10250"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://$eth_wan:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# default admission control policies
#KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"

# Add your own!
KUBE_API_ARGS=""
EOM

echo "$K8S_APISERVER" > /etc/kubernetes/apiserver


read -r -d '' K8S_KUBELET << EOM
# kubernetes kubelet (minion) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"

# The port for the info server to serve on
KUBELET_PORT="--port=10250"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override="

# location of the api-server
KUBELET_API_SERVER="--api-servers=http://$eth_wan:8080"

# Add your own!
KUBELET_ARGS="--cgroup-driver systemd"
EOM

echo "$K8S_KUBELET" > /etc/kubernetes/kubelet

for SERVICES in kube-apiserver kube-controller-manager kube-scheduler kube-proxy kubelet;do
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES -l
done

sleep 4

cd /etc/kubernetes/manifests
wget http://repointegration.bigstepcloud.com/extra/k8s/kube-dashboard.yaml
sed -i.bak "s/\$eth_wan/$eth_wan/" kube-dashboard.yaml

kubectl create -f /etc/kubernetes/manifests/kube-dashboard.yaml

sleep 4

#kubectl create -f http://repointegration.bigstepcloud.com/extra/k8s/kube-dns.yaml
