Scenario: working with micro-services

In this scenerio we’ll see how Dodo Commands can be used to work with two micro-services. It’s probably over-kill to use Dodo Commmands in this simple scenario, but as the project grows bigger, it will start to be worth it. To keep it simple the services are not Dockerized.

Tip

In the tutorials we’ll assume that Bash is used. In some cases we will source the output of a Dodo command using $(dodo <command>). In case you are curious what is being sourced, you can run the command without $() and source it manually on the command line. If you are using the Fish shell, then you can use dodo <command> | source instead of $(dodo <command>). In that case, be sure to also run dodo global-config setting.shell fish to tell Dodo Commands that you are using the fish shell.

Tip

Parts of the tutorial that give technical details or explain more advanced features are kept separate from the main text. To not be overwhelmed, it’s probably better to skip them on a first read.

Two simple micro-services

The first micro-service writes the time to a file in the /tmp directory, whereas the second micro-service runs a tail command that tracks the contents of this file. The source code for these services is found in part1/before of the dodo-commands-tutorial <https://github.com/mnieber/dodo-commands-tutorial>`_ repository. We will go ahead and clone the code for this part of the tutorial:

cd /tmp
git clone https://github.com/mnieber/dodo-commands-tutorial.git

# Copy part 1 of the tutorial so that we can work with a short path
cp -rf ./dodo-commands-tutorial/part1/before ./tutorial

Setting up an environment

The next step is to create a Dodo Commands environment for working with our project:

cd /tmp/tutorial
$(dodo env --init tutorial)

# Check that we are in the "tutorial" environment
dodo which

    tutorial

Working with the configuration

Each environment contains a set of configuration files:

# The main configuration file is called config.yaml
dodo which --config

    /tmp/tutorial/.dodo_commands/config.yaml

# Let's take a look at the configuration file:
cat $(dodo which --config)

    ROOT:
      command_path:
      - ~/.dodo_commands/default_project/commands/*
      version: 1.0.0

Extending the configuration

You can extend the configuration with new keys:

# (bottom of) /tmp/tutorial/.dodo_commands/config.yaml
MAKE:
  cwd: ${/ROOT/project_dir}/writer

Note

From here on, we will use the notation ${/FOO/bar} to refer to the bar key in the FOO section of the configuration file.

Adding an alias to run the writer service

We’ll now create a mk.py script that can be used as an alias for running the writer service. This alias will serve as a shortcut to running make in the directory of the writer service.

cd /tmp/tutorial
mkdir ./commands
touch ./commands/mk.py

Add the following code to mk.py:

from dodo_commands import Dodo

Dodo.parser.add_argument("what")
Dodo.run(["make", Dodo.args.what], cwd=Dodo.get("/MAKE/cwd"))

Using layers to run the reader and writer service

At the moment, the mk command operates on the writer service. What if we instead want to run the Makefile of the reader service?

As a first step to generalize our mk command we will move the ${/MAKE} section to a new configuration file: server.writer.yaml. This file should therefore look like this:

# /tmp/tutorial/.dodo_commands/server.writer.yaml
MAKE:
  cwd: ${/ROOT/project_dir}/writer

Then we add a similar file for the reader:

# /tmp/tutorial/.dodo_commands/server.reader.yaml
MAKE:
  cwd: ${/ROOT/project_dir}/reader

Tip

Don’t forget to remove the MAKE section from the main Dodo configuration file. To edit this file, you can run (substituting your favourite editor) nano $(dodo which –config).

Detail sections

Open the adjacent tabs for more detail sections