Skip to content

Commit

Permalink
Merge pull request #22 from ggiamarchi/config-show
Browse files Browse the repository at this point in the history
API & command for configuration show
  • Loading branch information
ggiamarchi authored Mar 29, 2020
2 parents 8f4a9cb + 2171836 commit 1f5b627
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func api(appConfig *model.AppConfig) *gin.Engine {
healthcheck(v1, appConfig)

readConfigurations(v1, appConfig)
showConfiguration(v1, appConfig)
deployConfiguration(v1, appConfig)

readHosts(v1, appConfig)
Expand Down
19 changes: 19 additions & 0 deletions api/configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ func readConfigurations(api *gin.RouterGroup, appConfig *model.AppConfig) {
})
}

func showConfiguration(api *gin.RouterGroup, appConfig *model.AppConfig) {
api.GET("/configurations/:name", func(c *gin.Context) {
name := c.Param("name")
config, err := service.ReadConfigurationContent(appConfig, name)

if err != nil {
switch v := err.(type) {
case *service.PXEError:
pxeErrorResponse(c, v)
default:
c.Writer.WriteHeader(500)
}
return
}

c.JSON(200, config)
})
}

func deployConfiguration(api *gin.RouterGroup, appConfig *model.AppConfig) {
api.PUT("/configurations/:name/deploy", func(c *gin.Context) {

Expand Down
27 changes: 27 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"fmt"
"os"

"github.com/ggiamarchi/pxe-pilot/api"
Expand Down Expand Up @@ -31,6 +32,32 @@ func setupCLI() {
})

app.Command("config", "PXE configuration commands", func(cmd *cli.Cmd) {
cmd.Command("show", "Show PXE configurations", func(cmd *cli.Cmd) {

cmd.Spec = "NAME"

var (
name = cmd.StringArg("NAME", "", "Configuration to show")
)

cmd.Action = func() {
logger.Init(!*debug)

var configuration = &model.ConfigurationDetails{}
statusCode, err := http.Request("GET", *serverURL, fmt.Sprintf("/v1/configurations/%s", *name), nil, configuration)
if err != nil || statusCode != 200 {
panic(err)
}

fmt.Println(configuration.Content)
// Print data table
// table := tablewriter.NewWriter(os.Stdout)
// table.SetAutoWrapText(false)
// table.SetHeader([]string{"Name", "Content"})
// table.Append([]string{configuration.Name, configuration.Content})
// table.Render()
}
})
cmd.Command("list", "List available PXE configurations", func(cmd *cli.Cmd) {
cmd.Action = func() {
logger.Init(!*debug)
Expand Down
9 changes: 9 additions & 0 deletions model/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ type Configuration struct {
func (c *Configuration) String() string {
return fmt.Sprintf("%+v", *c)
}

type ConfigurationDetails struct {
Name string `json:"name"`
Content string `json:"content"`
}

func (c *ConfigurationDetails) String() string {
return fmt.Sprintf("%+v", *c)
}
29 changes: 29 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,35 @@ func newPXEError(kind, msg string, a ...interface{}) *PXEError {
return &err
}

func ReadConfigurationContent(appConfig *model.AppConfig, name string) (*model.ConfigurationDetails, error) {
logger.Info("Show configuration :: %s", name)

configExists := false
for _, c := range ReadConfigurations(appConfig) {
if name == c.Name {
configExists = true
break
}
}
if !configExists {
return nil, newPXEError("NOT_FOUND", "Configuration '%s' does not exists", name)
}

file := fmt.Sprintf("%s/%s", appConfig.Configuration.Directory, name)

f, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

c := &model.ConfigurationDetails{
Name: name,
Content: string(f),
}

return c, nil
}

func DeployConfiguration(appConfig *model.AppConfig, name string, hosts []*model.HostQuery) error {
logger.Info("Deploy configuration :: %s :: %+v", name, hosts)

Expand Down
4 changes: 4 additions & 0 deletions test/tftp_root/pxelinux.cfg/conf/local
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
default local

label local
localboot 0
5 changes: 5 additions & 0 deletions test/tftp_root/pxelinux.cfg/conf/sheep
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DEFAULT sheeplive

label sheeplive
kernel /sheep-live/vmlinuz
append boot=live fetch=http://1.2.3.4/sheep-live.squashfs initrd=/sheep-live/initrd.img ssh=sheep console=ttyS1,57600n8 sheep.script=http://1.2.3.4/sheep sheep.config=http://1.2.3.4/config.yml
Empty file.
Empty file.

0 comments on commit 1f5b627

Please sign in to comment.