Skip to content

Commit

Permalink
Merge pull request #53 from tinynetwork/feature/template
Browse files Browse the repository at this point in the history
feature: template function
  • Loading branch information
slankdev committed Aug 24, 2020
2 parents 525a74d + 1fa34f6 commit b5157a1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
5 changes: 5 additions & 0 deletions command_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func CmdUp(c *cli.Context) error {
if node.Type == "docker" || node.Type == "" {
delNsCmd := node.DelNsCmd()
utils.PrintCmd(os.Stdout, delNsCmd, verbose)
mountTmplCmd, err := node.MountTmpl()
utils.PrintCmds(os.Stdout, mountTmplCmd, verbose)
if err != nil {
return err
}
}
}

Expand Down
79 changes: 65 additions & 14 deletions internal/pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package shell

import (
"fmt"
"os"
"strings"
"text/template"

l "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -48,20 +50,22 @@ type PostFini struct {

// Node
type Node struct {
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type" mapstructure:"type"`
NetBase string `yaml:"net_base" mapstructure:"net_base"`
VolumeBase string `yaml:"volume" mapstructure:"volume"`
Image string `yaml:"image" mapstructure:"image"`
BuildFile string `yaml:"buildfile" mapstructure:"buildfile"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
Mounts []string `yaml:"mounts,flow" mapstructure:"mounts,flow"`
DNS []string `yaml:"dns,flow" mapstructure:"dns,flow"`
DNSSearches []string `yaml:"dns_search,flow" mapstructure:"dns_search,flow"`
HostNameIgnore bool `yaml:"hostname_ignore" mapstructure:"hostname_ignore"`
EntryPoint string `yaml:"entrypoint" mapstructure:"entrypoint"`
ExtraArgs string `yaml:"docker_run_extra_args" mapstructure:"docker_run_extra_args"`
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type" mapstructure:"type"`
NetBase string `yaml:"net_base" mapstructure:"net_base"`
VolumeBase string `yaml:"volume" mapstructure:"volume"`
Image string `yaml:"image" mapstructure:"image"`
BuildFile string `yaml:"buildfile" mapstructure:"buildfile"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
Mounts []string `yaml:"mounts,flow" mapstructure:"mounts,flow"`
DNS []string `yaml:"dns,flow" mapstructure:"dns,flow"`
DNSSearches []string `yaml:"dns_search,flow" mapstructure:"dns_search,flow"`
HostNameIgnore bool `yaml:"hostname_ignore" mapstructure:"hostname_ignore"`
EntryPoint string `yaml:"entrypoint" mapstructure:"entrypoint"`
ExtraArgs string `yaml:"docker_run_extra_args" mapstructure:"docker_run_extra_args"`
Vars map[string]interface{} `yaml:"vars" mapstructure:"vars"`
Templates []Template `yaml:"templates" mapstructure:"templates"`
}

// Interface
Expand All @@ -77,6 +81,12 @@ type Sysctl struct {
Sysctl string `yaml:"string"`
}

type Template struct {
Src string `yaml:"src"`
Dst string `yaml:"dst"`
Content string `yaml:"content"`
}

// Switch
type Switch struct {
Name string `yaml:"name"`
Expand Down Expand Up @@ -599,3 +609,44 @@ func (node *Node) DelNsCmd() (delNsCmd string) {

return delNsCmd
}

// MountTmpl
func (node *Node) MountTmpl() (mountTmplCmd []string, err error) {
for _, tmpl := range node.Templates {
dest := strings.Split(tmpl.Dst, "/")
destfile := dest[len(dest)-1]
f, err := os.Create(destfile)
if err != nil {
return nil, err
}
defer f.Close()

if len(tmpl.Src) > 0 {
tpl, err := template.ParseFiles(tmpl.Src)
if err != nil {
return nil, err
}

err = tpl.Execute(f, node.Vars)
if err != nil {
return nil, err
}
} else if len(tmpl.Content) > 0 {
tpl, err := template.New("").Parse(tmpl.Content)
if err != nil {
return nil, err
}
err = tpl.Execute(f, node.Vars)
if err != nil {
return nil, err
}
}

tmplCmd := fmt.Sprintf("docker cp %s %s:%s", destfile, node.Name, tmpl.Dst)
mountTmplCmd = append(mountTmplCmd, tmplCmd)
removeFileCmd := fmt.Sprintf("rm -rf %s", destfile)
mountTmplCmd = append(mountTmplCmd, removeFileCmd)
}

return mountTmplCmd, nil
}

0 comments on commit b5157a1

Please sign in to comment.