forked from splunk/splunk-platform-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskfile.yml
278 lines (242 loc) · 8.53 KB
/
Taskfile.yml
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
version: '3'
vars:
TERRAFORM_DIR: terraform
ANSIBLE_DIR: ansible
CONFIG_DIR: config
EXAMPLES_DIR: examples
DEFAULT_ENV: dev
DEFAULT_PROVIDER: orbstack
INVENTORY_OUTPUT: "{{.CONFIG_DIR}}/inventory_output.yml"
tasks:
check:venv:
internal: true
cmds:
- test -f ./.venv/bin/pip
preconditions:
- msg: "Virtual environment not found. Please run 'task setup-venv' and activate it with '. ./.venv/bin/activate'"
sh: "test -f ./.venv/bin/pip"
default:
desc: "Show help information"
silent: true
cmds:
- echo "Splunk Platform Automator\n"
- echo "First time setup:"
- echo "1. Run 'task setup-venv' to create Python virtual environment"
- echo "2. Run '. ./.venv/bin/activate' to activate the environment"
- echo "3. Run 'task setup:deps' to install dependencies\n"
- echo "Available tasks:"
- task --list
# Setup and Environment Tasks
setup-venv:
desc: "Setup python virtual environment"
preconditions:
- msg: "Must not be run as root"
sh: "[ $(id -u) -ne 0 ]"
cmds:
- rm -rf ./.venv
- python3 -m venv ./.venv
- |
echo "Virtual environment created successfully!"
echo "Next step: Run '. ./.venv/bin/activate' to activate it"
setup:deps:
desc: "Install project dependencies"
silent: true
deps: [check:venv]
cmds:
- pip install -r requirements.txt
# Terraform-specific Tasks
tf:init:
desc: "Initialize Terraform working directory"
silent: true
dir: '{{.TERRAFORM_DIR}}/environments/{{.CLI_ARGS | default .DEFAULT_ENV}}'
deps: [check:venv, ansible:generate:inventory]
cmds:
- terraform init
tf:validate:
desc: "Validate Terraform configurations"
silent: true
dir: '{{.TERRAFORM_DIR}}/environments/{{.CLI_ARGS | default .DEFAULT_ENV}}'
deps: [check:venv, ansible:generate:inventory]
cmds:
- terraform validate
tf:plan:
desc: "Plan Terraform infrastructure changes"
silent: true
dir: '{{.TERRAFORM_DIR}}/environments/{{.CLI_ARGS | default .DEFAULT_ENV}}'
deps: [check:venv, ansible:generate:inventory]
cmds:
- terraform plan -out=tfplan
tf:apply:
desc: "Apply Terraform infrastructure changes"
silent: true
dir: '{{.TERRAFORM_DIR}}/environments/{{.CLI_ARGS | default .DEFAULT_ENV}}'
deps: [check:venv, ansible:generate:inventory]
cmds:
- terraform apply tfplan
- mkdir -p ../../../inventory
- terraform output -json hosts | jq -r '.ansible_inventory' > ../../../inventory/hosts
tf:destroy:
desc: "Destroy Terraform infrastructure (requires confirmation)"
silent: true
dir: '{{.TERRAFORM_DIR}}/environments/{{.CLI_ARGS | default .DEFAULT_ENV}}'
deps: [check:venv, ansible:generate:inventory]
cmds:
- terraform destroy
tf:destroy-auto:
desc: "Destroy Terraform infrastructure automatically without confirmation"
silent: true
dir: '{{.TERRAFORM_DIR}}/environments/{{.CLI_ARGS | default .DEFAULT_ENV}}'
deps: [check:venv, ansible:generate:inventory]
cmds:
- terraform destroy -auto-approve
# Ansible-specific Tasks
ansible:validate:config:
desc: "Validate Ansible inventory configuration"
silent: true
deps: [check:venv]
cmds:
- ansible-inventory --list --yaml -i {{.CLI_ARGS | default "config/splunk_config.yml"}}
ansible:generate:inventory:
desc: "Generate Ansible inventory from configuration"
silent: true
deps: [check:venv]
cmds:
- ansible-inventory --list --yaml -i {{.CLI_ARGS | default "config/splunk_config.yml"}} > {{.INVENTORY_OUTPUT}}
ansible:deploy:
desc: "Deploy Splunk configuration using Ansible"
deps: [check:venv]
silent: true
cmds:
- ansible-playbook {{.ANSIBLE_DIR}}/deploy_site.yml
# Example Tasks
example:list:
desc: "List available example configurations"
cmds:
- |
echo "Available example configurations:"
ls -1 examples/*_orbstack.yml | sed 's|examples/||' | sed 's|.yml$||'
example:use:
desc: "Use an example configuration (e.g., task example:use -- idx_sh_uf_orbstack)"
cmds:
- |
example_name="{{.CLI_ARGS}}"
if [ -z "$example_name" ]; then
echo "Error: Please specify an example name (without .yml extension)"
echo "Available examples:"
task example:list
exit 1
fi
# Backup existing config if it exists
if [ -f config/splunk_config.yml ]; then
backup_file="config/splunk_config.yml.backup.$(date +%Y%m%d_%H%M%S)"
echo "Backing up existing config to $backup_file"
cp config/splunk_config.yml "$backup_file"
fi
# Create config directory if it doesn't exist
mkdir -p config
# Copy example to config
example_file="examples/${example_name}.yml"
if [ ! -f "$example_file" ]; then
echo "Error: Example $example_file not found"
echo "Available examples:"
task example:list
exit 1
fi
echo "Using example configuration: ${example_name}"
cp "$example_file" config/splunk_config.yml
echo "Configuration copied to config/splunk_config.yml"
echo "You can now run: task tf:init && task tf:apply"
example:restore:
desc: "Restore the most recent backup of splunk_config.yml"
cmds:
- |
latest_backup=$(ls -t config/splunk_config.yml.backup.* 2>/dev/null | head -1)
if [ -z "$latest_backup" ]; then
echo "No backup files found in config directory"
exit 1
fi
echo "Restoring from backup: $latest_backup"
cp "$latest_backup" config/splunk_config.yml
echo "Configuration restored to config/splunk_config.yml"
# OrbStack-specific Tasks
orb:inventory:
desc: "Generate inventory from OrbStack VMs"
silent: true
cmds:
- |
# Get list of machines and their IPs
for vm in $(orb list --format json | jq -r '.[].name'); do
ip=$(orb run -m $vm hostname -I | cut -d' ' -f1 | tr -d '\n')
if [ -n "$ip" ]; then
echo "$vm ip_addr=$ip public_dns_name=$vm.orb.local"
fi
done
test:manual-provisioning:
desc: "Test manual provisioning workflow"
silent: true
cmds:
- |
echo "=== Starting manual provisioning test ==="
# Clean up any previous test VMs
echo "Cleaning up previous state..."
orbctl delete idx1 sh1 uf1 2>/dev/null || true
# Copy example config
echo "Setting up configuration..."
cp examples/idx_sh_uf_orbstack.yml config/splunk_config.yml
# Create test VMs
echo "Creating test VMs..."
orb create --arch amd64 alma:9 idx1
orb create --arch amd64 alma:9 sh1
orb create --arch amd64 alma:9 uf1
# Wait for VMs to be ready
sleep 10
# Generate and set inventory
echo "Setting up inventory..."
task orb:inventory > inventory/hosts
# Deploy Splunk
echo "Deploying Splunk..."
task ansible:deploy
# Test connectivity and basic Splunk functionality
echo "Testing connectivity..."
orb run -m idx1 ping -c 1 sh1
orb run -m sh1 ping -c 1 idx1
echo "Testing Splunk services..."
orb run -m idx1 systemctl status splunk
orb run -m sh1 systemctl status splunk
orb run -m uf1 systemctl status splunkforwarder
# Cleanup
echo "Cleaning up..."
echo "orbctl delete idx1 sh1 uf1"
echo "rm -f inventory/hosts"
echo "=== Test completed successfully ==="
orb:run:
desc: "Run a command on an OrbStack machine"
silent: true
cmds:
- orb -m {{.CLI_ARGS}}
# End-to-end Tasks
deploy:
desc: "Deploy complete infrastructure and configure Splunk"
deps: [setup:deps, check:venv]
vars:
ENV: '{{.CLI_ARGS | default .DEFAULT_ENV}}'
cmds:
- task: ansible:validate:config
- task: ansible:generate:inventory
- task: tf:init
vars: { CLI_ARGS: '{{.ENV}}' }
- task: tf:plan
vars: { CLI_ARGS: '{{.ENV}}' }
- task: tf:apply
vars: { CLI_ARGS: '{{.ENV}}' }
- task: ansible:deploy
destroy:all:
desc: "Destroy all infrastructure and clean up"
silent: true
deps: [check:venv]
vars:
ENV: '{{.CLI_ARGS | default .DEFAULT_ENV}}'
cmds:
- task: tf:destroy
vars: { CLI_ARGS: '{{.ENV}}' }
- rm -f {{.INVENTORY_OUTPUT}}