From b0909706ece998b588f58a0edb087cbedfc6d6d0 Mon Sep 17 00:00:00 2001 From: ouqiang Date: Tue, 14 Mar 2017 14:31:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BE=AA=E7=8E=AF=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- cmd/web.go | 10 +-- conf/app.ini | 8 -- models/host.go | 1 + models/model.go | 17 ++--- models/task_log.go | 4 +- models/user.go | 2 +- {utils => modules}/ansible/ansible.go | 8 +- modules/app/app.go | 95 ++++++++++++++++++++++++ {utils => modules/crontask}/cron_task.go | 15 ++-- {utils => modules}/setting/setting.go | 9 +-- {utils => modules/utils}/utils.go | 26 ------- scheduler.go | 6 +- service/task.go | 25 ++----- utils/app/app.go | 51 ------------- 15 files changed, 132 insertions(+), 148 deletions(-) delete mode 100644 conf/app.ini rename {utils => modules}/ansible/ansible.go (93%) create mode 100644 modules/app/app.go rename {utils => modules/crontask}/cron_task.go (89%) rename {utils => modules}/setting/setting.go (73%) rename {utils => modules/utils}/utils.go (65%) delete mode 100644 utils/app/app.go diff --git a/.gitignore b/.gitignore index 93d5409..8b2d491 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ _testmain.go .idea data/* -log/* \ No newline at end of file +log/* +conf/install.lock diff --git a/cmd/web.go b/cmd/web.go index 29b829d..4364360 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -6,9 +6,7 @@ import ( "github.com/go-macaron/gzip" "github.com/go-macaron/session" "github.com/go-macaron/csrf" - "scheduler/utils" - "fmt" - "scheduler/utils/app" + "scheduler/modules/app" ) // web服务器默认端口 @@ -31,7 +29,7 @@ var CmdWeb = cli.Command{ func run(ctx *cli.Context) { // 检测环境 - utils.CheckEnv() + app.CheckEnv() // 启动定时任务 runScheduler() m := macaron.Classic() @@ -44,7 +42,9 @@ func run(ctx *cli.Context) { } // 定时任务调度 -func runScheduler() {} +func runScheduler() { + +} // 路由注册 func registerRouter(m *macaron.Macaron) { diff --git a/conf/app.ini b/conf/app.ini deleted file mode 100644 index 40cb672..0000000 --- a/conf/app.ini +++ /dev/null @@ -1,8 +0,0 @@ -[db] -password = wozaixiamen -charset = utf8 -database = cron -host = 127.0.0.1 -port = 3306 -user = root -prefix = diff --git a/models/host.go b/models/host.go index a6ebb24..a15b6d7 100644 --- a/models/host.go +++ b/models/host.go @@ -1,5 +1,6 @@ package models + // 主机 type Host struct { Id int16 `xorm:"smallint pk autoincr"` diff --git a/models/model.go b/models/model.go index 7684fd6..4b0235a 100644 --- a/models/model.go +++ b/models/model.go @@ -3,24 +3,17 @@ package models import ( "github.com/go-xorm/xorm" "fmt" - "scheduler/utils/setting" + "scheduler/modules/setting" "github.com/go-xorm/core" _ "github.com/go-sql-driver/mysql" "gopkg.in/macaron.v1" - "scheduler/utils/app" ) -var Db *xorm.Engine - -func init() { - if app.Installed { - Db = createDb() - } -} - type Status int8 type CommonMap map[string]interface{} +var Db *xorm.Engine + const ( Disabled Status = 0 // 禁用 Failure Status = 0 // 失败 @@ -36,8 +29,8 @@ const ( ) // 创建Db -func createDb() *xorm.Engine{ - config,err := setting.Read() +func CreateDb(configFile string) *xorm.Engine{ + config,err := setting.Read(configFile) if err != nil { panic(err) } diff --git a/models/task_log.go b/models/task_log.go index c8d241f..3bc23af 100644 --- a/models/task_log.go +++ b/models/task_log.go @@ -1,6 +1,8 @@ package models -import "time" +import ( + "time" +) type TaskLog struct{ Id int `xorm:"pk autoincr"` diff --git a/models/user.go b/models/user.go index e8298f0..834ab49 100644 --- a/models/user.go +++ b/models/user.go @@ -2,7 +2,7 @@ package models import ( "time" - "scheduler/utils" + "scheduler/modules/utils" ) const PasswordSaltLength = 6; diff --git a/utils/ansible/ansible.go b/modules/ansible/ansible.go similarity index 93% rename from utils/ansible/ansible.go rename to modules/ansible/ansible.go index 22195c1..2af54fd 100644 --- a/utils/ansible/ansible.go +++ b/modules/ansible/ansible.go @@ -4,18 +4,12 @@ package ansible import ( "os" - "scheduler/utils" + "scheduler/modules/utils" "errors" "gopkg.in/yaml.v2" "io/ioutil" - "scheduler/utils/app" ) -func init() { - // ansible配置文件目录 - os.Setenv("ANSIBLE_CONFIG", app.ConfDir) -} - type Handler map[string]interface{} type Playbook struct { diff --git a/modules/app/app.go b/modules/app/app.go new file mode 100644 index 0000000..d79e320 --- /dev/null +++ b/modules/app/app.go @@ -0,0 +1,95 @@ +package app + +import ( + "os" + "scheduler/modules/crontask" + "scheduler/models" + "runtime" + "scheduler/modules/utils" +) + +var ( + AppDir string // 应用根目录 + ConfDir string // 配置目录 + LogDir string // 日志目录 + DataDir string // 数据目录,存放session文件等 + AppConfig string // 应用配置文件 + Installed bool // 应用是否安装过 + CronTask crontask.CronTask // 定时任务 +) + +func init() { + wd, err := os.Getwd() + if err != nil { + panic(err) + } + AppDir = wd + ConfDir = AppDir + "/conf" + LogDir = AppDir + "/log" + DataDir = AppDir + "/data" + AppConfig = AppDir + "/app.ini" + checkDirExists(ConfDir, LogDir, DataDir) + // ansible配置文件目录 + os.Setenv("ANSIBLE_CONFIG", ConfDir) + Installed = IsInstalled() + if Installed { + initResource() + } +} + +// 判断应用是否安装过 +func IsInstalled() bool { + _, err := os.Stat(ConfDir + "/install.lock") + if os.IsNotExist(err) { + return false + } + + return true +} + +// 检测环境 +func CheckEnv() { + // ansible不支持安装在windows上, windows只能作为被控机 + if runtime.GOOS == "windows" { + panic("不支持在windows上运行") + } + _, err := utils.ExecShell("ansible", "--version") + if err != nil { + panic(err) + } + _, err = utils.ExecShell("ansible-playbook", "--version") + if err != nil { + panic("ansible-playbook not found") + } +} + +// 创建安装锁文件 +func CreateInstallLock() error { + _, err := os.Create(ConfDir + "/install.lock") + if err != nil { + utils.RecordLog("创建安装锁文件失败") + } + + return err +} + + +// 初始化资源 +func initResource() { + crontask.DefaultCronTask = crontask.CreateCronTask() + + models.Db = models.CreateDb(AppConfig) +} + +// 检测目录是否存在 +func checkDirExists(path... string) { + for _, value := range(path) { + _, err := os.Stat(value) + if os.IsNotExist(err) { + panic(value + "目录不存在") + } + if os.IsPermission(err) { + panic(value + "目录无权限操作") + } + } +} \ No newline at end of file diff --git a/utils/cron_task.go b/modules/crontask/cron_task.go similarity index 89% rename from utils/cron_task.go rename to modules/crontask/cron_task.go index 8572f8a..231146c 100644 --- a/utils/cron_task.go +++ b/modules/crontask/cron_task.go @@ -1,25 +1,22 @@ -package utils +package crontask import ( "github.com/robfig/cron" "errors" - "scheduler/utils/app" "sync" ) -var DefaultCronTask CronTask; +var DefaultCronTask *CronTask type CronTask struct { sync.RWMutex tasks map[string]*cron.Cron } -func init() { - if app.Installed { - DefaultCronTask = CronTask{ - sync.RWMutex{}, - make(map[string]*cron.Cron), - } +func CreateCronTask() *CronTask { + return &CronTask { + sync.RWMutex{}, + make(map[string]*cron.Cron), } } diff --git a/utils/setting/setting.go b/modules/setting/setting.go similarity index 73% rename from utils/setting/setting.go rename to modules/setting/setting.go index c8815e7..8becf67 100644 --- a/utils/setting/setting.go +++ b/modules/setting/setting.go @@ -3,12 +3,11 @@ package setting import ( "gopkg.in/ini.v1" "errors" - "scheduler/utils/app" ) // 读取配置 -func Read() (config *ini.File, err error) { - config, err = ini.Load(app.AppConfig) +func Read(filename string) (config *ini.File, err error) { + config, err = ini.Load(filename) if err != nil { return } @@ -18,7 +17,7 @@ func Read() (config *ini.File, err error) { // 写入配置 -func Write(config map[string]map[string]string) (error) { +func Write(config map[string]map[string]string, filename string) (error) { if len(config) == 0 { return errors.New("参数不能为空") } @@ -39,7 +38,7 @@ func Write(config map[string]map[string]string) (error) { } } } - err := file.SaveTo(app.AppConfig) + err := file.SaveTo(filename) return err } \ No newline at end of file diff --git a/utils/utils.go b/modules/utils/utils.go similarity index 65% rename from utils/utils.go rename to modules/utils/utils.go index 0bee367..98dabe8 100644 --- a/utils/utils.go +++ b/modules/utils/utils.go @@ -7,34 +7,8 @@ import ( "crypto/md5" "encoding/hex" "log" - "os" - "runtime" - "scheduler/utils/app" ) -// 检测环境 -func CheckEnv() { - // ansible不支持安装在windows上, windows只能作为被控机 - if runtime.GOOS == "windows" { - panic("不支持在windows上运行") - } - _, err := ExecShell("ansible", "--version") - if err != nil { - panic(err) - } - _, err = ExecShell("ansible-playbook", "--version") - if err != nil { - panic("ansible-playbook not found") - } -} - -// 创建安装锁文件 -func CreateInstallLock() { - _, err := os.Create(app.ConfDir + "/install.lock") - if err != nil { - RecordLog("创建安装锁文件失败") - } -} // 执行shell命令 func ExecShell(command string, args... string) (string, error) { diff --git a/scheduler.go b/scheduler.go index a43ab90..8d99511 100644 --- a/scheduler.go +++ b/scheduler.go @@ -1,9 +1,9 @@ package main /*-------------------------------------------------------- - | 定时任务调度 | - | 兼容Linux crontab时间格式语法,最小粒度可精确到每秒 | - | 支持通过HTTP、SSH协议触发任务执行 | + 定时任务调度 + 兼容Linux crontab时间格式语法,最小粒度可精确到每秒 + 支持通过HTTP、SSH协议触发任务执行 --------------------------------------------------------*/ import ( diff --git a/service/task.go b/service/task.go index 8a1f18b..0cb6230 100644 --- a/service/task.go +++ b/service/task.go @@ -2,29 +2,14 @@ package service import ( "scheduler/models" - "scheduler/utils" + "scheduler/modules/utils" "net/http" "io/ioutil" "strconv" "time" + "scheduler/modules/crontask" ) -func initHosts() []models.Host { - // 获取所有主机 - hostModel := new(models.Host) - list, err := hostModel.List() - if err != nil { - utils.RecordLog("获取主机列表失败-", err.Error()) - return nil - } - if len(list) == 0 { - utils.RecordLog("主机列表为空") - return nil - } - - return list -} - type Task struct {} // 初始化任务,从数据库取出所有任务添加到定时任务 @@ -60,7 +45,7 @@ func(task *Task) Add(taskModel models.Task) { utils.RecordLog("任务协议不存在-协议编号: ", taskModel.Protocol) } if (taskFunc != nil) { - utils.DefaultCronTask.Add(strconv.Itoa(taskModel.Id), taskModel.Spec, taskFunc) + crontask.DefaultCronTask.Add(strconv.Itoa(taskModel.Id), taskModel.Spec, taskFunc) } } @@ -109,4 +94,6 @@ func(h *HTTPHandler) Run(taskModel models.Task) { type SSHHandler struct {} // 执行SSH任务 -func(ssh *SSHHandler) Run(taskModel models.Task) {} \ No newline at end of file +func(ssh *SSHHandler) Run(taskModel models.Task) { + +} \ No newline at end of file diff --git a/utils/app/app.go b/utils/app/app.go deleted file mode 100644 index d811900..0000000 --- a/utils/app/app.go +++ /dev/null @@ -1,51 +0,0 @@ -package app - -import ( - "os" -) - -var ( - AppDir string // 应用根目录 - ConfDir string // 配置目录 - LogDir string // 日志目录 - DataDir string // 数据目录,存放session文件等 - AppConfig string // 应用配置文件 - Installed bool // 应用是否安装过 -) - -func init() { - wd, err := os.Getwd() - if err != nil { - panic(err) - } - AppDir = wd - ConfDir = AppDir + "/conf" - LogDir = AppDir + "/log" - DataDir = AppDir + "/data" - AppConfig = AppDir + "/app.ini" - checkDirExists(ConfDir, LogDir, DataDir) - Installed = isInstalled() -} - -// 判断应用是否安装过 -func isInstalled() bool { - _, err := os.Stat(ConfDir + "/install.lock") - if os.IsExist(err) { - return true - } - - return false -} - -// 检测目录是否存在 -func checkDirExists(path... string) { - for _, value := range(path) { - _, err := os.Stat(value) - if os.IsNotExist(err) { - panic(value + "目录不存在") - } - if os.IsPermission(err) { - panic(value + "目录无权限操作") - } - } -} \ No newline at end of file