/
rename.go
129 lines (111 loc) Β· 4.5 KB
/
rename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package operations
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"github.com/aws/eks-anywhere/release/cli/pkg/constants"
releasetypes "github.com/aws/eks-anywhere/release/cli/pkg/types"
)
func RenameArtifacts(ctx context.Context, r *releasetypes.ReleaseConfig, eksaArtifacts releasetypes.ArtifactsTable) error {
fmt.Println("\n==========================================================")
fmt.Println(" Artifacts Rename")
fmt.Println("==========================================================")
errGroup, ctx := errgroup.WithContext(ctx)
eksaArtifacts.Range(func(k, v interface{}) bool {
artifacts := v.([]releasetypes.Artifact)
for _, artifact := range artifacts {
r, artifact := r, artifact
errGroup.Go(func() error {
// Change the name of the archive along with the checksum files
if artifact.Archive != nil {
return handleArchiveRename(ctx, r, artifact)
}
// Override images in the manifest with release URIs
if artifact.Manifest != nil {
return handleManifestRename(ctx, r, artifact)
}
return nil
})
}
return true
})
if err := errGroup.Wait(); err != nil {
return fmt.Errorf("renaming artifacts: %v", err)
}
fmt.Printf("%s Successfully renamed artifacts\n", constants.SuccessIcon)
return nil
}
func handleArchiveRename(_ context.Context, r *releasetypes.ReleaseConfig, artifact releasetypes.Artifact) error {
if r.DryRun && artifact.Archive.ImageFormat != "tarball" {
fmt.Println("Skipping OS image renames in dry-run mode")
} else {
archiveArtifact := artifact.Archive
oldArtifactFile := filepath.Join(archiveArtifact.ArtifactPath, archiveArtifact.SourceS3Key)
newArtifactFile := filepath.Join(archiveArtifact.ArtifactPath, archiveArtifact.ReleaseName)
fmt.Printf("Renaming archive - %s\n", newArtifactFile)
err := os.Rename(oldArtifactFile, newArtifactFile)
if err != nil {
return errors.Cause(err)
}
// Change the names of the checksum files
checksumExtensions := []string{".sha256", ".sha512"}
// Adding a special case for tinkerbell/hook project.
// The project builds linux kernel files that are not stored as tarballs and currently do not have SHA checksums.
// TODO(pokearu): Add logic to generate SHA for hook project
if artifact.Archive.ProjectPath == constants.HookProjectPath {
checksumExtensions = []string{}
}
for _, extension := range checksumExtensions {
oldChecksumFile := oldArtifactFile + extension
newChecksumFile := newArtifactFile + extension
fmt.Printf("Renaming checksum file - %s\n", newChecksumFile)
err = os.Rename(oldChecksumFile, newChecksumFile)
if err != nil {
return errors.Cause(err)
}
}
}
return nil
}
func handleManifestRename(_ context.Context, r *releasetypes.ReleaseConfig, artifact releasetypes.Artifact) error {
manifestArtifact := artifact.Manifest
oldArtifactFile := filepath.Join(manifestArtifact.ArtifactPath, manifestArtifact.SourceS3Key)
newArtifactFile := filepath.Join(manifestArtifact.ArtifactPath, manifestArtifact.ReleaseName)
fmt.Printf("Renaming manifest - %s\n", newArtifactFile)
err := os.Rename(oldArtifactFile, newArtifactFile)
if err != nil {
return errors.Cause(err)
}
for _, imageTagOverride := range manifestArtifact.ImageTagOverrides {
manifestFileContents, err := os.ReadFile(newArtifactFile)
if err != nil {
return errors.Cause(err)
}
regex := fmt.Sprintf("%s/.*%s.*", r.SourceContainerRegistry, imageTagOverride.Repository)
compiledRegex := regexp.MustCompile(regex)
fmt.Printf("Overriding image to %s in manifest %s\n", imageTagOverride.ReleaseUri, newArtifactFile)
updatedManifestFileContents := compiledRegex.ReplaceAllString(string(manifestFileContents), imageTagOverride.ReleaseUri)
err = os.WriteFile(newArtifactFile, []byte(updatedManifestFileContents), 0o644)
if err != nil {
return errors.Cause(err)
}
}
return nil
}