Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/fbsqlcom/public_html/youdidwhatwithtsql.com/wp-includes/pomo/plural-forms.php on line 210

Warning: Cannot modify header information - headers already sent by (output started at /home/fbsqlcom/public_html/youdidwhatwithtsql.com/wp-includes/pomo/plural-forms.php:210) in /home/fbsqlcom/public_html/youdidwhatwithtsql.com/wp-content/plugins/wordpress-mobile-pack/inc/class-wmp-cookie.php on line 50
youdidwhatwithtsql.com

How to tell if you’re in a docker container

Sometimes you need to know if you’re inside a docker container from the shell. Here’s how you can do that..

First spin up a container..

docker run -ti --rm ubuntu

You can perform a cat on the cgroups section of your process in /proc. N.B. self means the calling process…

cat /proc/self/cgroup

In a docker container this will output something like…

14:name=systemd:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
12:pids:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
11:hugetlb:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
10:net_prio:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
9:perf_event:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
8:net_cls:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
7:freezer:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
6:devices:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
5:memory:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
4:blkio:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
3:cpuacct:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
2:cpu:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6
1:cpuset:/docker/9a1abca96b85398a4fae1409a355628bd55a24d6ef0571788c04254763fe8ef6

For comparison this is output from a non-dockerised VM…

12:rdma:/
11:perf_event:/
10:devices:/user.slice
9:cpuset:/
8:memory:/user.slice
7:cpu,cpuacct:/user.slice
6:hugetlb:/
5:pids:/user.slice/user-1001.slice
4:blkio:/user.slice
3:net_cls,net_prio:/
2:freezer:/
1:name=systemd:/user.slice/user-1001.slice/session-c6.scope

We could grep this namespace as follows…

grep :/docker /proc/self/cgroup | wc -l

I’m not sure if there’s a specific number of entries, that all docker containers have, in fact I believe it can vary. Until I know better I think unless this returns 0 I will assume I’m in a docker container.

Disable line-length Yamllint rule in Molecule

Just a quick post on how to disable the yamllint line-length rule in molecule tests (I’m always forgetting).


....roles/mongodb_repository/tasks/main.yml
      8:81      error    line too long (104 > 80 characters)  (line-length)
      10:81     error    line too long (81 > 80 characters)  (line-length)
      27:81     error    line too long (124 > 80 characters)  (line-length)
      34:81     error    line too long (130 > 80 characters)  (line-length)

Disable the rule as shown below…

lint:
  name: yamllint
  options:
    config-data:
      line-length: disable

Ignore PEP rules with Molecule / Testinfra / flake8

I’m always forgetting how to configure my molecule.yml file to ignore certain PEP8 rules. Here’s a quick example showing how to ignore the E501 Line too long rule:

verifier:
  name: testinfra
  lint:
    name: flake8
    options:
      ignore: 'E501'

Using Ansible Modules with Testinfra

I’ve been looking at improving the quality of the testing I do with Molecule and Testinfra. Simple checks like service.is_running or package.is_installed have their place but they’re pretty limited as to what assurances they provide us. Part of the issue I have is that some tests need a fair bit of setup to make them worthwhile. Attempting to tackle that with raw Python might be a little bit tricky. A better approach is to use the Ansible module available within TestInfra. We can call an ansible module with the following Python code:

ansible = host.ansible(module name,
                       module arguments,
                       check mode,
                       become)

Some of the stuff I do involves Nagios Plugins that parse log files for interesting content. I can test the the plugin is doing what it’s supposed to by setting log file content and then running the plugin and checking the output. Here’s an example of that…

def test_nagios_plugin_test(host):
    with host.sudo():
        cmd = host.run("rm -f /var/log/mylogfile.log")
        assert cmd.rc == 0

    with host.sudo("reaper"):
        ansible = host.ansible("copy",
                               "src=dummy_log_content.log dest=/var/log/mylogfile.log",
                               check=False,
                               become=True)
        assert ansible['changed'] == True

    with host.sudo("nrpe"):
        cmd = host.run("/usr/lib64/nagios/plugins/check_logfile_custom")
        assert cmd.rc == 0
        assert cmd.stdout.strip() == "OK: All looks good in /var/log/mylogfile.log"

The dummy_log_content.log is placed into the roles molecule/default/files directory. By providing a range of dummy log files I can ensure the Nagios Plugin is working as expected.

Using Bash brace expansion to generate multiple files

I needed to generate a whole bunch of files, with identical content, for a recent task. You might automatically think of using a loop for such a task but there’s a much simpler method using brace expansion in the shell.

I wanted to generate files in the following format…

rhys-tmp01.txt
rhys-tmp02.txt
rhys-tmp03.txt
...
rhys-tmp91..txt
rhys-tmp92.txt

This is achievable with a simple one-liner once we have created the source file rhys-tmp01.txt:

tee rhys-tmp{02..92}.txt < rhys-tmp01.txt

Note that this uses zero-padding and this won't work in old versions of bash (probably needs to be at least version 4).