#!/bin/bash


config_selinux(){
   SELINUX_STATUS=$(getenforce)
   if [ ${SELINUX_STATUS} == "Enforcing" ];then
       setenforce 0
   fi
  
   SELINUX_CONFIG_STATUS=$(grep "^SELINUX=" /etc/selinux/config)
   if [ "${SELINUX_CONFIG_STATUS}" = "SELINUX=enforcing" ];then
       sed -i '/^SELINUX/s/enforcing/disabled/g' /etc/selinux/config
   fi
}

user_add(){
    id edoc2 &> /dev/null || useradd -m edoc2
    groupadd docker &> /dev/null
}

es_config(){
    if ! grep file-max /etc/sysctl.conf ;then
         echo "fs.file-max=65536" >> /etc/sysctl.conf
    fi
    
    if ! grep max_map_count /etc/sysctl.conf;then
        echo "vm.max_map_count=262144" >> /etc/sysctl.conf
    fi
   
    if ! grep vm.min_free_kbytes /etc/sysctl.conf;then
        echo "vm.min_free_kbytes=1024000" >> /etc/sysctl.conf
    fi
    
    if ! grep net.core.somaxconn /etc/sysctl.conf;then
        echo "net.core.somaxconn=2048" >> /etc/sysctl.conf
    fi
    
    if ! grep vm.swappiness /etc/sysctl.conf;then
        echo "vm.swappiness=0" >> /etc/sysctl.conf
    fi
    sysctl -p
}

limit_config(){
   if ! grep '* - nofile 655300' /etc/security/limits.conf;then
        echo '* - nofile 655300'  >> /etc/security/limits.conf
   fi 
}

open_firewalld(){
    systemctl disable firewalld
    systemctl status firewalld |grep dead && systemctl start firewalld
}

close_firewalld(){
    systemctl disable firewalld
    systemctl stop firewalld
}

network_config(){
    systemctl enable NetworkManager
    systemctl restart NetworkManager
}

config_selinux
user_add
es_config
limit_config
close_firewalld
#network_config

