47
from netaddr import IPNetwork
51
49
install('python-netaddr')
50
from netaddr import IPNetwork
55
install('python-dnspython')
55
59
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
60
64
return template.render(context)
68
""" # Ubuntu Cloud Archive
69
deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main
72
CLOUD_ARCHIVE_POCKETS = {
73
'precise-folsom': 'precise-updates/folsom',
74
'precise-folsom/updates': 'precise-updates/folsom',
75
'precise-folsom/proposed': 'precise-proposed/folsom',
76
'precise-grizzly': 'precise-updates/grizzly',
77
'precise-grizzly/updates': 'precise-updates/grizzly',
78
'precise-grizzly/proposed': 'precise-proposed/grizzly'
63
82
def configure_source():
64
source = config_get('source')
65
if (source.startswith('ppa:') or
66
source.startswith('cloud:')):
83
source = str(config_get('openstack-origin'))
86
if source.startswith('ppa:'):
68
88
'add-apt-repository',
71
91
subprocess.check_call(cmd)
72
if source.startswith('http:'):
73
with open('/etc/apt/sources.list.d/hacluster.list', 'w') as apt:
74
apt.write("deb " + source + "\n")
75
key = config_get('key')
92
if source.startswith('cloud:'):
93
install('ubuntu-cloud-keyring')
94
pocket = source.split(':')[1]
95
with open('/etc/apt/sources.list.d/cloud-archive.list', 'w') as apt:
96
apt.write(CLOUD_ARCHIVE.format(CLOUD_ARCHIVE_POCKETS[pocket]))
97
if source.startswith('deb'):
98
l = len(source.split('|'))
100
(apt_line, key) = source.split('|')
103
'adv', '--keyserver keyserver.ubuntu.com',
82
106
subprocess.check_call(cmd)
110
with open('/etc/apt/sources.list.d/quantum.list', 'w') as apt:
111
apt.write(apt_line + "\n")
178
208
def get_host_ip(hostname=unit_get('private-address')):
184
return subprocess.check_output(cmd).strip() # IGNORE:E1103
210
# Test to see if already an IPv4 address
211
socket.inet_aton(hostname)
216
answers = dns.resolver.query(hostname, 'A')
218
return answers[0].address
219
except dns.resolver.NXDOMAIN:
187
224
def restart(*services):
202
239
def running(service):
203
#output = subprocess.check_output(['service', service, 'status'])
204
output = commands.getoutput('service %s status' % service)
205
show_re = re.compile("start/running")
206
status = show_re.search(output)
241
output = subprocess.check_output(['service', service, 'status'])
242
except subprocess.CalledProcessError:
245
if ("start/running" in output or
246
"is running" in output):
211
252
def disable_upstart_services(*services):
212
253
for service in services:
213
#subprocess.check_call('sh -c "echo manual > /etc/init/%s.override"' % service, shell=True)
214
override = open("/etc/init/%s.override" % service, "w")
215
override.write("manual")
254
with open("/etc/init/{}.override".format(service), "w") as override:
255
override.write("manual")
219
258
def enable_upstart_services(*services):
220
259
for service in services:
221
path = '/etc/init/%s.override' % service
260
path = '/etc/init/{}.override'.format(service)
222
261
if os.path.exists(path):
223
subprocess.check_call(['rm', '-rf', path])
226
265
def disable_lsb_services(*services):
231
270
def enable_lsb_services(*services):
232
271
for service in services:
233
subprocess.call(['update-rc.d','-f',service,'defaults'])
272
subprocess.check_call(['update-rc.d', '-f', service, 'defaults'])
236
275
def get_iface_ipaddr(iface):
262
301
def get_network_address(iface):
263
network = "%s/%s" % (get_iface_ipaddr(iface), get_netmask_cidr(get_iface_netmask(iface)))
264
ip = IPNetwork(network)
265
return str(ip.network)
303
network = "{}/{}".format(get_iface_ipaddr(iface),
304
get_netmask_cidr(get_iface_netmask(iface)))
305
ip = IPNetwork(network)
306
return str(ip.network)