Automation and setup for sites I own.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

338 lines
9.2 KiB

  1. # gitea is a PITA to automate installation of, watch this pr for when it'll be
  2. # fully setup-able from the cli https://github.com/go-gitea/gitea/issues/9210
  3. variable "gitea_db_passwd" {
  4. type = string
  5. }
  6. variable "cloudflare_functionalidiot_zoneid" {
  7. type = string
  8. }
  9. variable "short_dns_ttl" {
  10. type = bool
  11. default = true
  12. description = "Whether to set short dns ttl's or not."
  13. }
  14. locals {
  15. dns_ttl = var.short_dns_ttl ? 120 : 43200
  16. }
  17. resource "cloudflare_record" "root" {
  18. depends_on = [
  19. linode_instance.prod,
  20. cloudflare_record.functionalidiot_com_ns1,
  21. cloudflare_record.functionalidiot_com_ns2 ]
  22. zone_id = var.cloudflare_functionalidiot_zoneid
  23. name = "@"
  24. value = linode_instance.prod.ip_address
  25. type = "A"
  26. ttl = local.dns_ttl
  27. }
  28. resource "cloudflare_record" "splat" {
  29. depends_on = [
  30. linode_instance.prod,
  31. cloudflare_record.functionalidiot_com_ns1,
  32. cloudflare_record.functionalidiot_com_ns2 ]
  33. zone_id = var.cloudflare_functionalidiot_zoneid
  34. name = "*"
  35. value = linode_instance.prod.ip_address
  36. type = "A"
  37. ttl = local.dns_ttl
  38. }
  39. # resource "cloudflare_record" "root6" {
  40. # depends_on = [
  41. # linode_instance.prod,
  42. # cloudflare_record.functionalidiot_com_ns1,
  43. # cloudflare_record.functionalidiot_com_ns2 ]
  44. # zone_id = var.cloudflare_functionalidiot_zoneid
  45. # name = "@"
  46. # value = local.ipv6
  47. # type = "AAAA"
  48. # ttl = local.dns_ttl
  49. # }
  50. # resource "cloudflare_record" "splat6" {
  51. # depends_on = [
  52. # linode_instance.prod,
  53. # cloudflare_record.functionalidiot_com_ns1,
  54. # cloudflare_record.functionalidiot_com_ns2 ]
  55. # zone_id = var.cloudflare_functionalidiot_zoneid
  56. # name = "*"
  57. # value = local.ipv6
  58. # type = "AAAA"
  59. # ttl = local.dns_ttl
  60. # proxied = true
  61. # }
  62. resource "cloudflare_record" "www" {
  63. depends_on = [
  64. cloudflare_record.root,
  65. cloudflare_record.splat ]
  66. zone_id = var.cloudflare_functionalidiot_zoneid
  67. name = "www"
  68. value = "functionalidiot.com"
  69. type = "CNAME"
  70. ttl = local.dns_ttl
  71. }
  72. resource "cloudflare_record" "git" {
  73. depends_on = [
  74. cloudflare_record.root,
  75. cloudflare_record.splat ]
  76. zone_id = var.cloudflare_functionalidiot_zoneid
  77. name = "git"
  78. value = "functionalidiot.com"
  79. type = "CNAME"
  80. ttl = local.dns_ttl
  81. }
  82. # MX record setup for fastmail dkim/spf on root and all subdomains
  83. resource "cloudflare_record" "mx-cname-1" {
  84. depends_on = [
  85. cloudflare_record.root,
  86. cloudflare_record.splat ]
  87. zone_id = var.cloudflare_functionalidiot_zoneid
  88. name = "fm1._domainkey"
  89. value = "fm1.functionalidiot.com.dkim.fmhosted.com"
  90. type = "CNAME"
  91. ttl = local.dns_ttl
  92. }
  93. resource "cloudflare_record" "mx-cname-2" {
  94. depends_on = [
  95. cloudflare_record.root,
  96. cloudflare_record.splat ]
  97. zone_id = var.cloudflare_functionalidiot_zoneid
  98. name = "fm2._domainkey"
  99. value = "fm2.functionalidiot.com.dkim.fmhosted.com"
  100. type = "CNAME"
  101. ttl = local.dns_ttl
  102. }
  103. resource "cloudflare_record" "mx-cname-3" {
  104. depends_on = [
  105. cloudflare_record.root,
  106. cloudflare_record.splat ]
  107. zone_id = var.cloudflare_functionalidiot_zoneid
  108. name = "fm3._domainkey"
  109. value = "fm3.functionalidiot.com.dkim.fmhosted.com"
  110. type = "CNAME"
  111. ttl = local.dns_ttl
  112. }
  113. resource "cloudflare_record" "mx-10-root" {
  114. depends_on = [
  115. cloudflare_record.root,
  116. cloudflare_record.splat ]
  117. zone_id = var.cloudflare_functionalidiot_zoneid
  118. name = "@"
  119. value = "in1-smtp.messagingengine.com"
  120. type = "MX"
  121. priority = "10"
  122. ttl = local.dns_ttl
  123. }
  124. resource "cloudflare_record" "mx-20-root" {
  125. depends_on = [
  126. cloudflare_record.root,
  127. cloudflare_record.splat ]
  128. zone_id = var.cloudflare_functionalidiot_zoneid
  129. name = "@"
  130. value = "in2-smtp.messagingengine.com"
  131. type = "MX"
  132. priority = "20"
  133. ttl = local.dns_ttl
  134. }
  135. resource "cloudflare_record" "mx-10-splat" {
  136. depends_on = [
  137. cloudflare_record.root,
  138. cloudflare_record.splat ]
  139. zone_id = var.cloudflare_functionalidiot_zoneid
  140. name = "*"
  141. value = "in1-smtp.messagingengine.com"
  142. type = "MX"
  143. priority = "10"
  144. ttl = local.dns_ttl
  145. }
  146. resource "cloudflare_record" "mx-20-splat" {
  147. depends_on = [
  148. cloudflare_record.root,
  149. cloudflare_record.splat ]
  150. zone_id = var.cloudflare_functionalidiot_zoneid
  151. name = "*"
  152. value = "in2-smtp.messagingengine.com"
  153. type = "MX"
  154. priority = "20"
  155. ttl = local.dns_ttl
  156. }
  157. resource "cloudflare_record" "txt-mx-root" {
  158. depends_on = [
  159. cloudflare_record.root,
  160. cloudflare_record.splat ]
  161. zone_id = var.cloudflare_functionalidiot_zoneid
  162. name = "@"
  163. value = "v=spf1 include:spf.messagingengine.com ?all"
  164. type = "TXT"
  165. ttl = local.dns_ttl
  166. }
  167. resource "cloudflare_record" "txt-mx-splat" {
  168. depends_on = [
  169. cloudflare_record.root,
  170. cloudflare_record.splat ]
  171. zone_id = var.cloudflare_functionalidiot_zoneid
  172. name = "*"
  173. value = "v=spf1 include:spf.messagingengine.com ?all"
  174. type = "TXT"
  175. ttl = local.dns_ttl
  176. }
  177. resource "cloudflare_record" "functionalidiot_com_ns1" {
  178. zone_id = var.cloudflare_functionalidiot_zoneid
  179. name = "@"
  180. value = "mitch.ns.cloudflare.com"
  181. type = "NS"
  182. }
  183. resource "cloudflare_record" "functionalidiot_com_ns2" {
  184. zone_id = var.cloudflare_functionalidiot_zoneid
  185. name = "@"
  186. value = "tegan.ns.cloudflare.com"
  187. type = "NS"
  188. }
  189. resource "null_resource" "functionalidiot_com_setup_new_gitea" {
  190. depends_on = [ null_resource.gitea_install ]
  191. count = fileexists("${path.module}/attic/gitea.txz") ? 0 : 1
  192. connection {
  193. host = linode_instance.prod.ip_address
  194. user = "root"
  195. private_key = file("~/.ssh/id_rsa")
  196. }
  197. provisioner "file" {
  198. source = "setup_gitea.sh"
  199. destination = "/tmp/setup_gitea.sh"
  200. }
  201. provisioner "remote-exec" {
  202. inline = [<<FIN
  203. install -m755 /tmp/setup_gitea.sh /usr/local/bin/setup_gitea
  204. rm -f /tmp/setup_gitea.sh
  205. rc-update add gitea default
  206. skey=$(su - gitea -c "gitea generate secret SECRET_KEY")
  207. itoken=$(su - gitea -c "gitea generate secret INTERNAL_TOKEN")
  208. SECRET_KEY=$skey INTERNAL_TOKEN=$itoken /usr/local/bin/setup_gitea
  209. rc-service gitea restart
  210. sleep 3
  211. chown gitea:www-data /var/lib/gitea/db/gitea.db
  212. su - gitea -c "gitea migrate --config /etc/gitea/app.ini"
  213. su - gitea -c "gitea admin create-user --username mitchty --password ${var.gitea_db_passwd} --email git@functionalidiot.com --admin --config /etc/gitea/app.ini"
  214. FIN
  215. ]
  216. }
  217. }
  218. resource "null_resource" "functionalidiot_com_setup_saved_gitea" {
  219. depends_on = [ null_resource.gitea_install ]
  220. count = fileexists("${path.module}/attic/gitea.txz") ? 1 : 0
  221. connection {
  222. host = linode_instance.prod.ip_address
  223. user = "root"
  224. private_key = file("~/.ssh/id_rsa")
  225. }
  226. provisioner "file" {
  227. source = "${path.module}/attic/gitea.txz"
  228. destination = "/tmp/gitea.txz"
  229. }
  230. # TODO: Finish post untar setup
  231. provisioner "remote-exec" {
  232. inline = [<<FIN
  233. cd /
  234. tar xvJf /tmp/gitea.txz
  235. rc-update add gitea default
  236. rc-service gitea restart
  237. FIN
  238. ]
  239. }
  240. }
  241. resource "null_resource" "functionalidiot_com_setup_saved_uacme" {
  242. depends_on = [ null_resource.uacme_install ]
  243. count = fileexists("${path.module}/attic/uacme.txz") ? 1 : 0
  244. connection {
  245. host = linode_instance.prod.ip_address
  246. user = "root"
  247. private_key = file("~/.ssh/id_rsa")
  248. }
  249. provisioner "file" {
  250. source = "${path.module}/attic/uacme.txz"
  251. destination = "/tmp/uacme.txz"
  252. }
  253. # TODO: Finish post untar setup
  254. provisioner "remote-exec" {
  255. inline = [<<FIN
  256. cd /
  257. tar xvJf /tmp/uacme.txz
  258. FIN
  259. ]
  260. }
  261. }
  262. # FIXME: This nginx setup isn't idempotent if its a new setup vs saved
  263. # uacme ssl setup.
  264. #
  265. # Maybe use the dns uacme crap instead of this stupid http bs?
  266. #
  267. # https://www.terraform.io/docs/providers/acme/r/certificate.html
  268. # https://www.terraform.io/docs/providers/acme/dns_providers/cloudflare.html
  269. resource "null_resource" "functionalidiot_com_setup_new_nginx" {
  270. depends_on = [
  271. null_resource.nginx_install,
  272. null_resource.functionalidiot_com_setup_new_gitea,
  273. null_resource.functionalidiot_com_setup_saved_gitea,
  274. null_resource.functionalidiot_com_setup_saved_uacme,
  275. null_resource.functionalidiot_com_setup_saved_uacme
  276. ]
  277. connection {
  278. host = linode_instance.prod.ip_address
  279. user = "root"
  280. private_key = file("~/.ssh/id_rsa")
  281. }
  282. provisioner "file" {
  283. source = "http-default.conf"
  284. destination = "/tmp/http-default.conf"
  285. }
  286. provisioner "file" {
  287. source = "https-functionalidiot.com.conf"
  288. destination = "/tmp/https-functionalidiot.com.conf"
  289. }
  290. provisioner "remote-exec" {
  291. inline = [<<FIN
  292. [ -e /etc/nginx/conf.d/default.conf ] && mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disable
  293. install -m644 /tmp/http-default.conf /etc/nginx/conf.d/http-default.conf
  294. install -m644 /tmp/https-functionalidiot.com.conf /etc/nginx/conf.d/https-functionalidiot.com.conf
  295. rm -fr /tmp/*.conf
  296. install -dm755 /var/www/default/htdocs /var/www/.well-known/acme-challenge
  297. echo todo write more stuff > /var/www/default/htdocs/index.html
  298. rc-service nginx restart
  299. FIN
  300. ]
  301. }
  302. }