-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DVKunion
committed
Mar 7, 2023
0 parents
commit c6c241f
Showing
22 changed files
with
1,327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
before: | ||
hooks: | ||
- go mod tidy | ||
builds: | ||
- binary: collar | ||
env: | ||
- CGO_ENABLED=0 | ||
main: ./cmd/cli.go | ||
goos: | ||
- linux | ||
- windows | ||
- darwin | ||
goarm: | ||
- 6 | ||
- 7 | ||
ldflags: | ||
- -s -w -X github.com/DVKunion/collar/pkg/config.Version={{.Version}} | ||
archives: | ||
- replacements: | ||
darwin: darwin | ||
linux: linux | ||
windows: windows | ||
386: i386 | ||
amd64: x86_64 | ||
checksum: | ||
name_template: 'checksums.txt' | ||
snapshot: | ||
name_template: "{{ .Tag }}-next" | ||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- '^docs:' | ||
- '^test:' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: collar | ||
|
||
on: | ||
push: | ||
tags: | ||
- "v*" | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.18 | ||
|
||
- name: Build | ||
run: go build --ldflags "-s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version=${{github.ref_name}}" cmd/cli.go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: release | ||
|
||
on: | ||
push: | ||
tags: | ||
- "v*" | ||
|
||
jobs: | ||
build: | ||
name: GoReleaser build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.18 | ||
id: go | ||
|
||
- name: Run GoReleaser | ||
uses: goreleaser/goreleaser-action@v2 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: -f .github/conf/.goreleaser.yml | ||
workdir: . | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
.DS_Store | ||
.config.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Collar | ||
|
||
![](https://cdn.dvkunion.cn/collar/collar.png) | ||
|
||
Collar 是长亭牧云主机助手(Collie) 的 CLI 工具,方便使用者在终端管理 Collie。 | ||
|
||
Collar 作为 Collie 牧羊犬的项圈,希望能让用户更加舒适的管理和使用 Collie。 | ||
|
||
会逐步从 Collie 核心功能(文件管理、在线终端)为主,抽空慢慢填齐其余的全部的功能(Docker、资源负载显示、登陆历史、进程清单等) | ||
|
||
在这个基础上,可能还会做一些功能的优化,比如多主机的数据聚合、多主机同步执行命令(类ansible)等。 | ||
|
||
目前已支持的功能模块: | ||
|
||
+ 主机列表 | ||
+ 进程列表 | ||
+ shell终端 | ||
|
||
## 开始使用 | ||
|
||
首先注册百川云平台,并开通 牧云主机助手 应用。 | ||
|
||
然后点击工作台-Token管理-生成Token,勾选所有牧云主机助手相关权限,生成您的Token信息。 | ||
|
||
![](https://cdn.dvkunion.cn/collar/c1d25c402e94487b8e0dcfd18d4c297a.png) | ||
|
||
> 请注意,token 存在有效期。为了方便使用,您可以申请一个时间比较长的 token。 | ||
> 您的 token 十分珍贵,可以获取您所有主机的权限!请妥善保管~ | ||
然后在github-release页面下载符合自己操作系统的二进制文件,放置在$PATH目录下 | ||
|
||
执行: | ||
`collar auth -t YOUT_TOKEN` | ||
|
||
初始化身份认证成功,即可开始使用。 | ||
|
||
## 使用手册 | ||
|
||
### 主机列表 | ||
|
||
`collar hosts` | ||
|
||
获取主机列表信息。 | ||
|
||
### 进程列表 | ||
`collar top [hostId/host_name/host_ip/host_inner_ip]` | ||
|
||
获取主机进程信息, 每3s更新一次。 | ||
|
||
### 登陆主机 Terminal | ||
|
||
`collar shell [hostId/host_name/host_ip/host_inner_ip]` | ||
|
||
可以通过 主机ID/主机名/主机IP/主机内网IP 进行登录。 | ||
|
||
**使用自动登陆模式**: | ||
|
||
`collar shell -a [hostId/host_name/host_ip/host_inner_ip]` | ||
|
||
这将会使用您配置的自动登录用户名进行登陆(暂不支持通过cli设置自动登录用户名) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/DVKunion/collar/pkg/utils" | ||
"os" | ||
"time" | ||
|
||
"github.com/DVKunion/collar/pkg/config" | ||
"github.com/DVKunion/collar/pkg/log" | ||
"github.com/DVKunion/collar/pkg/sdk" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
token string | ||
auto bool | ||
rootCmd = &cobra.Command{} | ||
authCmd = &cobra.Command{ | ||
Use: "auth", | ||
Short: "auth rivers.chaitin.cn", | ||
RunE: auth, | ||
} | ||
listCmd = &cobra.Command{ | ||
Use: "hosts", | ||
Short: "list hosts", | ||
PreRunE: auth, | ||
RunE: hosts, | ||
} | ||
|
||
topCmd = &cobra.Command{ | ||
Use: "top", | ||
Short: "list host process && systemInfo", | ||
PreRunE: auth, | ||
RunE: process, | ||
} | ||
|
||
shellCmd = &cobra.Command{ | ||
Use: "shell", | ||
Short: "run a shell at host", | ||
PreRunE: auth, | ||
RunE: terminal, | ||
} | ||
|
||
versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("Collie - Collar: %s\n", config.Version) | ||
}, | ||
} | ||
) | ||
|
||
func auth(cmd *cobra.Command, args []string) error { | ||
err := config.SingleConfig.Load() | ||
if err != nil { | ||
// 说明加载失败了,需要重新认证 | ||
if token == "" { | ||
return errors.New("empty token") | ||
} | ||
config.SingleConfig = &config.Config{ | ||
Token: token, | ||
HostList: make([]config.Host, 0), | ||
UpdateAt: time.Now(), | ||
} | ||
} | ||
|
||
resp, errSDK := sdk.GetHostList() | ||
if errSDK != nil { | ||
return errSDK | ||
} | ||
|
||
if v, ok := resp.Body().([]config.Host); ok { | ||
config.SingleConfig.HostList = v | ||
// 说明加载的情况 | ||
if err != nil { | ||
log.Infof(cmd.Context(), "success auth ! host total: %d", len(config.SingleConfig.HostList)) | ||
log.Infof(cmd.Context(), "online: %d", len(config.SingleConfig.GetOnline())) | ||
log.Infof(cmd.Context(), "offline: %d", len(config.SingleConfig.HostList)-len(config.SingleConfig.GetOnline())) | ||
} | ||
return config.SingleConfig.Save() | ||
} else { | ||
return errors.New("get host list error") | ||
} | ||
} | ||
|
||
func hosts(cmd *cobra.Command, args []string) error { | ||
log.Info(cmd.Context(), config.SingleConfig.HostList) | ||
return nil | ||
} | ||
|
||
func process(cmd *cobra.Command, args []string) error { | ||
resp, err := sdk.GetProcessList(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info(cmd.Context(), resp.Body()) | ||
return nil | ||
} | ||
|
||
func terminal(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
if len(args) != 1 { | ||
return errors.New("must have at least one host arg") | ||
} | ||
log.Infof(ctx, "connecting to %s ......", args[0]) | ||
hostId := utils.Trans2HostId(args[0]) | ||
if hostId == "" { | ||
return errors.New("host offline or not exists,use `collar hosts` to check host status。") | ||
} | ||
user := "" | ||
if auto { | ||
resp, err := sdk.GetAutoLoginUser(hostId) | ||
if err != nil { | ||
return err | ||
} | ||
user = resp.Body().(string) | ||
} | ||
tm, err := sdk.GetTerminal(ctx, hostId, user) | ||
if err != nil { | ||
return err | ||
} | ||
if err = tm.Transfer(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
authCmd.Flags().StringVarP(&token, "token", "t", "", "users token") | ||
shellCmd.Flags().BoolVarP(&auto, "auto", "a", false, "use auto login") | ||
|
||
rootCmd.AddCommand(authCmd) | ||
rootCmd.AddCommand(listCmd) | ||
rootCmd.AddCommand(topCmd) | ||
rootCmd.AddCommand(shellCmd) | ||
rootCmd.AddCommand(versionCmd) | ||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
ui "github.com/gizak/termui/v3" | ||
"github.com/gizak/termui/v3/widgets" | ||
) | ||
|
||
func main() { | ||
if err := ui.Init(); err != nil { | ||
log.Fatalf("failed to initialize termui: %v", err) | ||
} | ||
defer ui.Close() | ||
|
||
p := widgets.NewParagraph() | ||
p.Text = "Hello World!" | ||
p.SetRect(0, 0, 25, 5) | ||
|
||
ui.Render(p) | ||
|
||
for e := range ui.PollEvents() { | ||
if e.Type == ui.KeyboardEvent { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module github.com/DVKunion/collar | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/containerd/console v1.0.3 | ||
github.com/gizak/termui/v3 v3.1.0 | ||
github.com/go-resty/resty/v2 v2.7.0 | ||
github.com/gogf/gf/v2 v2.3.2 | ||
github.com/gorilla/websocket v1.5.0 | ||
github.com/spf13/cobra v1.6.1 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/fatih/color v1.13.0 // indirect | ||
github.com/fsnotify/fsnotify v1.5.4 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/inconshreveable/mousetrap v1.0.1 // indirect | ||
github.com/mattn/go-colorable v0.1.9 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/mattn/go-runewidth v0.0.9 // indirect | ||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect | ||
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
go.opentelemetry.io/otel v1.7.0 // indirect | ||
go.opentelemetry.io/otel/sdk v1.7.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.7.0 // indirect | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect | ||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect | ||
) |
Oops, something went wrong.