Skip to content

Commit 1a1a028

Browse files
committed
开放源码
1 parent 27ad623 commit 1a1a028

File tree

216 files changed

+10714
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

216 files changed

+10714
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gopub*
2+
data/*
3+
logs/*.log
4+
build.tar.gz
5+
*.bak

app/controllers/agent.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package controllers
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/astaxie/beego"
7+
"github.com/astaxie/beego/validation"
8+
"github.com/lisijie/gopub/app/entity"
9+
"github.com/lisijie/gopub/app/libs"
10+
"github.com/lisijie/gopub/app/service"
11+
"strconv"
12+
)
13+
14+
type AgentController struct {
15+
BaseController
16+
}
17+
18+
// 列表
19+
func (this *AgentController) List() {
20+
page, _ := strconv.Atoi(this.GetString("page"))
21+
if page < 1 {
22+
page = 1
23+
}
24+
count, err := service.ServerService.GetTotal(service.SERVER_TYPE_AGENT)
25+
this.checkError(err)
26+
serverList, err := service.ServerService.GetAgentList(page, this.pageSize)
27+
this.checkError(err)
28+
29+
this.Data["count"] = count
30+
this.Data["list"] = serverList
31+
this.Data["pageBar"] = libs.NewPager(page, int(count), this.pageSize, beego.URLFor("AgentController.List"), true).ToString()
32+
this.Data["pageTitle"] = "跳板机列表"
33+
this.display()
34+
}
35+
36+
// 添加
37+
func (this *AgentController) Add() {
38+
if this.isPost() {
39+
server := &entity.Server{}
40+
server.TypeId = service.SERVER_TYPE_AGENT
41+
server.Ip = this.GetString("server_ip")
42+
server.Area = this.GetString("area")
43+
server.SshPort, _ = this.GetInt("ssh_port")
44+
server.SshUser = this.GetString("ssh_user")
45+
server.SshPwd = this.GetString("ssh_pwd")
46+
server.SshKey = this.GetString("ssh_key")
47+
server.WorkDir = this.GetString("work_dir")
48+
server.Description = this.GetString("description")
49+
err := this.validServer(server)
50+
this.checkError(err)
51+
err = service.ServerService.AddServer(server)
52+
this.checkError(err)
53+
//service.ActionService.Add("add_agent", this.auth.GetUserName(), "server", server.Id, server.Ip)
54+
this.redirect(beego.URLFor("AgentController.List"))
55+
}
56+
57+
this.Data["pageTitle"] = "添加跳板机"
58+
this.display()
59+
}
60+
61+
// 编辑
62+
func (this *AgentController) Edit() {
63+
id, _ := this.GetInt("id")
64+
server, err := service.ServerService.GetServer(id, service.SERVER_TYPE_AGENT)
65+
this.checkError(err)
66+
67+
if this.isPost() {
68+
server.Ip = this.GetString("server_ip")
69+
server.Area = this.GetString("area")
70+
server.SshPort, _ = this.GetInt("ssh_port")
71+
server.SshUser = this.GetString("ssh_user")
72+
server.SshPwd = this.GetString("ssh_pwd")
73+
server.SshKey = this.GetString("ssh_key")
74+
server.WorkDir = this.GetString("work_dir")
75+
server.Description = this.GetString("description")
76+
err := this.validServer(server)
77+
this.checkError(err)
78+
err = service.ServerService.UpdateServer(server)
79+
this.checkError(err)
80+
//service.ActionService.Add("edit_agent", this.auth.GetUserName(), "server", server.Id, server.Ip)
81+
this.redirect(beego.URLFor("AgentController.List"))
82+
}
83+
84+
this.Data["pageTitle"] = "编辑跳板机"
85+
this.Data["server"] = server
86+
this.display()
87+
}
88+
89+
// 删除
90+
func (this *AgentController) Del() {
91+
id, _ := this.GetInt("id")
92+
93+
_, err := service.ServerService.GetServer(id, service.SERVER_TYPE_AGENT)
94+
this.checkError(err)
95+
96+
err = service.ServerService.DeleteServer(id)
97+
this.checkError(err)
98+
//service.ActionService.Add("del_agent", this.auth.GetUserName(), "server", id, "")
99+
100+
this.redirect(beego.URLFor("AgentController.List"))
101+
}
102+
103+
// 项目列表
104+
func (this *AgentController) Projects() {
105+
id, _ := this.GetInt("id")
106+
server, err := service.ServerService.GetServer(id, service.SERVER_TYPE_AGENT)
107+
this.checkError(err)
108+
envList, err := service.EnvService.GetEnvListByServerId(id)
109+
this.checkError(err)
110+
111+
result := make(map[int]map[string]interface{})
112+
for _, env := range envList {
113+
if _, ok := result[env.ProjectId]; !ok {
114+
project, err := service.ProjectService.GetProject(env.ProjectId)
115+
if err != nil {
116+
continue
117+
}
118+
row := make(map[string]interface{})
119+
row["projectId"] = project.Id
120+
row["projectName"] = project.Name
121+
row["envName"] = env.Name
122+
result[env.ProjectId] = row
123+
} else {
124+
result[env.ProjectId]["envName"] = result[env.ProjectId]["envName"].(string) + ", " + env.Name
125+
}
126+
}
127+
128+
this.Data["list"] = result
129+
this.Data["server"] = server
130+
this.Data["pageTitle"] = server.Ip + " 下的项目列表"
131+
this.display()
132+
}
133+
134+
func (this *AgentController) validServer(server *entity.Server) error {
135+
valid := validation.Validation{}
136+
valid.Required(server.Ip, "ip").Message("请输入服务器IP")
137+
valid.Range(server.SshPort, 1, 65535, "ssh_port").Message("SSH端口无效")
138+
valid.Required(server.SshUser, "ssh_user").Message("SSH用户名不能为空")
139+
valid.Required(server.WorkDir, "work_dir").Message("工作目录不能为空")
140+
valid.IP(server.Ip, "ip").Message("服务器IP无效")
141+
if valid.HasErrors() {
142+
for _, err := range valid.Errors {
143+
return errors.New(err.Message)
144+
}
145+
}
146+
if server.SshKey != "" && !libs.IsFile(libs.RealPath(server.SshKey)) {
147+
return errors.New("SSH Key不存在:" + server.SshKey)
148+
}
149+
150+
addr := fmt.Sprintf("%s:%d", server.Ip, server.SshPort)
151+
serv := libs.NewServerConn(addr, server.SshUser, server.SshKey)
152+
153+
if err := serv.TryConnect(); err != nil {
154+
return errors.New("无法连接到跳板机: " + err.Error())
155+
} else if _, err := serv.RunCmd("mkdir -p " + server.WorkDir); err != nil {
156+
return errors.New("无法创建跳板机工作目录: " + err.Error())
157+
}
158+
serv.Close()
159+
160+
return nil
161+
}

app/controllers/base.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"github.com/astaxie/beego"
6+
"github.com/beego/i18n"
7+
"github.com/lisijie/gopub/app/service"
8+
"io/ioutil"
9+
"strings"
10+
)
11+
12+
const (
13+
MSG_OK = 0 // ajax输出错误码,成功
14+
MSG_ERR = -1 // 错误
15+
MSG_REDIRECT = -2 // 重定向
16+
)
17+
18+
type BaseController struct {
19+
beego.Controller
20+
auth *service.AuthService // 验证服务
21+
userId int // 当前登录的用户id
22+
controllerName string // 控制器名
23+
actionName string // 动作名
24+
pageSize int // 默认分页大小
25+
lang string // 当前语言环境
26+
}
27+
28+
// 重写GetString方法,移除前后空格
29+
func (this *BaseController) GetString(name string, def ...string) string {
30+
return strings.TrimSpace(this.Controller.GetString(name, def...))
31+
}
32+
33+
func (this *BaseController) Prepare() {
34+
this.Ctx.Output.Header("X-Powered-By", "GoPub/"+beego.AppConfig.String("version"))
35+
this.Ctx.Output.Header("X-Author-By", "lisijie")
36+
controllerName, actionName := this.GetControllerAndAction()
37+
this.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
38+
this.actionName = strings.ToLower(actionName)
39+
this.pageSize = 20
40+
this.initAuth()
41+
this.initLang()
42+
this.getMenuList()
43+
}
44+
45+
func (this *BaseController) initLang() {
46+
this.lang = "zh-CN"
47+
this.Data["lang"] = this.lang
48+
if !i18n.IsExist(this.lang) {
49+
if err := i18n.SetMessage(this.lang, beego.AppPath+"/conf/locale_"+this.lang+".ini"); err != nil {
50+
beego.Error("Fail to set message file: " + err.Error())
51+
return
52+
}
53+
54+
}
55+
}
56+
57+
//登录验证
58+
func (this *BaseController) initAuth() {
59+
token := this.Ctx.GetCookie("auth")
60+
this.auth = service.NewAuth()
61+
this.auth.Init(token)
62+
this.userId = this.auth.GetUserId()
63+
64+
if !this.auth.IsLogined() {
65+
if this.controllerName != "main" ||
66+
(this.controllerName == "main" && this.actionName != "logout" && this.actionName != "login") {
67+
this.redirect(beego.URLFor("MainController.Login"))
68+
}
69+
} else {
70+
if !this.auth.HasAccessPerm(this.controllerName, this.actionName) {
71+
this.showMsg("您没有执行该操作的权限", MSG_ERR)
72+
}
73+
}
74+
}
75+
76+
//渲染模版
77+
func (this *BaseController) display(tpl ...string) {
78+
var tplname string
79+
if len(tpl) > 0 {
80+
tplname = tpl[0] + ".html"
81+
} else {
82+
tplname = this.controllerName + "/" + this.actionName + ".html"
83+
}
84+
85+
this.Layout = "layout/layout.html"
86+
this.TplName = tplname
87+
88+
this.LayoutSections = make(map[string]string)
89+
this.LayoutSections["Header"] = "layout/sections/header.html"
90+
this.LayoutSections["Footer"] = "layout/sections/footer.html"
91+
this.LayoutSections["Navbar"] = "layout/sections/navbar.html"
92+
this.LayoutSections["Sidebar"] = "layout/sections/sidebar.html"
93+
94+
user := this.auth.GetUser()
95+
96+
this.Data["version"] = beego.AppConfig.String("version")
97+
this.Data["curRoute"] = this.controllerName + "." + this.actionName
98+
this.Data["loginUserId"] = user.Id
99+
this.Data["loginUserName"] = user.UserName
100+
this.Data["loginUserSex"] = user.Sex
101+
this.Data["menuList"] = this.getMenuList()
102+
}
103+
104+
// 重定向
105+
func (this *BaseController) redirect(url string) {
106+
if this.IsAjax() {
107+
this.showMsg("", MSG_REDIRECT, url)
108+
} else {
109+
this.Redirect(url, 302)
110+
this.StopRun()
111+
}
112+
}
113+
114+
// 是否POST提交
115+
func (this *BaseController) isPost() bool {
116+
return this.Ctx.Request.Method == "POST"
117+
}
118+
119+
// 提示消息
120+
func (this *BaseController) showMsg(msg string, msgno int, redirect ...string) {
121+
out := make(map[string]interface{})
122+
out["status"] = msgno
123+
out["msg"] = msg
124+
out["redirect"] = ""
125+
if len(redirect) > 0 {
126+
out["redirect"] = redirect[0]
127+
}
128+
129+
if this.IsAjax() {
130+
this.jsonResult(out)
131+
} else {
132+
for k, v := range out {
133+
this.Data[k] = v
134+
}
135+
this.display("error/message")
136+
this.Render()
137+
this.StopRun()
138+
}
139+
}
140+
141+
//获取用户IP地址
142+
func (this *BaseController) getClientIp() string {
143+
if p := this.Ctx.Input.Proxy(); len(p) > 0 {
144+
return p[0]
145+
}
146+
return this.Ctx.Input.IP()
147+
}
148+
149+
// 功能菜单
150+
func (this *BaseController) getMenuList() []Menu {
151+
var menuList []Menu
152+
allMenu := make([]Menu, 0)
153+
content, err := ioutil.ReadFile("conf/menu.json")
154+
if err == nil {
155+
err := json.Unmarshal(content, &allMenu)
156+
if err != nil {
157+
beego.Error(err.Error())
158+
}
159+
}
160+
menuList = make([]Menu, 0)
161+
for _, menu := range allMenu {
162+
subs := make([]SubMenu, 0)
163+
for _, sub := range menu.Submenu {
164+
route := strings.Split(sub.Route, ".")
165+
if this.auth.HasAccessPerm(route[0], route[1]) {
166+
subs = append(subs, sub)
167+
}
168+
}
169+
if len(subs) > 0 {
170+
menu.Submenu = subs
171+
menuList = append(menuList, menu)
172+
}
173+
}
174+
//menuList = allMenu
175+
176+
return menuList
177+
}
178+
179+
// 输出json
180+
func (this *BaseController) jsonResult(out interface{}) {
181+
this.Data["json"] = out
182+
this.ServeJSON()
183+
this.StopRun()
184+
}
185+
186+
// 错误检查
187+
func (this *BaseController) checkError(err error) {
188+
if err != nil {
189+
this.showMsg(err.Error(), MSG_ERR)
190+
}
191+
}

0 commit comments

Comments
 (0)