22
33package com.faforever.coopmapdeployer
44
5- import ch.qos.logback.classic.Logger
6- import ch.qos.logback.classic.Level
5+ import com.faforever.FafDatabase
6+ import com.faforever.GitRepo
7+ import com.faforever.Log
78import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
89import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
9- import org.eclipse.jgit.api.Git
1010import org.slf4j.LoggerFactory
1111import java.nio.file.Files
1212import java.nio.file.Path
1313import java.nio.file.Paths
1414import java.nio.file.attribute.FileTime
1515import java.security.MessageDigest
16- import java.sql.Connection
17- import java.sql.DriverManager
1816import kotlin.io.path.copyTo
1917import kotlin.io.path.createDirectories
2018import kotlin.io.path.exists
@@ -26,38 +24,9 @@ import kotlin.io.path.walk
2624
2725private val log = LoggerFactory .getLogger(" coop-maps-updater" )
2826
29-
3027private const val FIXED_TIMESTAMP = 1078100502L // 2004-03-01T00:21:42Z
3128private val FIXED_FILE_TIME = FileTime .fromMillis(FIXED_TIMESTAMP )
3229
33- data class GitRepo (
34- val workDir : Path ,
35- val repoUrl : String ,
36- val gitRef : String ,
37- ) {
38- fun checkout (): Path {
39- if (Files .exists(workDir.resolve(" .git" ))) {
40- log.info(" Repo exists — fetching and checking out $gitRef ..." )
41- Git .open(workDir.toFile()).use { git ->
42- git.fetch().call()
43- git.checkout().setName(gitRef).call()
44- }
45- } else {
46- log.info(" Cloning repository $repoUrl " )
47- Git .cloneRepository()
48- .setURI(repoUrl)
49- .setDirectory(workDir.toFile())
50- .call()
51- log.info(" Checking out $gitRef " )
52- Git .open(workDir.toFile()).use { git ->
53- git.checkout().setName(gitRef).call()
54- }
55- }
56-
57- return workDir
58- }
59- }
60-
6130
6231data class CoopMap (
6332 val folderName : String ,
@@ -120,22 +89,11 @@ private val coopMaps = listOf(
12089 CoopMap (" FAF_Coop_Operation_Red_Revenge" , 49 , 4 ),
12190)
12291
123- data class FafDatabase (
124- val host : String ,
125- val database : String ,
126- val username : String ,
127- val password : String ,
92+ data class CoopMapDatabase (
12893 val dryRun : Boolean
129- ) : AutoCloseable {
130- private val connection: Connection =
131- DriverManager .getConnection(
132- " jdbc:mariadb://$host /$database ?useSSL=false&serverTimezone=UTC" ,
133- username,
134- password
135- )
136-
94+ ) : FafDatabase() {
13795 fun getLatestVersion (map : CoopMap ): Int {
138- connection. createStatement().use { st ->
96+ createStatement().use { st ->
13997 st.executeQuery(" SELECT version FROM coop_map WHERE id=${map.mapId} " )
14098 .use { rs ->
14199 if (! rs.next()) error(" Map ${map.mapId} not found" )
@@ -144,24 +102,20 @@ data class FafDatabase(
144102 }
145103 }
146104
147- fun updateDatabase (map : CoopMap , version : Int ) {
105+ fun update (map : CoopMap , version : Int ) {
148106 val sql = """
149107 UPDATE coop_map
150108 SET version=$version ,
151109 filename='maps/${map.zipName(version)} '
152110 WHERE id=${map.mapId}
153111 """ .trimIndent()
154112
155- connection.createStatement().use { it.executeUpdate(sql) }
156- }
157-
158- override fun close () {
159- connection.close()
113+ createStatement().use { it.executeUpdate(sql) }
160114 }
161115}
162116
163117private fun processCoopMap (
164- db : FafDatabase ,
118+ db : CoopMapDatabase ,
165119 map : CoopMap ,
166120 simulate : Boolean ,
167121 gitDir : String ,
@@ -200,7 +154,7 @@ private fun processCoopMap(
200154 if (! simulate) {
201155 val finalZip = Path .of(mapsDir, map.zipName(newVersion))
202156 createZip(map, newVersion, files, tmp, finalZip)
203- db.updateDatabase (map, newVersion)
157+ db.update (map, newVersion)
204158 }
205159 } finally {
206160 tmp.toFile().deleteRecursively()
@@ -253,23 +207,16 @@ private fun md5(path: Path): String {
253207}
254208
255209fun main (args : Array <String >) {
210+ Log .init ()
211+
256212 val MAP_DIR = System .getenv(" MAP_DIR" ) ? : " /opt/faf/data/faf-coop-maps"
257213 val PATCH_VERSION = System .getenv(" PATCH_VERSION" ) ? : error(" PATCH_VERSION required" )
258214 val REPO_URL = System .getenv(" GIT_REPO_URL" ) ? : " https://github.com/FAForever/faf-coop-maps"
259215 val GIT_REF = System .getenv(" GIT_REF" ) ? : " v$PATCH_VERSION "
260- val WORKDIR = System .getenv(" GIT_WORKDIR" ) ? : " /tmp/fa -coop-kt "
216+ val WORKDIR = System .getenv(" GIT_WORKDIR" ) ? : " /tmp/faf -coop-maps "
261217 val DRYRUN = (System .getenv(" DRY_RUN" ) ? : " false" ).lowercase() in listOf (" 1" , " true" , " yes" )
262218
263- val DB_HOST = System .getenv(" DATABASE_HOST" ) ? : " localhost"
264- val DB_NAME = System .getenv(" DATABASE_NAME" ) ? : " faf"
265- val DB_USER = System .getenv(" DATABASE_USERNAME" ) ? : " root"
266- val DB_PASS = System .getenv(" DATABASE_PASSWORD" ) ? : " banana"
267-
268- val level = System .getenv(" LOG_LEVEL" ) ? : " INFO"
269-
270- val root = LoggerFactory
271- .getLogger(Logger .ROOT_LOGGER_NAME ) as Logger
272- root.level = Level .toLevel(level, Level .INFO )
219+ log.info(" === Kotlin Coop Map Deployer v{} ===" , PATCH_VERSION )
273220
274221 Files .createDirectories(Paths .get(MAP_DIR ))
275222
@@ -279,13 +226,7 @@ fun main(args: Array<String>) {
279226 gitRef = GIT_REF ,
280227 ).checkout()
281228
282- FafDatabase (
283- host = DB_HOST ,
284- database = DB_NAME ,
285- username = DB_USER ,
286- password = DB_PASS ,
287- dryRun = DRYRUN
288- ).use { db ->
229+ CoopMapDatabase (dryRun = DRYRUN ).use { db ->
289230 coopMaps.forEach {
290231 try {
291232 processCoopMap(db, it, DRYRUN , WORKDIR , MAP_DIR )
@@ -294,5 +235,4 @@ fun main(args: Array<String>) {
294235 }
295236 }
296237 }
297-
298238}
0 commit comments