From 2d8fd4fca4eefe2082605d1c0566d8f74893fa87 Mon Sep 17 00:00:00 2001
From: Ubuntu <kop@karlpinc.com>
Date: Thu, 14 Sep 2023 19:08:12 +0000
Subject: [PATCH] Add targets for building cluster hosts on Azure

---
 make_files/make_db.mk | 78 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)

diff --git a/make_files/make_db.mk b/make_files/make_db.mk
index a4ca1fb..3fa0ff8 100644
--- a/make_files/make_db.mk
+++ b/make_files/make_db.mk
@@ -477,9 +477,44 @@ $(DB_TARGETS) db/creategroups.sql: %.sql: %.m4 $(DB_DEPENDS)
 ##     The password to assign.
 ##   NEW_DESCRIPTION
 ##     The description of the new object (database).
+##   RESOURCEGROUP
+##     The name of the Azure resource group to use when creating the
+##     cluster in Microsoft Azure.
+##   TARGET_SERVER
+##     The unqualified host name of the Azure postgres (flexible-server)
+##     to create.  To fully qualify this host name append
+##     ".postgres.database.azure.com".  The default is "sokwe-dbs".
 ##
 ## Usage:
 ##
+##  If you are on Azure:
+##
+##  You must first prepare your Azure environment by running:
+##
+##    az login
+##
+##  If you have more than one "subscription" you will need to use
+##  `az account list` followed by
+##  `az account set --subscription=SUBSCRIPTIONID` to set the default
+##  subscription to that you wish to use.
+##
+##  Use `az group list` to list your "resource group" names.  Use the
+##  desired name to set the RESOURCEGROUP variable when invoking make.
+##  
+##  1) Create the cluster with the "az-init" target.
+##     Be sure to note the ADMINUSER's password in the output and
+##     keep it in a secure location.  The ADMINUSER is the
+##     "administrative user" of this document.
+##
+##  2) Drop the "throwawaydb" database with the "az-drop-throwawaydb"
+##     target.
+##
+##  3) Skip step 1 of the "not on Azure" instructions below and
+##     finish the cluster installation by starting at step 2.
+##     (You have an administrative user.)
+##
+##  If you are not on Azure and have a PostgreSQL installation:
+##
 ##  1) If you already have a Postgres role that can login, create roles, and
 ##  create databases then that role should be used as the administrative
 ##  user.  Otherwise, first create an administrative user (while logged in
@@ -512,6 +547,18 @@ $(DB_TARGETS) db/creategroups.sql: %.sql: %.m4 $(DB_DEPENDS)
 ##  them with "make" will properly initialize them for use with SokweDB.
 
 export NEW_DESCRIPTION
+TARGET_SERVER ?= sokwe-dbs
+
+# Azure related variables
+# The (one) IP that that firewall allows to access the db server.
+# TODO: We would like to be able to resolve the sokwe.janegoodall.org name
+# but that's not happening right now.
+WEBSERVER_IP := 20.119.86.26
+# The name of a database to throw away.
+# Azure requires we create a db when we create the server, but we want
+# to specify our own settings when creating the db so we delete
+# the one Azure makes.
+THROWAWAY_DB := throwawaydb
 
 # Have m4 generate some shell scripts which contain expanded macro constants.
 GENERATED_SHELL := db/set_timezone.sh \
@@ -534,11 +581,40 @@ CREATE_SUPERUSER := CREATE ROLE $(TARGET_ROLE) LOGIN SUPERUSER \
 CREATE_ADMINUSER := CREATE ROLE $(TARGET_ROLE) LOGIN CREATEDB CREATEROLE \
                     PASSWORD '$(NEW_PASSWORD)';
 DROP_PUBLIC := DROP SCHEMA public RESTRICT;
+DROP_THROWAWAYDB := DROP DATABASE $(THROWAWAY_DB);
 SET_PASSWORD := ALTER ROLE $(TARGET_ROLE) PASSWORD '$(NEW_PASSWORD)'
 
-
 ## The available targets for make (make TARGET) are:
 ##
+##   az-init            Create a Postgres Azure server.  See above for
+##                      prerequisite actions.  Create the server named
+##                      TARGET_SERVER in the RESOURCEGROUP resource group
+##                      creating the administrator user ADMINUSER.
+.PHONY: az-init
+az-init:
+	[ -n "$(TARGET_SERVER)" ] \
+           || { printf 'The TARGET_SERVER variable must be set\n' >&2 ; \
+                exit 1 ; }
+	[ -n "$(RESOURCEGROUP)" ] \
+           || { printf 'The RESOURCEGROUP variable must be set\n' >&2 ; \
+                exit 1 ; }
+	az postgres flexible-server create \
+	   --public-access $(WEBSERVER_IP) \
+	   --database-name $(THROWAWAY_DB) \
+	   --name $(TARGET_SERVER) \
+	   --resource-group $(RESOURCEGROUP) \
+	   --storage-size 32 \
+	   --version 15 \
+	   --admin-user $(ADMINUSER)
+
+##   az-drop-throwawaydb
+##                      Drop the database which is required to be created
+##                      when creating an Azure cluster.
+.PHONY: az-drop-throwawaydb
+az-drop-throwawaydb:
+	printf '$(DROP_THROWAWAYDB)\n' \
+	  | psql -U $(ADMINUSER) -h $(HOST) postgres --set=ON_ERROR_STOP=y
+
 ##   create-adminuser   This target only works when the PostgreSQL cluster
 ##                      with which you are interacting is on the local host.
 ##                      If your cluster was created on a remote host you are
-- 
2.34.1