jenkins: leaf: Add ota_index script

Change-Id: I67a29a4c402b823961ed10fba5115da4509bb585
diff --git a/jenkins/index-ota.sh b/jenkins/index-ota.sh
new file mode 100755
index 0000000..643b7a0
--- /dev/null
+++ b/jenkins/index-ota.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+BASEDIR="$1"
+WWWDIR="$2"
+BASEURL="$3"
+INDEX_DEVICE="$4"
+
+if [ -z "$BASEDIR" ] || [ -z "$WWWDIR" ] || [ -z "$BASEURL" ]; then
+	echo "Usage: $0 <basedir> <wwwdir> <baseurl> [device]"
+	exit 1
+fi
+
+function get_metadata_value() {
+	local METADATA_LOCAL="$1"
+	local KEY="$2"
+	echo "$METADATA_LOCAL" | grep "$KEY=" | cut -f2- -d '='
+}
+
+echo "" > transaction.sql
+
+find "$BASEDIR" -name *.zip -or -name *.sha256 -mtime +50 -delete -print
+find "$BASEDIR" -empty -type d -delete -print
+
+if [ ! -z "$INDEX_DEVICE" ]; then
+	echo "DELETE FROM leaf_ota WHERE device = \"$INDEX_DEVICE\";" > transaction.sql
+else
+	echo "DELETE FROM leaf_ota;" > transaction.sql
+fi
+for OTA in $(find "$BASEDIR" -name *.zip); do
+	echo "$OTA"
+	[ ! -f "$OTA".sha256 ] && sha256sum "$OTA" > "$OTA".sha256
+
+	METADATA=$(unzip -p - "$OTA" META-INF/com/android/metadata 2>/dev/null)
+	if [ ! -z "$METADATA" ]; then
+		DEVICE=$(get_metadata_value "$METADATA" "pre-device")
+		DATETIME=$(get_metadata_value "$METADATA" "post-timestamp")
+		INCREMENTAL=$(get_metadata_value "$METADATA" "post-build-incremental")
+		INCREMENTAL_BASE=$(get_metadata_value "$METADATA" "pre-build-incremental")
+	else # GSI
+		DEVICE=$(echo "$OTA" | cut -f5 -d '-')
+		DATETIME=$(date -r "$OTA" +%s)
+		INCREMENTAL=$(echo "$OTA" | cut -f3 -d '-')
+	fi
+	FILENAME=$(basename "$OTA")
+	ID=$(cat "$OTA".sha256 | cut -f1 -d ' ')
+	ROMTYPE="OFFICIAL"
+	SIZE=$(du -b "$OTA" | cut -f1)
+	URL=$(echo "$OTA" | sed "s|$BASEDIR|$BASEURL|g")
+	VERSION=$(echo "$OTA" | cut -f2 -d '-')
+	FLAVOR=$(echo "$OTA" | cut -f4 -d '-')
+	INCREMENTAL=$(get_metadata_value "$METADATA" "post-build-incremental")
+	INCREMENTAL_BASE=$(get_metadata_value "$METADATA" "pre-build-incremental")
+	UPGRADE=$(cat "$WWWDIR/content/devices/$DEVICE.yml" | grep "format_on_upgrade:" | cut -f2 -d ':' | xargs)
+
+	echo "INSERT INTO leaf_ota(device, datetime, filename, id, romtype, size, url, version, " \
+		"flavor, incremental, incremental_base, upgrade) VALUES (\"$DEVICE\", \"$DATETIME\", " \
+		"\"$FILENAME\", \"$ID\", \"$ROMTYPE\", \"$SIZE\", \"$URL\", \"$VERSION\", " \
+		"\"$FLAVOR\", \"$INCREMENTAL\", \"$INCREMENTAL_BASE\", \"$UPGRADE\");" >> transaction.sql
+done
+
+echo "UPDATE leaf_ota SET incremental_base = NULL WHERE incremental_base = '';" >> transaction.sql
+cat transaction.sql | mariadb -u leaf -pleaf -D "leaf_ota"
+rm transaction.sql
diff --git a/jenkins/leaf-build.sh b/jenkins/leaf-build.sh
index 9bb552d..22efe21 100755
--- a/jenkins/leaf-build.sh
+++ b/jenkins/leaf-build.sh
@@ -10,7 +10,9 @@
 LEAF_FLAVORS=(VANILLA GMS microG)
 TARGET_FILES_DIR="/var/lib/jenkins/leaf/target-files/$RELEASE_DIR$JENKINS_DEVICE"
 MASTER_IP="$(echo $SSH_CLIENT | cut -f1 -d ' ')"
+BUILDDATE=$(echo "$BUILDDATE" | head -n 1)
 DL_DIR="/var/www/dl.leafos.org/$RELEASE_DIR$JENKINS_DEVICE/$BUILDDATE"
+WWW_DIR="/var/www/leafos.org"
 KEY_DIR="/var/lib/jenkins/.android-certs"
 AVB_ALGORITHM="SHA256_RSA4096"
 OTATOOLS="out/host/linux-x86/bin"
@@ -48,6 +50,10 @@
 
 function init() {
 	telegram sendMessage "$TELEGRAM_MESSAGE"
+	OTA_INDEX=$(ssh jenkins@$MASTER_IP mktemp)
+	scp ./jenkins/build/jenkins/index-ota.sh jenkins@$MASTER_IP:"$OTA_INDEX"
+	ssh jenkins@$MASTER_IP chmod +x "$OTA_INDEX"
+	echo "$OTA_INDEX" > .ota_index_tmp
 }
 
 function sync() {
@@ -254,6 +260,9 @@
 			echo "$JENKINS_DEVICE-target_files-$JENKINS_FLAVOR-$BUILD_ID-signed.zip" >"$TARGET_FILES_DIR/latest_$JENKINS_FLAVOR"
 		fi
 	done
+
+	OTA_INDEX=$(cat .ota_index_tmp)
+	ssh jenkins@$MASTER_IP "$OTA_INDEX \"$DL_DIR\" \"$WWW_DIR\" \"https://${DL_DIR/\/var\/www\//}\" \"$JENKINS_DEVICE\""
 }
 
 function cleanup() {
@@ -270,4 +279,10 @@
 	rm -f leaf*.zip
 	rm -f leaf*.img
 	rm -rf .repo/local_manifests
+
+	OTA_INDEX=$(cat .ota_index_tmp 2>/dev/null)
+	rm -f .ota_index_tmp
+	if [ ! -z "$OTA_INDEX" ]; then
+		ssh jenkins@$MASTER_IP rm -f "$OTA_INDEX"
+	fi
 }