1
0
forked from x/icebergs
icebergs/core/code/install.go
2020-09-11 21:37:49 +08:00

190 lines
7.4 KiB
Go

package code
import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/base/nfs"
"github.com/shylinux/icebergs/base/tcp"
"github.com/shylinux/icebergs/base/web"
kit "github.com/shylinux/toolkits"
"fmt"
"os"
"path"
"strings"
)
var _contexts = kit.Dict(
"tmux", `
# 终端环境
curl -fLo /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo && yum -y update
yum install -y tmux vim
`,
"base", `
# 生产环境
mkdir contexts; cd contexts
export ctx_dev={{.Option "httphost"}} ctx_log=/dev/stdout; curl $ctx_dev/publish/ice.sh |sh
bin/ice.sh`,
"miss", `
# 开发环境
yum install -y make git go
mkdir ~/.ssh &>/dev/null; touch ~/.ssh/config; [ -z "$(cat ~/.ssh/config|grep 'HOST {{.Option "hostname"}}')" ] && echo -e "HOST {{.Option "hostname"}}\n Port 9030" >> ~/.ssh/config
export ISH_CONF_HUB_PROXY={{.Option "userhost"}}:.ish/pluged/
git clone $ISH_CONF_HUB_PROXY/github.com/shylinux/contexts && cd contexts
source etc/miss.sh
touch ~/.gitconfig; [ -z "$(cat ~/.gitconfig|grep '\[url \"{{.Option "userhost"}}')" ] && echo -e "[url \"{{.Option "userhost"}}:ish/pluged/\"]\n insteadOf=\"https://github.com/\"\n" >> ~/.gitconfig
git clone https://github.com/shylinux/contexts && cd contexts
source etc/miss.sh
`,
)
const INSTALL = "install"
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
INSTALL: {Name: INSTALL, Help: "安装", Value: kit.Data(
kit.MDB_SHORT, kit.MDB_NAME, kit.MDB_PATH, "usr/install",
"contexts", _contexts,
)},
},
Commands: map[string]*ice.Command{
INSTALL: {Name: "install name=auto port=auto path=auto auto", Help: "安装", Meta: kit.Dict(), Action: map[string]*ice.Action{
"contexts": {Name: "contexts", Help: "环境", Hand: func(m *ice.Message, arg ...string) {
u := kit.ParseURL(m.Option(ice.MSG_USERWEB))
m.Option("httphost", fmt.Sprintf("%s://%s:%s", u.Scheme, strings.Split(u.Host, ":")[0], kit.Select(kit.Select("80", "443", u.Scheme == "https"), strings.Split(u.Host, ":"), 1)))
m.Option("hostport", fmt.Sprintf("%s:%s", strings.Split(u.Host, ":")[0], kit.Select(kit.Select("80", "443", u.Scheme == "https"), strings.Split(u.Host, ":"), 1)))
m.Option("hostname", strings.Split(u.Host, ":")[0])
m.Option("userhost", fmt.Sprintf("%s@%s", m.Option(ice.MSG_USERNAME), strings.Split(u.Host, ":")[0]))
m.Option("hostpath", kit.Path("./.ish/pluged"))
if buf, err := kit.Render(m.Conf(INSTALL, kit.Keys("meta.contexts", kit.Select("base", arg, 0))), m); m.Assert(err) {
m.Cmdy("web.wiki.spark", "shell", string(buf))
}
}},
"download": {Name: "download link", Help: "下载", Hand: func(m *ice.Message, arg ...string) {
name := path.Base(arg[0])
if m.Richs(INSTALL, "", name, func(key string, value map[string]interface{}) {
if _, e := os.Stat(path.Join(m.Conf(INSTALL, kit.META_PATH), kit.Format(value[kit.MDB_NAME]))); e == nil {
m.Push(key, value, []string{kit.MDB_TIME, kit.MDB_STEP, kit.MDB_SIZE, kit.MDB_TOTAL, kit.MDB_NAME, kit.MDB_LINK})
m.Option("_process", "_progress")
}
}) != nil && len(m.Appendv(kit.MDB_NAME)) > 0 {
// 已经下载
return
}
// 进度
m.Cmd(mdb.INSERT, m.Prefix(INSTALL), "", mdb.HASH, kit.MDB_NAME, name, kit.MDB_LINK, arg[0])
m.Richs(INSTALL, "", name, func(key string, value map[string]interface{}) {
m.Optionv("progress", func(size int, total int) {
s := size * 100 / total
if s != kit.Int(value[kit.MDB_STEP]) && s%10 == 0 {
m.Log_IMPORT(kit.MDB_FILE, name, kit.MDB_STEP, s, kit.MDB_SIZE, kit.FmtSize(int64(size)), kit.MDB_TOTAL, kit.FmtSize(int64(total)))
}
value[kit.MDB_STEP], value[kit.MDB_SIZE], value[kit.MDB_TOTAL] = s, size, total
})
})
// 占位
p := path.Join(m.Conf(INSTALL, kit.META_PATH), name)
if strings.Contains(p, "/") {
os.MkdirAll(path.Base(p), ice.MOD_DIR)
}
m.Cmd(cli.SYSTEM, "touch", p)
m.Option("_process", "_progress")
m.Gos(m, func(m *ice.Message) {
// 下载
m.Option(cli.CMD_DIR, m.Conf(INSTALL, kit.META_PATH))
if strings.HasPrefix(arg[0], "ftp") {
m.Cmdy(cli.SYSTEM, "wget", arg[0])
} else {
msg := m.Cmd(web.SPIDE, web.SPIDE_DEV, web.SPIDE_CACHE, web.SPIDE_GET, arg[0])
m.Cmdy(nfs.LINK, p, msg.Append(kit.MDB_FILE))
}
// 解压
m.Cmd(cli.SYSTEM, "tar", "xvf", name)
})
}},
"build": {Name: "build link", Help: "构建", Hand: func(m *ice.Message, arg ...string) {
p := m.Option(cli.CMD_DIR, path.Join(m.Conf(INSTALL, kit.META_PATH), kit.TrimExt(arg[0])))
switch cb := m.Optionv("prepare").(type) {
case func(string):
cb(p)
default:
if m.Cmdy(cli.SYSTEM, "./configure", "--prefix="+kit.Path(path.Join(p, INSTALL)), arg[1:]); m.Append(cli.CMD_CODE) != "0" {
return
}
}
if m.Cmdy(cli.SYSTEM, "make", "-j8"); m.Append(cli.CMD_CODE) != "0" {
return
}
if m.Cmdy(cli.SYSTEM, "make", "PREFIX="+kit.Path(path.Join(p, INSTALL)), "install"); m.Append(cli.CMD_CODE) != "0" {
return
}
}},
"spawn": {Name: "spawn link", Help: "新建", Hand: func(m *ice.Message, arg ...string) {
port := m.Cmdx(tcp.PORT, "select")
target := path.Join(m.Conf(cli.DAEMON, kit.META_PATH), port)
source := path.Join(m.Conf(INSTALL, kit.META_PATH), kit.TrimExt(arg[0]))
m.Cmd(nfs.DIR, path.Join(source, kit.Select("install", m.Option("install")))).Table(func(index int, value map[string]string, head []string) {
m.Cmd(cli.SYSTEM, "cp", "-r", strings.TrimSuffix(value[kit.MDB_PATH], "/"), target)
})
m.Echo(target)
}},
"start": {Name: "start link cmd...", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
p := m.Option(cli.CMD_DIR, m.Cmdx(INSTALL, "spawn", arg[0]))
args := []string{}
switch cb := m.Optionv("prepare").(type) {
case func(string) []string:
args = append(args, cb(p)...)
}
m.Cmdy(cli.DAEMON, arg[1:], args)
}},
"bench": {Name: "bench port cmd...", Help: "压测", Hand: func(m *ice.Message, arg ...string) {
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) == 0 {
// 源码列表
m.Option(mdb.FIELDS, "time,step,size,total,name,link")
m.Cmdy(mdb.SELECT, m.Prefix(INSTALL), "", mdb.HASH)
m.Sort(kit.MDB_TIME, "time_r")
return
}
arg[0] = path.Base(arg[0])
if key := strings.Split(strings.Split(arg[0], "-")[0], ".")[0]; len(arg) == 1 {
// 服务列表
u := kit.ParseURL(m.Option(ice.MSG_USERWEB))
m.Cmd(cli.DAEMON).Table(func(index int, value map[string]string, head []string) {
if strings.Contains(value[kit.MDB_NAME], key) {
m.Push(kit.MDB_TIME, value[kit.MDB_TIME])
m.Push(kit.MDB_PORT, path.Base(value[kit.MDB_DIR]))
m.Push(kit.MDB_STATUS, value[kit.MDB_STATUS])
m.Push(kit.MDB_NAME, value[kit.MDB_NAME])
m.Push(kit.MDB_LINK, m.Cmdx(mdb.RENDER, web.RENDER.A,
kit.Format("http://%s:%s", u.Hostname(), path.Base(value[kit.MDB_DIR]))))
}
})
m.Sort(kit.MDB_TIME, "time_r")
return
}
// 目录列表
m.Option(nfs.DIR_ROOT, path.Join(m.Conf(cli.DAEMON, kit.META_PATH), arg[1]))
m.Cmdy(nfs.DIR, kit.Select("./", arg, 2))
m.Sort(kit.MDB_TIME, "time_r")
}},
},
}, nil)
}