Nutanix Calm DSL – Remote Container Development Part 1

Nutanix Calm DSL - Remote Container Development Part 1

Table of Contents

Welcome back to the next instalment of the series I’m calling “stuff about the Calm DSL”. 🙂 In all honesty, the series doesn’t have a title but all the latest articles on Nutanix.dev have been about the Nutanix Calm DSL, so let’s just go with it.

You’ll recall from recent articles that we’ve done a few things so far. Up to this point we’ve looked at:

Today, however, we’ll look into doing things a little differently. Rather than re-hash information that’s already been covered, following this article will require that you’ve already completed the following tasks:

  • Installed the Nutanix Calm DSL
  • Built the local binary and can run the “calm” command without issues
  • Built the Calm DSL container using “make docker” (covered in Introducing the Nutanix Calm DSL)

My development system is running Kubuntu 20.04 i.e. the latest Kubuntu version. With that in mind, the steps in this article will all be carried out on Kubuntu 20.04 (although any Ubuntu-based distribution will work the same way i.e. Ubuntu, Lubuntu etc).

Setting Up

To complete the steps in this article we’ll need to install some specific software and then configure it a certain way. Let’s do that now.

Docker User Permissions

In order to allow our user to run the Docker binary without requiring sudo, the current logged in account must be added to the docker group. After making this change, you’ll need to logout and back in – we can then verify that the user account has been correctly added.

Note: Make sure you don’t omit the -a flag! Omitting this flag and specifying -G only will result in the user account being added to the docker group and removed from all other groups.

sudo usermod -aG docker $USER
groups
Add the current user to the “docker” group, then verify group membership

Visual Studio Code

My development environment, as well as the one outlined in the parent article for this process, uses Visual Studio Code. Aside from being a code editor that has gained popularity in recent times, it allows us to add a massive number of different plugins for things like language support, debugging environments and various development-focused tasks.

If you don’t have it already, please download and install Visual Studio Code from the links available in this article.

Once you have Visual Studio Code installed, we’ll need to install the Remote – Containers extension. At the time of writing this article, the extension is still officially in Preview mode, but that should be OK for now.

Click the View menu, select Extensions, then search for something like “remote containers”. The Remote – Containers extension should be the first match:

Installing the Remote – Containers extension inside Visual Studio Code

Starting the Calm DSL Container

At the start of this article I listed an assumption that you’ve already got the Calm DSL container built. At this point you may not yet have the container actually running, so let’s make sure it is running now.

My clone of the Nutanix Calm DSL repository is in ~/data/solutions/nutanix/calm-dsl.

cd ~/data/solutions/nutanix/calm-dsl
. venv/bin/activate
make run

This will activate our virtual environment if it’s not already activated, then start the Calm DSL container.

Because containers are inherently ephemeral, a Calm DSL container that’s been started for the first time won’t have the DSL configured, yet. If you haven’t done so already by following the Introducing the Nutanix Calm DSL article, configure it now with the following command:

calm init dsl

Follow the prompts and enter settings appropriate for your environment. Then, to make sure we have a blueprint to work with, initialise the demo blueprint as follows:

calm init bp

This will create a set of demo blueprint files in a directory named HelloBlueprint within the container.

Back to Visual Studio Code

Now that our container is running and has the Calm DSL configured, it’s time to head back over to VS Code – we’ll spend the majority of the rest of this article there.

Attach to Running Container

Because our container is already running and because we added the Remote – Containers plugin, we can immediately attach VS Code to that container.

If you have multiple containers running in your environment and need to verify the name of your Calm DSL container, do so as follows. You’ll see my development environment only has a single container.

docker ps
Listing running containers with docker ps

Now, in VS Code, we can use the “Open a remote window” button (shown below) to select Remote-Containers: Attach to Running Container …

VS Code “Open a remote window” button
Using VS Code to select Remote-Containers: Attach to Running Container …

When presented with the list of containers running in your environment, select the container identified in an earlier step. VS Code will then attach to that container and prepare a new project that’s ready to work with that container.

Using VS Code to attach to a running container

If all previous steps were completed successfully i.e. configuring the DSL and creating the demo blueprint, you’ll see HelloBlueprint on the left of VS Code.

VS Code connected to the Calm DSL container and showing the HelloBlueprint demo blueprint created earlier

Add Python Plugin to the Container

Now that we are attached to the Nutanix Calm DSL container, it’s worth keeping in mind that the DSL itself is written with Python 3. This is very much an explicit design choice as it allows us to do some pretty cool stuff. The thing we’ll focus on today is using Intellisense within VS Code when attached to a running container. Sounds simple? As a high level concept it is, since code completion isn’t exactly new. However, when working with a DSL like this one, having VS Code autocomplete while creating blueprints can be extremely helpful.

Within VS Code, select View > Extensions. The extension we’re looking for is simply called Python. If it is not already listed, please use the “Search Extensions in Marketplace” option.

When the correct extension has been found, you’ll have the option to install the extension in the container. Click the “Install in Container ntnx/calm-dsl” button and wait until the installation has finished.

Using VS Code to install the Python extension in the container

Once the extension has been installed, you may be presented with a button indicating a reload is required before continuing. If that happens, please click the button immediately. Once VS Code has reloaded, the view may change slightly so that your remote container’s files aren’t visible. To “fix” that, change to “Explorer” view:

VS Code Explorer view button

With this completed, there are two settings that need to be configured:

  • Selecting the correct Python interpreter
  • Selecting our Python linter (optional, but highly recommended)

Select Python Interpreter

  • Within VS Code, press Ctrl-Shift-P (Windows/Linux) or Cmd-Shift-P (Mac) to open the command palette
  • Start typing “interpreter” and select the Python: Select Interpreter option when it becomes available:
Python: Select Interpreter
  • When prompted, select /usr/local/bin/python as the Python 3.7 interpreter:
Selecting /usr/local/bin/python as the Python interpreter
  • Verify the correct Python binary is being used by selecting Terminal > New Terminal. Within the new terminal, enter the following command. /usr/local/bin/python should be returned as the correct intrepreter.
which python

Select Python Linter

  • Within VS Code, press Ctrl-Shift-P (Windows/Linux) or Cmd-Shift-P (Mac) to open the command palette
  • Start typing “linter” and select the Python: Select Linter option when it becomes available:
Python: Select Linter
  • When prompted, select flake8 as the Linter:
Selecting flake8 Python Linter

Note: flake8 isn’t mandatory at this point. If you have a preferred Linter you may select the Linter of your choice. flake8 is simply a common (and recommended) choice.

  • If VS Code informs you that flake8 is not installed, use the provided option to install the flake8 linter

Testing Intellisense

Now that VS Code is configured and ready to work with Python and the DSL within our attached container, we can open our existing demo blueprint and start testing out Intellisense. This test will make sure Intellisense works with objects made available by the Nutanix Calm DSL.

  • Open “blueprint.py” from the HelloBlueprint folder
  • We’ll use Calm DSL variables to demonstrate these concepts. To do this, navigate through the blueprint.py file until you find the following code block:
ENV = Variable.WithOptions.Predefined.string(
    ["DEV", "PROD"], default="DEV", is_mandatory=True, runtime=True
)

This small code block defines a Calm DSL variable that will be used when launching the demo blueprint. It’s a variable that will appear in the form of a predefined list via a dropdown box.

Somewhere around this block (above or below – it doesn’t matter) start typing the following code:

TEST_VAR = Variable.

As you enter the period character, the Python extension in conjunction with the Remote – Containers extension will be able to provide a list of available variable types for use as Calm DSL variables. There’s no need to know them before doing any of this and makes development custom blueprints extremely easy.

If everything has worked you’ll see something like the following:

Python extension via the Remote Container extension working together to provide Calm DSL Intellisense

Carrying on by either entering additional text, clicking the available options or navigating and pressing TAB will allow us to create a Simple string variable. By doing, we can see the available parameters for the simple string method – cool!

Available parameters for the Variable.Simple.string() method

Need a Calm runtime variable? Just set runtime=True. Easy. 🙂

Wrapping Up

By following today’s article, you’ve seen how to attach to an existing Calm DSL container and work with it in VS Code. The Remote – Containers and Python extensions were used to enable Intellisense/code autocomplete while creating Calm DSL blueprints using Python 3.

Hopefully this article was useful and informative. In the next article, we’ll look at something similar but will use VS Code to start run a new Calm DSL container for us.

Thanks for reading and have a great day!

© 2024 Nutanix, Inc. All rights reserved. Nutanix, the Nutanix logo and all Nutanix product, feature and service names mentioned herein are registered trademarks or trademarks of Nutanix, Inc. in the United States and other countries. Other brand names mentioned herein are for identification purposes only and may be the trademarks of their respective holder(s). This post may contain links to external websites that are not part of Nutanix.com. Nutanix does not control these sites and disclaims all responsibility for the content or accuracy of any external site. Our decision to link to an external site should not be considered an endorsement of any content on such a site. Certain information contained in this post may relate to or be based on studies, publications, surveys and other data obtained from third-party sources and our own internal estimates and research. While we believe these third-party studies, publications, surveys and other data are reliable as of the date of this post, they have not independently verified, and we make no representation as to the adequacy, fairness, accuracy, or completeness of any information obtained from third-party sources.