Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/google cloud #12

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions 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 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
121 changes: 121 additions & 0 deletions examples-infra/gcp/masters-and-workers/.terraform.lock.hcl

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

58 changes: 58 additions & 0 deletions examples-infra/gcp/masters-and-workers/Taskfile.yml
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}}"
23 changes: 23 additions & 0 deletions examples-infra/gcp/masters-and-workers/helm/longhorn-sc.yml
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

21 changes: 21 additions & 0 deletions examples-infra/gcp/masters-and-workers/helm/longhorn.sh
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
Loading