forked from digitalocean/OpenVPN-Pihole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
133 lines (114 loc) · 4.62 KB
/
fabfile.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from fabric import task
import os
f = open('./packages.txt', 'r')
APT_PACKAGES = f.read()
def clean_up(cxn):
"""
Clean up remote machine before taking snapshot.
"""
cxn.run(r'rm -rf /tmp/* /var/tmp/*')
cxn.run(r'history -c')
cxn.run(r'cat /dev/null > /root/.bash_history')
cxn.run(r'unset HISTFILE')
cxn.run(r'apt-get -y autoremove')
cxn.run(r'apt-get -y autoclean')
cxn.run(r'find /var/log -mtime -1 -type f -exec truncate -s 0 {} \;')
cxn.run(r'rm -rf /var/log/*.gz /var/log/*.[0-9] /var/log/*-????????')
cxn.run(r'rm -rf /var/lib/cloud/instances/*')
cxn.run(r'rm -rf /var/lib/cloud/instance')
cxn.run(r': > /var/mail/$USER')
cxn.run(r'echo "Removing keys..."')
cxn.run(r'rm -f /root/.ssh/authorized_keys /etc/ssh/*key*')
cxn.run(r'echo "Preparing disk..."')
cxn.run(r'dd if=/dev/zero of=/zerofile; sync; rm /zerofile; sync')
cxn.run(r'cat /dev/null > /var/log/lastlog; cat /dev/null > /var/log/wtmp')
def install_files(cxn):
"""
Install files onto remote machine.
Walk through the files in the 'files' directory and copy them to the build
system.
File permissions will be inherited. If you need to change permissions on
uploaded files you can do so in a script placed in the 'scripts' directory.
"""
print('--------------------------------------------------')
print('Copying files in ./files to remote server')
print('--------------------------------------------------')
rootDir = './files'
for dirName, subdirList, fileList in os.walk(rootDir):
cDir = dirName.replace('./files', '')
print("Entering Directory: %s" % cDir)
if cDir:
cxn.run("mkdir -p %s" % cDir)
for fname in fileList:
cwd = os.getcwd()
rpath = cDir + '/' + fname
lpath = cwd + '/files' + cDir + '/' + fname
print('Moving File: %s' % lpath)
cxn.put(lpath, rpath)
def install_pkgs(cxn):
"""
Install apt packages listed in APT_PACKAGES
"""
print('--------------------------------------------------')
print('Installing apt packages in packages.txt')
print('--------------------------------------------------')
cxn.run(r'echo iptables-persistent iptables-persistent/autosave_v4'
r' boolean true | debconf-set-selections')
cxn.run(r'echo iptables-persistent iptables-persistent/autosave_v6'
r' boolean true | sudo debconf-set-selections')
cxn.run(r'apt-get -qqy update')
cxn.run(r'')
cxn.run(r'')
cxn.run(r'DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFOLD=YES'
r' apt-get -qqy -o Dpkg::Options::="--force-confdef"'
r' -o Dpkg::Options::="--force-confold" upgrade')
cxn.run(r'DEBIAN_FRONTEND=noninteractive UCF_FORCE_CONFOLD=YES'
r' apt-get -qqy -o Dpkg::Options::="--force-confdef"'
r' -o Dpkg::Options::="--force-confold" dist-upgrade')
cxn.run(r'apt-get -qqy -o Dpkg::Options::="--force-confdef"'
r' -o Dpkg::Options::="--force-confold" install {}'
.format(APT_PACKAGES))
def run_scripts(cxn):
"""
Run all scripts in the 'scripts' directory on the build system
Scripts are run in alpha-numeric order. We recommend naming your scripts
with a name that starts with a two digit number 01-99 to ensure run order.
"""
print('--------------------------------------------------')
print('Running scripts in ./scripts')
print('--------------------------------------------------')
cwd = os.getcwd()
directory = cwd + '/scripts'
for f in sorted(os.listdir(directory)):
lfile = cwd + '/scripts/' + f
rfile = '/tmp/' + f
print("Processing script in %s" % lfile)
cxn.put(lfile, rfile)
cxn.run("chmod +x %s" % rfile)
cxn.run(rfile)
@task
def build(cxn):
"""
Configure the build droplet, clean up and shut down for snapshotting
"""
install_pkgs(cxn)
install_files(cxn)
run_scripts(cxn)
clean_up(cxn)
cxn.run('exit')
print('----------------------------------------------------------------')
print('Build Complete. Shut down your build droplet from the control')
print('panel before creating your snapshot.')
print('----------------------------------------------------------------')
@task
def testbuild(cxn):
"""
Configure the build droplet, but do not clean up or shut down
"""
install_pkgs(cxn)
install_files(cxn)
run_scripts(cxn)
print('Build complete. This droplet is NOT ready for use. ' +
'Use build instead of testbuild for your final build')