Skip to content

Commit

Permalink
feat(get): support get app info
Browse files Browse the repository at this point in the history
support get app info

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Jul 1, 2022
1 parent c6d347e commit 5c441a5
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 30 deletions.
23 changes: 16 additions & 7 deletions cmd/manage/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ package manage
import (
"os"

"github.com/davecgh/go-spew/spew"
"github.com/easysoft/qcadmin/internal/app/debug"
qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec"
"github.com/easysoft/qcadmin/internal/pkg/util/factory"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,15 +41,21 @@ func NewCmdGetApp(f factory.Factory) *cobra.Command {
Use: "app",
Aliases: []string{"apps"},
Short: "get app",
Example: `q get app http://console.efbb.haogs.cn`,
Args: cobra.ExactArgs(1),
Example: `q get app http://console.efbb.haogs.cn/instance-view-39.html`,
RunE: func(cmd *cobra.Command, args []string) error {
// node id为0 list
if len(args) == 0 {
log.Debug("get console")
return nil
url := args[0]
log.Infof("start fetch app: %s", url)
appdata, err := debug.GetNameByURL(url)
if err != nil {
return err
}
log.Debug("get app")
return nil
if log.GetLevel() == logrus.DebugLevel {
spew.Dump(appdata)
}
extargs := []string{"exp", "kubectl", "get", "-o", "wide", "pods,deploy,pvc,svc,ing", "-l", "release=" + appdata.K8Name, "-l", "app=" + appdata.Chart}
// extargs = append(extargs, args...)
return qcexec.CommandRun(os.Args[0], extargs...)
},
}
return app
Expand Down
48 changes: 27 additions & 21 deletions cmd/manage/resetpassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"

"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/app/config"
"github.com/easysoft/qcadmin/internal/pkg/k8s"
"github.com/easysoft/qcadmin/internal/pkg/util/factory"
"github.com/ergoapi/util/color"
Expand Down Expand Up @@ -43,31 +44,36 @@ func NewResetPassword(f factory.Factory) *cobra.Command {
Short: "reset qucheng superadmin password",
Aliases: []string{"rp", "re-pass"},
Run: func(cmd *cobra.Command, args []string) {
ips := exnet.LocalIPs()

// 获取节点
k8sClient, err := k8s.NewSimpleClient()
if err != nil {
log.Errorf("k8s client err: %v", err)
return
}
cneapiDeploy, err := k8sClient.GetDeployment(context.Background(), common.DefaultSystem, "cne-api", metav1.GetOptions{})
if err != nil {
log.Errorf("get k8s deploy err: %v", err)
return
}
apiToken := ""
for _, e := range cneapiDeploy.Spec.Template.Spec.Containers[0].Env {
if e.Name == "CNE_API_TOKEN" {
apiToken = e.Value
break
cfg, _ := config.LoadConfig()
if cfg.Token == "" {
k8sClient, err := k8s.NewSimpleClient()
if err != nil {
log.Errorf("k8s client err: %v", err)
return
}
cneapiDeploy, err := k8sClient.GetDeployment(context.Background(), common.DefaultSystem, "qucheng", metav1.GetOptions{})
if err != nil {
log.Errorf("get k8s deploy err: %v", err)
return
}
for _, e := range cneapiDeploy.Spec.Template.Spec.Containers[0].Env {
if e.Name == "CNE_API_TOKEN" {
cfg.APIToken = e.Value
break
}
}
cfg.SaveConfig()
}

if cfg.Domain == "" {
cfg.Domain = fmt.Sprintf("%s:32379", exnet.LocalIPs()[0])
}

log.Debug("fetch api token")
// 更新密码
if len(password) == 0 {
log.Warn("not found password, will generate random password")
password = expass.SaltMd5Pass(apiToken, expass.RandomPassword(16))
password = expass.SaltMd5Pass(cfg.APIToken, expass.RandomPassword(16))
}
log.Infof("update superadmin password: %s", "")
client := req.C()
Expand All @@ -77,9 +83,9 @@ func NewResetPassword(f factory.Factory) *cobra.Command {
var result Result
resp, err := client.R().
SetHeader("accept", "application/json").
SetHeader("TOKEN", apiToken).
SetHeader("TOKEN", cfg.APIToken).
SetBody(&Body{Password: password}).
Post(fmt.Sprintf("http://%s:32379/admin-resetpassword.html", ips[0]))
Post(fmt.Sprintf("http://%s/admin-resetpassword.html", cfg.Domain))
if err != nil {
log.Errorf("update password failed, reason: %v", err)
return
Expand Down
1 change: 1 addition & 0 deletions internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
Generated time.Time `json:"-" yaml:"-"`
DB string `yaml:"db" json:"db"`
Domain string `yaml:"domain" json:"domain"`
APIToken string `yaml:"api_token" json:"api_token"`
InitNode string `yaml:"init_node" json:"init_node"`
Token string `yaml:"token" json:"token"`
Master []Node `yaml:"master" json:"master"`
Expand Down
9 changes: 9 additions & 0 deletions internal/app/debug/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2021-2022 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved.
// Use of this source code is covered by the following dual licenses:
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// (2) Affero General Public License 3.0 (AGPL 3.0)
// license that can be found in the LICENSE file.

package debug

type APP struct{}
91 changes: 91 additions & 0 deletions internal/app/debug/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2021-2022 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved.
// Use of this source code is covered by the following dual licenses:
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// (2) Affero General Public License 3.0 (AGPL 3.0)
// license that can be found in the LICENSE file.

package debug

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/app/config"
"github.com/easysoft/qcadmin/internal/pkg/k8s"
"github.com/ergoapi/util/exnet"
"github.com/imroc/req/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Result struct {
Code int `json:"code"`
Message string `json:"message"`
Data AppData `json:"data"`
}

type AppData struct {
ID string `json:"id"`
Space string `json:"space"`
Name string `json:"name"`
AppID string `json:"appID"`
AppName string `json:"appName"`
AppVersion string `json:"appVersion"`
Chart string `json:"chart"`
Logo string `json:"logo"`
Version string `json:"version"`
Source string `json:"source"`
K8Name string `json:"k8name"`
Status string `json:"status"`
Domain string `json:"domain"`
CreatedBy string `json:"createdBy"`
CreatedAt string `json:"createdAt"`
Deleted string `json:"deleted"`
}

func GetNameByURL(url string) (*AppData, error) {
// 获取ID
k := strings.Split(url, "-")
key := strings.Trim(k[len(k)-1], ".html")

cfg, _ := config.LoadConfig()
if cfg.Token == "" {
k8sClient, err := k8s.NewSimpleClient()
if err != nil {
return nil, err
}
cneapiDeploy, err := k8sClient.GetDeployment(context.Background(), common.DefaultSystem, "qucheng", metav1.GetOptions{})
if err != nil {
return nil, err
}
for _, e := range cneapiDeploy.Spec.Template.Spec.Containers[0].Env {
if e.Name == "CNE_API_TOKEN" {
cfg.APIToken = e.Value
break
}
}
cfg.SaveConfig()
}

if cfg.Domain == "" {
cfg.Domain = fmt.Sprintf("%s:32379", exnet.LocalIPs()[0])
}

client := req.C()
client = client.DevMode().EnableDumpAll()
var result Result
resp, err := client.R().
SetHeader("accept", "application/json").
SetHeader("TOKEN", cfg.APIToken).
Get(fmt.Sprintf("http://%s//instance-apidetail-%s.html", cfg.Domain, key))
if err != nil {
return nil, fmt.Errorf("update password failed, reason: %v", err)
}
if !resp.IsSuccess() {
return nil, fmt.Errorf("update password failed, reason: bad response status %v", resp.Status)
}
json.Unmarshal([]byte(resp.String()), &result)
return &result.Data, nil
}
4 changes: 2 additions & 2 deletions internal/pkg/cluster/qucheng.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ func (p *Cluster) InstallQuCheng() error {
} else {
p.Log.Infof("use custom domain %s", p.Domain)
}

token := p.genQuChengToken()
cfg, _ := config.LoadConfig()
cfg.Domain = p.Domain
cfg.APIToken = token
cfg.SaveConfig()

output, err := qcexec.Command(os.Args[0], "experimental", "helm", "repo-add", "--name", common.DefaultHelmRepoName, "--url", common.GetChartRepo(p.QuchengVersion)).CombinedOutput()
Expand All @@ -134,7 +135,6 @@ func (p *Cluster) InstallQuCheng() error {
return err
}
p.Log.Done("update qucheng install repo done")
token := p.genQuChengToken()
helmchan := common.GetChannel(p.QuchengVersion)
// helm upgrade -i nginx-ingress-controller bitnami/nginx-ingress-controller -n kube-system
helmargs := []string{"experimental", "helm", "upgrade", "--name", common.DefaultChartName, "--repo", common.DefaultHelmRepoName, "--chart", common.DefaultChartName, "--namespace", common.DefaultSystem, "--set", fmt.Sprintf("ingress.host=console.%s", p.Domain), "--set", "env.APP_DOMAIN=" + p.Domain, "--set", "env.CNE_API_TOKEN=" + token, "--set", "cloud.defaultChannel=" + helmchan}
Expand Down

0 comments on commit 5c441a5

Please sign in to comment.