-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[system/container] Add container sub-package with methods for checkin…
…g container engine info
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Package container provides methods for checking container engine info | ||
package container | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
const ( | ||
DOCKER = "docker" // Docker | ||
PODMAN = "podman" // Podman | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var dockerEnv = "/.dockerenv" | ||
var podmanEnv = "/run/.containerenv" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// GetEngine returns container engine name if used | ||
func GetEngine() string { | ||
switch { | ||
case isFileExist(dockerEnv): | ||
return DOCKER | ||
case isFileExist(podmanEnv): | ||
return PODMAN | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// isFileExist returns true if given file exist | ||
func isFileExist(file string) bool { | ||
_, err := os.Stat(file) | ||
return !os.IsNotExist(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package container | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
. "github.com/essentialkaos/check" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type ContainerSuite struct{} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var _ = Suite(&ContainerSuite{}) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func (s *ContainerSuite) TestGetEngine(c *C) { | ||
testEnv := c.MkDir() + "/test" | ||
|
||
dockerEnv = "/_unknown_" | ||
podmanEnv = "/_unknown_" | ||
|
||
c.Assert(GetEngine(), Equals, "") | ||
|
||
os.WriteFile(testEnv, []byte("TEST"), 0644) | ||
|
||
dockerEnv = testEnv | ||
podmanEnv = "/_unknown_" | ||
|
||
c.Assert(GetEngine(), Equals, DOCKER) | ||
|
||
dockerEnv = "/_unknown_" | ||
podmanEnv = testEnv | ||
|
||
c.Assert(GetEngine(), Equals, PODMAN) | ||
} |