-
Notifications
You must be signed in to change notification settings - Fork 2
feat(deploy): add utils to deploy a workspace #11
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
| package cs | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| type TimedOut struct { | ||
| msg string | ||
| } | ||
|
|
||
| func (e *TimedOut) Error() string { | ||
| return e.msg | ||
| } | ||
|
|
||
| func NewTimedOut(operation string, timeout time.Duration) *TimedOut { | ||
| return &TimedOut{ | ||
| msg: fmt.Sprintf("%s timed out after %s", operation, timeout.String()), | ||
| } | ||
| } | ||
|
|
||
| type NotFound struct { | ||
| msg string | ||
| } | ||
|
|
||
| func (e *NotFound) Error() string { | ||
| return e.msg | ||
| } | ||
|
|
||
| func NewNotFound(msg string) *NotFound { | ||
| return &NotFound{ | ||
| msg: msg, | ||
| } | ||
| } | ||
|
|
||
| type Duplicated struct { | ||
| msg string | ||
| } | ||
|
|
||
| func (e *Duplicated) Error() string { | ||
| return e.msg | ||
| } | ||
|
|
||
| func NewDuplicated(msg string) *Duplicated { | ||
| return &Duplicated{ | ||
| msg: msg, | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,93 @@ | ||
| package cs | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/codesphere-cloud/cs-go/api" | ||
| ) | ||
|
|
||
| type WaitForWorkspaceRunningOptions struct { | ||
| Timeout time.Duration | ||
| Delay time.Duration | ||
| } | ||
|
|
||
| // Waits for a given workspace to be running. | ||
| // | ||
| // Returns [TimedOut] error if the workspace does not become running in time. | ||
| func WaitForWorkspaceRunning( | ||
| client *api.Client, | ||
| workspace *api.Workspace, | ||
| opts WaitForWorkspaceRunningOptions, | ||
| ) error { | ||
| timeout := opts.Timeout | ||
| if timeout == 0 { | ||
| timeout = 20 * time.Minute | ||
| } | ||
| delay := opts.Delay | ||
| if delay == 0 { | ||
| delay = 5 * time.Second | ||
| } | ||
|
|
||
| maxWaitTime := time.Now().Add(timeout) | ||
| for time.Now().Before(maxWaitTime) { | ||
| status, err := client.WorkspaceStatus(workspace.Id) | ||
|
|
||
| if err != nil { | ||
| // TODO: log error and retry until timeout is reached. | ||
| return err | ||
| } | ||
| if status.IsRunning { | ||
| return nil | ||
| } | ||
| time.Sleep(delay) | ||
| } | ||
|
|
||
| return NewTimedOut( | ||
| fmt.Sprintf("Waiting for workspace %s(%d) to be ready", workspace.Name, workspace.Id), | ||
| timeout) | ||
| } | ||
|
|
||
| type DeployWorkspaceArgs struct { | ||
| TeamId int | ||
| PlanId int | ||
| Name string | ||
| EnvVars map[string]string | ||
| VpnConfigName *string | ||
|
|
||
| Timeout time.Duration | ||
| } | ||
|
|
||
| // Deploys a workspace with the given configuration. | ||
| // | ||
| // Returns [TimedOut] error if the timeout is reached | ||
| func DeployWorkspace( | ||
| client api.Client, | ||
| args DeployWorkspaceArgs, | ||
| ) error { | ||
| workspace, err := client.CreateWorkspace(api.CreateWorkspaceArgs{ | ||
| TeamId: args.TeamId, | ||
| Name: args.Name, | ||
| PlanId: args.PlanId, | ||
| IsPrivateRepo: true, | ||
| GitUrl: nil, | ||
| InitialBranch: nil, | ||
| SourceWorkspaceId: nil, | ||
| WelcomeMessage: nil, | ||
| Replicas: 1, | ||
| VpnConfig: args.VpnConfigName, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := WaitForWorkspaceRunning(&client, workspace, WaitForWorkspaceRunningOptions{Timeout: args.Timeout}); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(args.EnvVars) != 0 { | ||
| if err := client.SetEnvVarOnWorkspace(workspace.Id, args.EnvVars); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } |
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.
not sure on the naming here.
WorkspaceSetEnvVarssounded a bit strange