-
Notifications
You must be signed in to change notification settings - Fork 12
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
OCM-10315 : Add instance addition script #49
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f749058
Add instance addition script
tirthct 375d1d3
Update golangci lint
tirthct 74d5a8c
Fix linter
tirthct 49dc289
Move command as sync-cloud-resources
tirthct 8b7f5e2
Fixed typo and improved dry run help message
tirthct 1b583fd
Removed SilenceUsage to help text when running the command incorrectly
tirthct File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package sync_cloud_resources | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "sync-cloud-resources [branch-name] [csv-path]", | ||
Short: "Syncs cloud resources in AMS and generates quota rules for them", | ||
Long: "Syncs cloud resources in AMS and generates quota rules for them", | ||
RunE: syncCloudResources, | ||
SilenceUsage: true, | ||
Args: cobra.ExactArgs(2), | ||
} | ||
|
||
var args struct { | ||
dryRun bool | ||
} | ||
|
||
func init() { | ||
flags := Cmd.Flags() | ||
flags.BoolVar(&args.dryRun, "dry-run", true, "When true, don't actually take any actions, just print the actions that would be taken") | ||
} | ||
|
||
func syncCloudResources(cmd *cobra.Command, argv []string) error { | ||
branchName := argv[0] | ||
csvPath := argv[1] | ||
if branchName == "" { | ||
return fmt.Errorf("branch name cannot be empty") | ||
} | ||
if csvPath == "" { | ||
return fmt.Errorf("csv path cannot be empty") | ||
} | ||
cristianoveiga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fmt.Println("Validating branch name", branchName) | ||
err := CheckRefFormat(branchName) | ||
if err != nil { | ||
return fmt.Errorf("incorrect branch name: %v", err) | ||
} | ||
|
||
fmt.Println("Validating csv path", csvPath) | ||
err = CheckIfFileExists(csvPath) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while checking if the csv files exists: %v", err) | ||
} | ||
|
||
fmt.Println("Creating temporary directory") | ||
tempDir, err := os.MkdirTemp(os.TempDir(), branchName) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while creating temporary directory: %v", err) | ||
} | ||
|
||
amsUpstreamRepo := "[email protected]:service/uhc-account-manager.git" | ||
amsRepo := gitRepo{ | ||
repoUrl: amsUpstreamRepo, | ||
localPath: tempDir, | ||
} | ||
|
||
fmt.Println("Cloning AMS repo at:", tempDir) | ||
err = amsRepo.Clone() | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while cloning AMS repo: %v", err) | ||
} | ||
|
||
fmt.Println("Creating a new branch") | ||
err = amsRepo.Branch(branchName) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while creating a new branch: %v", err) | ||
} | ||
|
||
fmt.Println("Replacing cloud resources file") | ||
err = ReplaceFileContent(fmt.Sprintf("%s/config/quota-cloud-resources.csv", tempDir), csvPath) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while getting the head of the reference: %v", err) | ||
} | ||
|
||
fmt.Println("Generating quota rules") | ||
_, err = ExecuteCmd(fmt.Sprintf("cd %s && make generate-quota", tempDir)) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while generating quota rules: %v", err) | ||
} | ||
|
||
fmt.Println("Staging changes") | ||
err = amsRepo.StageAllFiles() | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while staging the files: %v", err) | ||
} | ||
|
||
fmt.Println("Committing changes") | ||
err = amsRepo.Commit(fmt.Sprintf("Syncinc cloud resources and quota rules for %s", branchName)) | ||
cristianoveiga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return fmt.Errorf("an error occurred committing the changes: %v", err) | ||
} | ||
|
||
fmt.Println("Pushing changes to remote branch") | ||
if args.dryRun { | ||
fmt.Println("DRY RUN: Would push the changes to remote branch:", branchName) | ||
return nil | ||
} | ||
err = amsRepo.Push("origin", branchName) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while pushing the changes: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func ReplaceFileContent(originalFilePath, filePathWithUpdatedText string) error { | ||
originalFile, err := os.OpenFile(originalFilePath, os.O_WRONLY|os.O_TRUNC, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
defer originalFile.Close() | ||
|
||
newFile, err := os.Open(filePathWithUpdatedText) | ||
if err != nil { | ||
return err | ||
} | ||
defer newFile.Close() | ||
_, err = io.Copy(originalFile, newFile) | ||
if err != nil { | ||
return err | ||
} | ||
err = originalFile.Sync() | ||
return err | ||
} | ||
|
||
func CheckIfFileExists(filepath string) error { | ||
if _, err := os.Stat(filepath); errors.Is(err, os.ErrNotExist) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type gitRepo struct { | ||
repoUrl string | ||
localPath string | ||
} | ||
|
||
func ExecuteCmd(command string) (string, error) { | ||
fmt.Println(command) | ||
app := "bash" | ||
arg1 := "-c" | ||
cmd := exec.Command(app, arg1, command) | ||
var stdBuffer bytes.Buffer | ||
mw := io.MultiWriter(os.Stdout, &stdBuffer) | ||
cmd.Stdout = mw | ||
cmd.Stderr = mw | ||
err := cmd.Run() | ||
if err != nil { | ||
err := fmt.Errorf("%v : %s", err, stdBuffer.String()) | ||
return "", err | ||
} | ||
return stdBuffer.String(), nil | ||
} | ||
|
||
func CheckRefFormat(branchName string) error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git check-ref-format --branch %s", branchName)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (g gitRepo) Clone() error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git clone %s %s", g.repoUrl, g.localPath)) | ||
return err | ||
} | ||
|
||
func (g gitRepo) RemoteAdd(remoteName, remoteUrl string) error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git -C %s remote add %s %s", g.localPath, remoteName, remoteUrl)) | ||
return err | ||
} | ||
|
||
func (g gitRepo) Branch(branchName string) error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git -C %s checkout -b %s", g.localPath, branchName)) | ||
return err | ||
} | ||
|
||
func (g gitRepo) StageFiles(files *[]string) error { | ||
for _, file := range *files { | ||
_, err := ExecuteCmd(fmt.Sprintf("git -C %s stage %s", g.localPath, file)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (g gitRepo) StageAllFiles() error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git -C %s stage .", g.localPath)) | ||
return err | ||
} | ||
|
||
func (g gitRepo) Commit(message string) error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git -C %s commit -m \"%s\"", g.localPath, message)) | ||
return err | ||
} | ||
|
||
func (g gitRepo) Push(remote, remoteUrl string) error { | ||
_, err := ExecuteCmd(fmt.Sprintf("git -C %s push %s %s", g.localPath, remote, remoteUrl)) | ||
return err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message is quite generic, can we improve that/make more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed