How to use Terraform with Nectar For Beginner

What is Nectar?

Nectar is a cloud platform built on top of OpenStack. Nectar is used to support Australian researchers.

What is Terraform?

Terraform is a tool of Infrastructure-As-Code (IAC). By using it, we write codes in HCL language to provision VMs in automation manner. Other tools of IAC are Ansible, Chef, Puppet, etc. It provides us great maintainability and reduces the security flaws if a same procedure has to be done again and again to build and configure VMs.

Terraform’s file extension is *.tf.  In this tutorial, we will create three files:

  • variables.tf
    • Variables such as account credentials, login url, etc, are all declared here.
  • providers.tf
    • Terraform is built on modules. We need to let it know which module we will use.
  • deploy.tf
    • Configuration of VMs lies here.

Get Started

Install Terraform
brew install terraform
Account Credentials

Once log in Nectar, reset your password in order to access OpenStack in SSH. The credentials you log in Dashboard with is not the one for SSH.

Download the OpenStack RC file which is used to set environment variables on client-side.

Create your keypair

Variables.tf

Now that credentials are obtained, we can start putting them into the file.

variable "openstack_user_name" {
 description = "The username for the Tenant."
 default = "your account name : email"
}

variable "openstack_tenant_id" {
 description = "The id of the Tenant."
 default = "your project id"
}

variable "openstack_tenant_name" {
 description = "The name of the Tenant."
 default = "your project name"
}

variable "openstack_password" {
 description = "The password for the Tenant."
 default = "your password"
}

variable "openstack_auth_url" {
 description = "The endpoint url to connect to OpenStack."
 default = "https://keystone.rc.nectar.org.au:5000/v3/"
}

variable "openstack_keypair" {
 description = "The keypair to be used."
 default = "your-keypair"
}

variable "tenant_network" {
 description = "The network to be used."
 default = "Classic Provider"
}

We are using auth v3, therefore, Tenant name/ID is replaced by Project name/ID.

Providers.tf
provider "openstack" {
 user_name = "${var.openstack_user_name}"
 tenant_id = "${var.openstack_tenant_id}"
 tenant_name = "${var.openstack_tenant_name}"
 password = "${var.openstack_password}"
 auth_url = "${var.openstack_auth_url}"
}
Deploy.tf

We provision 2 instances. In OpenStack, the type of machine is called flavor and its ID can be found in the dashboard, so can the image (Operation System).

variable "count" {
 default = 2
}

resource "openstack_compute_instance_v2" "web" {
 count = "${var.count}"
 name = "${format("web-%02d", count.index+1)}"
 image_id = "d87a2d42-6a90-4d7d-918c-988e9ab13b56"
 availability_zone = "melbourne"
 flavor_id = "639b8b2a-a5a6-4aa2-8592-ca765ee7af63"
 key_pair = "${var.openstack_keypair}"
 security_groups = ["default"]
 network {
 name = "${var.tenant_network}"
 }
 user_data = "${file("bootstrapweb.sh")}"
}
Bootstrapweb.sh
#!/bin/bash

yum install -y httpd
chkconfig --level 345 httpd on

cat <<EOF > /var/www/html/index.html
<html>
<body>
<p>hostname is: $(hostname)</p>
</body>
</html>
EOF
chown -R apache:apache /var/www/html
service httpd start

Now we can tell Terraform to deploy it.
Before actually deploying it, we can preview it.

terraform plan

then apply(deploy) it

terraform apply
Test the instances

The deploy script has a key called user_data. Each instance will invoke this user data where we set up a static web page.

Initialising instances may take some time. Let’s wait…

Once it is done, we navigate to our instance with port 80.

You should able to see “host name is XXXX”

Resources

Wonder what can Terraform do more for OpenStack?

Check Terraform official guide for OpenStack – link

OpenStack is not fully supported by Terraform yet. Some functions may not be working.

Advertisements

IBM Bluemix MobileFirst Services Starter for Cordova Tutorial 教學

Create an account on https://new-console.ng.bluemix.net/
Search the template: MobileFirst Services Starter, and create it.
螢幕快照 2016-06-04 11.22.21

After that, download the source code from https://github.com/ibm-bluemix-mobile-services/bms-samples-cordova-helloworld

Before installing Cordova, you will require node.js which is downloaded from https://nodejs.org/en/

If you do not have Cordova installed, then type

npm install -g cordova

Update Cordova

sudo npm update -g cordova

Go to the directory of source code, add cordova platform

cordova platform add android
cordova platform add ios

Install the IBM Bluemix plugin
cordova plugin add ibm-mfp-core

Go to /project-dir/www/js/index.js, paste your route & GUID
route: https://your-proj-name/mybluemix.net
GUID: can be found in the first line of Cloud Foundry’s log in the panel

route: ""
GUID: ""

Open xcodeproj in /project-dir/platform/ios/
If it prompts to update latest swift version, cancel it.
Go to build settings tab and edit Objective-C Bridging Header
/your-proj/plugins/ibm-mfp-core/src/ios/Bridging-Header.h
Find Runpath search Path, and add
@executable_path/Frameworks

Build Cordova
cordova build
Then run
cordova run android

Run iOS platform on Xcode
yayconnected


Cloud Foundry

Install CF CLI from https://github.com/cloudfoundry/cli/releases

Install Bluemix CLI from http://clis.ng.bluemix.net/ui/home.html

If this error occurs

panic: Config error: open config.json: permission denied

type

sudo chmod -R 777 ~/.cf

If this error occurs

panic: no supported languages found []string{“zh_TW.UTF-8”}

go to ~/.cf/config.json

set “Local” : “zh-Hant”

Download your project source code

螢幕快照 2016-06-04 12.11.58

go to /proj-dir , type

bluemix login -a https://api.ng.bluemix.net

push your project

cf your-project-name

Now you are ready to build your own project.