Skip to content

Commit

Permalink
embed: update dm topo & adjust dir layout
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis committed Jul 28, 2021
1 parent c8e8087 commit 9172e07
Show file tree
Hide file tree
Showing 35 changed files with 60 additions and 33 deletions.
4 changes: 2 additions & 2 deletions components/cluster/command/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func newTemplateCmd() *cobra.Command {
name = "local.yaml"
}

fp := path.Join("templates", "examples", name)
tpl, err := embed.ReadFile(fp)
fp := path.Join("examples", "cluster", name)
tpl, err := embed.ReadExample(fp)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions components/dm/command/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func newTemplateCmd() *cobra.Command {
name = "topology.example.yaml"
}

fp := path.Join("templates", "examples", "dm", name)
tpl, err := embed.ReadFile(fp)
fp := path.Join("examples", "dm", name)
tpl, err := embed.ReadExample(fp)
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import (
//go:embed templates
var embededFiles goembed.FS

// ReadFile read the file embed.
func ReadFile(path string) ([]byte, error) {
// ReadTemplate read the template file embed.
func ReadTemplate(path string) ([]byte, error) {
return embededFiles.ReadFile(path)
}

//go:embed examples
var embedExamples goembed.FS

// ReadExample read an example file
func ReadExample(path string) ([]byte, error) {
return embedExamples.ReadFile(path)
}
21 changes: 20 additions & 1 deletion embed/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,26 @@ func (s *embedSuite) TestCanReadTemplates(c *check.C) {
data, err := os.ReadFile(path)
c.Assert(err, check.IsNil)

embedData, err := ReadFile(path)
embedData, err := ReadTemplate(path)
c.Assert(err, check.IsNil)

c.Assert(embedData, check.BytesEquals, data)
}
}

// Test can read all file in /examples
func (s *embedSuite) TestCanReadExamples(c *check.C) {
paths, err := getAllFilePaths("examples")
c.Assert(err, check.IsNil)
c.Assert(len(paths), check.Greater, 0)

for _, path := range paths {
c.Log("check file: ", path)

data, err := os.ReadFile(path)
c.Assert(err, check.IsNil)

embedData, err := ReadExample(path)
c.Assert(err, check.IsNil)

c.Assert(embedData, check.BytesEquals, data)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewAlertManagerConfig() *AlertManagerConfig {
// Config generate the config file data.
func (c *AlertManagerConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "alertmanager.yml")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/blackbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewBlackboxConfig(deployDir string, tlsEnabled bool) *BlackboxConfig {
// Config generate the config file data.
func (c *BlackboxConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "blackbox.yml.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ import (
// GetConfig returns a raw config file from embed templates
func GetConfig(filename string) ([]byte, error) {
fp := filepath.Join("templates", "config", filename)
return embed.ReadFile(fp)
return embed.ReadTemplate(fp)
}
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewDashboardConfig(cluster, deployDir string) *DashboardConfig {
// Config generate the config file data.
func (c *DashboardConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "dashboard.yml.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *DatasourceConfig) WithPort(port uint64) *DatasourceConfig {
// Config generate the config file data.
func (c *DatasourceConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "datasource.yml.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *GrafanaConfig) WithDomain(domain string) *GrafanaConfig {
// Config generate the config file data.
func (c *GrafanaConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "grafana.ini.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (c *PrometheusConfig) SetRemoteConfig(cfg string) *PrometheusConfig {
// Config generate the config file data.
func (c *PrometheusConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "prometheus.yml.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/config/tispark.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *TiSparkConfig) WithCustomFields(m map[string]interface{}) *TiSparkConfi
// Config generate the config file data.
func (c *TiSparkConfig) Config() ([]byte, error) {
fp := filepath.Join("templates", "config", "spark-defaults.conf.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *AlertManagerScript) ConfigToFile(file string) error {
// Config generate the config file data.
func (c *AlertManagerScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_alertmanager.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/blackbox_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *BlackboxExporterScript) WithNumaNode(numa string) *BlackboxExporterScri
// Config generate the config file data.
func (c *BlackboxExporterScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_blackbox_exporter.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/cdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *CDCScript) WithDataDirEnabled() *CDCScript {
// Config generate the config file data.
func (c *CDCScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_cdc.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/template/scripts/dm_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *DMMasterScript) AppendEndpoints(ends ...*DMMasterScript) *DMMasterScrip
// Config generate the config file data.
func (c *DMMasterScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_dm-master.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (c *DMMasterScaleScript) AppendEndpoints(ends ...*DMMasterScript) *DMMaster
// Config generate the config file data.
func (c *DMMasterScaleScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_dm-master_scale.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/dm_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *DMWorkerScript) AppendEndpoints(ends ...*DMMasterScript) *DMWorkerScrip
// Config generate the config file data.
func (c *DMWorkerScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_dm-worker.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/drainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *DrainerScript) AppendEndpoints(ends ...*PDScript) *DrainerScript {
// Config generate the config file data.
func (c *DrainerScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_drainer.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *GrafanaScript) Config() ([]byte, error) {
fp = path.Join("templates", "scripts", "run_grafana.sh.tpl")
}

tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/node_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *NodeExporterScript) WithNumaNode(numa string) *NodeExporterScript {
// Config generate the config file data.
func (c *NodeExporterScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_node_exporter.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *PDScript) Config() ([]byte, error) {

func (c *PDScript) configWithScript(script string) ([]byte, error) {
fp := path.Join("templates", "scripts", script)
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *PrometheusScript) Config() ([]byte, error) {
fp = path.Join("templates", "scripts", "run_prometheus.sh.tpl")
}

tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *PumpScript) AppendEndpoints(ends ...*PDScript) *PumpScript {
// Config generate the config file data.
func (c *PumpScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_pump.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ import (
// GetScript returns a raw config file from embed templates
func GetScript(filename string) ([]byte, error) {
fp := filepath.Join("templates", "scripts", filename)
return embed.ReadFile(fp)
return embed.ReadTemplate(fp)
}
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *TiDBScript) AppendEndpoints(ends ...*PDScript) *TiDBScript {
// Config generate the config file data.
func (c *TiDBScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_tidb.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/tiflash.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *TiFlashScript) AppendEndpoints(ends ...*PDScript) *TiFlashScript {
// Config generate the config file data.
func (c *TiFlashScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_tiflash.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/scripts/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *TiKVScript) AppendEndpoints(ends ...*PDScript) *TiKVScript {
// Config generate the config file data.
func (c *TiKVScript) Config() ([]byte, error) {
fp := path.Join("templates", "scripts", "run_tikv.sh.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/systemd/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *Config) ConfigToFile(file string) error {
// Config generate the config file data.
func (c *Config) Config() ([]byte, error) {
fp := path.Join("templates", "systemd", "system.service.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/template/systemd/tispark.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *TiSparkConfig) ConfigToFile(file string) error {
// Config generate the config file data.
func (c *TiSparkConfig) Config() ([]byte, error) {
fp := path.Join("templates", "systemd", "tispark.service.tpl")
tpl, err := embed.ReadFile(fp)
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9172e07

Please sign in to comment.