Rainbond/builder/clean/clean.go

133 lines
3.3 KiB
Go
Raw Normal View History

2018-06-08 22:43:37 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package clean
import (
2018-06-11 09:59:52 +08:00
"context"
2018-06-08 22:43:37 +08:00
"os"
"strings"
"time"
2018-06-11 09:59:52 +08:00
"github.com/Sirupsen/logrus"
2018-06-08 22:43:37 +08:00
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/util"
"github.com/docker/engine-api/client"
"github.com/goodrain/rainbond/builder/sources"
)
2018-06-11 09:59:52 +08:00
//Manager CleanManager
type Manager struct {
2018-06-08 22:43:37 +08:00
dclient *client.Client
ctx context.Context
cancel context.CancelFunc
}
2018-06-11 09:59:52 +08:00
//CreateCleanManager create clean manager
func CreateCleanManager() (*Manager, error) {
2018-06-08 22:43:37 +08:00
dclient, err := client.NewEnvClient()
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
2018-06-11 09:59:52 +08:00
c := &Manager{
2018-06-08 22:43:37 +08:00
dclient: dclient,
ctx: ctx,
cancel: cancel,
}
return c, nil
}
2018-06-11 09:59:52 +08:00
//Start start clean
func (t *Manager) Start(errchan chan error) error {
run := func() {
err := util.Exec(t.ctx, func() error {
now := time.Now()
datetime := now.AddDate(0, -1, 0)
// Find more than five versions
results, err := db.GetManager().VersionInfoDao().SearchVersionInfo()
if err != nil {
logrus.Error(err)
}
var serviceIDList []string
for _, v := range results {
serviceIDList = append(serviceIDList, v.ServiceID)
}
versions, err := db.GetManager().VersionInfoDao().GetVersionInfo(datetime, serviceIDList)
if err != nil {
logrus.Error(err)
}
2018-06-08 22:59:18 +08:00
2018-06-11 09:59:52 +08:00
for _, v := range versions {
if v.DeliveredType == "image" {
imagePath := v.DeliveredPath
err := sources.ImageRemove(t.dclient, imagePath) //remove image
2018-06-11 12:14:23 +08:00
if err != nil {
if strings.Contains(err.Error(), "No such image") {
logrus.Error(err)
} else {
2018-06-11 09:59:52 +08:00
logrus.Error(err)
continue
}
}
2018-06-08 22:43:37 +08:00
if err := db.GetManager().VersionInfoDao().DeleteVersionInfo(v); err != nil {
logrus.Error(err)
continue
}
2018-06-11 09:59:52 +08:00
logrus.Info("Image deletion successful:", imagePath)
2018-06-08 22:43:37 +08:00
2018-06-11 09:59:52 +08:00
}
if v.DeliveredType == "slug" {
filePath := v.DeliveredPath
if err := os.Remove(filePath); err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
logrus.Error(err)
} else {
2018-06-08 22:43:37 +08:00
logrus.Error(err)
continue
2018-06-11 09:59:52 +08:00
2018-06-08 22:43:37 +08:00
}
2018-06-11 09:59:52 +08:00
}
if err := db.GetManager().VersionInfoDao().DeleteVersionInfo(v); err != nil {
2018-06-08 22:43:37 +08:00
logrus.Error(err)
continue
}
2018-06-11 09:59:52 +08:00
logrus.Info("file deletion successful:", filePath)
2018-06-08 22:43:37 +08:00
}
}
2018-06-11 09:59:52 +08:00
return nil
}, 24*time.Hour)
if err != nil {
errchan <- err
2018-06-08 22:43:37 +08:00
}
}
2018-06-11 09:59:52 +08:00
go run()
2018-06-08 22:43:37 +08:00
return nil
}
2018-06-11 09:59:52 +08:00
//Stop stop
func (t *Manager) Stop() error {
2018-06-08 22:43:37 +08:00
logrus.Info("CleanManager is stoping.")
t.cancel()
return nil
}