first commit
This commit is contained in:
154
internal/space_creator/space_creator.go
Normal file
154
internal/space_creator/space_creator.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package space_creator
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"playbookctl/internal/types"
|
||||
"playbookctl/internal/utils"
|
||||
"playbookctl/internal/utils/logger"
|
||||
)
|
||||
|
||||
//goland:noinspection SpellCheckingInspection
|
||||
var (
|
||||
//go:embed static/gitignore.txt
|
||||
staticGitIgnore []byte
|
||||
|
||||
//go:embed static/editorconfig.txt
|
||||
staticEditorConfig []byte
|
||||
|
||||
//go:embed static/dd.datetime.sh
|
||||
staticLibDatetime []byte
|
||||
|
||||
//go:embed static/dd.restore.sh
|
||||
staticLibRestore []byte
|
||||
|
||||
//go:embed static/dd.sethostname.sh
|
||||
staticLibSetHostname []byte
|
||||
)
|
||||
|
||||
type ServerProps struct {
|
||||
Name string
|
||||
Host string
|
||||
Port uint16
|
||||
User string
|
||||
}
|
||||
|
||||
type SpaceCreator struct {
|
||||
log *logger.Logger
|
||||
|
||||
// Рабочая папка
|
||||
workDir string
|
||||
}
|
||||
|
||||
func NewSpaceCreator(verbose logger.LogVerbose, workDir string) *SpaceCreator {
|
||||
return &SpaceCreator{
|
||||
log: &logger.Logger{Verbose: verbose},
|
||||
workDir: workDir,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateSpace Создать пространство
|
||||
func (app *SpaceCreator) CreateSpace(name string, props *ServerProps) error {
|
||||
app.log.Debug(fmt.Sprintf("workDir: %s", app.workDir))
|
||||
app.log.Debug(fmt.Sprintf("space name: %s", name))
|
||||
app.log.Debug(fmt.Sprintf("server props: %+v", props))
|
||||
|
||||
spacePath := filepath.Join(app.workDir, name)
|
||||
|
||||
app.log.Trace(fmt.Sprintf("try create folder '%s'", name))
|
||||
if err := os.Mkdir(spacePath, 0755); os.IsExist(err) {
|
||||
return fmt.Errorf("Папка \"%s\" уже существует\n", name)
|
||||
}
|
||||
|
||||
for _, dir := range []string{"roles", "library", "vars"} {
|
||||
app.log.Trace(fmt.Sprintf("try create folder '%s/%s'", name, dir))
|
||||
if err := os.Mkdir(filepath.Join(spacePath, dir), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
type StaticPair struct {
|
||||
Name string
|
||||
Data []byte
|
||||
}
|
||||
|
||||
{ // root dir
|
||||
app.log.Trace("generate hosts.yaml")
|
||||
if err := generateHosts(spacePath, props); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
app.log.Trace("generate playbook.yml")
|
||||
if err := generatePlaybook(spacePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
app.log.Trace("generate default.host.txt")
|
||||
if err := utils.WriteStringFile(filepath.Join(spacePath, "default.host.txt"), props.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, pair := range []StaticPair{
|
||||
{".gitignore", staticGitIgnore},
|
||||
{".editorconfig", staticEditorConfig},
|
||||
} {
|
||||
app.log.Trace(fmt.Sprintf("export %s", pair.Name))
|
||||
if err := utils.SaveStaticFile(filepath.Join(spacePath, pair.Name), pair.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{ // library dir
|
||||
for _, pair := range []StaticPair{
|
||||
{"dd.datetime.sh", staticLibDatetime},
|
||||
{"dd.restore.sh", staticLibRestore},
|
||||
{"dd.sethostname.sh", staticLibSetHostname},
|
||||
} {
|
||||
app.log.Trace(fmt.Sprintf("export library/%s", pair.Name))
|
||||
if err := utils.SaveStaticFile(filepath.Join(spacePath, "library", pair.Name), pair.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{ // vars dir
|
||||
app.log.Trace(fmt.Sprintf("create vars/%s.vars.yml", props.Name))
|
||||
err := utils.WriteEmptyYaml(filepath.Join(spacePath, "vars", fmt.Sprintf("%s.vars.yml", props.Name)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
app.log.Info(fmt.Sprintf("Пространство \"%s\" создано.", name))
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateHosts(spacePath string, props *ServerProps) error {
|
||||
hosts := types.THosts{
|
||||
props.Name: types.THostProps{
|
||||
Host: props.Host,
|
||||
Port: props.Port,
|
||||
User: props.User,
|
||||
Interpreter: types.DefaultInterpreter,
|
||||
},
|
||||
}
|
||||
|
||||
return types.WriteHosts(spacePath, &hosts)
|
||||
}
|
||||
|
||||
func generatePlaybook(spacePath string) error {
|
||||
playbook := types.Playbook{
|
||||
Hosts: "all",
|
||||
GatherFacts: true,
|
||||
PreTasks: []types.Task{{
|
||||
Name: "Include vars",
|
||||
IncludeVars: "vars/{{ inventory_hostname }}.vars.yml",
|
||||
}},
|
||||
Roles: []string{},
|
||||
}
|
||||
|
||||
return types.WritePlaybook(spacePath, &playbook)
|
||||
}
|
||||
Reference in New Issue
Block a user