Skip to content

Commit 3320d44

Browse files
committed
feat(gcp): adds virtual machine setup
1 parent 7456868 commit 3320d44

38 files changed

Lines changed: 879 additions & 109 deletions

cmd/new-infra-template.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#! /usr/bin/env bash
2+
3+
providers_tf="./providers.tf"
4+
state_backend_tf="./state-backend.tf"
5+
taskfile_yml="./Taskfile.yml"
6+
main_tf="./main.tf"
7+
8+
if [ ! -f "$providers_tf" ]; then
9+
cat >${providers_tf} <<EOF
10+
terraform {
11+
required_providers {
12+
google = {
13+
source = "hashicorp/google"
14+
version = "5.19.0"
15+
}
16+
}
17+
}
18+
EOF
19+
fi
20+
21+
if [ ! -f "$state_backend_tf" ]; then
22+
cat >$state_backend_tf <<EOF
23+
terraform {
24+
required_version = ">= 1.2.0"
25+
26+
backend "kubernetes" {
27+
# read more at https://developer.hashicorp.com/terraform/language/settings/backends/kubernetes#configuration-variables
28+
secret_suffix = "state"
29+
30+
# when running on a kubernetes cluster, specify env-vars:
31+
# - KUBE_IN_CLUSTER_CONFIG="true"
32+
# - KUBE_NAMESPACE="some namespace"
33+
34+
# when running on local machine, uncomment the following, and pass appropriate values
35+
# namespace = "default"
36+
# config_path = "~/.kube/configs/kloudlite-dev.yml"
37+
}
38+
}
39+
EOF
40+
fi
41+
42+
bundle=$(fd '' ../../../terraform/bundles -t d | fzf --prompt="Choose Bundle> ")
43+
if [ -z "$bundle" ]; then
44+
echo "bundle not choosen" && exit 1
45+
fi
46+
47+
if [ ! -f "$taskfile_yml" ]; then
48+
cat >$taskfile_yml <<EOTF
49+
version: 3
50+
51+
tasks:
52+
sync-variables:
53+
vars:
54+
bundlePath: "$bundle"
55+
banner: |+
56+
/*
57+
DO NOT EDIT THIS FILE DIRECTLY. IT WILL BE OVERWRITTEN.
58+
If you need to change any variable, please edit the corresponding variables in the {{.bundlePath}} directory.
59+
If you want to create new variables, please create them in other files
60+
*/
61+
cmds:
62+
- |+ #sh
63+
bundlePath="{{.bundlePath}}"
64+
bundleName=\$(basename "\$bundlePath")
65+
if [ -f "./variables-\$bundleName.tf" ]; then
66+
chmod 600 "./variables-\$bundleName.tf"
67+
fi
68+
69+
[ -d "\$bundlePath" ] || (echo "bundle path does not exist" && exit 1)
70+
71+
cat > "./variables-\$bundleName.tf" <<EOF
72+
{{.banner}}
73+
EOF
74+
75+
cat \$bundlePath/variables.tf >> ./variables-\$bundleName.tf
76+
chmod 400 "./variables-\$bundleName.tf"
77+
EOTF
78+
fi
79+
80+
if [ ! -f "$main_tf" ]; then
81+
cat >$main_tf <<EOF
82+
module "$(basename "$bundle")" {
83+
source = "$bundle"
84+
}
85+
EOF
86+
fi

cmd/new-infrastructure.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SCRIPT_DIR=$(realpath $(dirname $0))
77
infra_template=$INFRA_TEMPLATE
88
if [ -z "$infra_template" ]; then
99
templates_dir="$SCRIPT_DIR/../infrastructure-templates"
10-
infra_template=$(ls "$templates_dir" | fzf --prompt "Choose An Infrastructure template")
10+
infra_template=$(fd '' "$templates_dir" | fzf --prompt "Choose An Infrastructure template")
1111
fi
1212

1313
[ -d "$destination_path" ] && echo "Directory $destination_path already exists" && exit 1

examples-infra/gcp/vm/.terraform.lock.hcl

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples-infra/gcp/vm/Taskfile.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
version: 3
2+
3+
dotenv:
4+
- .secrets/env
5+
6+
vars:
7+
Varsfile: ".secrets/varfile.json"
8+
9+
ApplyPlan: ".secrets/apply.plan"
10+
DestroyPlan: ".secrets/destroy.plan"
11+
12+
tasks:
13+
sync-from-template:
14+
vars:
15+
InfrastructureTemplate: ../../../infrastructure-templates/gcp/vm
16+
env:
17+
SHELL: bash
18+
silent: true
19+
cmds:
20+
- chmod -f 600 ./*.tf | true
21+
- cp {{.InfrastructureTemplate}}/*.tf ./
22+
- chmod 400 ./*.tf
23+
- echo "sync complete"
24+
25+
init:
26+
cmds:
27+
- terraform init
28+
silent: true
29+
30+
plan:
31+
dir: ./
32+
cmds:
33+
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
34+
- terraform plan --var-file "{{.Varsfile}}" --out "{{.ApplyPlan}}"
35+
36+
apply:
37+
dir: ./
38+
dotenv:
39+
- .secrets/env
40+
cmds:
41+
- terraform apply "{{.ApplyPlan}}"
42+
43+
validate:
44+
dir: ./
45+
cmds:
46+
- terraform validate -var-file={{.Varsfile}}
47+
48+
destroy:plan:
49+
dir: ./
50+
dotenv:
51+
- .secrets/env
52+
cmds:
53+
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
54+
- terraform plan --var-file={{.Varsfile}} --destroy --out "{{.DestroyPlan}}"
55+
56+
destroy:apply:
57+
dir: ./
58+
dotenv:
59+
- .secrets/env
60+
cmds:
61+
- terraform apply "{{.DestroyPlan}}"

examples-infra/gcp/vm/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module "vm" {
2+
source = "../../../terraform/bundles/gcp/vm"
3+
name_prefix = var.name_prefix
4+
vm_name = var.vm_name
5+
provision_mode = var.provision_mode
6+
availability_zone = var.availability_zone
7+
network = var.network
8+
service_account = var.service_account
9+
machine_type = var.machine_type
10+
bootvolume_type = var.bootvolume_type
11+
bootvolume_size = var.bootvolume_size
12+
labels = var.labels
13+
allow_incoming_http_traffic = var.allow_incoming_http_traffic
14+
allow_ssh = var.allow_ssh
15+
machine_state = var.machine_state
16+
startup_script = var.startup_script
17+
}

examples-infra/gcp/vm/providers.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_providers {
3+
google = {
4+
source = "hashicorp/google"
5+
version = "5.19.0"
6+
}
7+
}
8+
}
9+
10+
provider "google" {
11+
# Configuration options
12+
project = var.gcp_project_id
13+
region = var.gcp_region
14+
credentials = base64decode(var.gcp_credentials_json)
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_version = ">= 1.2.0"
3+
4+
# backend "kubernetes" {
5+
# # read more at https://developer.hashicorp.com/terraform/language/settings/backends/kubernetes#configuration-variables
6+
# secret_suffix = "state"
7+
#
8+
# # when running on a kubernetes cluster, specify env-vars:
9+
# # - KUBE_IN_CLUSTER_CONFIG="true"
10+
# # - KUBE_NAMESPACE="some namespace"
11+
#
12+
# # when running on local machine, uncomment the following, and pass appropriate values
13+
# # namespace = "default"
14+
# # config_path = "~/.kube/configs/kloudlite-dev.yml"
15+
# }
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
gcp_project_id: $GCP_PROJECT_ID
2+
gcp_region: $GCP_REGION
3+
gcp_credentials_json: $GCP_CREDENTIALS_JSON
4+
5+
name_prefix: "test"
6+
vm_name: "example-vm"
7+
provision_mode: "SPOT"
8+
availability_zone: "asia-south1-a"
9+
network: default
10+
service_account:
11+
enabled: false
12+
machine_type: "e2-custom-medium-2048"
13+
bootvolume_type: pd-ssd
14+
bootvolume_size: 10
15+
labels:
16+
built-for: example-testing
17+
allow_incoming_http_traffic: false
18+
allow_ssh: true
19+
machine_state: on
20+
startup_script: |+
21+
sudo mkdir -p /var/nxtcoder17
22+
echo "hi from nxtcoder17" > /var/nxtcoder17/README
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
DO NOT EDIT THIS FILE DIRECTLY. IT WILL BE OVERWRITTEN.
3+
If you need to change any variable, please edit the corresponding variables in the ../../../terraform/bundles/gcp/vm/ directory.
4+
If you want to create new variables, please create them in other files
5+
*/
6+
7+
variable "name_prefix" {
8+
type = string
9+
description = "name prefixes to use for resources"
10+
}
11+
12+
variable "vm_name" {
13+
type = string
14+
description = "name prefixes to use for k8s nodes"
15+
}
16+
17+
variable "provision_mode" {
18+
type = string
19+
}
20+
21+
variable "availability_zone" {
22+
description = "AZ"
23+
type = string
24+
}
25+
26+
variable "network" {
27+
type = string
28+
description = "GCP network to use"
29+
}
30+
31+
variable "service_account" {
32+
type = object({
33+
enabled = bool
34+
email = optional(string)
35+
scopes = optional(list(string))
36+
})
37+
}
38+
39+
variable "machine_type" {
40+
description = "machine_type"
41+
type = string
42+
}
43+
44+
variable "bootvolume_type" {
45+
type = string
46+
description = "bootvolume type"
47+
}
48+
49+
variable "bootvolume_size" {
50+
type = number
51+
description = "bootvolume size"
52+
}
53+
54+
variable "labels" {
55+
type = map(string)
56+
description = "map of Key => Value to be tagged along created resources"
57+
default = {}
58+
}
59+
60+
variable "allow_incoming_http_traffic" {
61+
type = bool
62+
description = "allow incoming http traffic"
63+
}
64+
65+
variable "allow_ssh" {
66+
type = bool
67+
description = "allow ssh traffic"
68+
}
69+
70+
variable "machine_state" {
71+
type = string
72+
description = "machine state either on or off"
73+
}
74+
75+
variable "startup_script" {
76+
type = string
77+
description = "startup script"
78+
}

examples-infra/gcp/vm/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "gcp_project_id" {
2+
type = string
3+
description = "GCP Project ID"
4+
}
5+
6+
variable "gcp_region" {
7+
type = string
8+
description = "GCP Region"
9+
}
10+
11+
variable "gcp_credentials_json" {
12+
type = string
13+
description = "Credentials JSON"
14+
}

0 commit comments

Comments
 (0)