Table of contents
1.
Introduction
2.
How to Build Packages? 
2.1.
Interactive Builds
2.2.
Non-Interactive Builds
3.
How to Promote Packages?
3.1.
Continuous Deployment
3.2.
Removal of a Package From a Channel
4.
How to Export Packages?
5.
Continuous Integration in Chef Habitat
6.
Runtime Binding in Chef Habitat
6.1.
Contract of Producer 
6.2.
Contract of Consumer
7.
Frequently Asked Questions
7.1.
What do you mean by the Habitat package?
7.2.
What role does the habitat supervisor serve?
7.3.
What do you mean by Chef habitat builder?
7.4.
What do you mean by runtime binding in packages in Chef habitat?
7.5.
What is continuous integration?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Packages in Chef Habitat

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Packages in chef

We are fond of using automated configuration management tools rather than manual ones. A configuration management tool called Chef is utilized to automate the provisioning of the infrastructure. It was created using the Ruby DSL(Domain Specific Language) language as a foundation. Configuring and managing the company's server is streamlined with it.

In this article, we will discuss packages in Chef habitat in detail. Let's discuss how to build packages in Chef habitat.

How to Build Packages? 

After finishing your plan and call build in Chef Habitat Studio, the build script follows the following actions:

  1. Verify that Studio's private origin key is needed to sign the artifact.
  2. If specified, download the source code from the location in pkg_source.
  3. If specified, validate the checksum of the downloaded file using the pkg_shasum value.
  4. Extract the sources into an interim cache.
  5. For Linux-based builds, creates and install the binary or library using make and make install. TODO: WHAT IS USED BY WINDOWS? Invoke-Unpack with Start-Process, perhaps? Copy-Item & Invoke-Install? Unless the plan overrides the callback methods.
  6. Creates a tarball by compressing the package's contents (binaries, runtime dependencies, libraries, assets, etc.).
  7. Give the tarball a.hart file extension and sign it with your private origin key.

Following the build script's completion, you can install and launch your package locally or upload it to Chef Habitat Builder.

At build time, packages must be signed using a private origin key. Run the command below on your host PC to manually create an origin key pair:

hab origin key generate <ORIGIN>

The files originname-timestamp.sig.key (the private key) and originname-timestamp.pub (the public key) are stored in the $HOME/.hab/cache/keys directory by the hab-origin subcommand. Your keys will be kept in /hab/cache/keys if you create origin keys in the Studio container or execute as root on a Linux computer.

Anyone who downloads and uses your artifact must have your public key (.pub) placed in their local $HOME/.hab/cache/keys or /hab/cache/keys directory. This is because the private key is needed to sign your artifact. Therefore, it shouldn't be shared openly. Chef Habitat tries to download the origin's public key from the Builder endpoint given by the --url option to hab pkg install, which is default set to https://bldr.habitat.sh if it is missing.

Interactive Builds

An interactive build is any build you carry from a Chef Habitat Studio. You can inspect the build environment before, during, and after with the help of interactive studio build.

Non-Interactive Builds

In a non-interactive build, Chef Habitat makes a Studio for you, builds the package inside of it, destructs the Studio, and leaves the finished product .hart on your PC. When you are certain that the build will succeed or use a continuous integration system, use a non-interactive build.

You can learn more about building packages in Chef habitat here. Let us discuss promoting packages.

How to Promote Packages?

A well-known software development technique called continuous deployment involves creating and testing code updates before their distribution to a production environment.

Continuous Deployment

Chef Habitat uses channels to facilitate processes for continuous deployment. The Supervisors in a service group can subscribe to a channel, which is a tag for a package. Channels are handy when you want to gate a package before making it the version users should always use in CI/CD environments. This division can be compared to the distinction between test and production or between nightly and stable product releases.

Builder automatically assigns each new package to the unstable channel. Without the --channel flag in the hab CLI or setting the HAB_BLDR_CHANNEL environment variable to a non-stable channel, packages in the unstable channel cannot be started or installed. This is due to the stable channel being the default channel utilized by the hab CLI when starting, installing, or loading packages. The stable channel denotes a capability and reliability suitable for usage as a dependence for your service or in multi-service systems.

You must either use Builder to create your package or upload it yourself to Builder before using the hab pkg promote subcommand to advertise the package to the desired channel. Use the --channel option when uploading your package in the hab CLI(Command Line Interface) to merge the two operations into a single command. The information below demonstrates how to upload and promote a package to the test custom channel.

$ hab pkg upload -z <TOKEN> results/<hart file> --channel test

You can notice that your package is marked for the test and unstable channels in the aforementioned example if you look it up in the Builder UI or by using the hab pkg channels subcommand.

Remember that each package's channels, such as tests, are scoped. If you want to use custom channels for upcoming package versions, you must promote those channels for every release because Builder does not construct channels scoped to an origin.

Use the hab pkg promote subcommand as displayed below to promote a package that has previously been posted to a channel to another channel.

$ hab pkg promote -z <TOKEN> <origin>/<package>/<version>/<release> stable

Removal of a Package From a Channel

If necessary, the hab pkg demote subcommand can separate a channel from a specific package release. From all channels except for unstable, packages can be downgraded.

$ hab pkg demote -z <TOKEN> <origin>/<package>/<version>/<release> test

The elimination of that channel will be reflected in both the hab pkg channels and the Builder UI for that package release.

If you run a package on a particular channel and downgrade.

Any other packages that rely on the stable version of a package that has been demoted from the stable channel would either fail to load or build, depending on how that package was used.

You can read here to learn more about promoting packages in Chef habitat.

How to Export Packages?

Depending on what you need and where you need it, Chef Habitat Artifacts .hart files can be exported in various formats. This is effective because you may use the same unchangeable Chef Habitat artifact by exporting it into the right format for the task.

Packages can be exported into a variety of external, immutable runtime formats. Now, exports are available for docker, mesos, tar, and cloudfoundry.

A package can be exported using the command hab pkg export <FORMAT> <PKG_IDENT>.

The Chef Habitat CLI will check Builder for the most recent stable version of the package if you specify an origin/package identification, such as core/postgresql, and export that.

Create a Chef Habitat artifact by running the build command, then point hab pkg to the .hart file located in the /results directory if you want to export a package that is not on Builder:

hab pkg export tar ./results/example-app.hart

You can read here to learn more about exporting packages in Chef habitat.

Continuous Integration in Chef Habitat

Utilizing CLI tools and plugins, continuous integration enables you to develop, test, and deploy your code. For interactive builds on your developer workstation or non-interactive builds with your continuous integration server, Chef Habitat provides the Chef Habitat Studio. To enable your applications to update themselves, your continuous integration server can also make calls to the Chef Habitat CLI to promote your Chef Habitat packages to other channels. Chef Habitat can streamline your continuous integration server's builds and promotion procedures, even if it is not a continuous integration server.

Your application can be built in a clean room environment using the Chef Habitat Studio. In practice, builds that take place on a continuous integration server or on a developer's workstation will build in the same way. Whole classes of "it works on my box" issues are no longer concerned for developers. For continuous integration servers, build engineers are no longer required to build distinctive and challenging-to-maintain worker nodes. Instead, the Chef Habitat plan.sh file contains all the data required to create the whole application, including packaging, application lifecycle hooks, runtime environment binaries, dependency management, and runtime environment binaries.

Runtime Binding in Chef Habitat

"Runtime binding" means establishing a connection to another service group and establishing a producer-consumer relationship where the consumer service can utilize the producer service's current configuration to configure itself at runtime. The consumer is alerted when the producer's configuration changes and can adjust it as necessary.

Using runtime binding, a consumer service can use a custom "binding name" as a handle to refer to the configuration information they need from the producer service in their configuration and lifecycle hook templates. There is no intrinsic connection between this name and any specific package or service group name. Instead, users identify a service group with that binding name when the service is invoked, giving Chef Habitat all the information required to connect the producer and consumer services.

Let's take a closer look at the configuration of this relationship.

Contract of Producer 

A producer service establishes the terms of its contract by "exporting" a portion of its runtime configuration. To accomplish this, define values in the pkg exports associative array defined in the plan.sh file for your package. For example, the exports listed below may be defined by a database server called amnesia:

pkg_exports=(
  [port]=network.port
  [ssl-port]=network.ssl.port
)

Be aware that Bash uses associative arrays while Powershell uses hashtables. Plan.ps1 would list the following as its exports:

$pkg_exports=@{
  port="network.port"
  ssl-port="network.ssl.port"
}

Contract of Consumer

The consumer service defines a "binding name" as a handle to refer to a service group from which it obtains configuration data. It must specify the configuration values it anticipates from the service group in addition to the bind's name. Chef Habitat will ensure that any service group to which a contract is attached outputs the desired data to the customer service.

For example, imagine that we have an application server called session-server that must connect to a database service, and that connection requires both a "port" and an "ssl-port." In our plan.sh file, we may explain this relationship as follows:

pkg_binds=(
  [database]="port ssl-port"
)

You can read here more about runtime binding in packages in Chef habitat.

Frequently Asked Questions

What do you mean by the Habitat package?

Chef Habitat is an open-source solution that offers automated features for creating, packaging, and delivering applications to practically any environment, regardless of the operating system or deployment platform.

What role does the habitat supervisor serve?

The Habitat supervisor is used to manage Habitat packages. The supervisor is in charge of starting the application included in a Habitat package, creating the necessary configurations, and ensuring your program operates properly at any given stage of its lifetime.

What do you mean by Chef habitat builder?

Developers can deploy their apps to any environment with Chef's Habitat Builder.

What do you mean by runtime binding in packages in Chef habitat?

In Chef Habitat, "runtime binding" refers to a connection between two service groups that creates a producer-consumer relationship in which the consumer service can use its current configuration to dynamically configure itself.

What is continuous integration?

Using continuous integration, you can use CLI tools and plugins to build, test, and deploy your code.

Conclusion

We learned about the packages in Chef habitat. We have discussed building, promoting, and exporting packages. We have also learned about continuous integration in the Chef habitat.

To learn more about the chef, you can check out the following articles:

To learn more about DSA, Competitive Programming, and many more knowledgeable topics, please look into the Guided Paths on Coding Ninjas Studio. Also, you can enroll in our Courses and check out the Mock Test and Problems available to you. Please check out our Interview Experiences and Interview Bundle for placement preparations.

Happy Learning!

Live masterclass