Last active
April 5, 2018 16:56
-
-
Save apahim/362c1160e5070d095e4a2772a025d053 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import signal | |
import tempfile | |
from avocado import main | |
from avocado import Test | |
from avocado.utils import vmimage | |
from avocado.utils import git | |
from avocado.utils import process | |
from avocado.utils import path as utils_path | |
from avocado.utils import wait | |
_VIRTINST_BIN = utils_path.find_command('virt-install') | |
def _get_ip(domain): | |
cmd = ['virsh', | |
'--connect', 'qemu:///system', | |
'domifaddr', domain] | |
result = process.run(' '.join(cmd)) | |
if 'ipv4' in result.stdout_text: | |
return True | |
return False | |
def _create_cloudinit_cdrom(domain, username, password): | |
""" | |
Creates a CDROM Iso Image with the required cloudinit files | |
(meta-data and user-data) to make the initial Cloud Image | |
configuration, attaching the CDROM to the VM. | |
""" | |
geniso_bin = utils_path.find_command('genisoimage') | |
data_dir = tempfile.mkdtemp() | |
os.chmod(data_dir, 755) | |
metadata_path = os.path.join(data_dir, 'meta-data') | |
userdata_path = os.path.join(data_dir, 'user-data') | |
iso_path = os.path.join(data_dir, 'cdrom.iso') | |
id_rsa_pub_path = os.path.join('~/.ssh/id_rsa.pub') | |
with open(os.path.expanduser(id_rsa_pub_path), 'r') as file_obj: | |
id_rsa_pub = file_obj.read().strip() | |
metadata_content = ['instance-id: %s' % domain, | |
'local-hostname: %s' % domain] | |
userdata_content = ['#cloud-config', | |
'ssh_authorized_keys:', | |
' - %s' % id_rsa_pub, | |
'password: %s' % password, | |
'ssh_pwauth: True', | |
'chpasswd: { expire: False }', | |
'system_info:', | |
' default_user:', | |
' name: %s' % username] | |
with open(metadata_path, 'w') as metadata_file: | |
metadata_file.write('\n'.join(metadata_content)) | |
with open(userdata_path, 'w') as userdata_file: | |
userdata_file.write('\n'.join(userdata_content)) | |
cmd = [geniso_bin, | |
'-output', iso_path, | |
'-volid', 'cidata', | |
'-joliet', | |
'-rock', metadata_path, userdata_path] | |
process.run(' '.join(cmd)) | |
return iso_path | |
class TestMinishift(Test): | |
def setUp(self): | |
# Deploy minishift from Host to Guest so Ansible doesn't have | |
# to download it later. If you have minishift.iso cached, this will | |
# speed up the test. | |
self.debug = self.params.get('debug', default=False) | |
if self.debug: | |
minishift_iso_url = ('http://artifacts.ci.centos.org/' | |
'fedora-atomic/minishift/iso/minishift.iso') | |
self.minishift_iso = self.fetch_asset(minishift_iso_url) | |
# Get the Repository/branch containing the Ansible Playbooks | |
repo = self.params.get('repo', default='https://github.com/' | |
'CentOS-PaaS-SIG/contra-env-setup.git') | |
branch = self.params.get('branch', default='master') | |
git.get_repo(repo, | |
branch=branch, | |
destination_dir=self.workdir) | |
# Get/Set VM Image | |
distro = self.params.get('distro', default='Fedora') | |
image = vmimage.get(distro) | |
self.username = 'avocado' | |
self.password = 'avocado' | |
self.vm_name = 'vm01' | |
cmd = [_VIRTINST_BIN, | |
'--import', '--noautoconsole', | |
'--connect', 'qemu:///system', | |
'--name', self.vm_name, | |
'--disk', image.path, | |
'--graphics', 'vnc', | |
'--cpu', 'host', | |
'--memory', '2048', | |
'--vcpus', '4', | |
'--os-variant', 'fedora27', | |
'--disk', '%s,device=cdrom,format=iso' % | |
_create_cloudinit_cdrom(self.vm_name, self.username, | |
self.password)] | |
process.run(' '.join(cmd)) | |
wait.wait_for(_get_ip, timeout=60, args=[self.vm_name]) | |
cmd = ['virsh', | |
'--connect', 'qemu:///system', | |
'domifaddr', self.vm_name] | |
result = process.run(' '.join(cmd)) | |
for line in result.stdout_text.splitlines(): | |
if 'ipv4' in line: | |
self.ip = line.split()[3].split('/')[0] | |
import time | |
time.sleep(10) | |
def test(self): | |
if self.debug: | |
curr_id = os.getpid() | |
self.log.debug('Process stopped. Run "kill -SIGCONT %s" ' | |
'to continue' % curr_id) | |
os.kill(curr_id, signal.SIGSTOP) | |
env = {} | |
ssh_args = ['-o UserKnownHostsFile=/dev/null', | |
'-o StrictHostKeyChecking=no'] | |
# VM Preparation Using Ansible AdHoc command | |
prep_cmd = ['resize2fs /dev/sda1', | |
'dnf install -y python libselinux-python', | |
'dnf update -y nettle'] | |
# Add minishift.iso preparation mommands if we are going to | |
# deploy minishift | |
if self.debug: | |
prep_cmd.extend(['mkdir /home/avocado/minishift', | |
'chown avocado:avocado /home/avocado/minishift']) | |
# Execute Ansible AdHoc preparation commands | |
cmd = ('ansible {ip} -i "{ip}," --become -m raw ' | |
'-a "{prep_cmd}" ' | |
'--user {username} ' | |
'--ssh-common-args="{ssh_args}"') | |
cmd = cmd.format(ip=self.ip, | |
prep_cmd='; '.join(prep_cmd), | |
username=self.username, | |
ssh_args=' '.join(ssh_args)) | |
process.run(cmd, env=env) | |
# Another Ansible AdHoc command to deploy minishift.iso | |
if self.debug: | |
cmd = ('ansible {ip} -i "{ip}," ' | |
'-m copy -a "src={minishift_iso} ' | |
'dest=~/minishift/minishift.iso" ' | |
'--user {username} ' | |
'--ssh-common-args="{ssh_args}" -vvv') | |
cmd = cmd.format(ip=self.ip, | |
minishift_iso=self.minishift_iso, | |
username=self.username, | |
ssh_args=' '.join(ssh_args)) | |
process.run(cmd, env=env) | |
# Main Ansible Playbook execution | |
cmd = ('ansible-playbook -i "%s," playbooks/setup.yml ' | |
'-e remote_user=%s --ssh-common-args="%s"' % | |
(self.ip, self.username, ' '.join(ssh_args))) | |
process.run(cmd, env=env) | |
def tearDown(self): | |
process.run('virsh --connect qemu:///system destroy %s' % | |
self.vm_name) | |
process.run('virsh --connect qemu:///system undefine %s' % | |
self.vm_name) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment