#!/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 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

setenforce 0
# systemctl disable firewalld
# systemctl stop firewalld

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

export ifname=$(route -n | grep "UG" | grep en | tr -s " " | cut -f 8 -d " ")

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="$ifname"
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_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 kubelet kube-proxy;do
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES -l
done
