-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
192 lines (164 loc) · 10.8 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from fabric.api import run, sudo, settings, hide, put, cd
from fabric.contrib.files import exists
from termcolor import colored
def lxd():
with settings(warn_only=True):
print colored('##################################', 'blue')
print colored('########### LXD INSTALL ##########', 'blue')
print colored('##################################', 'blue')
# Install LXC/LXD if not already installed
if exists('/usr/bin/lxd', use_sudo=True):
print colored('##################################', 'blue')
print colored('##### LDX already Installed ######', 'blue')
print colored('##################################', 'blue')
else:
sudo('sudo apt-get -y install lxd')
#Add the public LinuxContainers.org image repository by running
sudo('lxc remote add lxc-org images.linuxcontainers.org')
#Copy the 32-bit Ubuntu 14.04 container image to your system with the command
#Copy the 64-bit Centos 7.0 container image to your system with the command
#https: // images.linuxcontainers.org /
ubuntu_img = sudo('lxc image list | grep "trusty (i386)"')
if(ubuntu_img != ""):
print colored('##############################################', 'blue')
print colored('#### LXC Ubuntu Trusty IMG already exists ####', 'blue')
print colored('##############################################', 'blue')
else:
sudo('lxc image copy lxc-org:/ubuntu/trusty/i386 local: --alias=trusty32')
centos_img = sudo('lxc image list | grep "Centos 7"')
if (centos_img != ""):
print colored('##############################################', 'blue')
print colored('#### LXC Centos 7 x64 IMG already exists #####', 'blue')
print colored('##############################################', 'blue')
else:
sudo('lxc image copy lxc-org:/centos/7/amd64 local: --alias=centos764')
#centos_img = sudo('lxc image list | grep "Centos 6"')
#if (centos_img != ""):
# print colored('##############################################', 'blue')
# print colored('#### LXC Centos 6 x64 IMG already exists #####', 'blue')
# print colored('##############################################', 'blue')
#else:
# sudo('lxc image copy lxc-org:/centos/6/amd64 local: --alias=centos664')
print colored(' ', 'blue')
print colored('##########################', 'blue')
print colored('###### LXD PROVISION #####', 'blue')
print colored('##########################', 'blue')
#Launch a container based on this image with the command below.
#This will start a container named "lxd-test-01" based on the "trusty32"
# image (which is an alias for the image you copied in thre previous step).
#sudo('lxc launch trusty32 lxd-ubuntu-01')
#sudo('lxc launch images:centos/7/amd64 my-container')
sudo('lxc launch centos764 lxd-centos-01')
sudo('lxc launch centos764 lxd-centos-02')
sudo('lxc launch centos764 lxd-centos-03')
sudo('lxc image list')
#Virtual Switch/Bridge Configuration
#sudo('dpkg-reconfigure -p medium lxd')
lxd_bridge = sudo('ip address show | grep lxdbr')
if (lxd_bridge == ""):
sudo('dpkg-reconfigure -p medium lxd')
else:
print colored('##################################', 'blue')
print colored('##### NETWORK CONFIGURATION ######', 'blue')
print colored('##################################', 'blue')
print(lxd_bridge)
#List the configured LXD Conteiners
sudo('lxc list')
print colored('1) Run file /bin/ls and note the output. (You will use the output for comparison in just a moment.)', 'green')
print colored('##########################', 'green')
print colored('2) Open a shell in the 32-bit container you launched in previous step with the command:', 'green')
print colored('lxc exec lxd-test-01 bash', 'green')
print colored('##########################', 'green')
print colored('3) Inside the container, run file /bin/ls and compare the output to the output of the same command you ran', 'green')
print colored('outside the container. You will see that inside the container the file is reported as a 32-bit ELF executable', 'green')
print colored('outside the container the same file is listed as a 64-bit ELF executable.', 'green')
print colored('##########################', 'green')
print colored('4) Press Ctrl-D to exit the shell in the container.', 'green')
print colored('##########################', 'green')
print colored('5) The container is still running, so stop the container with:', 'green')
print colored('lxc stop lxd-test-01.', 'green')
print colored('##########################', 'red')
print colored('Its NOT currently possible to mount NFS in an LXC Contenier', attrs=['bold'])
print colored('Nothing that LXD can really do about it, nfs in the upstream', attrs=['bold'])
print colored('kernel would need userns support, after which things will just start working.', attrs=['bold'])
print colored('##########################', 'red')
print colored('###########################', 'blue')
print colored('## JUMPHOST PROVISIONING ##', 'blue')
print colored('###########################', 'blue')
sudo('lxc exec lxd-centos-01 -- echo root:toor | chpasswd')
sudo('lxc exec lxd-centos-01 -- dhclient eth0 -r')
sudo('lxc exec lxd-centos-01 -- dhclient eth0')
sudo('lxc exec lxd-centos-01 -- yum clean all')
sudo('lxc exec lxd-centos-01 -- yum install -y gcc glibc glibc-common gd gd-devel wget')
sudo('lxc exec lxd-centos-01 -- yum install -y python-devel vim net-tools sudo openssh-server openssh-clients')
sudo('lxc exec lxd-centos-01 -- yum install -y epel-release ')
print colored('#########################################', 'blue')
print colored('####### INSTALLING PYTHON FABRIC ########', 'blue')
print colored('#########################################', 'blue')
sudo('lxc exec lxd-centos-01 -- yum install -y python-pip')
sudo('lxc exec lxd-centos-01 -- pip install --upgrade pip')
sudo('lxc exec lxd-centos-01 -- pip install fabric')
sudo('lxc exec lxd-centos-01 -- pip install termcolor')
sudo('lxc exec lxd-centos-01 -- pip install iptools')
print colored('#########################################', 'blue')
print colored('######### INSTALLING SSH SERVER #########', 'blue')
print colored('#########################################', 'blue')
sudo('lxc exec lxd-centos-01 -- chkconfig sshd on')
sudo('lxc exec lxd-centos-01 -- service sshd start')
lxc_gw_ip = sudo('lxc exec lxd-centos-01 -- ifconfig eth0 | awk \'/inet /{print substr($2,1)}\'')
print colored('###########################', 'blue')
print colored('### CLIENT PROVISIONING ###', 'blue')
print colored('###########################', 'blue')
for i in range (2,4):
sudo('lxc exec lxd-centos-0'+str(i)+' -- echo root:toor | chpasswd')
sudo('lxc exec lxd-centos-0'+str(i)+' -- dhclient eth0 -r')
sudo('lxc exec lxd-centos-0'+str(i)+' -- dhclient eth0')
sudo('lxc exec lxd-centos-0'+str(i)+' -- yum clean all')
sudo('lxc exec lxd-centos-0'+str(i)+' -- yum install -y python-devel vim net-tools sudo openssh-server openssh-clients wget')
sudo('lxc exec lxd-centos-0'+str(i)+' -- yum install -y epel-release')
sudo('lxc exec lxd-centos-0'+str(i)+' -- chkconfig sshd on')
sudo('lxc exec lxd-centos-0'+str(i)+' -- service sshd start')
lxc_ip_addr = sudo('lxc exec lxd-centos-0'+str(i)+' -- ifconfig eth0 | awk \'/inet /{print substr($2,1)}\'')
if(i == 2):
with open("./scripts/out_users_test.txt", "w") as file1:
file1.write(lxc_ip_addr+'\n')
print(file1)
elif(i > 2):
with open("./scripts/out_users_test.txt", "a") as file2:
file2.write(lxc_ip_addr+'\n')
print(file2)
else:
print colored('#########################################', 'blue')
print colored('### CHECK THE FOR STATEMENT BOUNDRIES ###', 'blue')
print colored('#########################################', 'blue')
print colored('#################################################', 'blue')
print colored('######## SYNC FILES WITH LXD BASTION HOST #######', 'blue')
print colored('#################################################', 'blue')
sudo('lxc file push /vagrant/scripts/* lxd-centos-01/root/')
#sudo lxc file push /vagrant/scripts/* lxd-centos-01/home/ebarrirero/; sudo lxc exec lxd-centos-01 -- chown -R ebarrirero:ebarrirero /home/ebarrirero/
print colored('##########################', 'blue')
print colored('##### START FIREWALL #####', 'blue')
print colored('##########################', 'blue')
#https://help.ubuntu.com/lts/serverguide/lxc.html#lxc-network
sudo('iptables -t nat -A PREROUTING -p tcp -i lo --dport 80 -j DNAT --to-destination '+lxc_gw_ip+':80')
sudo('iptables -t nat -A PREROUTING -p tcp -i eth1 --dport 80 -j DNAT --to-destination '+lxc_gw_ip+':80')
sudo('iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 80 -j DNAT --to-destination '+lxc_gw_ip+':80')
#$ sudo iptables -A FORWARD -p tcp -d 10.0.3.201 --dport 80 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
sudo('iptables -t nat -A PREROUTING -p tcp -i lo --dport 8338 -j DNAT --to-destination '+lxc_gw_ip+':8338')
sudo('iptables -t nat -A PREROUTING -p tcp -i eth1 --dport 8338 -j DNAT --to-destination '+lxc_gw_ip+':8338')
sudo('iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 8338 -j DNAT --to-destination '+lxc_gw_ip+':8338')
with hide('output'):
fw = sudo('iptables -t nat -L')
print colored(fw, 'red')
print colored('######################################', 'blue')
print colored('FIREWALL - FILTER TABLE STATUS: ', 'blue')
print colored('######################################', 'blue')
with hide('output'):
fw = sudo('iptables -L')
print colored(fw, 'red')
print colored('##########################', 'blue')
print colored('## NETWORK CONFIGURATION #', 'blue')
print colored('##########################', 'blue')
with hide('output'):
netconf = sudo('ip addr show')
print colored(netconf, 'green')