Skip to content

Commit

Permalink
learned about golint, ran golint, got sad, fixed its complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
neophenix committed Jul 25, 2018
1 parent 733bbfe commit 8fb079b
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 81 deletions.
2 changes: 2 additions & 0 deletions cmd/lxdepot/lxdepot.go
Expand Up @@ -25,6 +25,8 @@ var port string
var conf string
var webroot string
var cacheTemplates bool

// Conf is our main config
var Conf *config.Config

func main() {
Expand Down
5 changes: 3 additions & 2 deletions internal/config/config.go
Expand Up @@ -26,7 +26,7 @@ type DNS struct {
Options map[string]string `yaml:"options"` // Providers options documented at the top of a provider implementation
}

// For bootstrapping or other setup, used as an array of sequential "things to do"
// FileOrCommand is for bootstrapping or other setup, used as an array of sequential "things to do"
// file will upload a file to the container, command will run a command on it
type FileOrCommand struct {
Type string `yaml:"type"` // file or command, what we are going to do
Expand All @@ -37,12 +37,13 @@ type FileOrCommand struct {
OkReturnValues []float64 `yaml:"ok_return_values"` // list of return values (other than 0) we accept as ok, 0 is always acceptable
}

// NetworkingConfig holds a network file template and the location where it should be placed in the container
type NetworkingConfig struct {
RemotePath string `yaml:"remote_path"` // path of the file in the container
Template string `yaml:"template"` // text/template parsable version of the file
}

// The main config structure mostly pulling together the above items, also holds our client PKI
// Config is the main config structure mostly pulling together the above items, also holds our client PKI
type Config struct {
Cert string `yaml:"cert"` // client cert, which can either be the cert contents or file:/path/here that we will read in later
Key string `yaml:"key"` // client key, same as cert, contents or file:/path/here
Expand Down
6 changes: 3 additions & 3 deletions internal/dns/dns.go
Expand Up @@ -11,18 +11,18 @@ type RecordList struct {
RecordSet []string // the values in the entry
}

// The Dns interface provides the list of functions all our 3rd party integrations should
// The DNS interface provides the list of functions all our 3rd party integrations should
// support. I don't like that I coded the record type in the name, but until I decide
// I need IPv6, etc its good enough
type Dns interface {
type DNS interface {
GetARecord(name string, network string) (string, error) // returns a string representation of an IPv4 address
RemoveARecord(name string) error // removes the record from our 3rd party
ListARecords() ([]RecordList, error) // returns a list of all the A records
}

// New should just hand back the appropriate interface for our config settings,
// returning from the correct "New" function for our integration
func New(conf *config.Config) Dns {
func New(conf *config.Config) DNS {
if conf.DNS.Provider == "google" {
return NewGoogleDNS(conf.DNS.Options["gcp_creds_file"], conf.DNS.Options["gcp_project_name"], conf.DNS.Options["gcp_zone_name"], conf.DNS.Options)
}
Expand Down
12 changes: 5 additions & 7 deletions internal/dns/google.go
Expand Up @@ -13,7 +13,7 @@ import (
"time"
)

// Stores all the options we need to talk to GCP
// GoogleDNS stores all the options we need to talk to GCP
type GoogleDNS struct {
Creds []byte // the contents of our service account json credentials file
Project string // the GCP project name we are operating on
Expand All @@ -26,17 +26,16 @@ type GoogleDNS struct {
Options map[string]string
}

// Cache of our calls to pull all the records, figure if the system is in
// regular use its better to store these for a few minutes than make a call
// every time
// RrsetCache is the cache of all the recordsets, figure if the system is in
// regular use its better to store these for a few minutes than make a call each time
type RrsetCache struct {
Rrsets []*gdns.ResourceRecordSet
CacheTime time.Time
}

var cache RrsetCache

// NewGoogleDNS will return our GCP Dns interface
// NewGoogleDNS will return our GCP DNS interface
// The creds, project, and zone here are actually in the options as well, but they are important
// enough to warrant being "top level" items
func NewGoogleDNS(creds string, project string, zone string, options map[string]string) *GoogleDNS {
Expand Down Expand Up @@ -73,9 +72,8 @@ func (g *GoogleDNS) getZoneRecordSet(token string) error {
now := time.Now()
if now.Sub(cache.CacheTime).Seconds() <= 30 {
return nil
} else {
cache = RrsetCache{}
}
cache = RrsetCache{}
}

service, err := g.getDNSService()
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/handler_containers.go
Expand Up @@ -98,7 +98,7 @@ func ContainerHandler(w http.ResponseWriter, r *http.Request) {
var playbooks []string
os := containerInfo[0].Container.ExpandedConfig["image.os"]
if pbs, ok := Conf.Playbooks[os]; ok {
for name, _ := range pbs {
for name := range pbs {
playbooks = append(playbooks, name)
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/handler_hosts.go
Expand Up @@ -31,12 +31,12 @@ func HostListHandler(w http.ResponseWriter, r *http.Request) {
if hostContainerInfo[container.Host.Host] == nil {
hostContainerInfo[container.Host.Host] = make(map[string]int)
}
hostContainerInfo[container.Host.Host]["total"] += 1
hostContainerInfo[container.Host.Host]["total"]++

if container.Container.Status == "Running" {
hostContainerInfo[container.Host.Host]["running"] += 1
hostContainerInfo[container.Host.Host]["running"]++
} else {
hostContainerInfo[container.Host.Host]["stopped"] += 1
hostContainerInfo[container.Host.Host]["stopped"]++
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/handlers.go
@@ -1,9 +1,9 @@
// Pacakge handlers is where all the "normal" web handlers are defined
// Package handlers is where all the "normal" web handlers are defined
package handlers

import (
"github.com/neophenix/lxdepot/internal/config"
)

// config from our main function
// Conf is our main config
var Conf *config.Config
1 change: 1 addition & 0 deletions internal/handlers/router.go
Expand Up @@ -11,6 +11,7 @@ type Route struct {
Handler func(w http.ResponseWriter, r *http.Request) // a func pointer to call if the regex matches
}

// Routes is the array in the order we will attempt to match the route with the incoming url, first one wins
var Routes []Route

// AddRoute compiles the regex string and appends it to our route list with its handler func pointer
Expand Down
4 changes: 3 additions & 1 deletion internal/handlers/templates.go
Expand Up @@ -6,8 +6,10 @@ import (
"log"
)

// some settings from our main func
// WebRoot is the path to the web templates + static files
var WebRoot string

// CacheTemplates is the setting on whether to cache the template files or read from disk each time
var CacheTemplates bool

// template cache
Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/ws/handler_containerplaybook.go
Expand Up @@ -14,7 +14,7 @@ func ContainerPlaybookHandler(conn *websocket.Conn, mt int, msg IncomingMessage)
containerInfo, err := lxd.GetContainers(msg.Data["host"], msg.Data["name"], false)
if err != nil {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed to get container info: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed to get container info: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}
Expand All @@ -23,13 +23,13 @@ func ContainerPlaybookHandler(conn *websocket.Conn, mt int, msg IncomingMessage)
if len(containerInfo) > 0 {
if !lxd.IsManageable(containerInfo[0]) {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "lock flag set, remote management denied", Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "lock flag set, remote management denied", Success: false})
conn.WriteMessage(mt, data)
return
}
} else {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "container does not exist", Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "container does not exist", Success: false})
conn.WriteMessage(mt, data)
return
}
Expand Down
30 changes: 15 additions & 15 deletions internal/handlers/ws/handler_createcontainer.go
Expand Up @@ -18,47 +18,47 @@ func CreateContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) {
// Create the container
// -------------------------
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Creating container", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Creating container", Success: true})
conn.WriteMessage(mt, data)

// options will be whatever the user wants set like container limits, priority, etc. Its
// called config in LXD land, but since we use config for our config I'm calling it options in here
var options map[string]string
err := json.Unmarshal([]byte(msg.Data["options"]), &options)
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}

err = lxd.CreateContainer(msg.Data["host"], msg.Data["name"], msg.Data["image"], options)
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true})
conn.WriteMessage(mt, data)
// -------------------------

// DNS? If this fails I don't think it is enough reason to bail, will see
// -------------------------
if !Conf.DNS.DHCP {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Creating DNS entry", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Creating DNS entry", Success: true})
conn.WriteMessage(mt, data)

d := dns.New(Conf)
if d == nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed to create DNS object for provider: " + Conf.DNS.Provider, Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed to create DNS object for provider: " + Conf.DNS.Provider, Success: false})
conn.WriteMessage(mt, data)
} else {
ip, err := d.GetARecord(msg.Data["name"], Conf.DNS.Options["network"])
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
} else {
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: ip, Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: ip, Success: true})
conn.WriteMessage(mt, data)

// upload our network config
Expand All @@ -76,13 +76,13 @@ func CreateContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) {
}

id = time.Now().UnixNano()
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "Waiting for networking", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "Waiting for networking", Success: true})
conn.WriteMessage(mt, data)
// just going to sleep for now, maybe ping later? This is to ensure the networking is up before
// we continue on. Otherwise because we can't really check the command status, we will think things
// like yum installed what we wanted, when really it bailed due to a network issue.
time.Sleep(5 * time.Second)
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true})
conn.WriteMessage(mt, data)

err = BootstrapContainer(conn, mt, msg.Data["host"], msg.Data["name"])
Expand All @@ -95,15 +95,15 @@ func CreateContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) {
// It then parses that template through text/template passing the IP and uploads it to the container
func setupContainerNetwork(conn *websocket.Conn, mt int, host string, name string, ip string) {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Configuring container networking", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Configuring container networking", Success: true})
conn.WriteMessage(mt, data)

// Even though we just created it, lxd doesn't give us a lot of info back about the image, etc.
// hell the GetImages call doesn't give us back a lot either. So we are going to pull the container state
// to be able to figure out what OS we are on, so we can then use the right network setup
containerInfo, err := lxd.GetContainers(host, name, true)
if err != nil {
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}
Expand All @@ -116,7 +116,7 @@ func setupContainerNetwork(conn *websocket.Conn, mt int, host string, name strin
var contents bytes.Buffer
tmpl, err := template.New(file.RemotePath).Parse(file.Template)
if err != nil {
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}
Expand All @@ -126,11 +126,11 @@ func setupContainerNetwork(conn *websocket.Conn, mt int, host string, name strin

err = lxd.CreateFile(host, name, file.RemotePath, 0644, contents.String())
if err != nil {
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true})
conn.WriteMessage(mt, data)
}
}
Expand Down
14 changes: 7 additions & 7 deletions internal/handlers/ws/handler_deletecontainer.go
Expand Up @@ -22,20 +22,20 @@ func DeleteContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) {
// -------------------------
if !Conf.DNS.DHCP {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Deleting DNS entry", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Deleting DNS entry", Success: true})
conn.WriteMessage(mt, data)

d := dns.New(Conf)
if d == nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed to create DNS object for provider: " + Conf.DNS.Provider, Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed to create DNS object for provider: " + Conf.DNS.Provider, Success: false})
conn.WriteMessage(mt, data)
} else {
err := d.RemoveARecord(msg.Data["name"])
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
} else {
data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true})
conn.WriteMessage(mt, data)
}
}
Expand All @@ -44,16 +44,16 @@ func DeleteContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) {

// Delete the container
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Deleting container", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Deleting container", Success: true})
conn.WriteMessage(mt, data)

err = lxd.DeleteContainer(msg.Data["host"], msg.Data["name"])
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}

data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true, Redirect: "/containers"})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true, Redirect: "/containers"})
conn.WriteMessage(mt, data)
}
6 changes: 3 additions & 3 deletions internal/handlers/ws/handler_movecontainer.go
Expand Up @@ -10,16 +10,16 @@ import (
// MoveContainerHandler wraps lxd.MoveContainer and reports any errors it returns
func MoveContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) {
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Moving container", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Moving container", Success: true})
conn.WriteMessage(mt, data)

err := lxd.MoveContainer(msg.Data["host"], msg.Data["dst_host"], msg.Data["name"])
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return
}

data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: false})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: false})
conn.WriteMessage(mt, data)
}
6 changes: 3 additions & 3 deletions internal/handlers/ws/handler_startcontainer.go
Expand Up @@ -11,17 +11,17 @@ import (
func StartContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) error {
// Start the container
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Starting container", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Starting container", Success: true})
conn.WriteMessage(mt, data)

err := lxd.StartContainer(msg.Data["host"], msg.Data["name"])
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return err
}

data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true})
conn.WriteMessage(mt, data)

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/ws/handler_stopcontainer.go
Expand Up @@ -11,17 +11,17 @@ import (
func StopContainerHandler(conn *websocket.Conn, mt int, msg IncomingMessage) error {
// Stop the container
id := time.Now().UnixNano()
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "Stopping container", Success: true})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "Stopping container", Success: true})
conn.WriteMessage(mt, data)

err := lxd.StopContainer(msg.Data["host"], msg.Data["name"])
if err != nil {
data, _ := json.Marshal(OutgoingMessage{Id: id, Message: "failed: " + err.Error(), Success: false})
data, _ := json.Marshal(OutgoingMessage{ID: id, Message: "failed: " + err.Error(), Success: false})
conn.WriteMessage(mt, data)
return err
}

data, _ = json.Marshal(OutgoingMessage{Id: id, Message: "done", Success: true})
data, _ = json.Marshal(OutgoingMessage{ID: id, Message: "done", Success: true})
conn.WriteMessage(mt, data)

return nil
Expand Down

0 comments on commit 8fb079b

Please sign in to comment.