Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix importing grafana admin user / password from ansible #937

Merged
merged 6 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions components/dm/ansible/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ func (im *Importer) ImportFromAnsibleDir() (clusterName string, meta *spec.Metad
}
topo := meta.Topology

// Grafana admin username and password
var grafanaUser string
var grafanaPass string
if group, ok := inventory.Groups["all"]; ok {
for k, v := range group.Vars {
switch k {
Expand All @@ -311,9 +314,10 @@ func (im *Importer) ImportFromAnsibleDir() (clusterName string, meta *spec.Metad
topo.GlobalOptions.DeployDir = v
// ansible convention directory for log
topo.GlobalOptions.LogDir = filepath.Join(v, "log")
// ignore user/pass, we will deploy new one.
case "grafana_admin_user":
grafanaUser = strings.Trim(v, "\"")
case "grafana_admin_password":
grafanaPass = strings.Trim(v, "\"")
default:
fmt.Println("ignore unknown global var ", k, v)
}
Expand Down Expand Up @@ -536,9 +540,11 @@ func (im *Importer) ImportFromAnsibleDir() (clusterName string, meta *spec.Metad
}
}
srv := spec.GrafanaSpec{
Host: host.Vars["ansible_host"],
SSHPort: ansible.GetHostPort(host, cfg),
Port: port,
Host: host.Vars["ansible_host"],
SSHPort: ansible.GetHostPort(host, cfg),
Port: port,
Username: grafanaUser,
Password: grafanaPass,
}

runFileName := filepath.Join(host.Vars["deploy_dir"], "scripts", "run_grafana.sh")
Expand Down
2 changes: 2 additions & 0 deletions components/dm/ansible/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ func TestImportFromAnsible(t *testing.T) {
SSHPort: 22,
DeployDir: "",
Port: 3001,
Username: "foo",
Password: "bar",
}
assert.Equal(expectedGrafana, grafana)

Expand Down
4 changes: 2 additions & 2 deletions components/dm/ansible/testdata/ansible/inventory.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ dm_version = v1.0.6

deploy_dir = /home/tidb/deploy

grafana_admin_user = "admin"
grafana_admin_password = "admin"
grafana_admin_user = "foo"
grafana_admin_password = bar
26 changes: 17 additions & 9 deletions pkg/cluster/ansible/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"

"github.com/creasty/defaults"
"github.com/pingcap/tiup/pkg/cluster/executor"
Expand Down Expand Up @@ -193,7 +194,7 @@ func parseGroupVars(dir, ansCfgFile string, clsMeta *spec.ClusterMeta, inv *aini
tmpIns.StatusPort, _ = strconv.Atoi(statusPort)
}
if logDir, ok := srv.Vars["tidb_log_dir"]; ok {
tmpIns.LogDir = logDir
tmpIns.LogDir = strings.Trim(logDir, "\"")
}

log.Debugf("Imported %s node %s:%d.", tmpIns.Role(), tmpIns.Host, tmpIns.GetMainPort())
Expand Down Expand Up @@ -235,10 +236,10 @@ func parseGroupVars(dir, ansCfgFile string, clsMeta *spec.ClusterMeta, inv *aini
tmpIns.StatusPort, _ = strconv.Atoi(statusPort)
}
if dataDir, ok := srv.Vars["tikv_data_dir"]; ok {
tmpIns.DataDir = dataDir
tmpIns.DataDir = strings.Trim(dataDir, "\"")
}
if logDir, ok := srv.Vars["tikv_log_dir"]; ok {
tmpIns.LogDir = logDir
tmpIns.LogDir = strings.Trim(logDir, "\"")
}

log.Debugf("Imported %s node %s:%d.", tmpIns.Role(), tmpIns.Host, tmpIns.GetMainPort())
Expand Down Expand Up @@ -283,10 +284,10 @@ func parseGroupVars(dir, ansCfgFile string, clsMeta *spec.ClusterMeta, inv *aini
tmpIns.PeerPort, _ = strconv.Atoi(peerPort)
}
if dataDir, ok := srv.Vars["pd_data_dir"]; ok {
tmpIns.DataDir = dataDir
tmpIns.DataDir = strings.Trim(dataDir, "\"")
}
if logDir, ok := srv.Vars["pd_log_dir"]; ok {
tmpIns.LogDir = logDir
tmpIns.LogDir = strings.Trim(logDir, "\"")
}

log.Debugf("Imported %s node %s:%d.", tmpIns.Role(), tmpIns.Host, tmpIns.GetMainPort())
Expand Down Expand Up @@ -352,10 +353,10 @@ func parseGroupVars(dir, ansCfgFile string, clsMeta *spec.ClusterMeta, inv *aini
tmpIns.StatusPort, _ = strconv.Atoi(statusPort)
}
if dataDir, ok := srv.Vars["data_dir"]; ok {
tmpIns.DataDir = dataDir
tmpIns.DataDir = strings.Trim(dataDir, "\"")
}
if logDir, ok := srv.Vars["tiflash_log_dir"]; ok {
tmpIns.LogDir = logDir
tmpIns.LogDir = strings.Trim(logDir, "\"")
}
if tmpDir, ok := srv.Vars["tmp_path"]; ok {
tmpIns.TmpDir = tmpDir
Expand Down Expand Up @@ -483,6 +484,13 @@ func parseGroupVars(dir, ansCfgFile string, clsMeta *spec.ClusterMeta, inv *aini
tmpIns.Port, _ = strconv.Atoi(port)
}

if username, ok := srv.Vars["grafana_admin_user"]; ok {
tmpIns.Username = strings.Trim(username, "\"")
}
if passwd, ok := srv.Vars["grafana_admin_password"]; ok {
tmpIns.Password = strings.Trim(passwd, "\"")
}

log.Debugf("Imported %s node %s:%d.", tmpIns.Role(), tmpIns.Host, tmpIns.GetMainPort())

clsMeta.Topology.Grafanas = append(clsMeta.Topology.Grafanas, tmpIns)
Expand Down Expand Up @@ -520,10 +528,10 @@ func parseGroupVars(dir, ansCfgFile string, clsMeta *spec.ClusterMeta, inv *aini
tmpIns.Port, _ = strconv.Atoi(port)
}
if dataDir, ok := srv.Vars["pump_data_dir"]; ok {
tmpIns.DataDir = dataDir
tmpIns.DataDir = strings.Trim(dataDir, "\"")
}
if logDir, ok := srv.Vars["pump_log_dir"]; ok {
tmpIns.LogDir = logDir
tmpIns.LogDir = strings.Trim(logDir, "\"")
}

log.Debugf("Imported %s node %s:%d.", tmpIns.Role(), tmpIns.Host, tmpIns.GetMainPort())
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/ansible/test-data/inventory.ini
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ wait_replication = True
# Format: alertmanager_host:alertmanager_port
alertmanager_target = ""

grafana_admin_user = "admin"
grafana_admin_password = "admin"
grafana_admin_user = "foo"
grafana_admin_password = bar


### Collect diagnosis
Expand Down
2 changes: 2 additions & 0 deletions pkg/cluster/ansible/test-data/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ topology:
ssh_port: 9999
imported: true
port: 3000
username: foo
password: bar
deploy_dir: /home/tiopsimport/ansible-deploy/grafana-3000
arch: amd64
os: linux
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/embed/autogen_pkger.go

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion pkg/cluster/spec/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type GrafanaSpec struct {
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
DashboardDir string `yaml:"dashboard_dir,omitempty" validate:"dashboard_dir:editable"`
Username string `yaml:"username,omitempty" default:"admin" validate:"username:editable"`
Password string `yaml:"password,omitempty" default:"admin" validate:"password:editable"`
}

// Role returns the component role of the instance
Expand Down Expand Up @@ -140,8 +142,13 @@ func (i *GrafanaInstance) InitConfig(
}

// transfer config
spec := i.InstanceSpec.(GrafanaSpec)
fp = filepath.Join(paths.Cache, fmt.Sprintf("grafana_%s.ini", i.GetHost()))
if err := config.NewGrafanaConfig(i.GetHost(), paths.Deploy).WithPort(uint64(i.GetPort())).ConfigToFile(fp); err != nil {
if err := config.NewGrafanaConfig(i.GetHost(), paths.Deploy).
WithPort(uint64(i.GetPort())).
WithUsername(spec.Username).
WithPassword(spec.Password).
ConfigToFile(fp); err != nil {
return err
}
dst = filepath.Join(paths.Deploy, "conf", "grafana.ini")
Expand Down
14 changes: 14 additions & 0 deletions pkg/cluster/template/config/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type GrafanaConfig struct {
DeployDir string
IP string
Port uint64
Username string // admin_user
Password string // admin_password
}

// NewGrafanaConfig returns a GrafanaConfig
Expand All @@ -44,6 +46,18 @@ func (c *GrafanaConfig) WithPort(port uint64) *GrafanaConfig {
return c
}

// WithUsername sets username of admin user
func (c *GrafanaConfig) WithUsername(user string) *GrafanaConfig {
c.Username = user
return c
}

// WithPassword sets password of admin user
func (c *GrafanaConfig) WithPassword(passwd string) *GrafanaConfig {
c.Password = passwd
return c
}

// Config generate the config file data.
func (c *GrafanaConfig) Config() ([]byte, error) {
fp := path.Join("/templates", "config", "grafana.ini.tpl")
Expand Down
4 changes: 2 additions & 2 deletions templates/config/grafana.ini.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ check_for_updates = true
#################################### Security ####################################
[security]
# default admin user, created on startup
;admin_user = admin
admin_user = {{.Username}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the Username is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be set to admin during unmarshal, the default value is set in spec tag


# default admin password, can be changed before first start of grafana, or in profile settings
;admin_password = admin
admin_password = {{.Password}}

# used for signing
;secret_key = SW2YcwTIb9zpOOhoPsMm
Expand Down