Skip to content

Commit

Permalink
Merge pull request #12 from kloudlite/feat/google-cloud
Browse files Browse the repository at this point in the history
Feat/google cloud
  • Loading branch information
nxtcoder17 authored Mar 7, 2024
2 parents 90cca78 + 8954ac5 commit 6ceca9c
Show file tree
Hide file tree
Showing 46 changed files with 2,449 additions and 41 deletions.
64 changes: 42 additions & 22 deletions infrastructure-as-code/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,55 @@ dotenv:
- .secrets/env

vars:
ImagePrefix: ghcr.io/kloudlite/infrastructure-as-code
Varsfile: ".secrets/varfile.json"

tasks:
local-build:
preconditions:
- sh: '[[ -n "{{.Image}}" ]]'
msg: 'var Image must have a value'
sync-from-template:
vars:
InfrastructureTemplate:
env:
SHELL: bash
silent: true
cmds:
- nerdctl build -t {{.Image}} .
- chmod -f 600 ./*.tf | true
- cp {{.InfrastructureTemplate}}/*.tf ./
- chmod 400 ./*.tf
- echo "sync complete"

container:build-and-push:
preconditions:
- sh: '[[ -n "{{.Image}}" ]]'
msg: 'var Image must have a value'
init:
cmds:
- terraform init
silent: true

plan:
dir: ./
vars:
Push: true
DockerArgs: ""
PlanOutput: ".secrets/plan.out"
cmds:
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
- terraform plan --var-file "{{.Varsfile}}" --out "{{.PlanOutput}}"

apply:
dir: ./
dotenv:
- .secrets/env
vars:
PlanOutput: ".secrets/plan.out"
cmds:
- terraform apply "{{.PlanOutput}}"

validate:
dir: ./
cmds:
- docker build -t {{.Image}} . {{.DockerArgs}}
- |+
if [ "{{.Push}}" == "true" ]; then
docker push {{.Image}}
fi
- terraform validate -var-file={{.Varsfile}}

tf:download:kubeconfig:dev:
destroy:
dir: ./
dotenv:
- .secrets/env
vars:
AuthToken: "{{.TF_TOKEN_app_terraform_io}}"
WorkspaceId: "{{.TF_CLOUD_DEV_WORKSPACE_ID}}"
PlanOutput: ".secrets/plan.destroy.out"
cmds:
- |+
curl --silent -H "Authorization: Bearer {{.AuthToken}}" 'https://app.terraform.io/api/v2/workspaces/{{.WorkspaceId}}/current-state-version?include=outputs' | jq '.included[] | select(.attributes.name == "kubeconfig") | .attributes.value' -r | base64 -d
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
- terraform plan --var-file={{.Varsfile}} --destroy --out "{{.PlanOutput}}"
- terraform apply "{{.PlanOutput}}"
35 changes: 20 additions & 15 deletions infrastructure-as-code/cmd/new-infrastructure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ destination_path=$(realpath "$1")

SCRIPT_DIR=$(realpath $(dirname $0))

templates_dir="$SCRIPT_DIR/../infrastructure-templates"
infra_template=$INFRA_TEMPLATE
if [ -z "$infra_template" ]; then
templates_dir="$SCRIPT_DIR/../infrastructure-templates"
infra_template=$(ls "$templates_dir" | fzf --prompt "Choose An Infrastructure template")
fi

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

infra_template=$(ls "$templates_dir" | fzf --prompt "Choose An Infrastructure template")

mkdir -p "$destination_path"

pushd "$destination_path" >/dev/null 2>&1 || exit
Expand All @@ -26,10 +28,13 @@ dotenv:
vars:
Varsfile: ".secrets/varfile.json"
ApplyPlan: "./secrets/apply.plan"
DestroyPlan: "./secrets/destroy.plan"
tasks:
sync-from-template:
vars:
InfrastructureTemplate: $(realpath $SCRIPT_DIR/../infrastructure-templates/${infra_template} --relative-to=$destination_path)
InfrastructureTemplate: $(realpath "${infra_template}" --relative-to="$destination_path")
env:
SHELL: bash
silent: true
Expand All @@ -46,36 +51,36 @@ tasks:
plan:
dir: ./
vars:
PlanOutput: ".secrets/plan.out"
cmds:
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
- terraform plan --var-file "{{.Varsfile}}" --out "{{.PlanOutput}}"
- terraform plan --var-file "{{.Varsfile}}" --out "{{.ApplyPlan}}"
apply:
dir: ./
dotenv:
- .secrets/env
vars:
PlanOutput: ".secrets/plan.out"
cmds:
- terraform apply "{{.PlanOutput}}"
- terraform apply "{{.ApplyPlan}}"
validate:
dir: ./
cmds:
- terraform validate -var-file={{.Varsfile}}
destroy:
destroy:plan:
dir: ./
dotenv:
- .secrets/env
vars:
PlanOutput: ".secrets/plan.destroy.out"
cmds:
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
- terraform plan --var-file={{.Varsfile}} --destroy --out "{{.PlanOutput}}"
- terraform apply "{{.PlanOutput}}"
- terraform plan --var-file={{.Varsfile}} --destroy --out "{{.DestroyPlan}}"
destroy:apply:
dir: ./
dotenv:
- .secrets/env
cmds:
- terraform apply "{{.DestroyPlan}}"
EOF

popd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
version: 3

dotenv:
- .secrets/env

vars:
Varsfile: ".secrets/varfile.json"

tasks:
sync-from-template:
vars:
InfrastructureTemplate: ../../../infrastructure-templates/gcp/master-and-worker-nodes
env:
SHELL: bash
silent: true
cmds:
- chmod -f 600 ./*.tf | true
- cp {{.InfrastructureTemplate}}/*.tf ./
- chmod 400 ./*.tf
- echo "sync complete"

init:
cmds:
- terraform init
silent: true

plan:
dir: ./
vars:
PlanOutput: ".secrets/plan.out"
cmds:
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
- terraform plan --var-file "{{.Varsfile}}" --out "{{.PlanOutput}}"

apply:
dir: ./
dotenv:
- .secrets/env
vars:
PlanOutput: ".secrets/plan.out"
cmds:
- terraform apply "{{.PlanOutput}}"

validate:
dir: ./
cmds:
- terraform validate -var-file={{.Varsfile}}

destroy:
dir: ./
dotenv:
- .secrets/env
vars:
PlanOutput: ".secrets/plan.destroy.out"
cmds:
- cat ./varfile.template.yml | envsubst | yq > {{.Varsfile}}
- terraform plan --var-file={{.Varsfile}} --destroy --out "{{.PlanOutput}}"
- terraform apply "{{.PlanOutput}}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: sc-ext4
parameters:
fsType: ext4
numberOfReplicas: "2"
provisioner: driver.longhorn.io
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: sc-xfs
parameters:
fsType: xfs
numberOfReplicas: "2"
provisioner: driver.longhorn.io
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /usr/bin/env bash

cmd=$1
shift;

case $cmd in
install)
helm repo add longhorn https://charts.longhorn.io
helm repo update longhorn

kubectl create namespace longhorn-system
helm upgrade --install longhorn longhorn/longhorn --namespace longhorn-system -f ./values.yml
# kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.5.1/deploy/longhorn.yaml
;;
uninstall)
helm uninstall longhorn --namespace longhorn-system
;;
*)
echo "invalid cmd ($cmd)" && exit 1
;;
esac
Loading

0 comments on commit 6ceca9c

Please sign in to comment.