Skip to content

Commit aee216c

Browse files
authored
Merge pull request #959 from merico-dev/feat-gitlab
Feat: The Plugin GitLab CE Enhancement
2 parents 944c288 + 8f02a77 commit aee216c

File tree

13 files changed

+207
-118
lines changed

13 files changed

+207
-118
lines changed

docs/plugins/gitlab-ce-docker.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,60 @@
22

33
This plugin installs [GitLab](https://about.gitlab.com/) CE(Community Edition) on Docker.
44

5-
## Usage
5+
_NOTICE: currently, this plugin support Linux only._
66

7-
Note:
8-
1. the user you are using must be `root` or is in `docker` group;
9-
2. `https` isn't supported now.
7+
## Background
108

11-
```yaml
12-
--8<-- "gitlab-ce-docker.yaml"
9+
GitLab officially provides an image [gitlab-ce](https://registry.hub.docker.com/r/gitlab/gitlab-ce). We can use this image to start a container:
10+
11+
```shell
12+
docker run --detach \
13+
--hostname gitlab.example.com \
14+
--publish 443:443 --publish 80:80 --publish 22:22 \
15+
--name gitlab \
16+
--restart always \
17+
--volume $GITLAB_HOME/config:/etc/gitlab \
18+
--volume $GITLAB_HOME/logs:/var/log/gitlab \
19+
--volume $GITLAB_HOME/data:/var/opt/gitlab \
20+
--shm-size 256m \
21+
gitlab/gitlab-ce:rc
1322
```
1423

15-
## Some Commands May Help You
24+
The variable `$GITLAB_HOME` here pointing to the directory where the configuration, logs, and data files will reside.
1625

17-
- get the password of user `root` in gitlab-ce-docker
26+
We can set this variable by the `export` command:
1827

1928
```shell
20-
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
29+
export GITLAB_HOME=/srv/gitlab
2130
```
2231

32+
The GitLab container uses host mounted volumes to store persistent data:
33+
34+
| Local location |Container location | Usage |
35+
| --------------------- | ----------------- | ------------------------------------------ |
36+
| `$GITLAB_HOME/data` | `/var/opt/gitlab` | For storing application data |
37+
| `$GITLAB_HOME/logs` | `/var/log/gitlab` | For storing logs |
38+
| `$GITLAB_HOME/config` | `/etc/gitlab` | For storing the GitLab configuration files |
39+
40+
So, we can customize the following configurations:
41+
42+
1. hostname
43+
2. host port
44+
3. persistent data path
45+
4. docker image tag
46+
47+
## Configuration
48+
49+
Note:
50+
1. the user you are using must be `root` or in the `docker` group;
51+
2. `https` isn't supported for now.
52+
53+
```yaml
54+
--8<-- "gitlab-ce-docker.yaml"
55+
```
56+
57+
## Some Commands That May Help
58+
2359
- clone code
2460

2561
```shell

docs/plugins/gitlab-ce-docker.zh.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,47 @@
22

33
这个插件用于以 Docker 的方式安装 [GitLab](https://about.gitlab.com/) CE(社区版)。
44

5-
## 用法
5+
_注意:目前本插件仅支持 Linux。_
6+
7+
## 背景知识
8+
9+
GitLab 官方提供了 [gitlab-ce](https://registry.hub.docker.com/r/gitlab/gitlab-ce) 镜像,通过这个镜像我们可以实现类似这样的命令来启动一个 GitLab 容器:
10+
11+
```shell
12+
docker run --detach \
13+
--hostname gitlab.example.com \
14+
--publish 443:443 --publish 80:80 --publish 22:22 \
15+
--name gitlab \
16+
--restart always \
17+
--volume $GITLAB_HOME/config:/etc/gitlab \
18+
--volume $GITLAB_HOME/logs:/var/log/gitlab \
19+
--volume $GITLAB_HOME/data:/var/opt/gitlab \
20+
--shm-size 256m \
21+
gitlab/gitlab-ce:rc
22+
```
23+
24+
其中 $GITLAB_HOME 表示的是本地存储卷路径,比如我们可以通过 export 命令来设置这个变量:
25+
26+
```shell
27+
export GITLAB_HOME=/srv/gitlab
28+
```
29+
30+
在上述命令中,我们可以看到这个容器使用了3个存储卷,含义分别如下:
31+
32+
| 本地路径 | 容器内路径 | 用途 |
33+
| --------------------- | ----------------- | ----------------- |
34+
| `$GITLAB_HOME/data` | `/var/opt/gitlab` | 保存应用数据 |
35+
| `$GITLAB_HOME/logs` | `/var/log/gitlab` | 保存日志 |
36+
| `$GITLAB_HOME/config` | `/etc/gitlab` | 保存 GitLab 配置文件 |
37+
38+
在此基础上,我们可以自定义如下一些配置:
39+
40+
1. hostname
41+
2. 本机端口
42+
3. 存储卷路径
43+
4. 镜像版本
44+
45+
## 配置
646

747
注意:
848
1. 你使用的用户必须是 `root` 或者在 `docker` 用户组里;
@@ -14,12 +54,6 @@
1454

1555
## 一些可能有用的命令
1656

17-
- 查看 gitlab 的 root 用户的密码:
18-
19-
```shell
20-
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
21-
```
22-
2357
- 克隆项目
2458

2559
```shell

internal/pkg/plugin/gitlabcedocker/create.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ import (
88

99
func Create(options map[string]interface{}) (map[string]interface{}, error) {
1010
// 1. create config and pre-handle operations
11-
opts, err := preHandleOptions(options)
11+
opts, err := validateAndDefault(options)
1212
if err != nil {
1313
return nil, err
1414
}
1515

16-
gitlabURL = opts.getGitLabURL()
17-
1816
// 2. config install operations
1917
runner := &plugininstaller.Runner{
2018
PreExecuteOperations: []plugininstaller.MutableOperation{
2119
dockerInstaller.Validate,
2220
},
2321
ExecuteOperations: []plugininstaller.BaseOperation{
2422
dockerInstaller.InstallOrUpdate,
25-
showGitLabURL,
23+
showHelpMsg,
2624
},
2725
TerminateOperations: []plugininstaller.BaseOperation{
2826
dockerInstaller.HandleRunFailure,

internal/pkg/plugin/gitlabcedocker/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func Delete(options map[string]interface{}) (bool, error) {
99
// 1. create config and pre-handle operations
10-
opts, err := preHandleOptions(options)
10+
opts, err := validateAndDefault(options)
1111
if err != nil {
1212
return false, err
1313
}
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
package gitlabcedocker
22

3-
import (
4-
"fmt"
5-
6-
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
7-
"github.com/devstream-io/devstream/pkg/util/log"
8-
)
3+
import "github.com/devstream-io/devstream/pkg/util/types"
94

105
const (
11-
gitlabImageName = "gitlab/gitlab-ce"
6+
defaultHostname = "gitlab.example.com"
7+
defaultGitlabHome = "/srv/gitlab"
8+
defaultSSHPort = 22
9+
defaultHTTPPort = 80
10+
defaultHTTPSPort = 443
1211
defaultImageTag = "rc"
12+
gitlabImageName = "gitlab/gitlab-ce"
1313
gitlabContainerName = "gitlab"
1414
dockerRunShmSizeParam = "--shm-size 256m"
1515
)
1616

17-
// gitlabURL is the access URL of GitLab.
18-
var gitlabURL string
19-
20-
func (opts *Options) getGitLabURL() string {
21-
return fmt.Sprintf("http://%s:%d", opts.Hostname, opts.HTTPPort)
22-
}
23-
24-
func showGitLabURL(options plugininstaller.RawOptions) error {
25-
log.Infof("GitLab access URL: %s", gitlabURL)
26-
27-
return nil
28-
}
17+
var (
18+
defaultRMDataAfterDelete = types.Bool(false)
19+
)

internal/pkg/plugin/gitlabcedocker/options.go

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,86 @@
11
package gitlabcedocker
22

33
import (
4+
"fmt"
45
"path/filepath"
56

7+
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
68
dockerInstaller "github.com/devstream-io/devstream/internal/pkg/plugininstaller/docker"
79
"github.com/devstream-io/devstream/pkg/util/docker"
10+
"github.com/devstream-io/devstream/pkg/util/log"
811
)
912

1013
// Options is the struct for configurations of the gitlab-ce-docker plugin.
1114
type Options struct {
15+
Hostname string `validate:"hostname" mapstructure:"hostname"`
1216
// GitLab home directory, we assume the path set by user is always correct.
13-
GitLabHome string `validate:"required" mapstructure:"gitlab_home"`
14-
Hostname string `validate:"required,hostname" mapstructure:"hostname"`
15-
SSHPort uint `validate:"required" mapstructure:"ssh_port"`
16-
HTTPPort uint `validate:"required" mapstructure:"http_port"`
17-
HTTPSPort uint `validate:"required" mapstructure:"https_port"`
18-
RmDataAfterDelete bool `mapstructure:"rm_data_after_delete"`
17+
GitLabHome string `mapstructure:"gitlab_home"`
18+
SSHPort uint `mapstructure:"ssh_port"`
19+
HTTPPort uint `mapstructure:"http_port"`
20+
HTTPSPort uint `mapstructure:"https_port"`
21+
RmDataAfterDelete *bool `mapstructure:"rm_data_after_delete"`
1922
ImageTag string `mapstructure:"image_tag"`
2023
}
2124

22-
func buildDockerOptions(opts *Options) *dockerInstaller.Options {
23-
dockerOpts := &dockerInstaller.Options{}
24-
dockerOpts.RmDataAfterDelete = opts.RmDataAfterDelete
25-
dockerOpts.ImageName = gitlabImageName
26-
dockerOpts.ImageTag = opts.ImageTag
27-
dockerOpts.Hostname = opts.Hostname
28-
dockerOpts.ContainerName = gitlabContainerName
29-
dockerOpts.RestartAlways = true
25+
func (opts *Options) Defaults() {
26+
if opts.Hostname == "" {
27+
opts.Hostname = defaultHostname
28+
}
29+
if opts.GitLabHome == "" {
30+
opts.GitLabHome = defaultGitlabHome
31+
}
32+
if opts.SSHPort == 0 {
33+
opts.SSHPort = defaultSSHPort
34+
}
35+
if opts.HTTPPort == 0 {
36+
opts.HTTPPort = defaultHTTPPort
37+
}
38+
if opts.HTTPSPort == 0 {
39+
opts.HTTPSPort = defaultHTTPSPort
40+
}
41+
if opts.RmDataAfterDelete == nil {
42+
opts.RmDataAfterDelete = defaultRMDataAfterDelete
43+
}
44+
if opts.ImageTag == "" {
45+
opts.ImageTag = defaultImageTag
46+
}
47+
}
48+
49+
// gitlabURL is the access URL of GitLab.
50+
var gitlabURL string
3051

52+
func (opts *Options) setGitLabURL() {
53+
if gitlabURL != "" {
54+
return
55+
}
56+
gitlabURL = fmt.Sprintf("http://%s:%d", opts.Hostname, opts.HTTPPort)
57+
}
58+
59+
func showHelpMsg(options plugininstaller.RawOptions) error {
60+
log.Infof("GitLab access URL: %s", gitlabURL)
61+
log.Infof("GitLab initial root password: execute the command -> docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password")
62+
63+
return nil
64+
}
65+
66+
func buildDockerOptions(opts *Options) *dockerInstaller.Options {
3167
portPublishes := []docker.PortPublish{
3268
{HostPort: opts.SSHPort, ContainerPort: 22},
3369
{HostPort: opts.HTTPPort, ContainerPort: 80},
3470
{HostPort: opts.HTTPSPort, ContainerPort: 443},
3571
}
36-
dockerOpts.PortPublishes = portPublishes
3772

38-
dockerOpts.Volumes = buildDockerVolumes(opts)
39-
40-
dockerOpts.RunParams = []string{dockerRunShmSizeParam}
73+
dockerOpts := &dockerInstaller.Options{
74+
RmDataAfterDelete: opts.RmDataAfterDelete,
75+
ImageName: gitlabImageName,
76+
ImageTag: opts.ImageTag,
77+
Hostname: opts.Hostname,
78+
ContainerName: gitlabContainerName,
79+
RestartAlways: true,
80+
Volumes: buildDockerVolumes(opts),
81+
RunParams: []string{dockerRunShmSizeParam},
82+
PortPublishes: portPublishes,
83+
}
4184

4285
return dockerOpts
4386
}

internal/pkg/plugin/gitlabcedocker/options_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import (
1111
var _ = Describe("Options", func() {
1212

1313
var opts *Options
14-
1514
BeforeEach(func() {
1615
opts = &Options{
1716
GitLabHome: "/srv/gitlab",
1817
Hostname: "gitlab.example.com",
1918
SSHPort: 8122,
2019
HTTPPort: 8180,
2120
HTTPSPort: 8443,
22-
RmDataAfterDelete: false,
21+
RmDataAfterDelete: nil,
2322
ImageTag: "rc",
2423
}
2524
})

internal/pkg/plugin/gitlabcedocker/read.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import (
88

99
func Read(options map[string]interface{}) (map[string]interface{}, error) {
1010
// 1. create config and pre-handle operations
11-
opts, err := preHandleOptions(options)
11+
opts, err := validateAndDefault(options)
1212
if err != nil {
1313
return nil, err
1414
}
1515

16-
gitlabURL = opts.getGitLabURL()
17-
1816
// 2. config read operations
1917
runner := &plugininstaller.Runner{
2018
PreExecuteOperations: []plugininstaller.MutableOperation{

internal/pkg/plugin/gitlabcedocker/update.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ import (
88

99
func Update(options map[string]interface{}) (map[string]interface{}, error) {
1010
// 1. create config and pre-handle operations
11-
opts, err := preHandleOptions(options)
11+
_, err := validateAndDefault(options)
1212
if err != nil {
1313
return nil, err
1414
}
1515

16-
gitlabURL = opts.getGitLabURL()
17-
1816
// 2. config install operations
1917
runner := &plugininstaller.Runner{
2018
PreExecuteOperations: []plugininstaller.MutableOperation{
2119
dockerInstaller.Validate,
2220
},
2321
ExecuteOperations: []plugininstaller.BaseOperation{
2422
dockerInstaller.InstallOrUpdate,
25-
showGitLabURL,
23+
showHelpMsg,
2624
},
2725
GetStatusOperation: dockerInstaller.GetRunningState,
2826
}

0 commit comments

Comments
 (0)