forked from Puppet-Finland/fabric
/
util.py
133 lines (114 loc) · 4.08 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
from fabric.api import *
from fabric.contrib.files import exists
from datetime import datetime
import re
import os
def getisotime():
"""Convenience method to get current UTC time in yyyymmddhhmm format"""
ct = datetime.utcnow()
return ct.strftime("%Y%m%d%H%M")
@task
def show_boot_partition_info():
"""Show information about the /boot partition, if any"""
with hide("everything"), settings(warn_only=True):
if run("mount|grep \" /boot \"").succeeded:
with show("output"):
run("df -h|grep -E '(^Filesystem|^.* /boot$)'")
@task
def set_clock():
"""Set clock on the server using ntpdate"""
import package
package.install("ntpdate")
sudo("ntpdate 0.fi.pool.ntp.org 1.fi.pool.ntp.org 2.fi.pool.ntp.org")
@task
def uname():
"""Show kernel version"""
run("uname -a")
@task
def df():
"""Check available disk space"""
run("df -h")
@task
def install_sudo():
"""Install sudo, if it's not present"""
import vars
vars = vars.Vars()
with settings(warn_only=True):
if run("which sudo").failed:
run(vars.os.package_install_cmd % "sudo")
@task
def put_and_chown(localfile, remotefile, mode="0644", owner="root", group="root", overwrite=True):
"""Put a file to remote server and chown it"""
# Configure the exists() check and chown differently depending on whether
# we're copying over a file or a directory.
with hide("everything"), settings(warn_only=True):
if local("test -d "+localfile).succeeded:
target = remotefile+"/"+os.path.basename(localfile)
chown_cmd = "chown -R"
else:
target = remotefile
chown_cmd = "chown"
# Only copy things that are not already there
if not exists(target) or overwrite:
put(localfile, remotefile, use_sudo=True, mode=mode)
sudo(chown_cmd+" "+owner+":"+group+" "+remotefile)
@task
def add_host_entry(ip, hostname, domain):
"""Add an entry to /etc/hosts"""
host_line = ip+" "+hostname+"."+domain+" "+hostname
# Only add entry if it does not exist already. We don't want warnings about
# grep not finding the entry, as that's to be expected.
with hide("warnings"), settings(warn_only=True):
if run("grep \""+host_line+"\" /etc/hosts").failed:
sudo("echo "+host_line+" >> /etc/hosts")
@task
def add_host_entries(hosts_file=None):
"""Add entries from local hosts file to a remote hosts file"""
from fabric.contrib.files import append
if hosts_file:
try:
hosts = open(hosts_file)
for line in hosts:
append("/etc/hosts", line.rstrip("\n"), use_sudo=True)
except IOError:
print "ERROR: defined hosts file is missing!"
@task
def symlink(source, target):
"""Make a symbolic link"""
run("ln -s "+source+" "+target)
def get_hostname():
"""Get hostname part of the current host"""
return re.split("\.", env.host)[0]
def get_domain():
"""Get domain part of the current host"""
domain=""
for item in re.split("\.", env.host)[1:]:
domain = domain + "." + item
return domain.lstrip(".")
@task
def set_hostname(hostname):
"""Set hostname"""
with hide("everything"), settings(warn_only=True):
if run("grep "+hostname+" /etc/hostname").failed:
sudo("echo "+hostname+" > /etc/hostname")
if not hostname == run("hostname"):
sudo("hostname "+hostname)
@task
def add_to_path(path):
"""Add a new directory to PATH for the default shell"""
from fabric.contrib.files import append
import vars
vars = vars.Vars()
for file in [ vars.os.default_shell_config, vars.os.default_loginshell_config ]:
append(file, "export PATH=$PATH:"+path, use_sudo=True)
@task
def reboot(really='no'):
"""Reboot the machine. Use reboot:really=YES to actually reboot."""
if really == 'YES':
sudo("shutdown -r now")
else:
print("Use reboot:really=YES to actually reboot")
def isTrue(s):
"""Convert a Yes into True and everything else into False"""
return s == "Yes"