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

Create Deploy initial commit #185

Merged
merged 1 commit into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions forjfile/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type DeploymentStruct struct {
Details *DeployForgeYaml `yaml:"define,omitempty"`
}


// MarshalYAML provides the encoding part for DeploymentStruct
//
// In short we do not want to encode forjj deployment details) info except the core.
Expand All @@ -15,11 +14,11 @@ func (d DeploymentStruct) MarshalYAML() (interface{}, error) {
}

// UpdateDeploymentCoreData set all DeploymentCore data
func (d *DeploymentStruct)UpdateDeploymentCoreData(data DeploymentCoreStruct) {
func (d *DeploymentStruct) UpdateDeploymentCoreData(data DeploymentCoreStruct) {
d.DeploymentCoreStruct = data
}

// RunInContext run GIT commands in the GIT repo context.
func (d *DeploymentStruct) RunInContext(doRun func() error) (err error) {
return d.DeploymentCoreStruct.runInContext(doRun)
}
return d.DeploymentCoreStruct.RunInContext(doRun)
}
16 changes: 8 additions & 8 deletions forjfile/deployment_core-git.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (d *DeploymentCoreStruct) GitSetRepo(aPath, origin string) (err error) {

// GitDefineRemote helps to configure a deployment repository with a remote
func (d *DeploymentCoreStruct) GitDefineRemote(name, uri string) (err error) {
return d.runInContext(func() (err error) {
return d.RunInContext(func() (err error) {
if err = git.EnsureRemoteIs(name, uri); err != nil {
return
}
Expand All @@ -44,7 +44,7 @@ func (d *DeploymentCoreStruct) GitDefineRemote(name, uri string) (err error) {

// GitSyncFrom refresh the remote, and synchronize.
func (d *DeploymentCoreStruct) GitSyncFrom(remote, branch string) error {
return d.runInContext(func() (_ error) {
return d.RunInContext(func() (_ error) {
if !git.RemoteExist(remote) {
return
}
Expand All @@ -57,7 +57,7 @@ func (d *DeploymentCoreStruct) GitSyncFrom(remote, branch string) error {

// GitSyncUp set and report sync status
func (d *DeploymentCoreStruct) GitSyncUp() error {
return d.runInContext(func() (_ error) {
return d.RunInContext(func() (_ error) {
if d.syncStatus == 0 {
return fmt.Errorf("Internal error! Unable to sync up. The synchronization was not initiliazed. You must call GitSyncFrom, Once")
}
Expand All @@ -81,7 +81,7 @@ func (d *DeploymentCoreStruct) GitSyncUp() error {
// !!! Conflict can happen !!!
//
func (d *DeploymentCoreStruct) SwitchTo(branch string) error {
return d.runInContext(func() (err error) {
return d.RunInContext(func() (err error) {
if git.GetCurrentBranch() != branch {

trackedFiles := git.GetStatus().CountTracked()
Expand All @@ -106,7 +106,7 @@ func (d *DeploymentCoreStruct) SwitchTo(branch string) error {

// GitCommit do the commit in the Deployment repository.
func (d *DeploymentCoreStruct) GitCommit(message string) (_ error) {
return d.runInContext(func() (err error) {
return d.RunInContext(func() (err error) {
status := git.GetStatus()
if status.Ready.CountFiles() > 0 {
git.Commit(message, true)
Expand All @@ -118,7 +118,7 @@ func (d *DeploymentCoreStruct) GitCommit(message string) (_ error) {
// GitPush do a git push
// depending on the previous Git SyncFrom, a push can take place
func (d *DeploymentCoreStruct) GitPush(force bool) (_ error) {
return d.runInContext(func() (err error) {
return d.RunInContext(func() (err error) {
if d.syncStatus == -2 {
return fmt.Errorf("Unable to push to an inexistent remote")
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (d *DeploymentCoreStruct) GitPush(force bool) (_ error) {
// GitResetBranchFromRemote clean current branch, check out to the requested branch and reset against remote branch.
// The reset is not made if the fetch return an error.
func (d *DeploymentCoreStruct) GitResetBranchFromRemote(branch, remote string) {
d.runInContext(func() (_ error) {
d.RunInContext(func() (_ error) {
git.Do("reset", "--hard", "HEAD")
git.Do("checkout", branch)
if git.Do("fetch", remote) == 0 {
Expand All @@ -159,7 +159,7 @@ func (d *DeploymentCoreStruct) GitResetBranchFromRemote(branch, remote string) {
// ------------------ Internal functions

// runInContext ensure GIT commands are executed in the right GIT repo context.
func (d *DeploymentCoreStruct) runInContext(doRun func() error) (err error) {
func (d *DeploymentCoreStruct) RunInContext(doRun func() error) (err error) {
if d.savedPath != "" {
return doRun()
}
Expand Down
40 changes: 36 additions & 4 deletions maintain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"forjj/creds"
"forjj/git"
"os"

"github.com/forj-oss/forjj-modules/trace"
)
Expand Down Expand Up @@ -130,17 +131,48 @@ func (a *Forj) doInstanceMaintain(instance string) error {

// after the first upstream maintain call remote repo should exist
// So, we can sync it up if the sync was not done successfully before.
if !a.d.InSync() {
if err := a.d.GitSyncUp(); err != nil {
return err

if err := a.d.RunInContext(func() error {
if !a.d.InSync() {
if err := a.d.GitSyncUp(); err != nil {
return err
}

// Create basic README.md file on deploy repos if no commit found
if _, err := git.Get("log", "-1", "--pretty=%H"); err != nil {
a.createInitialCommit()
git.Add([]string{"README.md"})
if err := git.Commit("Initial Source Deploy commit.", true); err != nil {
return err
}
}

return a.d.GitPush(false)
}
return a.d.GitPush(false)
return nil
}); err != nil {
return err
}
}

return nil
}

// Must be in the current repo dir
func (a *Forj) createInitialCommit() error {
fd, err := os.Create("README.md")
if err != nil {
return err
}
defer fd.Close()

_, err = fd.WriteString("# Information\n\nThis repository (source Deployment repository) has been created by forjj. \n\nUse forjj update to update it from your infra source repository.\n\nForj team.\n")
if err != nil {
return err
}
return nil
}

// get_infra_repo detect in the path given contains the infra repository.
func (a *Forj) get_infra_repo() error {
return a.i.Use(a.f.InfraPath())
Expand Down