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 https://xkcd.com/386/

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

https://tunir.rtfd.org

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 https://github.com/dominictarr/JSON.sh/blob/master/JSON.sh

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.

try:
    obj.connect(...)
except socket.timeout: # We have a timeout in the command
    status = False
    timeout_issue = True
    break
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
  else:
    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
@my_decorator
def hello():
    print "Hello"

hello()
Before call
Hello
After call

Great glue language

2 weeks before production, we need Vagrant

subprocess.Popen

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.")
    print(err)
    self.failed = True
    return
print(out)

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

Fabric

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

@kushaldas

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