def update_image(base_image): container = DockerContainer( hostname='nuka', image=base_image, command=['bash', '-c', 'while true; do sleep 1000000000000; done'], ) nuka.run(wait_for_boot(host=container)) image, tag = base_image.split(':', 1) container.cli.commit( container.container_id, repository='nuka_' + image, tag=tag) return container
from nuka.hosts import Vagrant from nuka.tasks import shell import nuka host = Vagrant() async def my_tasks(host): await shell.command('whoami') nuka.run(my_tasks(host))
nuka.config['openstack'] = { 'driver': '~/.ovh/nuka.json', 'user': '******', 'create_node_args': { 'flavor': 's1-2', 'image': 'Debian 8', 'key_name': 'gawel', }, } nuka.config['ssh'].update({ 'extra_options': ['-C', '-oStrictHostKeyChecking=no'], 'keysfile': '~/.ssh/authorized_keys', }) cloud = Cloud('openstack') host = cloud.get_or_create_node('myhost') async def my_tasks(host): await shell.command(['ls']) nuka.cli.add_argument('--destroy', action='store_true', default=False) nuka.cli.parse_args() if nuka.cli.args.destroy: nuka.run(cloud.destroy()) else: nuka.run(my_tasks(host))
# -*- coding: utf-8 -*- import nuka from nuka.hosts import DockerContainer from nuka.tasks import shell host = DockerContainer( hostname='debian_jessie', image='debian:jessie', command=['bash', '-c', 'while true; do sleep 1000000000000; done'], ) async def sleep(host): await shell.command(['sleep', '5'], watch=1) nuka.run(sleep(host))
import nuka from nuka.tasks import shell from nuka.hosts import LocalHost host = LocalHost() async def ls(host): print(await shell.command(['ls', '/'])) nuka.run( ls(host), )
if not await file.exists('app.wsgi', switch_user='******'): await http.fetch(src=WSGI_FILE, dst='app.wsgi', switch_user='******') with tempfile.NamedTemporaryFile(suffix='.j2') as fd: fd.write(b''' ServerName kinto.example.com WSGIScriptAlias / {{kinto_user.home}}/app.wsgi WSGIPythonPath {{kinto_user.home}} SetEnv KINTO_INI {{kinto_config}} <Directory {{kinto_user.home}}> <Files app.wsgi> Require all granted </Files> </Directory> ''') fd.flush() conf = await file.put([ dict(src=basename(fd.name), dst='/etc/apache2/sites-available/kinto.conf') ], ctx=dict(kinto_user=kinto_user, kinto_config=config)) if conf.changed: await shell.command(['a2ensite', 'kinto.conf']) nuka.run(install_kinto(kinto))
# -*- coding: utf-8 -*- import nuka from nuka.hosts import DockerContainer master = DockerContainer('master') slave = DockerContainer('slave') e = nuka.Event('master_ready') async def my_tasks(host): if host is slave: await nuka.wait(e) host.log.warn('starting slave') else: host.log.warn('master ready') e.release() nuka.run( my_tasks(slave), my_tasks(master), )
#!../bin/python import nuka from nuka.hosts import DockerContainer from nuka.tasks import apt async def install(host): res = await apt.update() print(res.res) res = await apt.install(['procps', 'varnish', 'nginx']) print(res.res) nuka.run(install(DockerContainer('apt')))
# -*- coding: utf-8 -*- import nuka from nuka.hosts import DockerContainer from tasks.timezone import timezone host = DockerContainer(hostname='debian_jessie') async def change_timezone(host): await timezone(tz='Europe/Paris') nuka.run(change_timezone(host))
# -*- coding: utf-8 -*- import nuka from nuka.hosts import (DockerCompose, Cloud, Provider) from nuka.tasks import (apt, shell, file, user, archive, service) import mysql nuka.cli.add_argument('--gce', action='store_true', default=False) nuka.cli.add_argument('--destroy', action='store_true', default=False) nuka.cli.parse_args() if nuka.cli.args.gce: all_hosts = Cloud(Provider.GCE).from_compose() else: all_hosts = DockerCompose() nuka.run(all_hosts.boot()) nuka.config['all_hosts'] = all_hosts master = all_hosts['wordpress_master_1'] slave = all_hosts['wordpress_slave_1'] web = all_hosts['wordpress_web_1'] mysql_password = nuka.utils.secret('dsmfk;m#lkf!(').next() db_password = nuka.utils.secret('q,sddsmfk;m#lkf!(').next() async def install_wordpress(host): waits = [ apt.install([ 'apache2', 'libapache2-mod-php5', 'ca-certificates', 'php5-mysql', 'mysql-client'
#!../bin/python from nuka.hosts import DockerCompose from nuka.tasks import shell import nuka hosts = DockerCompose(project_name='myproject') nuka.run(hosts.boot()) host = hosts['myproject_debian_stretch_1'] async def my_tasks(host): await shell.shell('whoami') nuka.run(my_tasks(host))
names = [h.strip() for h in sys.stdin.readlines() if h.strip()] if nuka.cli.args.n: names = names[:nuka.cli.args.n] hosts = [] for name in names: if name.split('.')[0].isdigit(): hosts.append(Host(address=name)) else: hosts.append(Host(name)) start = time.time() results = nuka.run(*[run_client(h) for h in hosts]) print('\nGot {0} valid results for {1} hosts in {2:.02f}s\n\n'.format( len([r for r in results if not isinstance(r, Exception)]), len(results), time.time() - start) ) fmt = '{0:10}{1:>10}{2:>10}{3:>10}{4:>10}' print(fmt.format('', 'min', 'max', 'avg', 'count')) fmt = '{0:10}{1:10.6f}{2:10.6f}{3:10.6f}{4:10.0f}' for key in ('connect', 'auth_time', 'timeouts'): values = [] for d in asyncssh_connections.values(): if isinstance(d, dict): if key in d: values.append(d[key])
host = DockerContainer(hostname='nukai') e = nuka.Event('event') @nuka.cancel_on_error(e) async def fail(host): await shell.command(['sleep', '2']) raise AttributeError() await shell.command(['sleep', '1']) e.release() async def wait(host): await nuka.wait(e) await shell.command(['sleep', '2']) async def not_wait(host): await shell.command(['sleep', '1']) await shell.command(['rm', '/usr']) await shell.command(['sleep', '3']) nuka.run( fail(host), wait(host), not_wait(host), ) nuka.run(host.destroy())
import nuka from nuka.hosts import DockerContainer from nuka.tasks import (shell, file) # setup a docker container using the default image host = DockerContainer('mycontainer') async def do_something(host): # we just echoing something using the shell.command task await shell.command(['echo', 'it works'], host=host) # if no host is provided, then a var named `host` is searched # from the stack. Mean that this will works to await shell.command(['echo', 'it works too']) async def do_something_else(host): # log /etc/resolv.conf content res = await file.cat('/etc/resolv.conf') host.log.info(res.content) # those coroutines will run in parallell nuka.run( do_something(host), do_something_else(host), )