Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Chef Habitat allows the developers to automate the processes by defining, packaging, and delivering the applications. It can run in any Operating System or Environment.
The main goal of chef habitat is not the infrastructure. It takes care of the application's configuration, management, and behavior.
Plans
The plan is the file with a complete account of how you will define, organize, configure and deploy your application. You can have plans for both the operating systems-Linux and Windows. The only difference is that in Linux, we save the file with plans.sh, and for the windows operating system, it is named plans.ps1.
Before we jump to Plans in Chef Habitat, Let us first briefly understand Installation, Origins, and Packages.
Pre-requisites
Installing Habitat
In Linux, Chef can be installed by typing the following command on the command line.
In Mac OS, Chef can be installed with the help of Homebrew by running this command
brew tap habitat-sh/habitat
brew install hab
In Windows, Chef can be installed using Chocolatey.
choco install habitat
Origin in Chef Habitat
A space in Chef Habitat where you can store, build and share the packages is known as Origin. It is a unique feature of Chef Habitat Builder. Though you can delete and modify the packages, renaming of packages is not permitted.
A classic example of Origin in chef habitat is the “core” origin which is maintained and managed by the maintainers of the Chef habitat Builder.
Users can create an origin by using the following command-
hab origin create <origin>
Packages in Chef Habitat
After the completion of the origin setup, the build script starts. You can now start uploading your package in the Chef Habitat Builder or access the package locally.
There are several ways to build the packages. It can be done manually using the Graphical user interface service or the Habitat Studio.
If the user intends to use builder, he should follow the following steps:
Log in to the service
Create an “origin.”
Add integration points to it.
Remember that the sources of the package can be GitHub repositories only. Users can amend this later.
Build your package using the Build latest version button after these steps.
The plans must include a plan.ps1 or a plan.sh file. The developer can consist of both files if Linux and windows are targeted simultaneously. To create a plan, Follow these steps-
Download the hab CLI and follow the instructions in the official documentation.
Run hab CLI setup
Create a plan using hab plan init
For defining a plan in case of multiple platforms, the structure would look like this
app_root/
└── habitat/
plan. sh
plan.ps1
Build time Workflow
Specific steps need to be included in the plan file for installation and configuration. These steps define how you will install your application files into a package.
Create a package identifier
The origin is used to set privacy rules, store packages, and collaborate with fellow developers. To build artifacts for an origin, it is necessary to have information about its private key. Origin keets are kept in$HOME/.hab/cache/keys when running as a non-root user and/hab/cache/keys when running as a root.
Standard naming conventions are used to declare the name of your package.
Add the information about licensing and details of the contact
The developer should provide the contact information, and most importantly, the User should update the value of pkg_license. The value should indicate the type of license assigned to the package.
Download the source files
Add the source of your file in pkg_source. Chef habitat allows obtaining files
from Github. An example of performing this operation is-
There are generally two types of dependencies, runtime, and build-time.
The developer must declare build dependency in pkg_build_deps and any run dependencies in pkg_deps.
Plan Quickstart
While the developers work with Plans, it is essential to include a plan. Sh or plan.ps1 file in the root directory of the planning context. Both these files are included when the application targets both Linux and Windows.
The first requirement is to create a plan. The simple way to do this ishab plan init subcommand. This command creates a directory known as plan context that includes many plan and configuration files and hooks.
Any environment variables you will set will be used to populate pkg_* variables.
Now you can modify the plan file according to your needs.
Plan Contents
The best practice to create a plan depends on the user and requirements.
Package Metadata
Package Metadata describes the content to the user. It gives additional information about the author, version, and repositories.
These guidelines need to be followed strictly:
pkg_description
Pkg_license should be in SPDX format
pkg_name
pkg_origin must be set to core
pkg_source
pkg_upstream_url
pkg_version must be the complete version number of the software
Package Naming Conventions
The value of the name should be the same as the project, and the directory's name should be the same as the repository name. Each package is identified by a unique string containing four sub-strings separated by a forward slash (/).
`origin`/`name`/`version`/`release`
Plan Basic Settings
A core plan demands the following-
pkg_name is set
pkg_origin is set
pkg_shasum is set
pkg_description is set
README.md
A file should also contain basic information about the maintainer, its uses, and step-by-step detail on how to follow up with the application.
Scaffoldings are designed to carry out automated builds for the application. They create the essential application lifecycle hooks and add runtime dependencies. Scaffolding also ensures reliability.
Ruby, Gradle, and Node.js are supported for carrying out the scaffolding.
A package must contain a lib/scaffolding.sh file for scaffolding tasks.
Binary Wrapper
Using a Binary wrapper, you can write plans for these binary artifacts without extensive handling.
A Chef habitat build has various phases like download, verification, unpacking, installation, etc. There is a feature of overriding these phases according to the user.
The following example shows the way to override the do_build and do_install phases-
In some situations, the above method does not work. This situation arises because of the version difference between the glibc software and the glibc Chef habitat Package. In this case, you have to rely on the default dependencies.
You can see the patchelf documentation for more reference.
Frequently Asked Questions
What are dependencies?
When a software segment is dependent on another, it is referred to as a dependency.
What is the benefit of writing plans?
They give a flow of how to work with applications from building to deployment.
What is $pkg_prefix?
It signifies the absolute path of the package.
Where can you find your plan file?
The plan file is found in the Habitat Directory.
What is Chef Habitat Builder?
Chef Habitat Builder provides package storage, search, and an API for clients.
Conclusion
In this blog, we studied the Plans in Chef Habitat. We discussed various Subtopics like writing plans, scaffolding, Working with Binary wrapper packages, and overriding.