Skip to content

Commit

Permalink
Merge branch 'local_feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
farnasirim committed Jan 23, 2016
2 parents 92e9e45 + be6ab5b commit b61e8e5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
31 changes: 19 additions & 12 deletions cloudconfig/http.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cloudconfig

import (
"bytes"
"net"
"strings"
// "net/url"
// "fmt"
"bytes"

"net/http"
"path"
"sync"
Expand Down Expand Up @@ -55,17 +56,8 @@ func (datasource *cloudConfigDataSource) handler(w http.ResponseWriter, r *http.
return
}

clientMacAddressString := req[1]
if strings.Index(clientMacAddressString, ":") == -1 {
var tmpmac bytes.Buffer
for i := 0; i < 12; i++ { // mac address length
tmpmac.WriteString(clientMacAddressString[i : i+1])
if i%2 == 1 {
tmpmac.WriteString(":")
}
}
clientMacAddressString = tmpmac.String()[:len(tmpmac.String())-1]
}
clientMacAddressString := colonLessMacToMac(req[1])

clientMac, err := net.ParseMAC(clientMacAddressString)
if err != nil {
return
Expand Down Expand Up @@ -142,3 +134,18 @@ func ServeCloudConfig(listenAddr net.TCPAddr, workspacePath string, datasource d

return http.ListenAndServe(listenAddr.String(), serveUtilityMultiplexer(ccdataSource))
}

func colonLessMacToMac(colonLess string) string {
coloned := colonLess
if strings.Index(colonLess, ":") == -1 {
var tmpmac bytes.Buffer
for i := 0; i < 12; i++ { // colon-less mac address length
tmpmac.WriteString(colonLess[i : i+1])
if i%2 == 1 {
tmpmac.WriteString(":")
}
}
coloned = tmpmac.String()[:len(tmpmac.String())-1]
}
return coloned
}
26 changes: 17 additions & 9 deletions datasource/etcd_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func (ds *EtcdDataSource) Machines() ([]Machine, error) {
ret := make([]Machine, 0)
for _, ent := range response.Node.Nodes {
pathToMachineDir := ent.Key
macStr := pathToMachineDir[strings.LastIndex(pathToMachineDir, "/")+1:]
machineName := pathToMachineDir[strings.LastIndex(pathToMachineDir, "/")+1:]
macStr := macFromName(machineName)
macAddr, err := net.ParseMAC(macStr)
if err != nil {
return nil, err
Expand All @@ -85,11 +86,12 @@ func (ds *EtcdDataSource) GetMachine(mac net.HardwareAddr) (Machine, bool) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

response, err := ds.keysAPI.Get(ctx, ds.prefixify(path.Join("machines/"+mac.String())), nil)
machineName := nameFromMac(mac.String())
response, err := ds.keysAPI.Get(ctx, ds.prefixify(path.Join("machines/"+machineName)), nil)
if err != nil {
return nil, false
}
if response.Node.Key[strings.LastIndex(response.Node.Key, "/")+1:] == mac.String() {
if response.Node.Key[strings.LastIndex(response.Node.Key, "/")+1:] == machineName {
return &EtcdMachine{mac, ds}, true
}
return nil, false
Expand Down Expand Up @@ -122,18 +124,18 @@ func (ds *EtcdDataSource) CreateMachine(mac net.HardwareAddr, ip net.IP) (Machin
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

ds.keysAPI.Set(ctx, ds.prefixify("machines/"+machine.Mac().String()), "", &etcd.SetOptions{Dir: true})
ds.keysAPI.Set(ctx, ds.prefixify("machines/"+machine.Name()), "", &etcd.SetOptions{Dir: true})
ctx1, cancel1 := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel1()
ds.keysAPI.Set(ctx1, ds.prefixify("machines/"+machine.Mac().String()+"/_IP"), ip.String(), &etcd.SetOptions{})
ds.keysAPI.Set(ctx1, ds.prefixify("machines/"+machine.Name()+"/_IP"), ip.String(), &etcd.SetOptions{})

ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel2()
ds.keysAPI.Set(ctx2, ds.prefixify("machines/"+machine.Mac().String()+"/_name"), machine.Name(), &etcd.SetOptions{})
ds.keysAPI.Set(ctx2, ds.prefixify("machines/"+machine.Name()+"/_mac"), machine.Mac().String(), &etcd.SetOptions{})

ctx3, cancel3 := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel3()
ds.keysAPI.Set(ctx3, ds.prefixify("machines/"+machine.Mac().String()+"/_first_seen"),
ds.keysAPI.Set(ctx3, ds.prefixify("machines/"+machine.Name()+"/_first_seen"),
strconv.FormatInt(time.Now().UnixNano(), 10), &etcd.SetOptions{})
machine.CheckIn()
machine.SetFlag("state", "unknown")
Expand Down Expand Up @@ -427,13 +429,19 @@ func (ds *EtcdDataSource) store(m Machine, ip net.IP) {

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
ds.keysAPI.Set(ctx, ds.prefixify("machines/"+m.Mac().String()+"/_IP"),
ds.keysAPI.Set(ctx, ds.prefixify("machines/"+m.Name()+"/_IP"),
ip.String(), &etcd.SetOptions{})

ctx1, cancel1 := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel1()
ds.keysAPI.Set(ctx1, ds.prefixify("machines/"+m.Mac().String()+"/_last_seen"),
ds.keysAPI.Set(ctx1, ds.prefixify("machines/"+m.Name()+"/_last_seen"),
strconv.FormatInt(time.Now().UnixNano(), 10), &etcd.SetOptions{})

ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel2()
ds.keysAPI.Set(ctx2, ds.prefixify("machines/"+m.Name()+"/_mac"),
m.Mac().String(), &etcd.SetOptions{})

}

// Assign assigns an ip to the node with the specified nic
Expand Down
29 changes: 26 additions & 3 deletions datasource/etcd_machine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datasource

import (
"bytes"
"errors"
"net"
"strconv"
Expand Down Expand Up @@ -35,8 +36,7 @@ func (m *EtcdMachine) IP() (net.IP, error) {

// Name returns this machine's hostname
func (m *EtcdMachine) Name() string {
tempName := "node" + m.Mac().String()
return strings.Replace(tempName, ":", "", -1)
return nameFromMac(m.Mac().String())
}

func unixNanoStringToTime(unixNano string) (time.Time, error) {
Expand Down Expand Up @@ -115,7 +115,7 @@ func (m *EtcdMachine) DeleteFlag(key string) error {
}

func (m *EtcdMachine) prefixify(str string) string {
return "machines/" + m.Mac().String() + "/" + str
return "machines/" + m.Name() + "/" + str
}

func (m *EtcdMachine) selfGet(key string) (string, error) {
Expand All @@ -129,3 +129,26 @@ func (m *EtcdMachine) selfSet(key, value string) error {
func (m *EtcdMachine) selfDelete(key string) error {
return m.etcd.Delete(m.prefixify(key))
}

func nameFromMac(mac string) string {
return strings.Replace("node"+mac, ":", "", -1)
}

func macFromName(name string) string {
return colonLessMacToMac(name[len("node"):])
}

func colonLessMacToMac(colonLess string) string {
coloned := colonLess
if strings.Index(colonLess, ":") == -1 {
var tmpmac bytes.Buffer
for i := 0; i < 12; i++ { // colon-less mac address length
tmpmac.WriteString(colonLess[i : i+1])
if i%2 == 1 {
tmpmac.WriteString(":")
}
}
coloned = tmpmac.String()[:len(tmpmac.String())-1]
}
return coloned
}

0 comments on commit b61e8e5

Please sign in to comment.