Skip to content

Commit 73e25ff

Browse files
Brutus5000Sheikah45
authored andcommitted
Util class
1 parent 878f05f commit 73e25ff

File tree

6 files changed

+114
-168
lines changed

6 files changed

+114
-168
lines changed

apps/faf-legacy-deployment/scripts/CoopDeployer.kt

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
package com.faforever.coopdeployer
44

5+
import com.faforever.FafDatabase
6+
import com.faforever.GitRepo
7+
import com.faforever.Log
58
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
69
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
7-
import org.eclipse.jgit.api.Git
810
import org.slf4j.LoggerFactory
911
import java.io.IOException
1012
import java.net.URI
@@ -18,8 +20,6 @@ import java.nio.file.StandardCopyOption
1820
import java.nio.file.attribute.FileTime
1921
import java.nio.file.attribute.PosixFilePermission
2022
import java.security.MessageDigest
21-
import java.sql.Connection
22-
import java.sql.DriverManager
2323
import java.time.Duration
2424
import kotlin.io.path.inputStream
2525

@@ -34,33 +34,6 @@ fun Path.setPerm664() {
3434
Files.setPosixFilePermissions(this, perms)
3535
}
3636

37-
data class GitRepo(
38-
val workDir: Path,
39-
val repoUrl: String,
40-
val gitRef: String,
41-
) {
42-
fun checkout(): Path {
43-
if (Files.exists(workDir.resolve(".git"))) {
44-
log.info("Repo exists — fetching and checking out $gitRef...")
45-
Git.open(workDir.toFile()).use { git ->
46-
git.fetch().call()
47-
git.checkout().setName(gitRef).call()
48-
}
49-
} else {
50-
log.info("Cloning repository $repoUrl")
51-
Git.cloneRepository()
52-
.setURI(repoUrl)
53-
.setDirectory(workDir.toFile())
54-
.call()
55-
log.info("Checking out $gitRef")
56-
Git.open(workDir.toFile()).use { git ->
57-
git.checkout().setName(gitRef).call()
58-
}
59-
}
60-
61-
return workDir
62-
}
63-
}
6437

6538
data class GithubReleaseAssetDownloader(
6639
val repoOwner: String = "FAForever",
@@ -160,25 +133,14 @@ data class GithubReleaseAssetDownloader(
160133

161134
}
162135

163-
data class FafDatabase(
164-
val host: String,
165-
val database: String,
166-
val username: String,
167-
val password: String,
136+
data class CoopDatabase(
168137
val dryRun: Boolean
169-
) : AutoCloseable {
138+
) : FafDatabase() {
170139
/**
171140
* Definition of an existing file in the database
172141
*/
173142
data class PatchFile(val mod: String, val fileId: Int, val name: String, val md5: String, val version: Int)
174143

175-
private val connection: Connection =
176-
DriverManager.getConnection(
177-
"jdbc:mariadb://$host/$database?useSSL=false&serverTimezone=UTC",
178-
username,
179-
password
180-
)
181-
182144
fun getCurrentPatchFile(mod: String, fileId: Int): PatchFile? {
183145
val sql = """
184146
SELECT uf.fileId, uf.name, uf.md5, t.v
@@ -191,7 +153,7 @@ data class FafDatabase(
191153
WHERE uf.fileId = ?
192154
""".trimIndent()
193155

194-
connection.prepareStatement(sql).use { stmt ->
156+
prepareStatement(sql).use { stmt ->
195157
stmt.setInt(1, fileId)
196158
val rs = stmt.executeQuery()
197159
while (rs.next()) {
@@ -213,23 +175,19 @@ data class FafDatabase(
213175
}
214176
val del = "DELETE FROM updates_${mod}_files WHERE fileId=? AND version=?"
215177
val ins = "INSERT INTO updates_${mod}_files (fileId, version, name, md5, obselete) VALUES (?, ?, ?, ?, 0)"
216-
connection.prepareStatement(del).use {
178+
prepareStatement(del).use {
217179
it.setInt(1, fileId)
218180
it.setInt(2, version)
219181
it.executeUpdate()
220182
}
221-
connection.prepareStatement(ins).use {
183+
prepareStatement(ins).use {
222184
it.setInt(1, fileId)
223185
it.setInt(2, version)
224186
it.setString(3, name)
225187
it.setString(4, md5)
226188
it.executeUpdate()
227189
}
228190
}
229-
230-
override fun close() {
231-
connection.close()
232-
}
233191
}
234192

235193
private const val MINIMUM_ZIP_DATE = 315532800000L // 1980-01-01
@@ -238,7 +196,7 @@ private val MINIMUM_ZIP_FILE_TIME = FileTime.fromMillis(MINIMUM_ZIP_DATE)
238196
class Patcher(
239197
val patchVersion: Int,
240198
val targetDir: Path,
241-
val db: FafDatabase,
199+
val db: CoopDatabase,
242200
val dryRun: Boolean,
243201
) {
244202
/**
@@ -409,17 +367,13 @@ class Patcher(
409367
}
410368

411369
fun main() {
370+
Log.init()
371+
412372
val PATCH_VERSION = System.getenv("PATCH_VERSION") ?: error("PATCH_VERSION required")
413373
val REPO_URL = System.getenv("GIT_REPO_URL") ?: "https://github.com/FAForever/fa-coop.git"
414374
val GIT_REF = System.getenv("GIT_REF") ?: "v$PATCH_VERSION"
415-
val WORKDIR = System.getenv("GIT_WORKDIR") ?: "/tmp/fa-coop-kt"
375+
val WORKDIR = System.getenv("GIT_WORKDIR") ?: "/tmp/fa-coop"
416376
val DRYRUN = (System.getenv("DRY_RUN") ?: "false").lowercase() in listOf("1", "true", "yes")
417-
418-
val DB_HOST = System.getenv("DATABASE_HOST") ?: "localhost"
419-
val DB_NAME = System.getenv("DATABASE_NAME") ?: "faf"
420-
val DB_USER = System.getenv("DATABASE_USERNAME") ?: "root"
421-
val DB_PASS = System.getenv("DATABASE_PASSWORD") ?: "banana"
422-
423377
val TARGET_DIR = Paths.get("./legacy-featured-mod-files")
424378

425379
log.info("=== Kotlin Coop Deployer v{} ===", PATCH_VERSION)
@@ -476,13 +430,7 @@ fun main() {
476430
Patcher.PatchFile(25, "FAF_Coop_Operation_Tight_Spot_VO.v%d.nx2", null),
477431
)
478432

479-
FafDatabase(
480-
host = DB_HOST,
481-
database = DB_NAME,
482-
username = DB_USER,
483-
password = DB_PASS,
484-
dryRun = DRYRUN
485-
).use { db ->
433+
CoopDatabase(dryRun = DRYRUN).use { db ->
486434
val patcher = Patcher(
487435
patchVersion = PATCH_VERSION.toInt(),
488436
targetDir = TARGET_DIR,

apps/faf-legacy-deployment/scripts/CoopMapDeployer.kt

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
package 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
78
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
89
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
9-
import org.eclipse.jgit.api.Git
1010
import org.slf4j.LoggerFactory
1111
import java.nio.file.Files
1212
import java.nio.file.Path
1313
import java.nio.file.Paths
1414
import java.nio.file.attribute.FileTime
1515
import java.security.MessageDigest
16-
import java.sql.Connection
17-
import java.sql.DriverManager
1816
import kotlin.io.path.copyTo
1917
import kotlin.io.path.createDirectories
2018
import kotlin.io.path.exists
@@ -26,38 +24,9 @@ import kotlin.io.path.walk
2624

2725
private val log = LoggerFactory.getLogger("coop-maps-updater")
2826

29-
3027
private const val FIXED_TIMESTAMP = 1078100502L // 2004-03-01T00:21:42Z
3128
private 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

6231
data 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

163117
private 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

255209
fun 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

Comments
 (0)