-
Notifications
You must be signed in to change notification settings - Fork 198
feat: docker installer #933
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,45 @@ | ||
| package gitlabcedocker | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/mitchellh/mapstructure" | ||
|
|
||
| "github.com/devstream-io/devstream/pkg/util/docker" | ||
| "github.com/devstream-io/devstream/internal/pkg/plugininstaller" | ||
| dockerInstaller "github.com/devstream-io/devstream/internal/pkg/plugininstaller/docker" | ||
| "github.com/devstream-io/devstream/pkg/util/log" | ||
| ) | ||
|
|
||
| func Create(options map[string]interface{}) (map[string]interface{}, error) { | ||
| var opts Options | ||
| if err := mapstructure.Decode(options, &opts); err != nil { | ||
| // 1. create config and pre-handle operations | ||
| opts, err := preHandleOptions(options) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defaults(&opts) | ||
| gitlabURL = opts.getGitLabURL() | ||
|
|
||
| if errs := validate(&opts); len(errs) != 0 { | ||
| for _, e := range errs { | ||
| log.Errorf("Options error: %s.", e) | ||
| } | ||
| return nil, fmt.Errorf("opts are illegal") | ||
| // 2. config install operations | ||
| runner := &plugininstaller.Runner{ | ||
| PreExecuteOperations: []plugininstaller.MutableOperation{ | ||
| dockerInstaller.Validate, | ||
| }, | ||
| ExecuteOperations: []plugininstaller.BaseOperation{ | ||
| dockerInstaller.InstallOrUpdate, | ||
| showGitLabURL, | ||
| }, | ||
| TerminateOperations: []plugininstaller.BaseOperation{ | ||
| dockerInstaller.HandleRunFailure, | ||
| }, | ||
| GetStatusOperation: dockerInstaller.GetStaticStateFromOptions, | ||
| } | ||
|
|
||
| op := GetDockerOperator(opts) | ||
|
|
||
| // 1. try to pull the image | ||
| // always pull the image because docker will check the image existence | ||
| if err := op.ImagePull(getImageNameWithTag(opts)); err != nil { | ||
| // 3. execute installer get status and error | ||
| rawOptions, err := buildDockerOptions(opts).Encode() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // 2. try to run the container | ||
| log.Info("Running container as the name <gitlab>") | ||
| if err := op.ContainerRun(buildDockerRunOptions(opts), dockerRunShmSizeParam); err != nil { | ||
| return nil, fmt.Errorf("failed to run container: %v", err) | ||
| } | ||
|
|
||
| // 3. check if the container is started successfully | ||
| if ok := op.ContainerIfRunning(gitlabContainerName); !ok { | ||
| return nil, fmt.Errorf("failed to run container") | ||
| } | ||
|
|
||
| // 4. check if the volume is created successfully | ||
| mounts, err := op.ContainerListMounts(gitlabContainerName) | ||
| status, err := runner.Execute(rawOptions) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get container mounts: %v", err) | ||
| } | ||
| volumes := mounts.ExtractSources() | ||
| if docker.IfVolumesDiffer(volumes, getVolumesDirFromOptions(opts)) { | ||
| return nil, fmt.Errorf("failed to create volumes") | ||
| } | ||
|
|
||
| // 5. show the access url | ||
| showGitLabURL(opts) | ||
|
|
||
| resource := gitlabResource{ | ||
| ContainerRunning: true, | ||
| Volumes: volumes, | ||
| Hostname: opts.Hostname, | ||
| SSHPort: strconv.Itoa(int(opts.SSHPort)), | ||
| HTTPPort: strconv.Itoa(int(opts.HTTPPort)), | ||
| HTTPSPort: strconv.Itoa(int(opts.HTTPSPort)), | ||
| } | ||
|
|
||
| return resource.toMap(), nil | ||
| } | ||
|
|
||
| func showGitLabURL(opts Options) { | ||
| accessUrl := opts.Hostname | ||
| if opts.HTTPPort != 80 { | ||
| accessUrl += ":" + strconv.Itoa(int(opts.HTTPPort)) | ||
| } | ||
| if !strings.HasPrefix(accessUrl, "http") { | ||
| accessUrl = "http://" + accessUrl | ||
| return nil, err | ||
| } | ||
| log.Debugf("Return map: %v", status) | ||
|
|
||
| log.Infof("GitLab access URL: %s", accessUrl) | ||
| return status, nil | ||
| } |
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 |
|---|---|---|
| @@ -1,102 +1,36 @@ | ||
| package gitlabcedocker | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/mitchellh/mapstructure" | ||
|
|
||
| "github.com/devstream-io/devstream/pkg/util/log" | ||
| "github.com/devstream-io/devstream/internal/pkg/plugininstaller" | ||
| dockerInstaller "github.com/devstream-io/devstream/internal/pkg/plugininstaller/docker" | ||
| ) | ||
|
|
||
| func Delete(options map[string]interface{}) (bool, error) { | ||
| var opts Options | ||
| if err := mapstructure.Decode(options, &opts); err != nil { | ||
| // 1. create config and pre-handle operations | ||
| opts, err := preHandleOptions(options) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| defaults(&opts) | ||
|
|
||
| if errs := validate(&opts); len(errs) != 0 { | ||
| for _, e := range errs { | ||
| log.Errorf("Options error: %s.", e) | ||
| } | ||
| return false, fmt.Errorf("opts are illegal") | ||
| // 2. config delete operations | ||
| runner := &plugininstaller.Runner{ | ||
| PreExecuteOperations: []plugininstaller.MutableOperation{ | ||
| dockerInstaller.Validate, | ||
| }, | ||
| ExecuteOperations: []plugininstaller.BaseOperation{ | ||
| dockerInstaller.Delete, | ||
| }, | ||
| } | ||
|
|
||
| op := GetDockerOperator(opts) | ||
|
|
||
| // 1. stop the container if it is running | ||
| if ok := op.ContainerIfRunning(gitlabContainerName); ok { | ||
| if err := op.ContainerStop(gitlabContainerName); err != nil { | ||
| log.Errorf("Failed to stop container: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // 2. remove the container if it exists | ||
| if ok := op.ContainerIfExist(gitlabContainerName); ok { | ||
| if err := op.ContainerRemove(gitlabContainerName); err != nil { | ||
| log.Errorf("failed to remove container %v: %v", gitlabContainerName, err) | ||
| } | ||
| } | ||
|
|
||
| // 3. remove the image if it exists | ||
| if ok := op.ImageIfExist(getImageNameWithTag(opts)); ok { | ||
| if err := op.ImageRemove(getImageNameWithTag(opts)); err != nil { | ||
| log.Errorf("failed to remove image %v: %v", getImageNameWithTag(opts), err) | ||
| } | ||
| } | ||
|
|
||
| // 4. remove the volume if it exists | ||
| volumesDirFromOptions := getVolumesDirFromOptions(opts) | ||
| if opts.RmDataAfterDelete { | ||
| for _, volume := range volumesDirFromOptions { | ||
| if err := os.RemoveAll(volume); err != nil { | ||
| log.Errorf("failed to remove data %v: %v", volume, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var errs []error | ||
|
|
||
| // 1. check if the container is stopped and deleted | ||
| if ok := op.ContainerIfRunning(gitlabContainerName); ok { | ||
| errs = append(errs, fmt.Errorf("failed to delete/stop container %s", gitlabContainerName)) | ||
| } | ||
| if ok := op.ContainerIfExist(gitlabContainerName); ok { | ||
| errs = append(errs, fmt.Errorf("failed to delete container %s", gitlabContainerName)) | ||
| } | ||
|
|
||
| // 2. check if the image is removed | ||
| if ok := op.ImageIfExist(getImageNameWithTag(opts)); ok { | ||
| errs = append(errs, fmt.Errorf("failed to delete image %s", getImageNameWithTag(opts))) | ||
| } | ||
|
|
||
| // 3. check if the data volume is removed | ||
| if opts.RmDataAfterDelete { | ||
| errs = append(errs, RemoveDirs(volumesDirFromOptions)...) | ||
| // 3. delete and get status | ||
| rawOptions, err := buildDockerOptions(opts).Encode() | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| // splice the errors | ||
| if len(errs) != 0 { | ||
| errsString := "" | ||
| for _, e := range errs { | ||
| errsString += e.Error() + "; " | ||
| } | ||
| return false, fmt.Errorf(errsString) | ||
| _, err = runner.Execute(rawOptions) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| // RemoveDirs removes the all the directories in the given list recursively | ||
| func RemoveDirs(dirs []string) []error { | ||
| var errs []error | ||
| for _, dir := range dirs { | ||
| if err := os.RemoveAll(dir); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } |
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,51 +1,28 @@ | ||
| package gitlabcedocker | ||
|
|
||
| import ( | ||
| "strings" | ||
| "fmt" | ||
|
|
||
| "github.com/devstream-io/devstream/pkg/util/docker" | ||
| "github.com/devstream-io/devstream/pkg/util/docker/dockersh" | ||
| "github.com/devstream-io/devstream/internal/pkg/plugininstaller" | ||
| "github.com/devstream-io/devstream/pkg/util/log" | ||
| ) | ||
|
|
||
| const ( | ||
| gitlabImageName = "gitlab/gitlab-ce" | ||
| defaultImageTag = "rc" | ||
| gitlabContainerName = "gitlab" | ||
| tcp = "tcp" | ||
| dockerRunShmSizeParam = "--shm-size 256m" | ||
| ) | ||
|
|
||
| func getImageNameWithTag(opt Options) string { | ||
| return gitlabImageName + ":" + opt.ImageTag | ||
| } | ||
|
|
||
| func defaults(opts *Options) { | ||
| if opts.ImageTag == "" { | ||
| opts.ImageTag = defaultImageTag | ||
| } | ||
| } | ||
| // gitlabURL is the access URL of GitLab. | ||
| var gitlabURL string | ||
|
|
||
| func GetDockerOperator(_ Options) docker.Operator { | ||
| // just return a ShellOperator for now | ||
| return &dockersh.ShellOperator{} | ||
| func (opts *Options) getGitLabURL() string { | ||
| return fmt.Sprintf("http://%s:%d", opts.Hostname, opts.HTTPPort) | ||
| } | ||
|
|
||
| type gitlabResource struct { | ||
| ContainerRunning bool | ||
| Volumes []string | ||
| Hostname string | ||
| SSHPort string | ||
| HTTPPort string | ||
| HTTPSPort string | ||
| } | ||
| func showGitLabURL(options plugininstaller.RawOptions) error { | ||
| log.Infof("GitLab access URL: %s", gitlabURL) | ||
|
|
||
| func (res *gitlabResource) toMap() map[string]interface{} { | ||
| return map[string]interface{}{ | ||
| "containerRunning": res.ContainerRunning, | ||
| "volumes": strings.Join(res.Volumes, ","), | ||
| "hostname": res.Hostname, | ||
| "SSHPort": res.SSHPort, | ||
| "HTTPPort": res.HTTPPort, | ||
| "HTTPSPort": res.HTTPSPort, | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.