def setup_container(name, template, ipadd): from fabtools import require from fabtools.require.openvz import container from fabtools.system import distrib_family import fabtools if distrib_family() == 'debian': require.deb.package('vzctl') require.openvz.template(template) with container(name, template, hostname=name, ipadd=ipadd) as ct: # Make sure the container is started if not ct.running(): ct.start() # Set up name servers NAMESERVERS = fabtools.network.nameservers() ct.set(nameserver=NAMESERVERS) # Wait until we can ping the container from the host with settings(warn_only=True): timeout = 0 while True: if run('ping -c 1 %s' % ipadd).succeeded: break time.sleep(1) timeout += 1 assert timeout < 10, "Timeout trying to ping container"
def container(): from fabtools.require.openvz import container name = 'debian' template = 'debian-6.0-x86_64' ipadd = '192.168.1.100' setup_host_networking() setup_container(name, template, ipadd) yield name with container(name, template, hostname=name, ipadd=ipadd) as ct: ct.stop() ct.destroy()
def remove_container(): from fabtools.require.openvz import container with container(NAME, TEMPLATE, hostname=NAME, ipadd=IPADD) as ct: ct.stop() ct.destroy()
def setup_containers(): require.deb.package('vzctl') NAME = 'debian' TEMPLATE = 'debian-6.0-x86_64' IPADD = '192.168.1.100' NAMESERVERS = fabtools.network.nameservers() require.openvz.template(TEMPLATE) with container(NAME, TEMPLATE, hostname=NAME, ipadd=IPADD) as ct: # Make sure the container is started if not ct.running(): ct.start() # Set up name servers ct.set(nameserver=NAMESERVERS) # Wait until we can ping the container from the host with settings(warn_only=True): timeout = 0 while True: if run('ping -c 1 %s' % IPADD).succeeded: break time.sleep(1) timeout += 1 assert timeout < 10, "Timeout trying to ping container" # Check run/sudo with guest(NAME): assert run('whoami') == 'root' assert sudo('whoami') == 'root' assert sudo('whoami', user='******') == 'nobody' # Check put with guest(name): local('echo "toto" > /tmp/toto') put('/tmp/toto', '/tmp/toto') assert run('test -f /tmp/toto').succeeded # Run more complex tasks inside container with guest(NAME): # Check require.file() require.file('/tmp/foo') assert fabtools.files.is_file('/tmp/foo') # Check cd() inside container with cd('/tmp'): run('touch bar') assert fabtools.files.is_file('bar') assert fabtools.files.is_file('/tmp/bar') # This directory does not exist in the host require.directory('/tmp/newdir') with cd('/tmp/newdir'): run('touch baz') assert fabtools.files.is_file('/tmp/newdir/baz') # Check that sudo as user works with cd('/tmp'): sudo('touch tata', user='******') assert run('stat -c "%U" tata') == 'nobody' # Check Debian package install fabtools.deb.update_index() require.deb.package('htop') assert fabtools.files.is_file('/usr/bin/htop') # Install Redis require.redis.instance('test') assert fabtools.files.is_file('/etc/redis/test.conf') assert run('echo PING | /opt/redis-2.4.15/redis-cli') == 'PONG' assert 'debian' in openvz.list_ctids() # Stop and destroy container with container(NAME, TEMPLATE, hostname=NAME, ipadd=IPADD) as ct: ct.stop() ct.destroy() assert 'debian' not in openvz.list_ctids()