2014-12-22 23:01:35 -06:00
|
|
|
from bootstrapvz.base import Task
|
2017-07-14 18:26:52 +02:00
|
|
|
from bootstrapvz.common.tasks import host
|
2014-12-22 23:01:35 -06:00
|
|
|
from bootstrapvz.common import phases
|
2017-07-14 18:26:25 +02:00
|
|
|
from bootstrapvz.common.tools import rel_path
|
2018-07-19 15:28:33 +02:00
|
|
|
from bootstrapvz.common.tools import log_check_call
|
2014-12-22 23:01:35 -06:00
|
|
|
import os
|
2017-07-26 14:09:55 +02:00
|
|
|
import json
|
2014-12-22 23:01:35 -06:00
|
|
|
|
|
|
|
|
2017-07-14 18:26:52 +02:00
|
|
|
class AddRequiredCommands(Task):
|
|
|
|
description = 'Adding commands required for provisioning with ansible'
|
|
|
|
phase = phases.validation
|
|
|
|
successors = [host.CheckExternalCommands]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
info.host_dependencies['ansible'] = 'ansible'
|
|
|
|
|
2017-07-14 19:46:35 +02:00
|
|
|
|
2014-12-22 23:01:35 -06:00
|
|
|
class CheckPlaybookPath(Task):
|
|
|
|
description = 'Checking whether the playbook path exist'
|
2016-09-12 00:52:10 +02:00
|
|
|
phase = phases.validation
|
2014-12-22 23:01:35 -06:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
from bootstrapvz.common.exceptions import TaskError
|
2017-07-14 18:26:25 +02:00
|
|
|
playbook = rel_path(info.manifest.path, info.manifest.plugins['ansible']['playbook'])
|
2014-12-22 23:01:35 -06:00
|
|
|
if not os.path.exists(playbook):
|
|
|
|
msg = 'The playbook file {playbook} does not exist.'.format(playbook=playbook)
|
|
|
|
raise TaskError(msg)
|
|
|
|
if not os.path.isfile(playbook):
|
|
|
|
msg = 'The playbook path {playbook} does not point to a file.'.format(playbook=playbook)
|
|
|
|
raise TaskError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
class AddPackages(Task):
|
|
|
|
description = 'Making sure python is installed'
|
|
|
|
phase = phases.preparation
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
info.packages.add('python')
|
|
|
|
|
|
|
|
|
|
|
|
class RunAnsiblePlaybook(Task):
|
2017-07-14 18:26:25 +02:00
|
|
|
description = 'Running ansible playbook'
|
2015-05-02 12:29:55 +02:00
|
|
|
phase = phases.user_modification
|
2014-12-22 23:01:35 -06:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
# Extract playbook and directory
|
2017-07-14 18:26:25 +02:00
|
|
|
playbook = rel_path(info.manifest.path, info.manifest.plugins['ansible']['playbook'])
|
2014-12-22 23:01:35 -06:00
|
|
|
|
|
|
|
# build the inventory file
|
|
|
|
inventory = os.path.join(info.root, 'tmp/bootstrap-inventory')
|
|
|
|
with open(inventory, 'w') as handle:
|
|
|
|
conn = '{} ansible_connection=chroot'.format(info.root)
|
2016-06-04 10:04:23 +02:00
|
|
|
content = ""
|
2014-12-22 23:01:35 -06:00
|
|
|
|
2017-07-14 19:46:35 +02:00
|
|
|
if 'groups' in info.manifest.plugins['ansible']:
|
|
|
|
for group in info.manifest.plugins['ansible']['groups']:
|
|
|
|
content += '[{}]\n{}\n'.format(group, conn)
|
2016-06-04 10:04:23 +02:00
|
|
|
else:
|
|
|
|
content = conn
|
2014-12-22 23:01:35 -06:00
|
|
|
|
|
|
|
handle.write(content)
|
|
|
|
|
|
|
|
# build the ansible command
|
2017-07-14 18:26:25 +02:00
|
|
|
cmd = ['ansible-playbook', '-i', inventory, playbook]
|
2017-07-14 19:23:14 +02:00
|
|
|
if 'extra_vars' in info.manifest.plugins['ansible']:
|
2017-07-26 14:09:55 +02:00
|
|
|
cmd.extend(['--extra-vars', json.dumps(info.manifest.plugins['ansible']['extra_vars'])])
|
2017-07-14 19:23:14 +02:00
|
|
|
if 'tags' in info.manifest.plugins['ansible']:
|
2017-07-26 14:09:55 +02:00
|
|
|
cmd.extend(['--tags', ','.join(info.manifest.plugins['ansible']['tags'])])
|
2017-07-14 19:23:14 +02:00
|
|
|
if 'skip_tags' in info.manifest.plugins['ansible']:
|
2017-07-26 14:37:24 +02:00
|
|
|
cmd.extend(['--skip-tags', ','.join(info.manifest.plugins['ansible']['skip_tags'])])
|
2017-07-14 19:23:14 +02:00
|
|
|
if 'opt_flags' in info.manifest.plugins['ansible']:
|
2014-12-22 23:01:35 -06:00
|
|
|
# Should probably do proper validation on these, but I don't think it should be used very often.
|
2017-07-14 19:23:14 +02:00
|
|
|
cmd.extend(info.manifest.plugins['ansible']['opt_flags'])
|
2014-12-22 23:01:35 -06:00
|
|
|
|
|
|
|
# Run and remove the inventory file
|
2017-07-14 18:26:25 +02:00
|
|
|
log_check_call(cmd)
|
2014-12-22 23:01:35 -06:00
|
|
|
os.remove(inventory)
|
2018-07-19 15:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RemoveAnsibleSSHUserDir(Task):
|
|
|
|
description = 'Removing .ansible directory'
|
|
|
|
phase = phases.user_modification
|
|
|
|
predecessors = [RunAnsiblePlaybook]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
ssh_user = info.manifest.plugins['ansible']['extra_vars']['ansible_ssh_user']
|
|
|
|
# os.path.expanduser does not work in a chroot,
|
|
|
|
# so we use sh instead
|
|
|
|
[ssh_user_home] = log_check_call(['chroot', info.root, 'sh', '-c', 'echo ~' + ssh_user])
|
|
|
|
from shutil import rmtree
|
|
|
|
# [1:] to remove the leading slash from e.g. /home/ansible
|
|
|
|
ansible_dir_path = os.path.join(info.root, ssh_user_home[1:], '.ansible')
|
|
|
|
rmtree(ansible_dir_path)
|