Deploy Psychz Dedicated Server Using Terraform Provider
Publisher: Psychz Networks, June 21,2023- Introduction
- Prequisites
- Install Terraform
- Configure Provider Environment
- Building Provider
- Provider Installation
- Initializing Terraform
- Order Plans
- Deploying Server
- Order Details
- Service Details
Introduction
Terraform is an open-source infrastructure-as-code (IaC) tool that allows you to define and provision infrastructure resources across various cloud platforms. Whether you are working with Ubuntu or Alma Linux, this article will guide you through the process of getting started with Terraform.
Prerequisites
Before you begin, ensure that you have the following prerequisites in place:
- Debian/Ubuntu or RHEL/CentOS/Alma Linux installed on your machine.
- A text editor of your choice (e.g., Vim, Nano, Visual Studio Code).
- Terraform 1.4.6
- Go 1.20.4
Install Terraform
You can install the latest version of Terraform on most operating systems from the command line using various package managers. Click your operating system’s tab below to view instructions on how to Terraform.
Debian/Ubuntu
To install Terraform on Ubuntu, add the HashiCorp GPG key to your system:
# curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
Next, add the official HashiCorp Terraform Linux repository to apt:
# apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Then update apt and install Terraform:
# apt-get update && sudo apt-get install terraform
Once installed, verify the installation:
# terraform -v
The command returns Terraform’s version information:
Terraform v1.5.1
CentOS/RHEL/AlmaLinux
# yum install -y yum-utils
# yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# yum -y install terraform
Configure Provider Environment
The Provider is responsible for understanding and managing the lifecycle of a specific type of infrastructure resource. It interfaces between Terraform and your desired infrastructure or service at Psychz. To configure the Provider environment, we will install Go
Installing GoLang
To get the latest version of Go, visit to the official Go downloads page in your web browser. From there, copy the URL for the current binary release’s tarball.
As of this writing, the latest release is go1.20.5. To install Go on an Ubuntu server (or any Linux server, for that matter), copy the URL of the file ending with linux-amd64.tar.gz.
IMPORTANT: Please confirm that you’re in the home directory before you initiate installation
Use the following command to retrieve the tarball
# curl -OL https://golang.org/dl/go1.20.5.linux-amd64.tar.gz
Now, extract the tar file using the following commmand into the following directory /usr/local/
# tar -C /usr/local -xvf go1.20.5.linux-amd64.tar.gz
Flags:
-C: instructs tar to change to the given directory
x: Extract tar file
v: Lists files being extracted
f: Tell we'll specify filename
Setting Go path
Add /usr/local/go/bin to the PATH environment variable.
You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):
# nano ~/.profile
Then, add the following information to the end of your file:
export PATH=$PATH:/usr/local/go/bin
refresh your profile by running the following command
# source ~/.profile
To check the installed version of Go, use the following command
# go version
Output
go version go1.20.4 linux/amd64
Building the Provider
We will now setup Psychz Terraform Provider repository on our system. We will copy/clone the repository from GIT into $HOME/development directory. Follow the steps given below to complete the setup
Cloning Psychz Terraform Provider repository
# mkdir -p $HOME/development; cd $HOME/development
# git clone https://github.com/psychz-networks/terraform-provider-psychz.git
Now let us build the Provider. To do so, you must be inside the cloned Psychz Terraform provider directory.
# cd terraform-provider-psychz
# go build
Provider Installation
Note: manual provider installation is needed only for manual testing of custom built Psychz provider plugin.
Manual installation process differs depending on Terraform version. Run terraform version command to determine version of your Terraform installation.
Create psychz.net/psychz/psychz/1.0.0/linux_amd64 directories under:
Note: adjust linux_amd64
from above structure to match your os_arch
# mkdir -p ~/.terraform.d/plugins/psychz.net/psychz/psychz/1.0.0/linux_amd64
Copy Psychz provider binary file there.
# cp terraform-provider-psychz ~/.terraform.d/plugins/psychz.net/psychz/psychz/1.0.0/linux_amd64
Initializing Terraform
We will now initialize Terraform by running the "terraform init" command. This command prepares Terraform project by downloading the necessary provider plugins and setting up the working directory. It ensures that Terraform has all the required dependencies to manage your infrastructure and allows you to begin creating and managing resources.
# terraform init
OUTPUT
Initializing the backend...
Initializing provider plugins...
- Finding psychz.net/psychz/psychz versions matching "1.0.0"...
- Installing psychz.net/psychz/psychz v1.0.0...
- Installed psychz.net/psychz/psychz v1.0.0 (unauthenticated)Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future.
╷
│ Warning: Incomplete lock file information for providers
│
│ Due to your customized provider installation methods, Terraform was forced to │ calculate lock file checksums locally for the following providers: │ - psychz.net/psychz/psychz
│
│ The current .terraform.lock.hcl file only includes checksums for linux_amd64, so │ Terraform running on another platform will fail to install these providers.
│
│ To calculate additional checksums for another platform, run: │ terraform providers lock -platform=linux_amd64 │ (where linux_amd64 is the platform to generate)
╵Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
Order Plan
Order Plan gives you all the details about the different types of hardware you can choose from. You get the nitty-gritty technical stuff, like specs and pricing, and even where they're located. This makes it super easy for you to pick and set up the perfect machine whenever you need it, just by using Psychz Terraform Provider and plugging in the info you want.
Note: Before you execute the init command, ensure that you are in the folder ~/order_plan
# terraform init
# terraform apply
Input details
var.availability
var.billing_type
var.category_id
var.location_code
var.option_detail
var.plan_id
provider.psychz.access_token
provider.psychz.access_username
You can skip all the information except for the access_token and the access_username.
Sample output
data.psychz_order_plans.server: Reading...
data.psychz_order_plans.server: Read complete after 1s [id=psychz_order_plans]
Changes to Outputs:
+ server_order_plan = jsonencode(
{
+ data = {
+ dedicated_servers = {
+ "138" = {
+ base_price = "34.00"
+ billing_type = "1"
+ category_id = 2
+ category_name = "Dedicated Servers"
+ location_name = "Los Angeles, USA"
+ plan_id = "138"
+ plan_name = "Los Angeles :: E3-1230 v2"
+ reseller_price = "28.9"
+ standard_note = ""
}
The output provides a big list of all the hardware options with their specs, neatly organized in JSON format. You can save this info on your computer and check it out whenever you want. It's like having a handy reference guide for later!
IMPORTANT: You will also need Access Token and Access Username to successfully perform the above action. Refer to the following article How to setup API Access to know how to gain information on Token and Username.
Order Express
We will now run Terraform to input the changes. The "terraform apply" command executes the changes specified in the Terraform configuration file and apply them to the infrastructure. It provisions or modifies resources based on the desired state and updates the infrastructure accordin
Note: You must be inside the ~/order_express folder before you execute the following commands
# terraform init
# terraform apply
The system will now request you to input information against the following fields (in the same sequence)
+ auth_method
+ client_id
+ disk_partition_id
+ enforce_password_change
+ hostname
+ id
+ message
+ order_quantity
+ os_cat
+ os_id
+ partner_id
+ password
+ payment_mode
+ plan_id
+ private_key
+ software_raid
Once you have successfully entered all the required details, the system will ask you for final approval before executing the code. You need to type "yes" to proceed further.
Sample output
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.Enter a value: yes
After entering "yes, " Terraform will connect with our API server using your provided credentials and execute the order. This typically takes about a minute or less. Once done, the system will post the following output
psychz_order_express.express_server: Creating...
psychz_order_express.express_server: Still creating... [10s elapsed]
psychz_order_express.express_server: Still creating... [20s elapsed]
psychz_order_express.express_server: Still creating... [30s elapsed]
psychz_order_express.express_server: Still creating... [40s elapsed]
psychz_order_express.express_server: Still creating... [50s elapsed]
psychz_order_express.express_server: Creation complete after 53s [id=858145]
data.psychz_order_detail.server: Reading...
data.psychz_order_detail.server: Read complete after 1s [id=858145]Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
server_order_express_status = {
"auth_method" = 2
"client_id" = 1234
"disk_partition_id" = 7
"enforce_password_change" = 0
"hostname" = "temp1234"
"id" = "876084"
"message" = "Order#876084 processed. You can check order status using api /v1/order_details"
"order_quantity" = 1
"os_cat" = 1
"os_id" = 1234
"partner_id" = 0
"password" = "qwerty1234567890"
"payment_mode" = 1
"plan_id" = 123
"private_key" = "rsa-ghjkdfghjkdfghjkfghjkdfghjkdfghjkfghjkfghjkfghj"
"software_raid" = 1234
}
After a successful execution, the output displays the details of the orders and its attributes that you have inputted to commission the server.
Order Details
The order_details showcases information about the server order you placed. This information is encoded in JSON format. This information you can save to maintain a record of all the machines and their billing details.
Note: Ensure that you are in ~/order_detail folder before you execute the following commands
# terraform init
# terraform apply
Sample Output
Address Contact Information:
- Address: Los Angeles, CA, US
- Company: Test Account
- Email: abc@xyz.net
- Fax: 5555555555
- First Name: Test
- Last Name: Account Do Not Process
- Phone: 123-456-7890
- Referred By: (No reference provided)
- Invoice Due Date Method: 0
- Invoice Send Date: 7
Device Information:
- Device ID: 1234567
- Service ID: 1234567
Invoice Information:
- Invoice Amount: $94
- Invoice Date: 2023-08-07 11:08
- Invoice Due Date: 2023-08-21 00:08
- Invoice ID: 123456789
IP Assignments:
- No IP assignments specified
Order Details:
- Last Activity: 2023-08-07 11:08
- Order ID: 12345678
- Order Time: 2023-08-07 11:08
- Order Information:
- Period: Monthly
- Price: $94
- Service: Rate Plan: Ashburn :: E3-1270 v6
- Setup Fee: $0
Payment Information:
- Payment Type: Company Credit
Total Amount:
- Total: $94.00
this output provides a comprehensive snapshot of the order you placed, including contact details, device and service information, payment details, invoice information, and more. It serves as a confirmation and reference for your order commissioning process.
Services Details
This feature provides a detailed overview of all the services associated with your device. It focuses mainly on providing information about your IP addresses and ranges. To make use of this feature, you need to input a service ID, which can be obtained from the "order_details" feature. This service ID acts as a key to unlock access to information about the services linked to your device, particularly the IP-related details.
Note: Ensure that you are in ~/services_details folder before you execute the following commands
# terraform init
# terraform apply
Sample output
data.psychz_service_detail.server: Reading...
data.psychz_service_detail.server: Read complete after 1s [id=psychz_service_detail]
Changes to Outputs:
+ server_service_detail = {
+ id = "psychz_service_detail"
+ ip_assignments = jsonencode(
[
+ {
+ address = "123.456.789.000/31"
+ assign_description = "123.456.789.012/31"
},
]
)
+ service_id = 123456
}
Congratulations! If you've made it to the end of this article without encountering any errors or issues, you have successfully installed the Psychz Terraform provider and deployed a server using it. You're now equipped with the knowledge to leverage Terraform and Psychz to efficiently manage and provision your infrastructure. Happy building and automating your infrastructure with ease!