prune.R 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #' Remove Docker Containers, Images, and Networks
  2. #'
  3. #' This function removes stopped containers, all networks
  4. #' not used by at least one container, all dangling images,
  5. #' and all build cache.
  6. #'
  7. #' @param volumes Logical. Should we prune volumes? Default is \code{FALSE}.
  8. #'
  9. #' @export prune_all_auto
  10. #'
  11. #' @return prune results
  12. #'
  13. #' @references \url{https://docs.docker.com/engine/admin/pruning/}
  14. #'
  15. #' @examples
  16. #' \dontrun{
  17. #' prune_all_auto()}
  18. prune_all_auto = function(volumes = FALSE) {
  19. cat('Cleaning up everything...\n')
  20. if (volumes) system('docker system prune --volumes --force') else
  21. system('docker system prune --force')
  22. }
  23. #' Remove Dangling Docker Containers
  24. #'
  25. #' This function prunes all dangling Docker containers automatically.
  26. #'
  27. #' @export prune_container_auto
  28. #'
  29. #' @return prune results
  30. #'
  31. #' @references \url{https://docs.docker.com/engine/admin/pruning/}
  32. #'
  33. #' @examples
  34. #' \dontrun{
  35. #' prune_container_auto()}
  36. prune_container_auto = function() {
  37. cat('Cleaning up dangling containers...\n')
  38. system('docker container prune --force')
  39. }
  40. #' Remove Specific Docker Containers
  41. #'
  42. #' This function stops and removes the Docker containers used
  43. #' for rendering the R Markdown document based on the output
  44. #' YAML file from the (possibly unsuccessful) rendering process.
  45. #'
  46. #' @param input_yml The YAML file path (output of \code{\link{render_docker}})
  47. #' when \code{prune_info = TRUE} which stores the information of the Docker
  48. #' container to be stopped and removed. If not specified, will prune all
  49. #' dangling containers.
  50. #'
  51. #' @importFrom yaml yaml.load_file
  52. #'
  53. #' @export prune_container
  54. #'
  55. #' @return prune results
  56. #'
  57. #' @examples
  58. #' \dontrun{
  59. #' prune_container()}
  60. prune_container = function(input_yml) {
  61. if (!file.exists(normalizePath(input_yml)))
  62. stop('input file does not exist')
  63. lst = yaml.load_file(normalizePath(input_yml))
  64. container_name = lst$'container_name'
  65. # TODO: needs exception handling
  66. cat('Cleaning up dangling containers...\n')
  67. system(paste0("docker stop \"", container_name, "\""))
  68. system(paste0("docker rm -f \"", container_name, "\""))
  69. }
  70. #' @rdname prune_container
  71. #' @export purge_container
  72. purge_container = function() {
  73. .Deprecated('prune_container')
  74. }
  75. #' Remove Dangling Docker Images
  76. #'
  77. #' This function prunes all dangling Docker images automatically.
  78. #'
  79. #' @export prune_image_auto
  80. #'
  81. #' @return prune results
  82. #'
  83. #' @references \url{https://docs.docker.com/engine/admin/pruning/}
  84. #'
  85. #' @examples
  86. #' \dontrun{
  87. #' prune_image_auto()}
  88. prune_image_auto = function() {
  89. cat('Cleaning up dangling images...\n')
  90. system('docker image prune --force')
  91. }
  92. #' Remove Specific Docker Images
  93. #'
  94. #' This function removes the Docker images used
  95. #' for rendering the R Markdown document based on the output
  96. #' YAML file from the (possibly unsuccessful) rendering process.
  97. #'
  98. #' @param input_yml The YAML file path (output of \code{\link{render_docker}})
  99. #' when \code{prune_info = TRUE} which stores the information of the
  100. #' Docker image to be removed. If not specified, will prune all
  101. #' dangling images.
  102. #'
  103. #' @importFrom yaml yaml.load_file
  104. #'
  105. #' @export prune_image
  106. #'
  107. #' @return prune results
  108. #'
  109. #' @examples
  110. #' \dontrun{
  111. #' prune_image()}
  112. prune_image = function(input_yml) {
  113. if (!file.exists(normalizePath(input_yml)))
  114. stop('input file does not exist')
  115. lst = yaml.load_file(normalizePath(input_yml))
  116. image_name = lst$'image_name'
  117. # TODO: needs exception handling
  118. cat('Cleaning up dangling images...\n')
  119. system(paste0("docker rmi -f \"", image_name, "\""))
  120. }
  121. #' @rdname prune_image
  122. #' @export purge_image
  123. purge_image = function() {
  124. .Deprecated('prune_image')
  125. }