-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathInstructions.scala
432 lines (363 loc) · 13.6 KB
/
Instructions.scala
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package sbtdocker
import org.apache.commons.lang3.StringEscapeUtils
import sbtdocker.staging.SourceFile
import scala.concurrent.duration.FiniteDuration
sealed trait Instruction
/**
* A real dockerfile instruction. Contains a instruction name and arguments.
*/
trait DockerfileInstruction extends Instruction {
def arguments: String
def instructionName: String
override def toString = s"$instructionName $arguments"
@deprecated("Use toString instead.", "0.4.0")
def toInstructionString = toString
}
/**
* Helper for product classes.
* Sets instruction name from the class name and uses product values as arguments.
*/
trait ProductDockerfileInstruction extends DockerfileInstruction {
this: Product =>
def arguments = productIterator.mkString(" ")
def instructionName = productPrefix.toUpperCase
}
/**
* Instruction that stages files to the staging directory.
*/
trait FileStagingInstruction extends Instruction {
require(sources.nonEmpty, "Must have at least one source path")
require(sources.length == 1 || destination.endsWith("/"),
"When multiple source files are specified the destination must end with a slash \"/\".")
def sources: Seq[SourceFile]
def destination: String
}
/**
* Instruction that both stages files to the staging directory and adds a Dockerfile instruction.
*
* A [[sbtdocker.staging.DockerfileProcessor]] is used to stage files in the staging directory,
* which also requests the Dockerfile instruction when the file paths have been finalized.
*/
trait FileStagingDockerfileInstruction extends FileStagingInstruction {
/**
* Creates a Dockerfile instruction given the final paths of the sources.
*
* @param sources Paths to the sources in the staging directory.
* @return Dockerfile instruction.
*/
def dockerInstruction(sources: Seq[String]): DockerfileInstruction
}
object Instructions {
import InstructionUtils._
object From {
def apply(image: ImageName): From = new From(image.toString)
}
/**
* Sets the base image for the image.
* @param image Name of the image.
*/
case class From(image: String) extends ProductDockerfileInstruction
object Label {
def apply(variables: Map[String, String]): Label = {
Label(variables.map { case (key, value) => formatKeyValue(key, value) }.mkString(" "))
}
def apply(key: String, value: String): Label = {
Label(formatKeyValue(key, value))
}
def formatKeyValue(key: String, value: String): String = {
key + "=" + escapeVariable(value)
}
}
/**
* Sets label(s) for the image. Docker Version >= 1.6 required.
* Example: "A=1 B=2 C=3"
* @param labels image labels
*/
case class Label(labels: String) extends ProductDockerfileInstruction
/**
* Author of the image.
* @param name Author name.
*/
case class Maintainer(name: String) extends ProductDockerfileInstruction
object Run {
/**
* Executes a command directly, without using a shell which means that variables will not be substituted.
* @param commands Command list.
*/
def exec(commands: Seq[String]) = Run(jsonArrayString(commands))
/**
* Executes a command through a shell (`/bin/sh`).
* @param commands Command.
*/
def shell(commands: Seq[String]) = Run(shellCommandString(commands))
}
/**
* Executes a command when building the image.
* @param command Command which should be executed.
*/
case class Run(command: String) extends ProductDockerfileInstruction
object EntryPoint {
/**
* Command to execute when the image starts.
* @param commands Command list.
*/
def exec(commands: Seq[String]) = EntryPoint(jsonArrayString(commands))
/**
* Command to execute through a shell (`/bin/sh`) when the image starts.
* @param commands Command list.
*/
def shell(commands: Seq[String]) = EntryPoint(shellCommandString(commands))
}
/**
* Command to execute when the image starts.
* @param command Command.
*/
case class EntryPoint(command: String) extends ProductDockerfileInstruction
object Cmd {
/**
* Command to execute when the image starts, or default arguments to the entry point.
* @param commands Command list.
*/
def exec(commands: Seq[String]) = Cmd(jsonArrayString(commands))
/**
* Command to execute through a shell (`/bin/sh`) when the image starts, or default arguments
* to the entry point.
* @param commands Command list.
*/
def shell(commands: Seq[String]) = Cmd(shellCommandString(commands))
}
/**
* When an entry point is specified `CMD` acts as default arguments that is appended the the
* entry point command.
* If an entry point is not specified `CMD` will be the default command for the image.
* @param command Command.
*/
case class Cmd(command: String) extends ProductDockerfileInstruction
/**
* Exposes network ports at runtime.
* @param ports Port numbers to expose.
*/
case class Expose(ports: Seq[Int]) extends ProductDockerfileInstruction {
require(ports.nonEmpty, "Must expose at least one port")
override def arguments = ports.mkString(" ")
}
object Env {
def apply(variables: Map[String, String]): Env = {
Env(variables.map { case (key, value) => formatKeyValue(key, value) }.mkString(" "))
}
def apply(key: String, value: String): Env = {
Env(formatKeyValue(key, value))
}
def formatKeyValue(key: String, value: String): String = {
key + "=" + escapeVariable(value)
}
}
/**
* Sets environment variables in the image.
* Example: "A=1 B=2 C=3"
* @param variables Environment variables.
*/
case class Env(variables: String) extends ProductDockerfileInstruction
object Add {
def apply(source: SourceFile, destination: String): Add = Add(Seq(source), destination)
}
/**
* Adds files to the image.
* If a source file is a tar file it will be extracted as a directory.
* @param sources Source files.
* @param destination Destination path inside the container.
* @param chown Set the owner user and group of the files in the container. Added in 17.09.0-ce.
*/
case class Add(sources: Seq[SourceFile], destination: String, chown: Option[String] = None) extends FileStagingDockerfileInstruction {
def dockerInstruction(sources: Seq[String]) = AddRaw(sources, destination, chown)
}
object AddRaw {
def apply(source: String, destination: String): AddRaw = AddRaw(Seq(source), destination)
}
/**
* Adds files from the staging directory to the image.
* If a source file is a tar file it will be extracted as a directory.
* @param sources Source path inside the staging directory.
* @param destination Destination path inside the container.
* @param chown Set the owner user and group of the files in the container. Added in 17.09.0-ce.
*/
case class AddRaw(sources: Seq[String], destination: String, chown: Option[String] = None) extends ProductDockerfileInstruction {
override def instructionName = "ADD"
override def arguments = {
val paths = sources.mkString(" ") + " " + destination
chown match {
case Some(chown) =>
s"--chown=$chown $paths"
case None =>
paths
}
}
}
object Copy {
def apply(source: SourceFile, destination: String): Copy = Copy(Seq(source), destination)
}
/**
* Adds files to the image.
* @param sources Source files. Cannot be empty.
* @param destination Destination path inside the container.
* @param chown Set the owner user and group of the files in the container. Added in 17.09.0-ce.
*/
case class Copy(sources: Seq[SourceFile], destination: String, chown: Option[String] = None) extends FileStagingDockerfileInstruction {
def dockerInstruction(sources: Seq[String]) = CopyRaw(sources, destination, chown)
}
object CopyRaw {
def apply(source: String, destination: String): CopyRaw = CopyRaw(Seq(source), destination)
}
/**
* Adds files from the staging directory to the image.
* @param sources Source path inside the staging directory. Cannot be empty.
* @param destination Destination path inside the container.
* @param chown Set the owner user and group of the files in the container. Added in 17.09.0-ce.
*/
case class CopyRaw(sources: Seq[String], destination: String, chown: Option[String] = None) extends ProductDockerfileInstruction {
override def instructionName = "COPY"
override def arguments = {
val paths = sources.mkString(" ") + " " + destination
chown match {
case Some(chown) =>
s"--chown=$chown $paths"
case None =>
paths
}
}
}
object Volume {
def apply(path: String): Volume = Volume(Seq(path))
}
/**
* Add mount points inside the container.
* @param paths Paths inside the container. Cannot be empty.
*/
case class Volume(paths: Seq[String]) extends ProductDockerfileInstruction {
require(paths.nonEmpty, "Must have at least one volume path")
override def arguments = jsonArrayString(paths)
}
/**
* Sets the user inside the container that should be used for the next instructions.
* @param username Name of the user.
*/
case class User(username: String) extends ProductDockerfileInstruction
/**
* Sets the current working directory for the instructions that follow.
* @param path Path inside the container.
*/
case class WorkDir(path: String) extends ProductDockerfileInstruction
/**
* Adds a trigger of a instruction that will be run when the image is used as a base image (`FROM`).
* @param instruction Instruction to run.
*/
case class OnBuild(instruction: DockerfileInstruction) extends ProductDockerfileInstruction
object StageFiles {
def apply(source: SourceFile, destination: String): StageFiles = StageFiles(Seq(source), destination)
}
/**
* Stages files. Only adds files and directories to the staging directory, will not yield an
* instruction in the Dockerfile.
* To use the staged files use the [[sbtdocker.Instructions.AddRaw]] and [[sbtdocker.Instructions.CopyRaw]] instructions.
* @param sources Source files.
* @param destination Destination path.
*/
case class StageFiles(sources: Seq[SourceFile], destination: String) extends FileStagingInstruction
object HealthCheck {
/**
* Command to execute for health check.
* @param commands Command list.
*/
def exec(
commands: Seq[String],
interval: Option[FiniteDuration],
timeout: Option[FiniteDuration],
startPeriod: Option[FiniteDuration],
retries: Option[Int]
) = HealthCheck(
command = jsonArrayString(commands),
interval = interval,
timeout = timeout,
startPeriod = startPeriod,
retries = retries
)
/**
* Command to execute through a shell (`/bin/sh`) for health check.
* @param commands Command list.
*/
def shell(
commands: Seq[String],
interval: Option[FiniteDuration],
timeout: Option[FiniteDuration],
startPeriod: Option[FiniteDuration],
retries: Option[Int]
) = HealthCheck(
command = shellCommandString(commands),
interval = interval,
timeout = timeout,
startPeriod = startPeriod,
retries = retries
)
def none = HealthCheckNone
}
/**
* Defines health check command to run inside the container to check that it is still working.
* @param command Command to execute for health check
* @param interval The health check will first run interval seconds after the container is started,
* and then again interval seconds after each previous check completes.
* @param timeout If a single run of the check takes longer than specified then the check is considered to have failed.
* @param startPeriod Provides initialization time for containers that need time to bootstrap.
* @param retries How many consecutive failures of the health check for the container to be considered unhealthy.
*/
case class HealthCheck(
command: String,
interval: Option[FiniteDuration] = None,
timeout: Option[FiniteDuration] = None,
startPeriod: Option[FiniteDuration] = None,
retries: Option[Int] = None
) extends DockerfileInstruction {
override def instructionName = "HEALTHCHECK"
override def arguments: String = Seq(
interval.map(x => s"--interval=${x.toSeconds}s"),
timeout.map(x => s"--timeout=${x.toSeconds}s"),
startPeriod.map(x => s"--start-period=${x.toSeconds}s"),
retries.map(x => s"--retries=$x"),
Some(s"CMD $command")
).flatten.mkString(" ")
}
/**
* Disable any health check inherited from the base image.
*/
case object HealthCheckNone extends DockerfileInstruction {
override def instructionName = "HEALTHCHECK"
override def arguments = "NONE"
}
/**
* This class allows the user to specify a raw Dockerfile instruction.
* Example:
* {{{
* Raw("RUN", "echo '123'")
* // Will yield the following string in the Dockerfile:
* "RUN echo '123'"
* }}}
* @param instructionName Name of the instruction.
* @param arguments Argument string.
*/
case class Raw(instructionName: String, arguments: String) extends DockerfileInstruction
}
private[sbtdocker] object InstructionUtils {
def escapeVariable(value: String) = {
val escapedValue = value.replace("\"", "\\\"")
'"' + escapedValue + '"'
}
def shellCommandString(commands: Seq[String]) = {
def escape(str: String) = {
str.replace(" ", """\ """)
.replaceAll("\\t", """\\t""")
.replaceAll("\\n", """\\n""")
.replace("\"", "\\\"")
}
commands.map(escape).mkString(" ")
}
def jsonArrayString(args: Seq[String]) = args.map(StringEscapeUtils.escapeJson).mkString("[\"", "\", \"", "\"]")
}