Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With Infrastructor

DZone's Guide to

Getting Started With Infrastructor

Learn how to start running provisioning scripts with Infrastructor, an open source Infrastructure as Code framework for provisioning bare-metal servers or VMs.

· DevOps Zone
Free Resource

“Automated Testing: The Glue That Holds DevOps Together” to learn about the key role automated testing plays in a DevOps workflow, brought to you in partnership with Sauce Labs.

What Is Infrastructor?

Infrastructor is an open source Infrastructure as Code framework. It is written in Groovy and provides a Domain Specific Language (DSL) to provision either bare-metal servers or virtual machines. Infrastructor doesn't need any agents to be installed on a target host. It connects to the target host by SSH and runs commands you want to run. 

This article will describe how to start working with Infrastructor to create and run a simple provisioning script.

A Simple Example

Let's start with a simple example:

inlineInventory {

  node host: 'localhost', port: 10022, username: 'devops', password: 'devops'

}.provision {

  task(name: 'install some packages') {

    info 'updating apt cache'
    shell sudo: true, command: 'apt update'

    info 'installing packages:'
    ['tmux', 'mc', 'htop'].each { pkg ->
       info "- $pkg" 
       shell sudo: true, command: "apt install $pkg -y"
    }

    info 'all packages have been installed!'
  }

}

There are two main parts in the example above: 

  1. Inventorydefinition: a set of nodes (servers or virtual machines) to provision. In our case, there is only a (virtual) node, which runs on the local host and listens to port 10022 for SSH connections.

  2. Provisioningplandefinition: a set of tasks to run on the defined nodes. A provisioning plan can have as many tasks as you need. Typically, a single task defines a single unit of work, which consists of several actions to run on a node.

So, the example above contains an inline inventory with a single node definition and a provisioning plan with two actions: run apt update and install three packages (tmux, mc and htop). Since this is a Groovy DSL, we can use the power of the Groovy programming language to define inventories, nodes, tasks, and actions.

To run the example, download the latest verion of Infrastructor from the GitHub release page. Unpack the zip file and add the bin directory to the PATH. Java Virtual Machine is also required to run Infrastructor. Oracle JRE 8 is the recommended one.

Once all tools are installed, we can test it by running the version command:

$ infrastructor version

Usage: infrastructor COMMAND [OPTIONS]

Commands:
  run Run specified file.
  encrypt Encrypt specified files (AES + Base64).
  decrypt Decrypt specified files (AES + Base64).
  version Print version information.
  help Print usage information. Use 'help [command]' for a command specific usage information.

Let's run the node we are going to provision. It might be a VirtualBox VM created with Vagrant, or it might be a specially prepared Docker container: infrastructor/ubuntu-sshd - this Docker container is based on Ubuntu 16.04 with an instance of sshd service running inside. To run a node with this Docker container, make sure you have Docker installed and then launch it in a terminal using this command:

$ docker run -d -p 10022:22 --name infra-test infrastructor/ubuntu-sshd:latest

Now everything is ready to run our first provisioning script! Save the example to a file (example.groovy) and run it using Infrastructor:

$ infrastructor run -f example.groovy

You should see an output like this: 

$ infrastructor run -f example.groovy
:task 'install some packages'
updating apt cache
installing packages:
- tmux
- mc
- htop
all packages have been installed!
:task 'install some packages' - done

Now we can jump to the Docker container to check the packages are there:

docker exec -it infra-test bash

And in the end, we can terminate the node:

docker rm -f infra-test

That's it!

Links and References

With Infrastructor, you can do not only package installation or shell command execution- it currently provides a set of several useful actions like template, upload, fetch, and others. Please check a list of available actions on the wiki page. 

Infrastructor can also run a task on a set of nodes in parallel. There is encryption and decryption support, and there is out of the box support for AWS, so it is possible to provision EC2 instances.  

For more information about Infrastructor features, please visit GitHub Project Wiki page.  

Infrastructor is a young product, but I feel a great potential in the idea of having a provisioning tool as simple as Ansible and as portable and flexible as Apache Gradle. There are many plans and there are many areas to improve, so I personally would be happy to hear any feedback about it. Infrastructor is also waiting for your contribution: feel free to fork the code base, report a bug, or submit a feature/pull request! And the most important thing is: enjoy your infrastructure automation with Infrastructor! Thank you!

Learn about the importance of automated testing as part of a healthy DevOps practice, brought to you in partnership with Sauce Labs.

Topics:
provisioning ,devops ,automation ,infrastructure as code

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}