-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Phillezi/bugfixes
some bugfixes + stop command
- Loading branch information
Showing
7 changed files
with
309 additions
and
32 deletions.
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
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,113 @@ | ||
package jobs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"go-deploy/dto/v2/body" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Phillezi/kthcloud-cli/pkg/util" | ||
"github.com/Phillezi/kthcloud-cli/pkg/v1/auth/client" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type DeploymentJob struct { | ||
Type string | ||
ID string `json:"id"` | ||
JobID *string `json:"jobId,omitempty"` | ||
} | ||
|
||
func From(job interface{}) *DeploymentJob { | ||
switch v := job.(type) { | ||
case *body.DeploymentCreated: | ||
return &DeploymentJob{ | ||
Type: "Created", | ||
ID: v.ID, | ||
JobID: &v.JobID, | ||
} | ||
case *body.DeploymentUpdated: | ||
return &DeploymentJob{ | ||
Type: "Updated", | ||
ID: v.ID, | ||
JobID: v.JobID, | ||
} | ||
case *body.DeploymentDeleted: | ||
return &DeploymentJob{ | ||
Type: "Deleted", | ||
ID: v.ID, | ||
JobID: &v.JobID, | ||
} | ||
default: | ||
logrus.Debugln("cant create DeploymentJob from this type") | ||
return nil | ||
} | ||
} | ||
|
||
func FromCreated(job *body.DeploymentCreated) *DeploymentJob { | ||
return &DeploymentJob{ | ||
Type: "Created", | ||
ID: job.ID, | ||
JobID: &job.JobID, | ||
} | ||
} | ||
|
||
func FromUpdated(job *body.DeploymentUpdated) *DeploymentJob { | ||
return &DeploymentJob{ | ||
Type: "Updated", | ||
ID: job.ID, | ||
JobID: job.JobID, | ||
} | ||
} | ||
|
||
func FromDeleted(job *body.DeploymentDeleted) *DeploymentJob { | ||
return &DeploymentJob{ | ||
Type: "Deleted", | ||
ID: job.ID, | ||
JobID: &job.JobID, | ||
} | ||
} | ||
|
||
func (job *DeploymentJob) Track(ctx context.Context, deploymentName string, tickerInterval time.Duration, onCancel func()) error { | ||
if job.JobID == nil { | ||
logrus.Debugln("no jobID to track") | ||
} | ||
|
||
c := client.Get().Client() | ||
ticker := time.NewTicker(tickerInterval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
logrus.Debugf("deployment %s was cancelled\n", deploymentName) | ||
if onCancel != nil { | ||
onCancel() | ||
} | ||
return nil | ||
case <-ticker.C: | ||
jobResp, err := c.R().Get("/v2/jobs/" + *job.JobID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get job status for deployment %s: %w", deploymentName, err) | ||
} | ||
|
||
jobStatus, err := util.ProcessResponse[body.JobRead](jobResp.String()) | ||
if err != nil { | ||
return fmt.Errorf("error processing job status for deployment %s: %w", deploymentName, err) | ||
} | ||
|
||
switch jobStatus.Status { | ||
case "finished": | ||
logrus.Debugf("Deployment %s %s successfully", deploymentName, strings.ToLower(job.Type)) | ||
return nil | ||
case "terminated": | ||
logrus.Debugf("Job for deployment %s was terminated", deploymentName) | ||
return nil | ||
} | ||
|
||
if jobStatus.LastError != nil { | ||
return fmt.Errorf("failed to %s deployment %s: %s", strings.ToLower(strings.TrimSuffix(job.Type, "d")), deploymentName, *jobStatus.LastError) | ||
} | ||
} | ||
} | ||
} |
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,96 @@ | ||
package compose | ||
|
||
import ( | ||
"context" | ||
"go-deploy/dto/v2/body" | ||
"time" | ||
|
||
"github.com/Phillezi/kthcloud-cli/pkg/scheduler" | ||
"github.com/Phillezi/kthcloud-cli/pkg/util" | ||
"github.com/Phillezi/kthcloud-cli/pkg/v1/auth/client" | ||
"github.com/Phillezi/kthcloud-cli/pkg/v1/commands/compose/jobs" | ||
"github.com/Phillezi/kthcloud-cli/pkg/v1/commands/compose/parser" | ||
"github.com/Phillezi/kthcloud-cli/pkg/v1/commands/compose/response" | ||
"github.com/briandowns/spinner" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Stop() { | ||
ctx, cancelStop := context.WithCancel(context.Background()) | ||
done := make(chan bool) | ||
scheduleContext, cancelScheduler := context.WithCancel(ctx) | ||
sched := scheduler.NewSched(scheduleContext) | ||
|
||
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) | ||
|
||
util.SetupSignalHandler(done, func() { | ||
sched.CancelJobsBlock() | ||
cancelStop() | ||
<-ctx.Done() | ||
}) | ||
|
||
composeInstance, err := parser.GetCompose() | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
c := client.Get() | ||
if !c.HasValidSession() { | ||
logrus.Fatal("no valid session, log in and try again") | ||
} | ||
|
||
go sched.Start() | ||
defer cancelScheduler() | ||
|
||
s.Color("blue") | ||
s.Start() | ||
defer s.Stop() | ||
|
||
jobIDs := make(map[string]string) | ||
|
||
c.DropDeploymentsCache() | ||
depls, err := c.Deployments() | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
deploymentMap := make(map[string]*body.DeploymentRead) | ||
for _, depl := range depls { | ||
deploymentMap[depl.Name] = &depl | ||
} | ||
|
||
for name := range composeInstance.Services { | ||
if deployment, exists := deploymentMap[name]; exists { | ||
sjob := scheduler.NewJob(func(ctx context.Context, cancelCallback func()) error { | ||
disableDepl := &body.DeploymentUpdate{ | ||
Replicas: util.IntPointer(0), | ||
} | ||
resp, err := c.Update(disableDepl, deployment.ID) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
err = response.IsError(resp.String()) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
job, err := util.ProcessResponse[body.DeploymentUpdated](resp.String()) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
return jobs.From(job).Track(ctx, deployment.Name, time.Millisecond*500, cancelCallback) | ||
}, func() {}) | ||
jobIDs[name] = sched.AddJob(sjob) | ||
} | ||
} | ||
|
||
if err := jobs.MonitorJobStates(jobIDs, sched, s); err != nil { | ||
logrus.Debugln("erroccurred") | ||
s.Color("red") | ||
} else { | ||
logrus.Debugln("alldone") | ||
s.Color("green") | ||
} | ||
} |
Oops, something went wrong.