Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MGMT-9451: Moving to new service-agent protocol #76

Open
wants to merge 17 commits into
base: igal/new_agent_protocol
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 30 additions & 63 deletions internal/host/hostcommands/logs_cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package hostcommands

import (
"bytes"
"context"
"encoding/json"
"net"
"strconv"
"strings"
"text/template"
"time"

"github.com/go-openapi/swag"
Expand All @@ -32,87 +29,57 @@ func NewLogsCmd(log logrus.FieldLogger, db *gorm.DB, instructionConfig Instructi
}
}

func (i *logsCmd) GetSteps(ctx context.Context, host *models.Host) ([]*models.Step, error) {
// added to run upload logs if install command fails
if !time.Time(host.LogsCollectedAt).Equal(time.Time{}) {
return nil, nil
}

func (i *logsCmd) prepareParam(ctx context.Context, host *models.Host) (string, error) {
var mastersIPs []string
var err error
if host.Bootstrap {
mastersIPs, err = i.getNonBootstrapMastersIPsInHostCluster(ctx, host)
if err != nil {
i.log.WithError(err).Errorf("Failed to get non-bootstrap masters IPs from cluster %s", host.ClusterID)
return nil, err
return "", err
}
}

logsCommand, err := i.createUploadLogsCmd(host, i.instructionConfig.ServiceBaseURL,
i.instructionConfig.AgentImage, strings.Join(mastersIPs, ","),
i.instructionConfig.SkipCertVerification, false, true)
if err != nil {
return nil, err
}
logsCommandAsArgs := strings.Fields(logsCommand)
step := &models.Step{
StepType: models.StepTypeExecute,
Command: logsCommandAsArgs[0],
Args: logsCommandAsArgs[1:],
}

return []*models.Step{step}, nil
}

func (i *logsCmd) createUploadLogsCmd(host *models.Host, baseURL, agentImage, mastersIPs string, skipCertVerification, preservePreviousCommandReturnCode,
withInstallerGatherLogging bool) (string, error) {

cmdArgsTmpl := ""
if preservePreviousCommandReturnCode {
cmdArgsTmpl = "( returnCode=$?; "
request := models.LogsGatherCmdRequest{
BaseURL: swag.String(strings.TrimSpace(i.instructionConfig.ServiceBaseURL)),
ClusterID: host.ClusterID,
HostID: host.ID,
InfraEnvID: &host.InfraEnvID,
Insecure: swag.Bool(i.instructionConfig.SkipCertVerification),
Bootstrap: swag.Bool(host.Bootstrap),
InstallerGather: true,
MasterIps: mastersIPs,
}

data := map[string]string{
"BASE_URL": strings.TrimSpace(baseURL),
"CLUSTER_ID": host.ClusterID.String(),
"HOST_ID": host.ID.String(),
"INFRA_ENV_ID": host.InfraEnvID.String(),
"AGENT_IMAGE": strings.TrimSpace(agentImage),
"SKIP_CERT_VERIFICATION": strconv.FormatBool(skipCertVerification),
"BOOTSTRAP": strconv.FormatBool(host.Bootstrap),
"INSTALLER_GATHER": strconv.FormatBool(withInstallerGatherLogging),
"MASTERS_IPS": mastersIPs,
if i.instructionConfig.ServiceCACertPath != "" {
request.CaCertPath = common.HostCACertPath
}

if i.instructionConfig.ServiceCACertPath != "" {
data["CACERTPATH"] = common.HostCACertPath
b, err := json.Marshal(&request)
if err != nil {
i.log.WithError(err).Warn("Json marshal")
return "", err
}
return string(b), nil
}

cmdArgsTmpl += "timeout 1h podman run --rm --privileged --net=host " +
"-v /run/systemd/journal/socket:/run/systemd/journal/socket -v /var/log:/var/log -v /etc/pki:/etc/pki " +
"{{if .CACERTPATH}} -v {{.CACERTPATH}}:{{.CACERTPATH}} {{end}}" +
"{{if eq .BOOTSTRAP `true`}} -v /root/.ssh:/root/.ssh -v /tmp:/tmp {{end}}" +
"--env PULL_SECRET_TOKEN --name logs-sender --pid=host {{.AGENT_IMAGE}} logs_sender " +
"-url {{.BASE_URL}} -cluster-id {{.CLUSTER_ID}} -host-id {{.HOST_ID}} -infra-env-id {{.INFRA_ENV_ID}} " +
"--insecure={{.SKIP_CERT_VERIFICATION}} -bootstrap={{.BOOTSTRAP}} -with-installer-gather-logging={{.INSTALLER_GATHER}}" +
"{{if .MASTERS_IPS}} -masters-ips={{.MASTERS_IPS}} {{end}}" +
"{{if .CACERTPATH}} --cacert {{.CACERTPATH}} {{end}}"

if preservePreviousCommandReturnCode {
cmdArgsTmpl = cmdArgsTmpl + "; exit $returnCode; )"
func (i *logsCmd) GetSteps(ctx context.Context, host *models.Host) ([]*models.Step, error) {
// added to run upload logs if install command fails
if !time.Time(host.LogsCollectedAt).Equal(time.Time{}) {
return nil, nil
}

t, err := template.New("cmd").Parse(cmdArgsTmpl)
logsCommandAsArgs, err := i.prepareParam(ctx, host)
if err != nil {
return "", err
return nil, err
}

buf := &bytes.Buffer{}
if err := t.Execute(buf, data); err != nil {
return "", err
step := &models.Step{
StepType: models.StepTypeLogsGather,
Command: "",
Args: []string{logsCommandAsArgs},
}

return buf.String(), nil
return []*models.Step{step}, nil
}

func (i *logsCmd) getNonBootstrapMastersIPsInHostCluster(ctx context.Context, host *models.Host) ([]string, error) {
Expand Down
10 changes: 3 additions & 7 deletions internal/host/hostcommands/stop_installation_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ func NewStopInstallationCmd(log logrus.FieldLogger) *stopInstallationCmd {
}

func (h *stopInstallationCmd) GetSteps(ctx context.Context, host *models.Host) ([]*models.Step, error) {
command := "/usr/bin/podman"

step := &models.Step{
StepType: models.StepTypeExecute,
Command: command,
Args: []string{
"stop", "-i", "-t", "5", "assisted-installer",
},
StepType: models.StepTypeStopInstallation,
Command: "",
Args: []string{},
}

return []*models.Step{step}, nil
Expand Down
215 changes: 215 additions & 0 deletions models/logs_gather_cmd_request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading