Răsfoiți Sursa

add stop container func

reid 2 ani în urmă
părinte
comite
1bd21f4e12
2 a modificat fișierele cu 29 adăugiri și 4 ștergeri
  1. 29 2
      docker/docker.go
  2. 0 2
      docker/urbit.go

+ 29 - 2
docker/docker.go

@@ -116,7 +116,7 @@ func StartContainer(containerName string, containerType string) error {
 		return err
 	}
 	// get the desired tag and hash from config
-	containerInfo, err := getCurrentContainerInfo(containerType)
+	containerInfo, err := getLatestContainerInfo(containerType)
 	if err != nil {
 		return err
 	}
@@ -223,7 +223,7 @@ func StartContainer(containerName string, containerType string) error {
 
 // convert the version info back into json then a map lol
 // so we can easily get the correct repo/release channel/tag/hash
-func getCurrentContainerInfo(containerType string) (map[string]string, error) {
+func getLatestContainerInfo(containerType string) (map[string]string, error) {
 	var res map[string]string
 	conf := config.Conf()
 	releaseChannel := conf.UpdateBranch
@@ -270,3 +270,30 @@ func getCurrentContainerInfo(containerType string) (map[string]string, error) {
 	res["repo"] = repo
 	return res, nil
 }
+
+// stop a container with the name
+func StopContainerByName(containerName string) error {
+	ctx := context.Background()
+	cli, err := client.NewClientWithOpts(client.FromEnv)
+	if err != nil {
+		return err
+	}
+	// fetch all containers incl stopped
+	containers, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true})
+	if err != nil {
+		return err
+	}
+	for _, container := range containers {
+		for _, name := range container.Names {
+			if name == "/"+containerName {
+				// Stop the container
+				if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
+					return fmt.Errorf("failed to stop container %s: %v", containerName, err)
+				}
+				logger.Info(fmt.Printf("Successfully stopped container %s\n", containerName))
+				return nil
+			}
+		}
+	}
+	return fmt.Errorf("container with name %s not found", containerName)
+}

+ 0 - 2
docker/urbit.go

@@ -27,8 +27,6 @@ func LoadUrbits() error {
 			continue
 		}
 	}
-	// apply latest version info (if automated updates)
-	// start containers
 	return nil
 }