Skip to content

Commit

Permalink
MGMT-13454: Group Host and boot logs to a single tarball
Browse files Browse the repository at this point in the history
  • Loading branch information
Nir Magnezi committed Feb 9, 2023
1 parent ef2761d commit e73267b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
25 changes: 15 additions & 10 deletions internal/bminventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3565,35 +3565,32 @@ func (b *bareMetalInventory) DownloadMinimalInitrd(ctx context.Context, params i
func (b *bareMetalInventory) getLogFileForDownload(ctx context.Context, clusterId *strfmt.UUID, hostId *strfmt.UUID, logsType string) (string, string, error) {
var fileName string
var downloadFileName string
var hostObject *common.Host

c, err := b.getCluster(ctx, clusterId.String(), common.UseEagerLoading, common.IncludeDeletedRecords)
if err != nil {
return "", "", err
}
b.log.Debugf("log type to download: %s", logsType)
switch logsType {
case string(models.LogsTypeHost), string(models.LogsTypeNodeBoot):
case string(models.LogsTypeHost):
if hostId == nil {
return "", "", common.NewApiError(http.StatusBadRequest, errors.Errorf("Host ID must be provided for downloading host logs"))
}

var hostObject *common.Host
hostObject, err = common.GetClusterHostFromDB(b.db, clusterId.String(), hostId.String())
if err != nil {
return "", "", err
}
if time.Time(hostObject.LogsCollectedAt).Equal(time.Time{}) {
return "", "", common.NewApiError(http.StatusConflict, errors.Errorf("Logs for host %s were not found", hostId))
}
fileName = b.getLogsFullName(logsType, clusterId.String(), hostObject.ID.String())
role := string(hostObject.Role)
if hostObject.Bootstrap {
role = string(models.HostRoleBootstrap)
}
name := sanitize.Name(hostutil.GetHostnameForMsg(&hostObject.Host))
if logsType == string(models.LogsTypeNodeBoot) {
name = fmt.Sprintf("boot_%s", name)
}
downloadFileName = fmt.Sprintf("%s_%s_%s.tar.gz", sanitize.Name(c.Name), role, name)
fileName, err = b.preparHostLogs(ctx, c, hostObject)
if err != nil {
return "", "", err
}
case string(models.LogsTypeController):
if time.Time(c.Cluster.ControllerLogsCollectedAt).Equal(time.Time{}) {
return "", "", common.NewApiError(http.StatusConflict, errors.Errorf("Controller Logs for cluster %s were not found", clusterId))
Expand Down Expand Up @@ -3987,6 +3984,14 @@ func (b *bareMetalInventory) prepareClusterLogs(ctx context.Context, cluster *co
return fileName, nil
}

func (b *bareMetalInventory) preparHostLogs(ctx context.Context, cluster *common.Cluster, host *common.Host) (string, error) {
fileName, err := b.clusterApi.PrepareHostLogFile(ctx, cluster, host, b.objectHandler)
if err != nil {
return "", err
}
return fileName, nil
}

func (b *bareMetalInventory) getLogsFullName(logType string, clusterId string, logId string) string {
filename := "logs.tar.gz"
if logType == string(models.LogsTypeNodeBoot) {
Expand Down
45 changes: 45 additions & 0 deletions internal/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type API interface {
HandlePreInstallSuccess(ctx context.Context, c *common.Cluster)
SetVipsData(ctx context.Context, c *common.Cluster, apiVip, ingressVip, apiVipLease, ingressVipLease string, db *gorm.DB) error
IsReadyForInstallation(c *common.Cluster) (bool, string)
PrepareHostLogFile(ctx context.Context, c *common.Cluster, host *common.Host, objectHandler s3wrapper.API) (string, error)
PrepareClusterLogFile(ctx context.Context, c *common.Cluster, objectHandler s3wrapper.API) (string, error)
SetUploadControllerLogsAt(ctx context.Context, c *common.Cluster, db *gorm.DB) error
SetConnectivityMajorityGroupsForCluster(clusterID strfmt.UUID, db *gorm.DB) error
Expand Down Expand Up @@ -1031,6 +1032,50 @@ func (m *Manager) createClusterDataFiles(ctx context.Context, c *common.Cluster,
_ = m.uploadDataAsFile(ctx, log, events, fileName, objectHandler)
}
}
func (m *Manager) PrepareHostLogFile(ctx context.Context, c *common.Cluster, host *common.Host, objectHandler s3wrapper.API) (string, error) {
var (
fileName string
tarredFilename string
tarredFilenames []string
)
log := logutil.FromContext(ctx, m.log)

files, err := objectHandler.ListObjectsByPrefix(ctx, fmt.Sprintf("%s/logs/%s", c.ID, host.ID))
if err != nil {
return "", common.NewApiError(http.StatusNotFound, err)
}

role := string(host.Role)
if host.Bootstrap {
role = string(models.HostRoleBootstrap)
}

fileName = fmt.Sprintf("%s_%s_%s.tar.gz", sanitize.Name(c.Name), role, sanitize.Name(hostutil.GetHostnameForMsg(&host.Host)))
files = funk.Filter(files, func(x string) bool { return x != fileName }).([]string)

for _, file := range files {
name := fmt.Sprintf("logs_%s", sanitize.Name(hostutil.GetHostnameForMsg(&host.Host)))

if strings.Contains(file, "boot_") {
name = fmt.Sprintf("boot_%s", name)
}
tarredFilename = fmt.Sprintf("%s_%s_%s.tar.gz", sanitize.Name(c.Name), role, name)
tarredFilenames = append(tarredFilenames, tarredFilename)
}

if len(files) < 1 {
return "", common.NewApiError(http.StatusNotFound,
errors.Errorf("Logs for host %s were not found", host.ID))
}

log.Debugf("List of files to include into %s is %s", fileName, files)
err = s3wrapper.TarAwsFiles(ctx, fileName, files, tarredFilenames, objectHandler, log)
if err != nil {
log.WithError(err).Errorf("failed to download file %s", fileName)
return "", common.NewApiError(http.StatusInternalServerError, err)
}
return fileName, nil
}

func (m *Manager) PrepareClusterLogFile(ctx context.Context, c *common.Cluster, objectHandler s3wrapper.API) (string, error) {
log := logutil.FromContext(ctx, m.log)
Expand Down
15 changes: 15 additions & 0 deletions internal/cluster/mock_cluster_api.go

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

2 changes: 0 additions & 2 deletions restapi/embedded_spec.go

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

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

2 changes: 1 addition & 1 deletion swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,7 @@ paths:
name: logs_type
description: The type of logs to be downloaded.
type: string
enum: ['host', 'controller', 'node-boot', 'all']
enum: ['host', 'controller', 'all']
required: false
- in: query
name: host_id
Expand Down

0 comments on commit e73267b

Please sign in to comment.