This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
forked from DataBiosphere/terra-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateVersions.sc
executable file
·179 lines (164 loc) · 6.26 KB
/
updateVersions.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* This script is written in Scala 2.13+
* Run it with `ammonite for Scala 2.13+`
*/
val workbenchUtil2 = "0.10-c3ef6b80-SNAP"
val circeVersion = "0.13.0"
interp.load.ivy(
"co.fs2" %% "fs2-io" % "2.4.2",
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"io.circe" %% "circe-generic" % circeVersion,
"io.circe" %% "circe-optics" % circeVersion
)
@
import cats.effect.IO
import java.nio.file.Paths
import java.nio.file.Path
import io.circe.parser._
import io.circe.Printer
import io.circe.Json
import io.circe.syntax._
import io.circe.optics.JsonPath._
import io.circe.generic.semiauto._
import sys.process._
import scala.concurrent.ExecutionContext.global
import cats.effect.Blocker
import fs2.{io, text}
import java.time.Instant
import cats.implicits._
implicit val cs = IO.contextShift(global)
val blocker = Blocker.liftExecutionContext(global)
// Example: oldVersion 1.0.4, newVersion 1.0.5
private def newVersion(oldVersion: String, bumpMajorVersion: Boolean): String = {
val splited = oldVersion.split("\\.")
if(bumpMajorVersion)
s"${splited(0).toInt + 1}.0.0"
else
s"${splited(0)}.${splited(1)}.${splited(2).toInt + 1}"
}
def modifyVersion(bumpMajorVersion: Boolean): Json => Json =
root.version.string.modify {
s =>
newVersion(s, bumpMajorVersion)
}
def modifyImageData(imagesToUpdate: List[String], bumpMajorVersion: Boolean): Json => Json =
root.image_data.arr.modify {
listOfImageData =>
listOfImageData.map {
image =>
val imageName = image.hcursor.downField("name").as[String].fold(e => throw e, identity)
if(imagesToUpdate.contains(imageName))
modifyVersion(bumpMajorVersion)(image)
else image
}
}
/**
* In repo root dir, run `amm ./updateVersions.sc terra-jupyter-base 'update notebook to 6.1.1'`
* @param updatedImage e.g. "terra-jupyter-base"
*/
@main
def main(updatedImage: String, updatedImageReleaseNote: String, bumpMajorVersion: Boolean = false): Unit = {
val imagesToUpdate: List[String] = updatedImage match {
case "terra-jupyter-base" => List(
"terra-jupyter-bioconductor",
"terra-jupyter-hail",
"terra-jupyter-base",
"terra-jupyter-python",
"terra-jupyter-r",
"terra-jupyter-gatk",
"terra-jupyter-aou",
"terra-jupyter-gatk-ovtf"
)
case "terra-jupyter-r" => List(
"terra-jupyter-r",
"terra-jupyter-bioconductor",
"terra-jupyter-gatk",
"terra-jupyter-aou",
"terra-jupyter-gatk-ovtf"
)
case "terra-jupyter-python" => List(
"terra-jupyter-python",
"terra-jupyter-hail",
"terra-jupyter-gatk",
"terra-jupyter-aou",
"terra-jupyter-gatk-ovtf"
)
case "terra-jupyter-hail" => List(
"terra-jupyter-hail"
)
case "terra-jupyter-gatk" => List(
"terra-jupyter-gatk"
)
case "terra-jupyter-gatk-ovtf" => List(
"terra-jupyter-gatk-ovtf"
)
case updatedImage =>
throw new Exception(s"${updatedImage} is not supported yet. Please update the script to support the image")
}
val res = for {
configFileRawJson <- (io.file.readAll[IO](Paths.get("./config/conf.json"), blocker, 4096).through(text.utf8Decode)).compile.string
config <- IO.fromEither(parse(configFileRawJson))
newConfig = modifyImageData(imagesToUpdate, bumpMajorVersion)(config)
_ <- (fs2.Stream.emit(newConfig.asJson.printWith(Printer.spaces4)).covary[IO]
.through(text.utf8Encode)
.through(io.file.writeAll(Paths.get("./config/conf.json"), blocker))).compile.drain
// Update updatedImage's changelog
updatedImageChangeLogFile = Paths.get(s"${updatedImage}/CHANGELOG.md")
updatedImageNewVersion <- updateChangeLogFile(
updatedImage,
s"""- ${updatedImageReleaseNote}""".stripMargin,
updatedImageChangeLogFile,
bumpMajorVersion
)
// Update `Dockerfile` and `CHANGELOG.md`
_ <- imagesToUpdate.filterNot(_.contains(updatedImage)).traverse {
image =>
val dockerFileToUpdate = Paths.get(s"${image}/Dockerfile")
val changelogFile = Paths.get(s"./${image}/CHANGELOG.md")
for {
lines <- (io.file.readAll[IO] (dockerFileToUpdate, blocker, 4096)
.through(text.utf8Decode)
.through(text.lines)).compile.toList
newLines = lines.zipWithIndex.map {
lineWithIndex =>
val s = lineWithIndex._1
if(s.contains("FROM") && !s.contains("AS")) {
val firstSplit = s.split(":")
val imageName = firstSplit(0).split("\\/")(2)
if(imagesToUpdate.contains(imageName)) {
s"${firstSplit(0)}:${newVersion(firstSplit(1), bumpMajorVersion)}\n"
} else s"${s}\n"
} else if(s.contains(" AS ") && imagesToUpdate.contains("terra-jupyter-python")) {
val firstSplit = s.split(":")
val secondSplit = firstSplit(1).split(" ")
s"${firstSplit(0)}:${newVersion(secondSplit(0), bumpMajorVersion)} AS python\n"
} else if(lineWithIndex._2 == lines.length - 1)
s
else s"${s}\n"
}
_ <- (fs2.Stream.emits(newLines).covary[IO].through(text.utf8Encode)
.through(io.file.writeAll(dockerFileToUpdate, blocker))).compile.drain
_ <- updateChangeLogFile(image, s"- Update `${updatedImage}` to `${updatedImageNewVersion}`\n - ${updatedImageReleaseNote}", changelogFile, bumpMajorVersion)
} yield ()
}
} yield ()
res.unsafeRunSync
}
// returns the new version String of this file
def updateChangeLogFile(image: String, msg: String, path: Path, bumpMajorVersion: Boolean): IO[String] = for {
originalFileContent <- (io.file.readAll[IO] (path, blocker, 4096)
.through(text.utf8Decode)).compile.string
firstLine = originalFileContent.split("\n")(0)
newV = newVersion(firstLine.split(" ")(1), bumpMajorVersion)
newFileContent =
s"""## ${newV} - ${Instant.now()}
|
|${msg}
|
|Image URL: `us.gcr.io/broad-dsp-gcr-public/${image}:${newV}`
|
|${originalFileContent}""".stripMargin
_ <- (fs2.Stream.emit(newFileContent).covary[IO].through(text.utf8Encode)
.through(io.file.writeAll(path, blocker))).compile.drain
} yield newV