Skip to content

Commit

Permalink
Use create debug project instead of create job from job api (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamir-suspect committed Feb 14, 2023
1 parent d958e74 commit ea188d1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
21 changes: 21 additions & 0 deletions api/client/jobs_v1_alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ func (c *JobsApiV1AlphaApi) CreateDebugJob(j *models.DebugJobV1Alpha) (*models.J
return models.NewJobV1AlphaFromJson(body)
}

func (c *JobsApiV1AlphaApi) CreateDebugProject(j *models.DebugProjectV1Alpha) (*models.JobV1Alpha, error) {
json_body, err := j.ToJson()

if err != nil {
return nil, errors.New(fmt.Sprintf("failed to serialize object '%s'", err))
}

path := fmt.Sprintf("%s/%s/%s", c.ResourceNamePlural, "project_debug", j.ProjectIdOrName)
body, status, err := c.BaseClient.Post(path, json_body)

if err != nil {
return nil, errors.New(fmt.Sprintf("creating debug %s on Semaphore failed '%s'", c.ResourceNameSingular, err))
}

if status != 200 {
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body))
}

return models.NewJobV1AlphaFromJson(body)
}

func (c *JobsApiV1AlphaApi) StopJob(id string) error {
path := fmt.Sprintf("%s/%s/%s", c.ResourceNamePlural, id, "stop")
body, status, err := c.BaseClient.Post(path, []byte{})
Expand Down
23 changes: 23 additions & 0 deletions api/models/debug_project_v1_alpha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package models

import "encoding/json"

type DebugProjectV1Alpha struct {
ProjectIdOrName string `json:"project_id_or_name,omitempty" yaml:"project_id_or_name"`
Duration int `json:"duration,omitempty,string" yaml:"duration,omitempty"`
MachineType string `json:"machine_type,omitempty" yaml:"machine_type,omitempty"`
}

func NewDebugProjectV1Alpha(project string, duration int, machine string) *DebugProjectV1Alpha {
j := DebugProjectV1Alpha{}

j.ProjectIdOrName = project
j.Duration = duration
j.MachineType = machine

return &j
}

func (j *DebugProjectV1Alpha) ToJson() ([]byte, error) {
return json.Marshal(j)
}
23 changes: 5 additions & 18 deletions cmd/debug_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package cmd

import (
"fmt"
"github.com/semaphoreci/cli/api/models"
"os"
"time"

client "github.com/semaphoreci/cli/api/client"
models "github.com/semaphoreci/cli/api/models"
"github.com/semaphoreci/cli/cmd/jobs"
"github.com/semaphoreci/cli/cmd/ssh"
"github.com/semaphoreci/cli/cmd/utils"
Expand Down Expand Up @@ -49,25 +48,13 @@ func RunDebugProjectCmd(cmd *cobra.Command, args []string) {

utils.Check(err)

projectName := args[0]
pc := client.NewProjectV1AlphaApi()
project, err := pc.GetProject(projectName)
projectNameOrId := args[0]

utils.Check(err)

jobName := fmt.Sprintf("Debug Session for %s", projectName)
job := models.NewJobV1Alpha(jobName)
debugPrj := models.NewDebugProjectV1Alpha(projectNameOrId, int(duration.Seconds()), machineType)

job.Spec = &models.JobV1AlphaSpec{}
job.Spec.Agent.Machine.Type = machineType
job.Spec.Agent.Machine.OsImage = "ubuntu1804"
job.Spec.ProjectId = project.Metadata.Id

job.Spec.Commands = []string{
fmt.Sprintf("sleep %d", int(duration.Seconds())),
}

fmt.Printf("* Creating debug session for project '%s'\n", projectName)
fmt.Printf("* Creating debug session for project '%s'\n", projectNameOrId)
fmt.Printf("* Setting duration to %d minutes\n", int(duration.Minutes()))

sshIntroMessage := `
Expand All @@ -79,5 +66,5 @@ Semaphore CI Debug Session.
Documentation: https://docs.semaphoreci.com/essentials/debugging-with-ssh-access/.
`

ssh.StartDebugProjectSession(job, sshIntroMessage)
ssh.StartDebugProjectSession(debugPrj, sshIntroMessage)
}
4 changes: 2 additions & 2 deletions cmd/ssh/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func StartDebugJobSession(debug *models.DebugJobV1Alpha, message string) error {
return StartDebugSession(job, message)
}

func StartDebugProjectSession(job *models.JobV1Alpha, message string) error {
func StartDebugProjectSession(debug_project *models.DebugProjectV1Alpha, message string) error {
c := client.NewJobsV1AlphaApi()
job, err := c.CreateJob(job)
job, err := c.CreateDebugProject(debug_project)
utils.Check(err)

return StartDebugSession(job, message)
Expand Down

0 comments on commit ea188d1

Please sign in to comment.