-
Notifications
You must be signed in to change notification settings - Fork 1
147 lines (125 loc) · 5.34 KB
/
vm-creation-terraform.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
---
name: Python
on:
push:
branches:
- main
- feature/*
- review/*
- fix/*
pull_request:
types: [opened, reopened]
jobs:
build-terraform-file:
runs-on: ghr-proxmox-vm-sthings-cicd
environment: k8s
container:
image: eu.gcr.io/stuttgart-things/machineshop:v1.7.2
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
id: pip
run: |
pip install github-action-utils PyYAML Jinja2
- name: Create VM config
id: renderConfig
uses: jannekem/[email protected]
with:
script: |
import yaml as yaml
import random
import string
from jinja2 import Environment, FileSystemLoader
import github_action_utils as gha_utils
def random_string_generation(length):
# choose random lowercase letters for unique name
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
def write_file(testVars, output_file_name):
environment = Environment(loader=FileSystemLoader("tests/templates/"))
template = environment.get_template("module.tpl")
filename = "main.tf"
content = template.render(
name = output_file_name,
vm_count = random.choice(testVars['vm_count']),
vm_num_cpus = random.choice(testVars['vm_num_cpus']),
pve_datastore = random.choice(testVars['pve_datastore']),
pve_network = random.choice(testVars['pve_network']),
vm_disk_size = random.choice(testVars['vm_disk_size']),
vm_memory = random.choice(testVars['vm_memory']),
)
# Save template
with open(filename, mode="w", encoding="utf-8") as message:
message.write(content)
print(f"... wrote {filename}")
def main():
### Generate Random String for VM name
str_tfvarName = "pipeline-" + random_string_generation(length = 5)
gha_utils.append_job_summary("Unique Name for VM's: " + str_tfvarName)
### Import Yaml file with all possible test values
with open('tests/test_values.yaml', 'r') as file:
testVars = yaml.safe_load(file)
print(testVars)
write_file(testVars, str_tfvarName)
if __name__ == '__main__':
main()
- name: Upload main file for job 2
uses: actions/upload-artifact@v4
with:
name: terraform_main
path: main.tf
test-terraform-apply:
needs: build-terraform-file
runs-on: ghr-proxmox-vm-sthings-cicd
environment: k8s
container:
image: hashicorp/terraform:1.6
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
path: "./tests"
- name: Download main
uses: actions/download-artifact@v4
with:
name: terraform_main
- name: Run teraform init, plan and apply
run: |
terraform init
terraform plan -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}"
terraform apply --auto-approve -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}"
- name: Upload tfstate file for cleanup
if: always()
uses: actions/upload-artifact@v4
with:
name: terraform_state
path: terraform.tfstate
- name: Run Terraform Destroy
run: |
terraform destroy --auto-approve -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}"
cleanup:
if: ${{ always() }}
needs: test-terraform-apply
runs-on: ghr-proxmox-vm-sthings-cicd
environment: k8s
container:
image: hashicorp/terraform:1.6
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
path: "./tests"
- name: Download tfstate
uses: actions/download-artifact@v4
with:
name: terraform_state
- name: Download main
uses: actions/download-artifact@v4
with:
name: terraform_main
- name: Run Terraform Destroy
run: |
terraform init
terraform destroy --auto-approve -var="pve_api_url=${{ secrets.PVE_API_URL }}" -var="pve_api_user=${{ secrets.PVE_API_USER }}" -var="pve_api_password=${{ secrets.PVE_API_PASSWORD }}" -var="vm_ssh_user=${{ secrets.VM_SSH_USER }}" -var="vm_ssh_password=${{ secrets.VM_SSH_PASSWORD }}" -var="pve_api_tls_verify=${{ vars.PVE_API_TLS_VERIFY }}"