/
enos_vagrant.py
140 lines (128 loc) · 4.63 KB
/
enos_vagrant.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
from .host import Host
from ipaddress import IPv4Network
from jinja2 import Environment, FileSystemLoader
from provider import Provider
from ..utils.constants import TEMPLATE_DIR
from ..utils.extra import build_roles, gen_resources
import logging
import os
import vagrant
SIZES = {
'tiny': {
'cpu': 1,
'mem': 512
},
'small': {
'cpu': 1,
'mem': 1024
},
'medium': {
'cpu': 2,
'mem': 2048
},
'big': {
'cpu': 3,
'mem': 3072,
},
'large': {
'cpu': 4,
'mem': 4096
},
'extra-large': {
'cpu': 6,
'mem': 6144
}
}
class Enos_vagrant(Provider):
def init(self, conf, force_deploy=False):
"""enos up
Read the resources in the configuration files. Resource claims must be
grouped by sizes according to the predefined SIZES map.
"""
provider_conf = conf['provider']
net_pools = {
'ip1': list(IPv4Network(u'192.168.142.0/25')),
'ip2': list(IPv4Network(u'192.168.143.0/25')),
'ip3': list(IPv4Network(u'192.168.144.0/25')),
}
# Build a list of machines that will be used to generate the
# Vagrantfile
machines = []
for size, role, nb in gen_resources(conf['resources']):
for i in range(nb):
ip1 = str(net_pools['ip1'].pop())
_, _, _, name = ip1.split('.')
machines.append({
'role': role,
# NOTE(matrohon): don't base the name of the VM on its
# role, build_roles will then set the final role of
# each VM
'name': "enos-%s" % name,
'size': size,
'cpu': SIZES[size]['cpu'],
'mem': SIZES[size]['mem'],
'ip1': ip1,
'ip2': str(net_pools['ip2'].pop()),
'ip3': str(net_pools['ip3'].pop()),
})
loader = FileSystemLoader(searchpath=TEMPLATE_DIR)
env = Environment(loader=loader)
template = env.get_template('Vagrantfile.j2')
vagrantfile = template.render(machines=machines,
provider_conf=provider_conf)
vagrantfile_path = os.path.join(os.getcwd(), "Vagrantfile")
with open(vagrantfile_path, 'w') as f:
f.write(vagrantfile)
# Build env for Vagrant with a copy of env variables (needed by
# subprocess opened by vagrant
v_env = dict(os.environ)
v_env['VAGRANT_DEFAULT_PROVIDER'] = provider_conf['backend']
v = vagrant.Vagrant(root=os.getcwd(),
quiet_stdout=False,
quiet_stderr=False,
env=v_env)
if force_deploy:
v.destroy()
v.up()
v.provision()
# Distribute the machines according to the resource/topology
# specifications
r = build_roles(conf,
machines,
lambda m: m['size'])
roles = {}
for role, machines in r.items():
roles.setdefault(role, [])
for machine in machines:
keyfile = v.keyfile(vm_name=machine['name'])
port = v.port(vm_name=machine['name'])
address = v.hostname(vm_name=machine['name'])
roles[role].append(Host(address,
alias=machine['name'],
user=provider_conf['user'],
port=port,
keyfile=keyfile))
logging.info(roles)
network = {'cidr': '192.168.142.0/24',
'start': str(net_pools['ip1'][3]),
'end': str(net_pools['ip1'][-1]),
'dns': '8.8.8.8',
'gateway': '192.168.142.1'}
network_interface = provider_conf['interfaces'][0]
external_interface = provider_conf['interfaces'][1]
return (roles, network, (network_interface, external_interface))
def destroy(self, env):
v = vagrant.Vagrant(root=os.getcwd(),
quiet_stdout=False,
quiet_stderr=True)
v.destroy()
def default_config(self):
return {
'backend': 'virtualbox',
'box': 'bento/debian-9',
'user': 'root',
# NOTE(msimonin) bento enforce old interface naming
# eth0 is the default nat-ed interface 10.0.2.15
# we don't use it
'interfaces': ('eth1', 'eth2')
}