Clarify process CPU and improve Docker actions
This commit is contained in:
@@ -63,10 +63,10 @@ func (s *Store) QueueAgentAction(agentID, action, target string) (AgentAction, e
|
||||
}
|
||||
}
|
||||
}
|
||||
case "docker-restart", "docker-check", "docker-update":
|
||||
case "docker-restart", "docker-check", "docker-update", "docker-remove":
|
||||
if safeContainerName.MatchString(target) {
|
||||
for _, container := range report.Containers {
|
||||
if container.ID == target || container.Name == target {
|
||||
if (container.ID == target || container.Name == target) && (action != "docker-remove" || container.State != "running") {
|
||||
valid = true
|
||||
break
|
||||
}
|
||||
@@ -168,6 +168,8 @@ func executeAgentCommand(command AgentCommand) AgentActionResult {
|
||||
output = updateAgentContainer(command.Target, true)
|
||||
case "docker-check":
|
||||
output = updateAgentContainer(command.Target, false)
|
||||
case "docker-remove":
|
||||
output = removeAgentContainer(command.Target)
|
||||
case "system-update":
|
||||
output = updateAgentSystem()
|
||||
case "updates-refresh":
|
||||
@@ -189,6 +191,24 @@ func executeAgentCommand(command AgentCommand) AgentActionResult {
|
||||
return result
|
||||
}
|
||||
|
||||
func removeAgentContainer(target string) string {
|
||||
transport := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, "unix", "/var/run/docker.sock")
|
||||
}}
|
||||
defer transport.CloseIdleConnections()
|
||||
request, _ := http.NewRequest(http.MethodDelete, "http://docker/containers/"+target+"?v=false&force=false", nil)
|
||||
response, err := (&http.Client{Transport: transport, Timeout: 20 * time.Second}).Do(request)
|
||||
if err != nil {
|
||||
return "ERROR: " + err.Error()
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusNoContent {
|
||||
message, _ := io.ReadAll(io.LimitReader(response.Body, 2048))
|
||||
return fmt.Sprintf("ERROR: Docker вернул %s: %s", response.Status, strings.TrimSpace(string(message)))
|
||||
}
|
||||
return "Остановленный контейнер удалён; volumes сохранены"
|
||||
}
|
||||
|
||||
func updateAgentSystem() string {
|
||||
if output := runAgentCombinedCommand(3*time.Minute, "apt-get", "update"); strings.HasPrefix(output, "ERROR: ") {
|
||||
return output
|
||||
|
||||
Reference in New Issue
Block a user