{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Lago SDK Example - one VM one Network\n", "----------------------------------------------" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import logging\n", "import tempfile\n", "from textwrap import dedent\n", "from lago import sdk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a LagoInitFile, normally this file should be saved to the disk. Here we will use a temporary file instead. Our environment includes one CentOS 7.3 VM with one network." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with tempfile.NamedTemporaryFile(delete=False) as init_file:\n", " init_file.write(dedent(\"\"\"\n", " domains:\n", " vm-01:\n", " memory: 1024\n", " nics:\n", " - net: net-01\n", " disks:\n", " - template_name: el7.3-base\n", " type: template\n", " name: root\n", " dev: sda\n", " format: qcow2\n", " nets:\n", " net-01:\n", " type: nat\n", " dhcp:\n", " start: 100\n", " end: 254\n", " \"\"\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will initialize the environment by using the init file. Our ``workdir`` will be created automatically if it does not exists. If this is the first time you are running Lago, it might take a while as it will download the CentOS 7.3 template. You can monitor its progress by watching the log file we configured in ``/tmp/lago.log``. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "env = sdk.init(config=init_file.name,\n", " workdir='/tmp/lago_sdk_simple_example',\n", " loglevel=logging.DEBUG,\n", " log_fname='/tmp/lago.log')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the method returns, the environment can be started:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "env.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check which VMs are available and get some meta data:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'vm-01': }\n" ] } ], "source": [ "vms = env.get_vms()\n", "print vms" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'192.168.202.2'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vm = vms['vm-01']\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'el7'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vm.distro()\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'192.168.202.2'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vm.ip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Executing commands in the VM can be done with ``ssh`` method:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "res = vm.ssh(['hostname', '-f'])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "CommandStatus(code=0, out='vm-01.lago.local\\n', err='')" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets stop the environment, here we will use the ``destroy`` method, however you may also use ``stop`` and ``start`` if you would like to turn the environment off. " ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "env.destroy()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }