add version support

This commit is contained in:
wy65701436 2017-03-21 04:56:59 -07:00
parent 7f099ebe50
commit c5633f7ce8
5 changed files with 33 additions and 7 deletions

View File

@ -76,6 +76,8 @@ before_script:
script:
- sudo mkdir -p /harbor_storage/ca_download
- sudo mv ./tests/ca.crt /harbor_storage/ca_download
- sudo mkdir -p /harbor
- sudo mv ./VERSION /harbor/VERSION
- sudo service mysql stop
- sudo ./tests/testprepare.sh
- docker-compose -f ./make/docker-compose.test.yml up -d

View File

@ -164,8 +164,8 @@ DOCKERCOMPOSEFILENAME=docker-compose.yml
DOCKERCOMPOSENOTARYFILENAME=docker-compose.notary.yml
# version prepare
VERSIONFILEPATH=$(SRCPATH)/ui/views/sections
VERSIONFILENAME=header-content.htm
VERSIONFILEPATH=$(CURDIR)
VERSIONFILENAME=VERSION
GITCMD=$(shell which git)
GITTAG=$(GITCMD) describe --tags
ifeq ($(DEVFLAG), true)
@ -189,9 +189,7 @@ REGISTRYUSER=user
REGISTRYPASSWORD=default
version:
@if [ "$(DEVFLAG)" = "false" ] ; then \
$(SEDCMD) -i 's/version=\"{{.Version}}\"/version=\"$(VERSIONTAG)\"/' -i $(VERSIONFILEPATH)/$(VERSIONFILENAME) ; \
fi
@printf $(VERSIONTAG) > $(VERSIONFILEPATH)/$(VERSIONFILENAME);
check_environment:
@$(MAKEPATH)/$(CHECKENVCMD)
@ -420,7 +418,7 @@ cleandockercomposefile:
cleanversiontag:
@echo "cleaning version TAG"
@$(SEDCMD) -i 's/version=\"$(VERSIONTAG)\"/version=\"{{.Version}}\"/' -i $(VERSIONFILEPATH)/$(VERSIONFILENAME)
@rm -rf $(VERSIONFILEPATH)/$(VERSIONFILENAME)
cleanpackage:
@echo "cleaning harbor install package"

View File

@ -7,6 +7,7 @@ COPY ./make/dev/ui/harbor_ui /harbor/
COPY ./src/ui/views /harbor/views
COPY ./src/ui/static /harbor/static
COPY ./src/favicon.ico /harbor/favicon.ico
COPY ./VERSION /harbor/VERSION
RUN chmod u+x /harbor/harbor_ui

View File

@ -1,6 +1,7 @@
package api
import (
"io/ioutil"
"net/http"
"os"
"strings"
@ -20,6 +21,7 @@ type SystemInfoAPI struct {
}
const defaultRootCert = "/harbor_storage/ca_download/ca.crt"
const harborVersionFile = "/harbor/VERSION"
//SystemInfo models for system info.
type SystemInfo struct {
@ -42,6 +44,7 @@ type GeneralInfo struct {
ProjectCreationRestrict string `json:"project_creation_restriction"`
SelfRegistration bool `json:"self_registration"`
HasCARoot bool `json:"has_ca_root"`
HarborVersion string `json:"harbor_version"`
}
// validate for validating user if an admin.
@ -113,6 +116,7 @@ func (sia *SystemInfoAPI) GetGeneralInfo() {
registryURL = l[0]
}
_, caStatErr := os.Stat(defaultRootCert)
harbor_version := sia.getVersion()
info := GeneralInfo{
AdmiralEndpoint: cfg[common.AdmiralEndpoint].(string),
WithAdmiral: config.WithAdmiral(),
@ -122,7 +126,26 @@ func (sia *SystemInfoAPI) GetGeneralInfo() {
SelfRegistration: cfg[common.SelfRegistration].(bool),
RegistryURL: registryURL,
HasCARoot: caStatErr == nil,
HarborVersion: harbor_version,
}
sia.Data["json"] = info
sia.ServeJSON()
}
// GetVersion gets harbor version.
func (sia *SystemInfoAPI) getVersion() string {
if _, err := os.Stat(harborVersionFile); err != nil {
if os.IsNotExist(err) {
log.Errorf("Version File doesn't exist.")
return ""
}
}
version, err := ioutil.ReadFile(harborVersionFile)
if err != nil {
log.Errorf("Error occured getting harbor version: %v", err)
return ""
}
return string(version[:])
}

View File

@ -3,8 +3,9 @@ package api
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetVolumeInfo(t *testing.T) {
@ -49,6 +50,7 @@ func TestGetGeneralInfo(t *testing.T) {
assert.Nil(err, fmt.Sprintf("Unexpected Error: %v", err))
assert.Equal(false, g.WithNotary, "with notary should be false")
assert.Equal(true, g.HasCARoot, "has ca root should be true")
assert.NotEmpty(g.HarborVersion, "harbor version should not be empty")
}
func TestGetCert(t *testing.T) {