diff --git a/pkg/bootstrap/registry/module.go b/pkg/bootstrap/registry/module.go index 1e96c4113..ec781b265 100644 --- a/pkg/bootstrap/registry/module.go +++ b/pkg/bootstrap/registry/module.go @@ -18,6 +18,8 @@ package registry import ( "fmt" + "path/filepath" + "github.com/kubesphere/kubekey/pkg/bootstrap/registry/templates" "github.com/kubesphere/kubekey/pkg/common" "github.com/kubesphere/kubekey/pkg/container" @@ -26,7 +28,6 @@ import ( "github.com/kubesphere/kubekey/pkg/core/prepare" "github.com/kubesphere/kubekey/pkg/core/task" "github.com/kubesphere/kubekey/pkg/core/util" - "path/filepath" ) type RegistryCertsModule struct { @@ -232,6 +233,22 @@ func InstallHarbor(i *InstallRegistryModule) []task.Interface { Retry: 2, } + // generate Harbor Systemd + generateHarborService := &task.RemoteTask{ + Name: "GenerateHarborService", + Desc: "Generate harbor service", + Hosts: i.Runtime.GetHostsByRole(common.Registry), + Action: &action.Template{ + Template: templates.HarborServiceTempl, + Dst: "/etc/systemd/system/harbor.service", + Data: util.Data{ + "Harbor_install_path": "/opt/harbor", + }, + }, + Parallel: true, + Retry: 1, + } + generateHarborConfig := &task.RemoteTask{ Name: "GenerateHarborConfig", Desc: "Generate harbor config", @@ -265,6 +282,7 @@ func InstallHarbor(i *InstallRegistryModule) []task.Interface { enableDocker, installDockerCompose, syncHarborPackage, + generateHarborService, generateHarborConfig, startHarbor, } diff --git a/pkg/bootstrap/registry/tasks.go b/pkg/bootstrap/registry/tasks.go index a8d1a8262..c84a2c3fe 100644 --- a/pkg/bootstrap/registry/tasks.go +++ b/pkg/bootstrap/registry/tasks.go @@ -18,13 +18,14 @@ package registry import ( "fmt" + "path/filepath" + "strings" + "github.com/kubesphere/kubekey/pkg/common" "github.com/kubesphere/kubekey/pkg/core/connector" "github.com/kubesphere/kubekey/pkg/files" "github.com/kubesphere/kubekey/pkg/utils" "github.com/pkg/errors" - "path/filepath" - "strings" ) type SyncCertsFile struct { @@ -215,7 +216,7 @@ type StartHarbor struct { } func (g *StartHarbor) Execute(runtime connector.Runtime) error { - startCmd := "cd /opt/harbor && chmod +x install.sh && export PATH=$PATH:/usr/local/bin; ./install.sh --with-notary --with-trivy --with-chartmuseum" + startCmd := "cd /opt/harbor && chmod +x install.sh && export PATH=$PATH:/usr/local/bin; ./install.sh --with-notary --with-trivy --with-chartmuseum && systemctl daemon-reload && systemctl enable harbor && systemctl restart harbor" if _, err := runtime.GetRunner().SudoCmd(startCmd, false); err != nil { return errors.Wrap(errors.WithStack(err), "start harbor failed") } diff --git a/pkg/bootstrap/registry/templates/harbor.go b/pkg/bootstrap/registry/templates/harbor.go index 8aca4546c..517a29d65 100644 --- a/pkg/bootstrap/registry/templates/harbor.go +++ b/pkg/bootstrap/registry/templates/harbor.go @@ -14,11 +14,27 @@ limitations under the License. package templates import ( - "github.com/lithammer/dedent" "text/template" + + "github.com/lithammer/dedent" ) var ( + // HarborServiceTempl defines the template of registry's configuration file. + HarborServiceTempl = template.Must(template.New("harborSerivce").Parse( + dedent.Dedent(`[Unit] +Description=Harbor +After=docker.service systemd-networkd.service systemd-resolved.service +Requires=docker.service + +[Service] +Type=simple +ExecStart=/usr/local/bin/docker-compose -f {{ .Harbor_install_path }}/docker-compose.yml up +ExecStop=/usr/local/bin/docker-compose -f {{ .Harbor_install_path }}/docker-compose.yml down +Restart=on-failure +[Install] +WantedBy=multi-user.target + `))) // HarborConfigTempl defines the template of registry's configuration file. HarborConfigTempl = template.Must(template.New("harborConfig").Parse( dedent.Dedent(`# Configuration file of Harbor