Skip to content

Instantly share code, notes, and snippets.

@qiukeren
Last active May 5, 2017 01:05
Show Gist options
  • Save qiukeren/40cb755d94d74bda4c77e2c6761d8bd9 to your computer and use it in GitHub Desktop.
Save qiukeren/40cb755d94d74bda4c77e2c6761d8bd9 to your computer and use it in GitHub Desktop.
centos7 init script
#!/usr/bin/python
import commands
preCommand = ["yum makecache","yum install epel-release -y","yum update -y"]
rpms = ["java-1.8.0-openjdk.x86_64","golang","docker","denyhosts","ttssXX",
"telnet","ftp","nmap","dstat","iftop","sysstat","net-tools","iotop","htop"]
rpmGroups=["Development Tools"]
service = ["denyhosts","docker"]
firewallPort = ["80/tcp"]
standaloneCommand = [
"service ntpd stop",
"firewall-cmd --reload",
"ntpdate time.apple.com",
"service ntpd start"
]
profiles = [
{
'path':'/etc/profile.d/',
'file':'gopath.sh',
'content':'''export GOPATH=/usr/local/gopath\n''',
'description':"gopath profile",
"mode":"644"
},
{
'path':'/etc/profile.d/',
'file':'cmd_logger.sh',
'content':'''export PROMPT_COMMAND="history -a; history 1|logger -i -t '`who -u am i|awk '{print $1 $NF}'`'; $PROMPT_COMMAND"''',
'description':"logger",
"mode":"644"
},
{
'path':'/etc/profile.d/',
'file':'bash_ps.sh',
'content':r"PS1='\n\e[1;37m[\e[m\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m \e[4m`pwd`\e[m\e[1;37m]\e[m\e[1;36m\e[m \A\n$ '",
'description':"bash_ps",
"mode":"644"
},
{
'path':'/root/.ssh/',
'file':'authorized_keys',
'content':"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCzv16zHZvO5xvBDLN05GEFwF9krcDM65b54Fd2gVmVRfaWIJqowmQtPKnMBozLitB600YPjoQ4xIxsRRIbTkDlkPa4HfBVJyctr5/SoLCvoXaockC1eGggkE7ekvUfcFxy4QPH+VG0kg6sP279rOTf81GTAhCBjn2pC4fBfPKHVtvliU6qic+SKchv70lxDCBNVEnfwMfIKx/atrM5etAdv8cs9VMKFzdbeRYD7MtmHLkQRH09vKO0CN1FRhO+lWKEmw6TEZiOjWZcbJJNXpWjrASSgsEEehioAJdE0y2v3iQusLLE4fAdeR7HCMdVwgChhphtfk0qoymv8L0NYiQpNmfDzJ2xJb/eZhen3JonyN2j1qtYxff16UxXptAXQLiLrpzwNJLmMdLeVQjS4wB0YhUNew9lYqBRptLLyh+h380+WzNuYYFMt728rDMN559OmJSDmD7O95na/mNe3UamSkHfbQzK1UZL0h9ArDSKWH0Jn16ACcuTXOnB7SAeJ9JvnDz5OvuAccAOXMsMKnwAeW1ZzseZDQ/f4i3Prnti+RNUoHMjeQqIKSXjdzC/t9Lu2EtB5rfbQclcERojYxfx53zG9no2g+kGtY0v17zeJkBjqF3qo6eODcbisftSjhUF5xzJjfPVhKwDGfQHttVcZ++/1uAAP5ND2vAYxNE8HQ== [email protected]",
"description":"ssh key",
"mode":"600"
}
]
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = "\033[1m"
def info(msg):
print OKGREEN + "[INFO] " + ENDC + msg
def infob(msg):
print OKBLUE + "[INFO] " + ENDC + msg
def warn(msg):
print WARNING + "[WARN] " + ENDC + msg
def err(msg):
print FAIL + "[ERROR] " + ENDC + msg
def pre(msg):
print OKGREEN + "[INFO] " + ENDC + msg
for i in preCommand:
pre(i)
status,output = commands.getstatusoutput(i)
if status is not 0:
err(i+" failed ")
else:
info(i)
for i in rpms:
pre("now installing "+i)
status,output = commands.getstatusoutput("yum install -y " + i)
if status is not 0:
err("yum install "+i+" failed ")
else:
info("yum install "+i)
for i in rpmGroups:
pre(i)
status,output = commands.getstatusoutput("yum groupinstall -y " + i)
if status is not 0:
err("yum groupinstall "+i+" failed ")
else:
info("yum groupinstall "+i)
for i in service:
pre("starting service"+i)
status,output = commands.getstatusoutput("systemctl enable " + i)
if status is not 0:
err("systemctl enable "+i+" failed ")
else:
info("systemctl enable "+i)
status,output = commands.getstatusoutput("systemctl start " + i)
if status is not 0:
err("systemctl start "+i+" failed ")
else:
info("systemctl start "+i)
for i in firewallPort:
pre("opening port "+i)
status,output = commands.getstatusoutput("firewall-cmd --add-port="+i+" --permanent")
if status is not 0:
err("firewall-cmd --add-port="+i+" --permanent"+" failed ")
else:
info("firewall-cmd --add-port="+i+" --permanent")
for i in standaloneCommand:
pre(i)
status,output = commands.getstatusoutput(i)
if status is not 0:
err(i+" failed ")
else:
info(i)
for i in profiles:
pre("writing "+i['description'])
try:
f=open(i['path']+i['file'],'w+')
f.write(i['content'])
f.close()
except IOError,e:
err("error in %s, %s" %(e.errno, e.strerror))
status,output = commands.getstatusoutput("chmod "+i['mode']+" "+i['path']+i['file'])
if status is not 0:
err("chmod "+i['mode']+" "+i['path']+i['file'])
else:
info("chmod "+i['mode']+" "+i['path']+i['file'])
pre("source /etc/profile")
status,output = commands.getstatusoutput("source /etc/profile")
if status is not 0:
err("source /etc/profile failed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment