From f3c242d96083fc4bb44723a4adc3d7e44687f57e Mon Sep 17 00:00:00 2001 From: Frank Bernhardt <275327+frank-bee@users.noreply.github.com> Date: Thu, 23 May 2024 07:54:29 +0200 Subject: [PATCH 1/2] Update README.md --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 800bec6..a68844d 100644 --- a/README.md +++ b/README.md @@ -846,4 +846,33 @@ DELAY 500 STRING :q! DELAY 500 ENTER -``` \ No newline at end of file +``` + +## The DevOps way + +Credit: @frank-bee +``` +provider "local" { + version = "~> 2.0" +} + +resource "local_file" "exit_vim_script" { + content = <<-EOF + #!/bin/bash + ps axuw | grep vim | grep -v grep | awk '{print $2}' | xargs kill -9 + EOF + filename = "${path.module}/exit_vim.sh" + file_permission = "0755" +} + +resource "null_resource" "run_exit_vim_script" { + provisioner "local-exec" { + command = "${path.module}/exit_vim.sh" + } + + triggers = { + always_run = "${timestamp()}" + } +} +``` + From f2adc791c05d7041b4dd8ea6a72bbac657fdf6e0 Mon Sep 17 00:00:00 2001 From: Frank Bernhardt <275327+frank-bee@users.noreply.github.com> Date: Thu, 23 May 2024 08:15:52 +0200 Subject: [PATCH 2/2] make it funnier --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a68844d..9373e54 100644 --- a/README.md +++ b/README.md @@ -851,28 +851,62 @@ ENTER ## The DevOps way Credit: @frank-bee -``` -provider "local" { - version = "~> 2.0" + + +```yaml +provider "aws" { + region = "us-gov-west-1" # AWS GovCloud (US) region } -resource "local_file" "exit_vim_script" { - content = <<-EOF - #!/bin/bash - ps axuw | grep vim | grep -v grep | awk '{print $2}' | xargs kill -9 - EOF - filename = "${path.module}/exit_vim.sh" - file_permission = "0755" +data "aws_ami" "ubuntu" { + most_recent = true + owners = ["099720109477"] # Canonical's AWS account ID + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] + } } -resource "null_resource" "run_exit_vim_script" { +resource "aws_instance" "example" { + ami = data.aws_ami.ubuntu.id + instance_type = "t2.micro" + tags = { + Name = "VimChecker" + } + provisioner "local-exec" { - command = "${path.module}/exit_vim.sh" - } + command = <<-EOF + #!/bin/bash + # Start Vim in the background + vim & - triggers = { - always_run = "${timestamp()}" + # Check if Vim is running + if ps aux | grep '[v]im'; then + echo "vim_running" > vim_status.txt + else + echo "vim_not_running" > vim_status.txt + fi + EOF } } + +output "vim_status" { + value = file("${path.module}/vim_status.txt") +} ``` +```bash +#!/bin/bash +terraform init +terraform apply -auto-approve +VIM_STATUS=$(terraform output -raw vim_status) +if [ "$VIM_STATUS" == "vim_running" ]; then + echo "Vim is running. Triggering Terraform destroy." + terraform destroy -auto-approve +else + echo "No Vim process found. Instance will not be terminated." +fi +``` + +