Kaynağa Gözat

Some refactoring before I start adding further domains.

master
Mitch Tishmack 6 yıl önce
ebeveyn
işleme
3f0eae1ea6
6 değiştirilmiş dosya ile 247 ekleme ve 39 silme
  1. +11
    -6
      GNUmakefile
  2. +147
    -24
      functionalidiot.com.tf
  3. +22
    -4
      https-functionalidiot.com.conf
  4. +9
    -1
      main.tf
  5. +49
    -3
      post_setup.tf
  6. +9
    -1
      setup_gitea.sh

+ 11
- 6
GNUmakefile Dosyayı Görüntüle

@@ -26,19 +26,24 @@ attic:
install -dm755 $@ install -dm755 $@


.PHONY: backup .PHONY: backup
backup: backup-history backup-ssl backup-gitea
backup: backup-history backup-ssl backup-gitea backup-syncthing


.PHONY: backup-history .PHONY: backup-history
backup-history: attic backup-history: attic
-$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'cat /root/.ash_history' > attic/.ash_history.$(RUN_EPOCH) -$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'cat /root/.ash_history' > attic/.ash_history.$(RUN_EPOCH)
[ -z attic/.ash_history.$(RUN_EPOCH) ] && rm attic/.ash_history.$(RUN_EPOCH) || ln -sf attic/.ash_history.$(RUN_EPOCH) attic/.ash_history
[ -z attic/.ash_history.$(RUN_EPOCH) ] && rm attic/.ash_history.$(RUN_EPOCH) || ln -f attic/.ash_history.$(RUN_EPOCH) attic/.ash_history


.PHONY: backup-gitea .PHONY: backup-gitea
backup-gitea: attic backup-gitea: attic
$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'tar cvf - /var/lib/gitea /etc/gitea' > /attic/var-lib-gitea.tar.$(RUN_EPOCH)
ln -sf attic/var-lib-gitea.tar.$(RUN_EPOCH) attic/var-lib-gitea.tar
$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'tar --exclude /var/lib/gitea/unix-domain-socket -cvJf - /var/lib/gitea /etc/gitea' > attic/gitea.txz.$(RUN_EPOCH)
ln -f attic/gitea.txz.$(RUN_EPOCH) attic/gitea.txz


.PHONY: backup-ssl .PHONY: backup-ssl
backup-ssl: attic backup-ssl: attic
$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'tar cvf - /etc/ssl/uacme' > attic/etc-ssl-uacme.tar.$(RUN_EPOCH)
ln -sf attic/etc-ssl-uacme.tar.$(RUN_EPOCH) attic/etc-ssl-uacme.tar
$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'tar cvJf - /etc/ssl/uacme' > attic/uacme.txz.$(RUN_EPOCH)
ln -f attic/uacme.txz.$(RUN_EPOCH) attic/uacme.txz

.PHONY: backup-syncthing
backup-syncthing: attic
$(SSH) $(SSH_OPTS) root@`$(TF) output ipv4` 'tar cvJf - /var/lib/syncthing' > attic/syncthing.txz.$(RUN_EPOCH)
ln -f attic/syncthing.txz.$(RUN_EPOCH) attic/syncthing.txz

+ 147
- 24
functionalidiot.com.tf Dosyayı Görüntüle

@@ -8,6 +8,16 @@ variable "cloudflare_functionalidiot_zoneid" {
type = string type = string
} }


variable "short_dns_ttl" {
type = bool
default = true
description = "Whether to set short dns ttl's or not."
}

locals {
dns_ttl = var.short_dns_ttl ? 120 : 43200
}

resource "cloudflare_record" "root" { resource "cloudflare_record" "root" {
depends_on = [ depends_on = [
linode_instance.prod, linode_instance.prod,
@@ -17,7 +27,7 @@ resource "cloudflare_record" "root" {
name = "@" name = "@"
value = linode_instance.prod.ip_address value = linode_instance.prod.ip_address
type = "A" type = "A"
ttl = 120
ttl = local.dns_ttl
} }


resource "cloudflare_record" "splat" { resource "cloudflare_record" "splat" {
@@ -29,7 +39,7 @@ resource "cloudflare_record" "splat" {
name = "*" name = "*"
value = linode_instance.prod.ip_address value = linode_instance.prod.ip_address
type = "A" type = "A"
ttl = 1
ttl = local.dns_ttl
} }


# resource "cloudflare_record" "root6" { # resource "cloudflare_record" "root6" {
@@ -41,7 +51,7 @@ resource "cloudflare_record" "splat" {
# name = "@" # name = "@"
# value = local.ipv6 # value = local.ipv6
# type = "AAAA" # type = "AAAA"
# ttl = 120
# ttl = local.dns_ttl
# } # }


# resource "cloudflare_record" "splat6" { # resource "cloudflare_record" "splat6" {
@@ -53,7 +63,7 @@ resource "cloudflare_record" "splat" {
# name = "*" # name = "*"
# value = local.ipv6 # value = local.ipv6
# type = "AAAA" # type = "AAAA"
# ttl = 1
# ttl = local.dns_ttl
# proxied = true # proxied = true
# } # }


@@ -65,7 +75,7 @@ resource "cloudflare_record" "www" {
name = "www" name = "www"
value = "functionalidiot.com" value = "functionalidiot.com"
type = "CNAME" type = "CNAME"
ttl = 120
ttl = local.dns_ttl
} }


resource "cloudflare_record" "git" { resource "cloudflare_record" "git" {
@@ -76,7 +86,111 @@ resource "cloudflare_record" "git" {
name = "git" name = "git"
value = "functionalidiot.com" value = "functionalidiot.com"
type = "CNAME" type = "CNAME"
ttl = 120
ttl = local.dns_ttl
}

# MX record setup for fastmail dkim/spf on root and all subdomains
resource "cloudflare_record" "mx-cname-1" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "fm1._domainkey"
value = "fm1.functionalidiot.com.dkim.fmhosted.com"
type = "CNAME"
ttl = local.dns_ttl
}

resource "cloudflare_record" "mx-cname-2" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "fm2._domainkey"
value = "fm2.functionalidiot.com.dkim.fmhosted.com"
type = "CNAME"
ttl = local.dns_ttl
}

resource "cloudflare_record" "mx-cname-3" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "fm3._domainkey"
value = "fm3.functionalidiot.com.dkim.fmhosted.com"
type = "CNAME"
ttl = local.dns_ttl
}

resource "cloudflare_record" "mx-10-root" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "@"
value = "in1-smtp.messagingengine.com"
type = "MX"
priority = "10"
ttl = local.dns_ttl
}

resource "cloudflare_record" "mx-20-root" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "@"
value = "in2-smtp.messagingengine.com"
type = "MX"
priority = "20"
ttl = local.dns_ttl
}

resource "cloudflare_record" "mx-10-splat" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "*"
value = "in1-smtp.messagingengine.com"
type = "MX"
priority = "10"
ttl = local.dns_ttl
}

resource "cloudflare_record" "mx-20-splat" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "*"
value = "in2-smtp.messagingengine.com"
type = "MX"
priority = "20"
ttl = local.dns_ttl
}

resource "cloudflare_record" "txt-mx-root" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "@"
value = "v=spf1 include:spf.messagingengine.com ?all"
type = "TXT"
ttl = local.dns_ttl
}

resource "cloudflare_record" "txt-mx-splat" {
depends_on = [
cloudflare_record.root,
cloudflare_record.splat ]
zone_id = var.cloudflare_functionalidiot_zoneid
name = "*"
value = "v=spf1 include:spf.messagingengine.com ?all"
type = "TXT"
ttl = local.dns_ttl
} }


resource "cloudflare_record" "functionalidiot_com_ns1" { resource "cloudflare_record" "functionalidiot_com_ns1" {
@@ -94,8 +208,8 @@ resource "cloudflare_record" "functionalidiot_com_ns2" {
} }


resource "null_resource" "functionalidiot_com_setup_new_gitea" { resource "null_resource" "functionalidiot_com_setup_new_gitea" {
depends_on = [ null_resource.post_setup ]
count = fileexists("${path.module}/attic/var-lib-gitea.tar") ? 0 : 1
depends_on = [ null_resource.gitea_install ]
count = fileexists("${path.module}/attic/gitea.txz") ? 0 : 1
connection { connection {
host = linode_instance.prod.ip_address host = linode_instance.prod.ip_address
user = "root" user = "root"
@@ -126,62 +240,71 @@ chown gitea:www-data /var/lib/gitea/db/gitea.db


su - gitea -c "gitea migrate --config /etc/gitea/app.ini" su - gitea -c "gitea migrate --config /etc/gitea/app.ini"


su - gitea -c "gitea admin create-user --username mitch --password ${var.gitea_db_passwd} --email spam@mitchty.com --admin --config /etc/gitea/app.ini"
su - gitea -c "gitea admin create-user --username mitchty --password ${var.gitea_db_passwd} --email git@functionalidiot.com --admin --config /etc/gitea/app.ini"
FIN FIN
] ]
} }
} }


resource "null_resource" "functionalidiot_com_setup_saved_uacme" {
depends_on = [ null_resource.post_setup ]
count = fileexists("${path.module}/attic/etc-ssl-uacme.tar") ? 1 : 0
resource "null_resource" "functionalidiot_com_setup_saved_gitea" {
depends_on = [ null_resource.gitea_install ]
count = fileexists("${path.module}/attic/gitea.txz") ? 1 : 0
connection { connection {
host = linode_instance.prod.ip_address host = linode_instance.prod.ip_address
user = "root" user = "root"
private_key = file("~/.ssh/id_rsa") private_key = file("~/.ssh/id_rsa")
} }
provisioner "file" { provisioner "file" {
source = "etc-ssl-uacme.tar"
destination = "/tmp/etc-ssl-uacme.tar"
source = "${path.module}/attic/gitea.txz"
destination = "/tmp/gitea.txz"
} }
# TODO: Finish post untar setup # TODO: Finish post untar setup
provisioner "remote-exec" { provisioner "remote-exec" {
inline = [<<FIN inline = [<<FIN
cd / cd /
tar xvf /tmp/etc-ssl-uacme.tar
tar xvJf /tmp/gitea.txz
rc-update add gitea default
rc-service gitea restart
FIN FIN
] ]
} }
} }


resource "null_resource" "functionalidiot_com_setup_saved_gitea" {
depends_on = [ null_resource.post_setup ]
count = fileexists("${path.module}/attic/var-lib-gitea.tar") ? 1 : 0
resource "null_resource" "functionalidiot_com_setup_saved_uacme" {
depends_on = [ null_resource.uacme_install ]
count = fileexists("${path.module}/attic/uacme.txz") ? 1 : 0
connection { connection {
host = linode_instance.prod.ip_address host = linode_instance.prod.ip_address
user = "root" user = "root"
private_key = file("~/.ssh/id_rsa") private_key = file("~/.ssh/id_rsa")
} }
provisioner "file" { provisioner "file" {
source = "var-lib-gitea.tar"
destination = "/tmp/var-lib-gitea.tar"
source = "${path.module}/attic/uacme.txz"
destination = "/tmp/uacme.txz"
} }
# TODO: Finish post untar setup # TODO: Finish post untar setup
provisioner "remote-exec" { provisioner "remote-exec" {
inline = [<<FIN inline = [<<FIN
cd / cd /
tar xvf /tmp/var-lib-gitea.tar
rc-update add gitea default
rc-service gitea restart
tar xvJf /tmp/uacme.txz
FIN FIN
] ]
} }
} }


# FIXME: This nginx setup isn't idempotent if its a new setup vs saved
# uacme ssl setup.
#
# Maybe use the dns uacme crap instead of this stupid http bs?
#
# https://www.terraform.io/docs/providers/acme/r/certificate.html
# https://www.terraform.io/docs/providers/acme/dns_providers/cloudflare.html
resource "null_resource" "functionalidiot_com_setup_new_nginx" { resource "null_resource" "functionalidiot_com_setup_new_nginx" {
depends_on = [ depends_on = [
null_resource.post_setup,
null_resource.nginx_install,
null_resource.functionalidiot_com_setup_new_gitea,
null_resource.functionalidiot_com_setup_saved_gitea, null_resource.functionalidiot_com_setup_saved_gitea,
null_resource.functionalidiot_com_setup_saved_uacme,
null_resource.functionalidiot_com_setup_saved_uacme null_resource.functionalidiot_com_setup_saved_uacme
] ]
connection { connection {


+ 22
- 4
https-functionalidiot.com.conf Dosyayı Görüntüle

@@ -1,11 +1,29 @@
server { server {
listen 443 ssl default_server; listen 443 ssl default_server;
listen [::]:443 ssl default_server; listen [::]:443 ssl default_server;
# TODO: fixme
# server_name functionalidiot.com;
server_name _;
# server_name_in_redirect off;
server_name functionalidiot.com;
root /var/www/default/htdocs; root /var/www/default/htdocs;
index index.htm index.html;

ssl_certificate /etc/ssl/uacme/functionalidiot.com/cert.pem;
ssl_certificate_key /etc/ssl/uacme/private/functionalidiot.com/key.pem;
ssl_stapling on;
ssl_protocols TLSv1.2;

location / {
try_files $uri $uri/ =404;
}

location ^~ /.well-known/acme-challenge {
alias /var/www/.well-known/acme-challenge;
}
}

server {
listen 443 ssl;
listen [::]:443 ssl;

server_name git.functionalidiot.com;


ssl_certificate /etc/ssl/uacme/functionalidiot.com/cert.pem; ssl_certificate /etc/ssl/uacme/functionalidiot.com/cert.pem;
ssl_certificate_key /etc/ssl/uacme/private/functionalidiot.com/key.pem; ssl_certificate_key /etc/ssl/uacme/private/functionalidiot.com/key.pem;


+ 9
- 1
main.tf Dosyayı Görüntüle

@@ -18,5 +18,13 @@ resource "linode_instance" "prod" {
} }


output "ipv4" { output "ipv4" {
value = "${linode_instance.prod.ip_address}"
value = linode_instance.prod.ip_address
}

locals {
ipv6 = split("/", linode_instance.prod.ipv6)[0]
}

output "ipv6" {
value = local.ipv6
} }

+ 49
- 3
post_setup.tf Dosyayı Görüntüle

@@ -9,12 +9,58 @@ resource "null_resource" "post_setup" {
inline = [<<FIN inline = [<<FIN
apk update apk update
apk upgrade apk upgrade
apk add nginx muacme gitea iproute2 sqlite bind-tools
rc-update add nginx default
rc-service nginx restart
apk add iproute2 sqlite bind-tools tar xz
sed -i -e 's/AllowTcpForwarding .*/AllowTcpForwarding yes/g' /etc/ssh/sshd_config sed -i -e 's/AllowTcpForwarding .*/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
rc-service sshd restart rc-service sshd restart
FIN FIN
] ]
} }
} }

resource "null_resource" "nginx_install" {
depends_on = [ linode_instance.post_setup ]
connection {
host = linode_instance.prod.ip_address
user = "root"
private_key = file("~/.ssh/id_rsa")
}
provisioner "remote-exec" {
inline = [<<FIN
flock -x /tmp/apk -c 'apk add nginx'
apk add nginx muacme gitea iproute2 sqlite bind-tools tar xz
rc-update add nginx default
rc-service nginx restart
FIN
]
}
}

resource "null_resource" "uacme_install" {
depends_on = [ linode_instance.post_setup ]
connection {
host = linode_instance.prod.ip_address
user = "root"
private_key = file("~/.ssh/id_rsa")
}
provisioner "remote-exec" {
inline = [<<FIN
flock -x /tmp/apk -c 'apk add muacme'
FIN
]
}
}

resource "null_resource" "gitea_install" {
depends_on = [ linode_instance.post_setup ]
connection {
host = linode_instance.prod.ip_address
user = "root"
private_key = file("~/.ssh/id_rsa")
}
provisioner "remote-exec" {
inline = [<<FIN
flock -x /tmp/apk -c 'apk add gitea'
FIN
]
}
}

+ 9
- 1
setup_gitea.sh Dosyayı Görüntüle

@@ -7,6 +7,10 @@ if ! grep HTTP_ADDR $gitea_ini; then
sed -i -e '/\[server\]/a HTTP_ADDR = /var/lib/gitea/unix-domain-socket' $gitea_ini sed -i -e '/\[server\]/a HTTP_ADDR = /var/lib/gitea/unix-domain-socket' $gitea_ini
sed -i -e '/\[server\]/a PROTOCOL = unix' $gitea_ini sed -i -e '/\[server\]/a PROTOCOL = unix' $gitea_ini
sed -i -e '/\[server\]/a UNIX_SOCKET_PERMISSION = 666' $gitea_ini sed -i -e '/\[server\]/a UNIX_SOCKET_PERMISSION = 666' $gitea_ini
sed -i -e '/\[server\]/a START_SSH_SERVER = true' $gitea_ini
sed -i -e '/\[server\]/a SSH_DOMAIN = git.functionalidiot.com' $gitea_ini
sed -i -e '/\[server\]/a SSH_PORT = 2222' $gitea_ini
sed -i -e '/\[server\]/a SSH_LISTEN_PORT = 2222' $gitea_ini
fi fi


if ! grep OFFLINE_MODE $gitea_ini; then if ! grep OFFLINE_MODE $gitea_ini; then
@@ -16,7 +20,7 @@ fi


# Change the gitea APP_NAME # Change the gitea APP_NAME
if ! grep APP_NAME $gitea_ini; then if ! grep APP_NAME $gitea_ini; then
sed -i -e '/^RUN_MODE.*/a APP_NAME = mitchtys git house' $gitea_ini
sed -i -e '/^RUN_MODE.*/a APP_NAME = just a bunch of git repos' $gitea_ini
fi fi


# Ensure all new repos are private by default # Ensure all new repos are private by default
@@ -76,5 +80,9 @@ ENABLED = false
[picture] [picture]
DISABLE_GRAVATAR = false DISABLE_GRAVATAR = false
ENABLE_FEDERATED_AVATAR = true ENABLE_FEDERATED_AVATAR = true

[git]
MAX_GIT_DIFF_LINES = 20000
MAX_GIT_DIFF_LINE_CHARACTERS = 100000
FIN FIN
fi fi

Yükleniyor…
İptal
Kaydet