|
| 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 |
0 commit comments