docker_util.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2enode
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/blang/semver"
  18. systemdutil "github.com/coreos/go-systemd/util"
  19. )
  20. // getDockerAPIVersion returns the Docker's API version.
  21. func getDockerAPIVersion() (semver.Version, error) {
  22. output, err := runCommand("docker", "version", "-f", "{{.Server.APIVersion}}")
  23. if err != nil {
  24. return semver.Version{}, fmt.Errorf("failed to get docker server version: %v", err)
  25. }
  26. return semver.MustParse(strings.TrimSpace(output) + ".0"), nil
  27. }
  28. // isSharedPIDNamespaceSupported returns true if the Docker version is 1.13.1+
  29. // (API version 1.26+), and false otherwise.
  30. func isSharedPIDNamespaceSupported() (bool, error) {
  31. version, err := getDockerAPIVersion()
  32. if err != nil {
  33. return false, err
  34. }
  35. return version.GTE(semver.MustParse("1.26.0")), nil
  36. }
  37. // isDockerLiveRestoreSupported returns true if live-restore is supported in
  38. // the current Docker version.
  39. func isDockerLiveRestoreSupported() (bool, error) {
  40. version, err := getDockerAPIVersion()
  41. if err != nil {
  42. return false, err
  43. }
  44. return version.GTE(semver.MustParse("1.26.0")), nil
  45. }
  46. // getDockerInfo returns the Info struct for the running Docker daemon.
  47. func getDockerInfo(key string) (string, error) {
  48. output, err := runCommand("docker", "info", "-f", "{{."+key+"}}")
  49. if err != nil {
  50. return "", fmt.Errorf("failed to get docker info: %v", err)
  51. }
  52. return strings.TrimSpace(output), nil
  53. }
  54. // isDockerLiveRestoreEnabled returns true if live-restore is enabled in the
  55. // Docker.
  56. func isDockerLiveRestoreEnabled() (bool, error) {
  57. info, err := getDockerInfo("LiveRestoreEnabled")
  58. if err != nil {
  59. return false, err
  60. }
  61. return info == "true", nil
  62. }
  63. // getDockerLoggingDriver returns the name of the logging driver.
  64. func getDockerLoggingDriver() (string, error) {
  65. info, err := getDockerInfo("LoggingDriver")
  66. if err != nil {
  67. return "", err
  68. }
  69. return info, nil
  70. }
  71. // isDockerSELinuxSupportEnabled checks whether the Docker daemon was started
  72. // with SELinux support enabled.
  73. func isDockerSELinuxSupportEnabled() (bool, error) {
  74. info, err := getDockerInfo("SecurityOptions")
  75. if err != nil {
  76. return false, err
  77. }
  78. return strings.Contains(info, "name=selinux"), nil
  79. }
  80. // startDockerDaemon starts the Docker daemon.
  81. func startDockerDaemon() error {
  82. switch {
  83. case systemdutil.IsRunningSystemd():
  84. _, err := runCommand("systemctl", "start", "docker")
  85. return err
  86. default:
  87. _, err := runCommand("service", "docker", "start")
  88. return err
  89. }
  90. }
  91. // stopDockerDaemon stops the Docker daemon.
  92. func stopDockerDaemon() error {
  93. switch {
  94. case systemdutil.IsRunningSystemd():
  95. _, err := runCommand("systemctl", "stop", "docker")
  96. return err
  97. default:
  98. _, err := runCommand("service", "docker", "stop")
  99. return err
  100. }
  101. }