Failure at Cloud & rescued by Python

RootConf 2016

Fedora Cloud

Talk about testing issues we had for the releasing regular cloud builds

Places where we have to test

Actual bash test cases

Show them the actual test case, like checking if no service is failed or not during bootup.

systemctl --all --failed

Failure: Not tested enough

Failure: missing docker package

How does a project start?

Scratch your own itch

Talk about open source projects, all those small projects we have laying around in our servers/laptops.

What happens next?

Original work from

Now remember about supporting this for 10 years. Do you remember the code you wrote 6 months back?

Something which is installed in all our servers

Readability counts.

  "name": "default",
  "type": "vm",
  "image": "/home/Fedora-Cloud-Base-20141203-21.x86_64.qcow2",
  "ram": 1024,
  "user": "fedora",

Show the Python code to read a json file for configuration. Then the same for ini/cfg files, and then letting people to think about how to do the same in bash. One example project is

import json
json.load(...) # For file like objects
json.loads() # From string

Moving to Python Unittest

Show one example test case, talk about how is enabled a new group of contributors to come in and write more test cases than ever.

def test_services(self):
   "No service should fail in the startup."
   out, err, eid = system('systemctl --all --failed')
   out = out.decode('utf-8')
   self.assertIn('0 loaded units listed', out)

Batteries included

About the default modules available on a Python system. It will be there on every linux server, means you can use your code, without adding any new dependency. Talk about ksc project, which ran from python2.2 to Python2.7.

Errors should never pass silently.

Show Error handling in Python, a few different examples about how detailed it can be.

except socket.timeout: # We have a timeout in the command
    status = False
    timeout_issue = True
except paramiko.ssh_exception.SSHException:
    status = False
    ssh_issue = True

Debugging using pdb

import pdb;pdb.set_strace()

Python decorators

We may have to retry the same function for any error, let us use python decorators to do the same. We want logging too.

class TestDockerInstalled(unittest.TestCase):
    def test_run(self):
        out, err, eid = system('rpm -q docker')
        out = out.decode('utf-8')
        self.assertFalse('not installed' in out, out)
@unittest.skipUnless(if_atomic(), "It's not an Atomic image")
class TestDockerInstalled(unittest.TestCase):
    def test_run(self):
        out, err, eid = system('rpm -q docker')
        out = out.decode('utf-8')
        self.assertFalse('not installed' in out, out)

Functions as first class object

def check_double(num):
  if num > 10 and num % 2 == 0:
    return num * 2
    return -1

numbers = [56, 76, 30, 9, 23]
map(check_double, numbers)
[112, 152, 60, -1, -1]
def my_decorator(func):
    def wrapper(*args, **kwargs):
         print("Before call")
         result = func(*args, **kwargs)
         print("After call")
         return result
return wrapper
def hello():
    print "Hello"

Before call
After call

Great glue language

2 weeks before production, we need Vagrant


Let us see how do we use this to create a class for managing Vagrant instnaces.

cmd = 'vagrant box add {0} --name {1}'.format(image_url, name)
out, err, retcode = system(cmd)
# Now check for error, I will skip this.
if retcode != 0:
    print("Error while trying to add the box.")
    self.failed = True

print("Up the vagrant")

# Let us up the vagrant
cmd = 'vagrant up --provider {0}'.format(self.provider)
out, err, retcode = system(cmd)

Libvirt storage failure

How do we add a function to take care of the same.

For happy users we need to get the same in AWS

libcloud module

30 providers

C library usage

cffi, raw c extension by hand

Great community


Demo a simple fabric code to run on many servers.

from fabric.api import run

def host_type():
    run('uname -s')

Who all can identify this logo? Who all uses Ansible here?

Fedora Ansible repo

Documentation using rst

Thank you


Left, Down, Page DownNext slide
Right, Up, Page UpPrevious slide
POpen presenter console
HToggle this help