In buildconfig, detect current branch and it's remote url as defaults.

This commit is contained in:
Ratnadeep Debnath 2016-12-01 16:52:31 +05:30
parent 875c7d95dc
commit a1797a8d07
3 changed files with 44 additions and 8 deletions

View File

@ -152,7 +152,7 @@ func ConvertOpenShiftCommand() cli.Command {
},
cli.StringFlag{
Name: "build-branch",
Value: "master",
Value: "",
Usage: "Specify repository branch to use for buildconfig (default master)",
EnvVar: "BUILDBRANCH",
},

View File

@ -113,8 +113,8 @@ Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasAvailableLocalFlags}}
Resource Flags:
--build-branch Specify repository branch to use for buildconfig (default master)
--build-repo Specify source repository for buildconfig (default remote origin)
--build-branch Specify repository branch to use for buildconfig (default is current branch name)
--build-repo Specify source repository for buildconfig (default is current branch's remote url
-c, --chart Create a Helm chart for converted objects
--daemon-set Generate a Kubernetes daemonset object
-d, --deployment Generate a Kubernetes deployment object

View File

@ -100,6 +100,29 @@ func getGitRemote(composeFileDir string, remote string) (string, error) {
return url, nil
}
// getGitCurrentBranch gets current git branch name for the current git repo
func getGitCurrentBranch(composeFileDir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = composeFileDir
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimRight(string(out), "\n"), nil
}
// getGitRemoteForBranch gets git remote for a branch
func getGitRemoteForBranch(composeFileDir string, branch string) (string, error) {
cmd := exec.Command("sh", "-c", fmt.Sprintf("git branch -r | grep %s", branch))
cmd.Dir = composeFileDir
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.Split(strings.Trim(string(out), "\n "), "/")[0], nil
}
// getComposeFileDir returns compose file directory
func getComposeFileDir(inputFile string) (string, error) {
if strings.Index(inputFile, "/") != 0 {
@ -302,6 +325,7 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
var composeFileDir string
hasBuild := false
buildRepo := opt.BuildRepo
buildBranch := opt.BuildBranch
for name, service := range komposeObject.ServiceConfigs {
var objects []runtime.Object
@ -326,14 +350,26 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
logrus.Warningf("Error in detecting compose file's directory.")
continue
}
if !hasGitBinary() && (buildRepo == "" || buildBranch == "") {
logrus.Fatalf("Git is not installed! Please install Git to create buildconfig, else supply source repository to use for build using '--build-repo' option.")
}
if buildBranch == "" {
buildBranch, err = getGitCurrentBranch(composeFileDir)
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because current git branch couldn't be detected.")
}
}
if opt.BuildRepo == "" {
if hasGitBinary() {
buildRepo, err = getGitRemote(composeFileDir, "origin")
var buildRemote string
buildRemote, err = getGitRemoteForBranch(composeFileDir, buildBranch)
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because remote for current git branch couldn't be detected.")
}
buildRepo, err = getGitRemote(composeFileDir, buildRemote)
if err != nil {
logrus.Fatalf("Buildconfig cannot be created because git remote origin repo couldn't be detected.")
}
}
}
hasBuild = true
}
objects = append(objects, initBuildConfig(name, service, opt.InputFile, buildRepo, opt.BuildBranch)) // Openshift BuildConfigs