-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add netconf support * fix doc descriptions * add netconf support * fix issue * move all docker calls in one new package * fix image name * change version display on main page * change version before merging --------- Co-authored-by: roysoldier <[email protected]>
- Loading branch information
Showing
5 changed files
with
130 additions
and
92 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
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
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,104 @@ | ||
package container | ||
|
||
import ( | ||
"context" | ||
"jtso/logger" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/client" | ||
) | ||
|
||
func ListContainers() []types.Container { | ||
// Open Docker API | ||
var containers []types.Container | ||
containers = make([]types.Container, 0) | ||
|
||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to open Docker session: %v", err) | ||
} | ||
defer cli.Close() | ||
|
||
containers, err = cli.ContainerList(context.Background(), types.ContainerListOptions{}) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to list container state: %v", err) | ||
} | ||
logger.Log.Info(" List of containers has been retrieved") | ||
return containers | ||
} | ||
|
||
func RestartContainer(name string) { | ||
timeout := 30 | ||
|
||
// Open Docker API | ||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to open Docker session: %v", err) | ||
return | ||
} | ||
defer cli.Close() | ||
|
||
// Restart container | ||
err = cli.ContainerRestart(context.Background(), name, container.StopOptions{Signal: "SIGTERM", Timeout: &timeout}) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to restart %s container: %v", name, err) | ||
return | ||
} | ||
logger.Log.Infof("%s container has been restarted", name) | ||
|
||
} | ||
|
||
func StopContainer(name string) { | ||
timeout := 30 | ||
|
||
// Open Docker API | ||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to open Docker session: %v", err) | ||
return | ||
} | ||
defer cli.Close() | ||
|
||
err = cli.ContainerStop(context.Background(), name, container.StopOptions{Signal: "SIGTERM", Timeout: &timeout}) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to stop %s container: %v", name, err) | ||
return | ||
} | ||
logger.Log.Infof("%s container has been stopped - no more router attached", name) | ||
|
||
} | ||
|
||
func GetVersionLabel(names []string) string { | ||
|
||
// Open Docker API | ||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to open Docker session: %v", err) | ||
return "N/A" | ||
} | ||
defer cli.Close() | ||
|
||
version := "" | ||
for _, name := range names { | ||
// Get the image details using the Docker API | ||
imageInspect, _, err := cli.ImageInspectWithRaw(context.Background(), "compose-"+name) | ||
if err != nil { | ||
logger.Log.Errorf("Unable to retrieve Docker %s inspect data: %v", "compose-"+name, err) | ||
version += name + "(N/A) " | ||
continue | ||
} | ||
|
||
// Extract the version label from imageInspect.Config.Labels | ||
vers, ok := imageInspect.Config.Labels["version"] | ||
if !ok { | ||
logger.Log.Errorf("Unable to retrieve Docker %s version", name) | ||
version += name + "(N/A) " | ||
continue | ||
} | ||
version += name + "(" + vers + ") " | ||
logger.Log.Infof("%s container version is %s", name, version) | ||
} | ||
return version | ||
|
||
} |
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
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