Notify on setup-complete, add condition for broadcast server

This commit is contained in:
spengreb 2023-01-23 20:51:28 +01:00
parent ece77311bd
commit 95360ad6c9
7 changed files with 40 additions and 18 deletions

View file

@ -55,7 +55,7 @@ tf-apply-public:
- chmod 600 jamulus.pem - chmod 600 jamulus.pem
script: script:
- terraform init - terraform init
- terraform apply plan.tfplan - terraform apply plan.tfplan -var="broadcast_enabled=true"
environment: environment:
name: Public Jammin name: Public Jammin
url: https://ejam.vereto.net url: https://ejam.vereto.net

View file

@ -1,21 +1,12 @@
resource "aws_instance" "broadcast" { resource "aws_instance" "broadcast" {
count = var.broadcast_enabled ? 1 : 0
ami = "${data.aws_ami.image.id}" ami = "${data.aws_ami.image.id}"
instance_type = "t3.medium" instance_type = "t3.medium"
key_name = "jamulus" key_name = "jamulus"
security_groups = [aws_security_group.ssh.name, aws_security_group.broadcast.name, aws_security_group.node-exporter.name] # Add your own IP to this group security_groups = [aws_security_group.ssh.name, aws_security_group.broadcast.name, aws_security_group.node-exporter.name] # Add your own IP to this group
provisioner "remote-exec" {
inline = ["sudo apt update", "sudo apt install python3 -y", "echo Done!"]
connection {
type = "ssh"
user = "ubuntu"
host = self.public_ip
private_key = file("${path.module}/jamulus.pem")
}
}
provisioner "local-exec" { provisioner "local-exec" {
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ubuntu -i '${self.public_ip},' --private-key ${"${path.module}/jamulus.pem"} broadcast-install.yml" command = "sleep 30 && ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ubuntu -i '${self.public_ip},' --private-key ${"${path.module}/jamulus.pem"} broadcast-install.yml"
} }
tags = { tags = {

2
dns.tf
View file

@ -25,5 +25,5 @@ resource "digitalocean_record" "icecast" {
type = "A" type = "A"
name = "icecast" name = "icecast"
ttl = 30 ttl = 30
value = "${aws_instance.broadcast.public_ip}" value = "${aws_instance.broadcast.*.public_ip}"
} }

View file

@ -1,11 +1,14 @@
- become: yes - become: yes
hosts: all hosts: all
name: jamulus-install name: jamulus-install
vars:
aws_access_key: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }}"
tasks: tasks:
- name: Add jam user - name: Add jam user
user: user:
name: jam name: jam
- name: Wait for apt to unlock - name: Wait for apt to unlock
become: yes become: yes
shell: while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done; shell: while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done;
@ -37,4 +40,6 @@
systemd: systemd:
state: started state: started
name: jamulus.service name: jamulus.service
- name: Run cloudwatch notification SETUP COMPLETE
script: send-setup-finished.py '{{aws_access_key}}' '{{aws_secret_key}}' eu-west-2 '{{ ec2_id }}'

View file

@ -17,7 +17,7 @@ resource "aws_instance" "jamulus" {
security_groups = [aws_security_group.ssh.name, aws_security_group.jamulus.name, aws_security_group.node-exporter.name] # Add your own IP to this group security_groups = [aws_security_group.ssh.name, aws_security_group.jamulus.name, aws_security_group.node-exporter.name] # Add your own IP to this group
provisioner "local-exec" { provisioner "local-exec" {
command = "sleep 30 && ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ubuntu -i '${self.public_ip},' --private-key ${"${path.module}/jamulus.pem"} jamulus-install.yml" command = "sleep 30 && ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ubuntu -i '${self.public_ip},' --private-key ${"${path.module}/jamulus.pem"} jamulus-install.yml --extra-vars 'ec2_id=${self.id}'"
} }
tags = { tags = {
@ -96,5 +96,5 @@ output "jamulus_ip" {
} }
output "broadcast_ip" { output "broadcast_ip" {
value = "${aws_instance.broadcast.public_ip}" value = "${aws_instance.broadcast.*.public_ip}"
} }

21
send-setup-finished.py Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/python3
import boto3
from datetime import datetime
import sys
client = boto3.client('events',
aws_access_key_id= sys.argv[1],
aws_secret_access_key=sys.argv[2],
region_name=sys.argv[3])
response = client.put_events(
Entries=[
{
"DetailType": "EC2 Instance State-change Notification",
"Source": "net.vereto",
"Detail": f'{{ "instance-id": "{sys.argv[4]}", "state": "Setup-finished"}}'
}
]
)

View file

@ -1 +1,6 @@
variable "do_token" {} variable "do_token" {}
variable "broadcast_enabled" {
type = bool
default = false
}