Skip to content

Commit 84bd56a

Browse files
✨ Add options for configuration (#3)
They will be used to alter behaviour, e.g. show current implementation of module operations. Co-authored-by: ChristophShyper <45788587+ChristophShyper@users.noreply.github.com>
1 parent 8bfa0be commit 84bd56a

File tree

3 files changed

+83
-70
lines changed

3 files changed

+83
-70
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ For example for the following directory structure:
7272
│ ├── prod-account
7373
│ │ └── terragrunt.hcl
7474
│ └── aws.hcl
75+
├── .env
7576
└── root.hcl
7677
```
7778

@@ -82,6 +83,15 @@ velez --terragrunt plan aws/dev-account
8283
```
8384

8485

86+
## Configuration
87+
88+
Velez expects following environment variables to be set:
89+
* `VELEZ_USE_S3_BACKEND` - set to `true` if you use AWS S3 backend for storing Terraform state. Acts as set to `true` by default.
90+
* `VELEZ_USE_DYNAMODB_LOCKS` - set to `true` if you use AWS DynamoDB for locking Terraform state. Acts as set to `true` by default.
91+
92+
For your convenience, you can set these variables in a `.env` file in the project directory and use the `dotenv` to load them.
93+
94+
8595
## Features
8696

8797
- Supporting following services/tools and operations on them:

terragrunt_ops.py

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
import os
22
import subprocess
3-
43
import boto3
5-
64
from pick import pick
75

8-
menu_plan = "📋 Plan"
9-
menu_apply = "✅ Apply"
10-
menu_import = "📎 Import"
11-
menu_destroy = "💣 Destroy"
12-
menu_state = "📦 State operations"
13-
menu_module = "📦 Module operations"
14-
menu_taint = "🚫 Taint"
15-
menu_untaint = "♻️ Untaint"
16-
menu_unlock = "🔓 Unlock"
17-
menu_output = "📤 Output"
18-
menu_init = "🚀 Initialize"
19-
menu_validate = "☑️ Validate"
20-
menu_refresh = "🔄 Refresh"
21-
state_list = "📄 List"
22-
state_move = "📦 Move"
23-
state_rm = "🗑️ Remove"
24-
state_show = "🔍 Show"
25-
state_pull = "⤵️ Pull"
26-
state_push = "⤴ Push"
27-
module_move = "↔️ Move module"
28-
module_destroy_backend = "⌫ Destroy backend"
29-
module_destroy = "🗑️ Destroy module"
30-
316

327
def list_folders_to_ignore():
338
return ['.terragrunt-cache', '.terraform-plugin-cache']
349

3510

3611
class TerragruntOperations:
12+
menu_plan = "📋 Plan"
13+
menu_apply = "✅ Apply"
14+
menu_import = "📎 Import"
15+
menu_destroy = "💣 Destroy"
16+
menu_state = "📦 State operations"
17+
menu_module = "📦 Module operations"
18+
menu_taint = "🚫 Taint"
19+
menu_untaint = "♻️ Untaint"
20+
menu_unlock = "🔓 Unlock"
21+
menu_output = "📤 Output"
22+
menu_init = "🚀 Initialize"
23+
menu_validate = "☑️ Validate"
24+
menu_refresh = "🔄 Refresh"
25+
state_list = "📄 List"
26+
state_move = "📦 Move"
27+
state_rm = "🗑️ Remove"
28+
state_show = "🔍 Show"
29+
state_pull = "⤵️ Pull"
30+
state_push = "⤴ Push"
31+
module_move = "↔️ Move module"
32+
module_destroy_backend = "⌫ Destroy backend"
33+
module_destroy = "🗑️ Destroy module"
34+
35+
3736
def __init__(self, velez):
3837
self.velez = velez
3938

@@ -95,19 +94,20 @@ def folder_menu(self, current_dir=None):
9594

9695
def action_menu(self, module):
9796
options = [
98-
menu_plan,
99-
menu_apply,
100-
menu_import,
101-
menu_destroy,
102-
menu_state,
103-
menu_taint,
104-
menu_untaint,
105-
menu_unlock,
106-
menu_output,
107-
menu_init,
108-
menu_validate,
109-
menu_refresh
110-
] + [self.velez.menu_back, self.velez.menu_exit]
97+
self.menu_plan,
98+
self.menu_apply,
99+
self.menu_import,
100+
self.menu_destroy,
101+
self.menu_state,
102+
self.menu_module if self.velez.use_s3_backend and self.velez.use_dynamodb_locks else None,
103+
self.menu_taint,
104+
self.menu_untaint,
105+
self.menu_unlock,
106+
self.menu_output,
107+
self.menu_init,
108+
self.menu_validate,
109+
self.menu_refresh
110+
] + [self.velez.menu_back, self.velez.menu_exit]
111111
title = f"Choose an action. Current Module: {os.path.relpath(module, self.velez.base_dir)}:"
112112
option, index = pick(options, title)
113113

@@ -117,7 +117,7 @@ def action_menu(self, module):
117117
exit()
118118

119119
# Plan
120-
elif option == menu_plan:
120+
elif option == self.menu_plan:
121121
resource = input("Enter the target to plan (e.g., module.resource; will run the whole module if empty): ")
122122
if resource:
123123
self.execute_terragrunt(module, ['plan', '-target', resource])
@@ -127,7 +127,7 @@ def action_menu(self, module):
127127
self.action_menu(module)
128128

129129
# Apply
130-
elif option == menu_apply:
130+
elif option == self.menu_apply:
131131
resource = input("Enter the target to apply (e.g., module.resource; will run the whole module if empty): ")
132132
if resource:
133133
self.execute_terragrunt(module, ['apply', '-target', resource])
@@ -137,15 +137,15 @@ def action_menu(self, module):
137137
self.action_menu(module)
138138

139139
# Import
140-
elif option == menu_import:
140+
elif option == self.menu_import:
141141
resource = input("Enter the address of the resource to import (e.g., aws_instance.example): ")
142142
resource_id = input("Enter the resource ID to import (e.g., i-12345678): ")
143143
if resource and resource_id:
144144
self.execute_terragrunt(module, ['import', resource, resource_id])
145145
self.action_menu(module)
146146

147147
# Destroy
148-
elif option == menu_destroy:
148+
elif option == self.menu_destroy:
149149
resource = input(
150150
"Enter the target to destroy (e.g., module.resource; will run the whole module if empty): ")
151151
if resource:
@@ -156,14 +156,14 @@ def action_menu(self, module):
156156
self.action_menu(module)
157157

158158
# State operations
159-
elif option == menu_state:
159+
elif option == self.menu_state:
160160
state_options = [
161-
state_list,
162-
state_move,
163-
state_rm,
164-
state_show,
165-
state_pull,
166-
state_push,
161+
self.state_list,
162+
self.state_move,
163+
self.state_rm,
164+
self.state_show,
165+
self.state_pull,
166+
self.state_push,
167167
self.velez.menu_back,
168168
self.velez.menu_exit
169169
]
@@ -173,37 +173,37 @@ def action_menu(self, module):
173173
self.action_menu(module)
174174
elif state_option == self.velez.menu_exit:
175175
exit()
176-
elif state_option == state_list:
176+
elif state_option == self.state_list:
177177
resource = input("Enter the address of the resource to list (e.g., module.example): ")
178178
self.execute_terragrunt(module, ['state', 'list', resource])
179179
self.action_menu(module)
180-
elif state_option == state_move:
180+
elif state_option == self.state_move:
181181
source = input("Enter the source address of the resource to move (e.g., aws_instance.example): ")
182182
destination = input(
183183
"Enter the destination address of the resource to move (e.g., aws_instance.example): ")
184184
self.execute_terragrunt(module, ['state', 'mv', source, destination])
185185
self.action_menu(module)
186-
elif state_option == state_rm:
186+
elif state_option == self.state_rm:
187187
resource = input("Enter the address of the resource to remove (e.g., aws_instance.example): ")
188188
self.execute_terragrunt(module, ['state', 'rm', resource])
189189
self.action_menu(module)
190-
elif state_option == state_show:
190+
elif state_option == self.state_show:
191191
resource = input("Enter the address of the resource to show (e.g., aws_instance.example): ")
192192
self.execute_terragrunt(module, ['state', 'show', resource])
193193
self.action_menu(module)
194-
elif state_option == state_pull:
194+
elif state_option == self.state_pull:
195195
self.execute_terragrunt(module, ['state', 'pull'])
196196
self.action_menu(module)
197-
elif state_option == state_push:
197+
elif state_option == self.state_push:
198198
self.execute_terragrunt(module, ['state', 'push'])
199199
self.action_menu(module)
200200

201201
# Module operations
202-
elif option == menu_module:
202+
elif option == self.menu_module:
203203
module_options = [
204-
module_move,
205-
module_destroy,
206-
module_destroy_backend,
204+
self.module_move if self.velez.use_s3_backend and self.velez.use_dynamodb_locks else None,
205+
self.module_destroy if self.velez.use_s3_backend and self.velez.use_dynamodb_locks else None,
206+
self.module_destroy_backend if self.velez.use_s3_backend and self.velez.use_dynamodb_locks else None,
207207
self.velez.menu_back,
208208
self.velez.menu_exit
209209
]
@@ -215,7 +215,7 @@ def action_menu(self, module):
215215
exit()
216216

217217
# Move module
218-
elif module_option == module_move:
218+
elif module_option == self.module_move:
219219
destination = input("Enter the destination path (e.g., aws/prod): ")
220220
dynamodb_name = input("Enter the DynamoDB lock table name (e.g., my_table): ")
221221
dynamodb_lockid = input("Enter the DynamoDB LockID (e.g., my-terragrunt/aws/dev/account/terraform.tfstate-md5): ")
@@ -255,7 +255,7 @@ def action_menu(self, module):
255255
self.action_menu(destination)
256256

257257
# Destroy whole module
258-
elif module_option == module_destroy:
258+
elif module_option == self.module_destroy:
259259
dynamodb_name = input("Enter the DynamoDB lock table name (e.g., my_table): ")
260260
dynamodb_lockid = input("Enter the DynamoDB LockID (e.g., my-terragrunt/aws/dev/account/terraform.tfstate-md5): ")
261261
s3_state = input("Enter the S3 state path (e.g., s3://my-terragrunt/aws/dev/account): ")
@@ -283,7 +283,7 @@ def action_menu(self, module):
283283
self.action_menu(os.path.dirname(module))
284284

285285
# Destroy backend for the module
286-
elif module_option == module_destroy_backend:
286+
elif module_option == self.module_destroy_backend:
287287
dynamodb_name = input("Enter the DynamoDB lock table name (e.g., my_table): ")
288288
dynamodb_lockid = input("Enter the DynamoDB LockID (e.g., my-terragrunt/aws/dev/account/terraform.tfstate-md5): ")
289289
s3_state = input("Enter the S3 state path (e.g., s3://my-terragrunt/aws/dev/account): ")
@@ -306,40 +306,40 @@ def action_menu(self, module):
306306
self.action_menu(module)
307307

308308
# Taint
309-
elif option == menu_taint:
309+
elif option == self.menu_taint:
310310
address = input("Enter the address of the resource to taint (e.g., aws_instance.example): ")
311311
self.execute_terragrunt(module, ['taint', address])
312312
self.action_menu(module)
313313

314314
# Untaint
315-
elif option == menu_untaint:
315+
elif option == self.menu_untaint:
316316
address = input("Enter the address of the resource to untaint (e.g., aws_instance.example): ")
317317
self.execute_terragrunt(module, ['untaint', address])
318318
self.action_menu(module)
319319

320320
# Unlock
321-
elif option == menu_unlock:
321+
elif option == self.menu_unlock:
322322
self.execute_terragrunt(module, ['force-unlock'])
323323
self.action_menu(module)
324324

325325
# Output
326-
elif option == menu_output:
326+
elif option == self.menu_output:
327327
variable = input("Enter the output variable to show (e.g., my_output; will show all if empty): ")
328328
self.execute_terragrunt(module, ['output', variable])
329329
self.action_menu(module)
330330

331331
# Initialize
332-
elif option == menu_init:
332+
elif option == self.menu_init:
333333
self.execute_terragrunt(module, ['init'])
334334
self.action_menu(module)
335335

336336
# Validate
337-
elif option == menu_validate:
337+
elif option == self.menu_validate:
338338
self.execute_terragrunt(module, ['validate'])
339339
self.action_menu(module)
340340

341341
# Refresh
342-
elif option == menu_refresh:
342+
elif option == self.menu_refresh:
343343
self.execute_terragrunt(module, ['refresh'])
344344
self.action_menu(module)
345345

velez.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
from terragrunt_ops import TerragruntOperations
77

88

9+
910
class Velez:
1011
menu_back = "🔙 Back"
1112
menu_exit = "🚪 Exit"
1213
menu_terragrunt = "🌟 Run Terragrunt"
1314

1415
def __init__(self, base_dir=None):
1516
self.base_dir = base_dir if base_dir else os.getcwd()
17+
self.use_s3_backend = os.getenv('VELEZ_USE_S3_BACKEND', 'true') == 'true'
18+
self.use_dynamodb_locks = os.getenv('VELEZ_USE_DYNAMODB_LOCKS', 'true') == 'true'
1619
self.terragrunt_ops = TerragruntOperations(self)
1720

1821
def main_menu(self):
@@ -43,7 +46,7 @@ def run(self, terragrunt=False, **kwargs):
4346

4447

4548
def main():
46-
parser = argparse.ArgumentParser(description='Velez CLI')
49+
parser = argparse.ArgumentParser(description='Velez CLI')
4750
parser.add_argument('--terragrunt', '-tg', action='store_true', help='Run Terragrunt operations')
4851
parser.add_argument('pos_args', nargs=argparse.REMAINDER, help='Arguments to pass further')
4952

0 commit comments

Comments
 (0)