From 527e568217c24c4c28e64485bbc80d1572c8d73b Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 14 Jul 2014 23:19:37 +0000 Subject: [PATCH] Update /etc/hosts when linked container is restarted Docker-DCO-1.1-Signed-off-by: Victor Vieux (github: vieux) --- graphdb/graphdb.go | 34 ++++++++++++++++++++++++++++++++++ networkfs/etchosts/etchosts.go | 10 ++++++++++ 2 files changed, 44 insertions(+) diff --git a/graphdb/graphdb.go b/graphdb/graphdb.go index d0e049b..59873fe 100644 --- a/graphdb/graphdb.go +++ b/graphdb/graphdb.go @@ -281,6 +281,18 @@ func (db *Database) Children(name string, depth int) ([]WalkMeta, error) { return db.children(e, name, depth, nil) } +// Return the parents of a specified entity +func (db *Database) Parents(name string) ([]string, error) { + db.mux.RLock() + defer db.mux.RUnlock() + + e, err := db.get(name) + if err != nil { + return nil, err + } + return db.parents(e) +} + // Return the refrence count for a specified id func (db *Database) Refs(id string) int { db.mux.RLock() @@ -466,6 +478,28 @@ func (db *Database) children(e *Entity, name string, depth int, entities []WalkM return entities, nil } +func (db *Database) parents(e *Entity) (parents []string, err error) { + if e == nil { + return parents, nil + } + + rows, err := db.conn.Query("SELECT parent_id FROM edge where entity_id = ?;", e.id) + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var parentId string + if err := rows.Scan(&parentId); err != nil { + return nil, err + } + parents = append(parents, parentId) + } + + return parents, nil +} + // Return the entity based on the parent path and name func (db *Database) child(parent *Entity, name string) *Entity { var id string diff --git a/networkfs/etchosts/etchosts.go b/networkfs/etchosts/etchosts.go index 144a039..6cf29b0 100644 --- a/networkfs/etchosts/etchosts.go +++ b/networkfs/etchosts/etchosts.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io/ioutil" + "regexp" ) var defaultContent = map[string]string{ @@ -41,3 +42,12 @@ func Build(path, IP, hostname, domainname string, extraContent *map[string]strin return ioutil.WriteFile(path, content.Bytes(), 0644) } + +func Update(path, IP, hostname string) error { + old, err := ioutil.ReadFile(path) + if err != nil { + return err + } + var re = regexp.MustCompile(fmt.Sprintf("(\\S*)(\\t%s)", regexp.QuoteMeta(hostname))) + return ioutil.WriteFile(path, re.ReplaceAll(old, []byte(IP+"$2")), 0644) +}