You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

  1. package org.wikimedia.integration
  2. import java.net.URLEncoder
  3. import static org.wikimedia.integration.Utility.arg
  4. /**
  5. * Provides an interface to Blubber for generating Dockerfiles.
  6. */
  7. class Blubber implements Serializable {
  8. /**
  9. * Blubber config path.
  10. */
  11. final String configPath
  12. /**
  13. * Jenkins pipeline workflow script context.
  14. */
  15. final def workflowScript
  16. /**
  17. * Blubberoid base service URL.
  18. */
  19. final String blubberoidURL
  20. /**
  21. * Blubber constructor.
  22. *
  23. * @param workflowScript Jenkins workflow script context.
  24. * @param configPath Blubber config path.
  25. * @param blubberoidURL Blubberoid service URL.
  26. */
  27. Blubber(workflowScript, String configPath, String blubberoidURL) {
  28. this.workflowScript = workflowScript
  29. this.configPath = configPath
  30. this.blubberoidURL = blubberoidURL
  31. }
  32. /**
  33. * Returns a valid Dockerfile for the given variant.
  34. *
  35. * @param variant Blubber variant name.
  36. */
  37. String generateDockerfile(String variant) {
  38. def config = workflowScript.readFile(file: configPath)
  39. def headers = [[name: "content-type", value: getConfigMediaType()]]
  40. def response = workflowScript.httpRequest(url: getRequestURL(variant),
  41. httpMode: "POST",
  42. customHeaders: headers,
  43. requestBody: config,
  44. consoleLogResponseBody: true,
  45. validResponseCodes: "200")
  46. response.content
  47. }
  48. /**
  49. * Returns a request media type based on the config file extension.
  50. */
  51. String getConfigMediaType() {
  52. def ext = configPath.substring(configPath.lastIndexOf(".") + 1)
  53. switch (ext) {
  54. case ["yaml", "yml"]:
  55. return "application/yaml"
  56. default:
  57. return "application/json"
  58. }
  59. }
  60. /**
  61. * Return a request URL for the given variant.
  62. */
  63. String getRequestURL(String variant) {
  64. blubberoidURL + URLEncoder.encode(variant, "UTF-8")
  65. }
  66. }