Update /etc/hosts when linked container is restarted
Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)
This commit is contained in:
parent
241f52bc17
commit
527e568217
2 changed files with 44 additions and 0 deletions
|
@ -281,6 +281,18 @@ func (db *Database) Children(name string, depth int) ([]WalkMeta, error) {
|
||||||
return db.children(e, name, depth, nil)
|
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
|
// Return the refrence count for a specified id
|
||||||
func (db *Database) Refs(id string) int {
|
func (db *Database) Refs(id string) int {
|
||||||
db.mux.RLock()
|
db.mux.RLock()
|
||||||
|
@ -466,6 +478,28 @@ func (db *Database) children(e *Entity, name string, depth int, entities []WalkM
|
||||||
return entities, nil
|
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
|
// Return the entity based on the parent path and name
|
||||||
func (db *Database) child(parent *Entity, name string) *Entity {
|
func (db *Database) child(parent *Entity, name string) *Entity {
|
||||||
var id string
|
var id string
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultContent = map[string]string{
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue