Skip to content

Commit

Permalink
2.0.0-dev finished
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamans committed Mar 2, 2019
1 parent 92f5860 commit 764fb64
Show file tree
Hide file tree
Showing 30 changed files with 1,082 additions and 228 deletions.
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (db *DB) Open() error {
return errors.New(fmt.Sprintf("mysql connect failed, %s", err.Error()))
}

c.LogMode(true)
c.LogMode(false)
c.DB().SetMaxIdleConns(db.cfg.MaxIdleConns)
c.DB().SetMaxOpenConns(db.cfg.MaxOpenConns)
c.DB().SetConnMaxLifetime(time.Second * time.Duration(db.cfg.ConnMaxLifeTime))
Expand Down
68 changes: 60 additions & 8 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
STATUS_RUNNING = 1
STATUS_DONE = 2
STATUS_FAILED = 3
STATUS_TERMINATE = 4
)

type Deploy struct {
Expand All @@ -32,11 +33,6 @@ type Deploy struct {
wg sync.WaitGroup
}

type deployTask struct {
deployList map[int][]*Deploy
mu sync.Mutex
}

type taskCallbackFn func(int, bool)

type deployCallbackFn func(int, int, *ServerStatus)
Expand All @@ -62,7 +58,10 @@ func NewDepoly(deploy *Deploy) (*Deploy, error) {
return deploy, nil
}

func DeployGroups (id int, deployGroups []*Deploy, callbackFn taskCallbackFn) {
func DeployGroups (id int, deployGroups []*Deploy, callbackFn taskCallbackFn) error {
if err := deployTaskList.append(id, deployGroups); err != nil {
return err
}
go func() {
haveError := false
for _, dep := range deployGroups {
Expand All @@ -71,8 +70,15 @@ func DeployGroups (id int, deployGroups []*Deploy, callbackFn taskCallbackFn) {
haveError = true
}
}
deployTaskList.remove(id)
callbackFn(id, haveError)
}()
return nil
}

func StopDeploy(id int) {
deployTaskList.stop(id)
deployTaskList.remove(id)
}

func (deploy *Deploy) deploy() {
Expand All @@ -82,8 +88,18 @@ func (deploy *Deploy) deploy() {
if deploy.StartCallbackFn != nil {
deploy.StartCallbackFn(deploy.ID, srv.ID, nil)
}
srv.Deploy(deploy)
srvStatus := srv.Status()
// check task exists
var srvStatus *ServerStatus
if exists := deployTaskList.exists(deploy.ID); exists {
srv.Deploy(deploy)
srvStatus = srv.Status()
} else {
srvStatus = &ServerStatus{
Status: STATUS_TERMINATE,
Error: errors.New("deploy task run failed, cmd is terminated"),
}
}

if deploy.CallbackFn != nil {
deploy.CallbackFn(deploy.ID, srv.ID, srvStatus)
}
Expand Down Expand Up @@ -122,3 +138,39 @@ func (deploy *Deploy) Terminate() {
}
}

type deployTask struct {
deployList map[int][]*Deploy
mu sync.Mutex
}

func (dt *deployTask) exists(id int) bool {
dt.mu.Lock()
defer dt.mu.Unlock()
_, exists := dt.deployList[id]
return exists
}

func (dt *deployTask) append(id int, deploy []*Deploy) error {
if dt.exists(id) {
return errors.New("deploy task have exists")
}
dt.mu.Lock()
defer dt.mu.Unlock()
dt.deployList[id] = deploy
return nil
}

func (dt *deployTask) remove(id int) {
dt.mu.Lock()
defer dt.mu.Unlock()
delete(dt.deployList, id)
}

func (dt *deployTask) stop(id int) {
deploy, exists := dt.deployList[id]
if exists {
for _, d := range deploy {
d.Terminate()
}
}
}
22 changes: 12 additions & 10 deletions deploy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (srv *Server) Deploy(deploy *Deploy) {
}

func (srv *Server) Terminate() {
srv.task.Terminate()
if srv.status.Status == STATUS_RUNNING {
srv.task.Terminate()
}
}

func (srv *Server) Status() *ServerStatus {
Expand Down Expand Up @@ -130,14 +132,14 @@ func (srv *Server) deployCmd(deploy *Deploy) []string {
cmds = append(
cmds,
fmt.Sprintf("/usr/bin/env ssh -o StrictHostKeyChecking=no %s %s %s@%s '%s'",
useCustomKey,
useSshPort,
srv.User,
srv.Addr,
deploy.PostCmd,
),
)
}
return cmds
useCustomKey,
useSshPort,
srv.User,
srv.Addr,
deploy.PostCmd,
),
)
}
return cmds
}

5 changes: 4 additions & 1 deletion model/deploy_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ type DeployApply struct {
BranchName string `gorm:"type:varchar(100);not null;default:''"`
CommitVersion string `gorm:"type:varchar(50);not null;default:''"`
AuditStatus int `gorm:"type:int(11);not null;default:0"`
AuditRefusalReasion string `gorm:"type:varchar(500);not null;default:''"`
AuditRefusalReasion string `gorm:"type:varchar(500);not null;default:''"`
Status int `gorm:"type:int(11);not null;default:0"`
UserId int `gorm:"type:int(11);not null;default:0"`
RollbackId int `gorm:"type:int(11);not null;default:0"`
RollbackApplyId int `gorm:"type:int(11);not null;default:0"`
IsRollbackApply int `gorm:"type:int(11);not null;default:0"`
Ctime int `gorm:"type:int(11);not null;default:0"`
}

Expand Down
68 changes: 67 additions & 1 deletion module/deploy/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ type Apply struct {
CommitVersion string `json:"commit_version"`
AuditStatus int `json:"audit_status"`
AuditRefusalReasion string `json:"audit_refusal_reasion"`
RollbackId int `json:"rollback_id"`
RollbackApplyId int `json:"rollback_apply_id"`
IsRollbackApply int `json:"is_rollback_apply"`
Status int `json:"status"`
UserId int `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
RollbackStatus int `json:"rollback_status"`
Ctime int `json:"ctime"`
}

Expand All @@ -41,8 +45,43 @@ const (
APPLY_STATUS_SUCCESS = 3
APPLY_STATUS_FAILED = 4
APPLY_STATUS_DROP = 5
APPLY_STATUS_ROLLBACK = 6
)

func (a *Apply) RollbackList() ([]Apply, error) {
apply := &model.DeployApply{}
list, ok := apply.List(model.QueryParam{
Fields: "id, name",
Limit: 10,
Order: "ctime DESC",
Where: []model.WhereParam{
model.WhereParam{
Field: "project_id",
Prepare: a.ProjectId,
},
model.WhereParam{
Field: "audit_status",
Prepare: AUDIT_STATUS_OK,
},
model.WhereParam{
Field: "status",
Prepare: APPLY_STATUS_SUCCESS,
},
},
})
if !ok {
return nil, errors.New("get apply list failed")
}
var applyList []Apply
for _, l := range list {
applyList = append(applyList, Apply{
ID: l.ID,
Name: l.Name,
})
}
return applyList, nil
}

func (a *Apply) DropStatus() error {
apply := &model.DeployApply{}
updateData := map[string]interface{}{
Expand Down Expand Up @@ -103,6 +142,26 @@ func (a *Apply) UpdateStatus() error {
return nil
}

func (a *Apply) UpdateRollback() error {
apply := &model.DeployApply{}
updateData := map[string]interface{}{
"rollback_apply_id": a.RollbackApplyId,
"status": APPLY_STATUS_ROLLBACK,
}
if ok := apply.UpdateByFields(updateData, model.QueryParam{
Where: []model.WhereParam{
model.WhereParam{
Field: "id",
Prepare: a.ID,
},
},
}); !ok {
return errors.New("update deploy apply rollback_apply_id failed")
}

return nil
}

func (a *Apply) UpdateAuditStatus() error {
apply := &model.DeployApply{}
updateData := map[string]interface{}{
Expand Down Expand Up @@ -140,6 +199,10 @@ func (a *Apply) Detail() error {
a.AuditStatus = apply.AuditStatus
a.Status = apply.Status
a.UserId = apply.UserId
a.RollbackId = apply.RollbackId
a.RollbackApplyId = apply.RollbackApplyId
a.IsRollbackApply = apply.IsRollbackApply
a.RollbackStatus = APPLY_STATUS_NONE
a.Ctime = apply.Ctime
return nil
}
Expand Down Expand Up @@ -196,7 +259,7 @@ func (a *Apply) List(keyword string, spaceIds []int, offset, limit int) ([]Apply
Where: a.parseWhereConds(keyword, spaceIds),
})
if !ok {
return nil, errors.New("get project list failed")
return nil, errors.New("get apply list failed")
}
var applyList []Apply
for _, l := range list {
Expand Down Expand Up @@ -225,10 +288,13 @@ func (a *Apply) Create() error {
Status: a.Status,
UserId: a.UserId,
AuditStatus: a.AuditStatus,
RollbackId: a.RollbackId,
IsRollbackApply: a.IsRollbackApply,
}
if ok := apply.Create(); !ok {
return errors.New("create deploy apply failed")
}
a.ID = apply.ID
return nil
}

Expand Down
16 changes: 16 additions & 0 deletions module/deploy/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ func (b *Build) Create() error {
return nil
}

func (b *Build) CreateFull() error {
build := &model.DeployBuild{
ApplyId: b.ApplyId,
StartTime: b.StartTime,
FinishTime: b.FinishTime,
Status: b.Status,
Tar: b.Tar,
Output: b.Output,
Errmsg: b.Errmsg,
}
if ok := build.Create(); !ok {
return errors.New("create deploy build failed")
}
return nil
}

func (b *Build) Detail() error {
build := &model.DeployBuild{}
if ok := build.GetByApplyId(b.ApplyId); !ok {
Expand Down
3 changes: 3 additions & 0 deletions module/user/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ var privToApiMap = map[int][]string{
reqApi.PROJECT_LIST,
reqApi.DEPLOY_APPLY_PROJECT_DETAIL,
reqApi.DEPLOY_APPLY_SUBMIT,
reqApi.DEPLOY_APPLY_ROLLBACK,
},
DEPLOY_VIEW: []string{
reqApi.DEPLOY_APPLY_PROJECT_ALL,
Expand Down Expand Up @@ -217,6 +218,8 @@ var privToApiMap = map[int][]string{
reqApi.DEPLOY_BUILD_STOP,
reqApi.DEPLOY_DEPLOY_START,
reqApi.DEPLOY_DEPLOY_STATUS,
reqApi.DEPLOY_DEPLOY_STOP,
reqApi.DEPLOY_DEPLOY_ROLLBACK,
},
/*
DEPLOY_AUDIT_MY: []string{
Expand Down
42 changes: 42 additions & 0 deletions module/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ func (u *User) UserCheckExists() (bool, error) {
return count > 0, nil
}

func (u *User) UpdatePassword() error {
user := &model.User{}
salt := gostring.StrRandom(10)
password := gostring.StrMd5(gostring.JoinStrings(u.Password, salt))
updateData := map[string]interface{}{
"password": password,
"salt": salt,
}
ok := user.UpdateByFields(updateData, model.QueryParam{
Where: []model.WhereParam{
model.WhereParam{
Field: "id",
Prepare: u.ID,
},
},
})
if !ok {
return errors.New("user password update failed")
}
return nil
}

func (u *User) CreateOrUpdate() error {
var salt, password string
if u.Password != "" {
Expand Down Expand Up @@ -277,6 +299,26 @@ func (u *User) CreateOrUpdate() error {
return nil
}

func (u *User) UserSettingUpdate() error {
user := &model.User{}
updateData := map[string]interface{}{
"truename": u.Truename,
"mobile": u.Mobile,
}
ok := user.UpdateByFields(updateData, model.QueryParam{
Where: []model.WhereParam{
model.WhereParam{
Field: "id",
Prepare: u.ID,
},
},
})
if !ok {
return errors.New("user update failed")
}
return nil
}

func (u *User) parseWhereConds(keyword string) []model.WhereParam {
var where []model.WhereParam
if keyword != "" {
Expand Down
Loading

0 comments on commit 764fb64

Please sign in to comment.