-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: allen.wang <allen.wq@alipay.com>
- Loading branch information
1 parent
ff5d11f
commit f16afaa
Showing
23 changed files
with
2,054 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package mgr | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/alibaba/pouch/pkg/nsexec" | ||
) | ||
|
||
func init() { | ||
nsexec.Register("sethostname", handleUpdateHostname) | ||
} | ||
|
||
func handleUpdateHostname(data []byte) (res []byte, err error) { | ||
oldHostname, err := os.Hostname() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = syscall.Sethostname(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//update /etc/hostname and /etc/hosts | ||
err = ioutil.WriteFile("/etc/hostname", append(data, '\n'), 0644) | ||
if err != nil { | ||
return nil, fmt.Errorf("update /etc/hostname failed:%s", err.Error()) | ||
} | ||
|
||
hosts, err := ioutil.ReadFile("/etc/hosts") | ||
if err != nil { | ||
return nil, fmt.Errorf("update /etc/hosts failed:%s", err.Error()) | ||
} | ||
|
||
exp1 := regexp.MustCompile(fmt.Sprintf(`\s%s\s`, oldHostname)) | ||
exp2 := regexp.MustCompile(fmt.Sprintf(`\s%s$`, oldHostname)) | ||
|
||
replaceStr1 := fmt.Sprintf(" %s ", string(data)) | ||
replaceStr2 := fmt.Sprintf(" %s", string(data)) | ||
|
||
d := string(hosts) | ||
lines := strings.Split(d, "\n") | ||
for i, line := range lines { | ||
if !strings.Contains(line, oldHostname) { | ||
continue | ||
} | ||
|
||
newLine := line | ||
|
||
if exp1.FindString(newLine) != "" { | ||
newLine = exp1.ReplaceAllString(newLine, replaceStr1) | ||
} | ||
|
||
if exp2.FindString(newLine) != "" { | ||
newLine = exp2.ReplaceAllString(newLine, replaceStr2) | ||
} | ||
|
||
lines[i] = newLine | ||
} | ||
|
||
newHosts := strings.Join(lines, "\n") | ||
err = ioutil.WriteFile("/etc/hosts", []byte(newHosts), 0644) | ||
|
||
return nil, err | ||
} | ||
|
||
func updateHostnameForRunningContainer(hostname string, container *Container) error { | ||
pid := container.State.Pid | ||
if pid <= 0 { | ||
return fmt.Errorf("container pid %d is not vaild", pid) | ||
} | ||
|
||
_, err := nsexec.NsExec(int(pid), nsexec.Op{OpCode: "sethostname", Data: []byte(hostname)}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// +build linux | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"strconv" | ||
|
||
_ "github.com/opencontainers/runc/libcontainer/nsenter" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/alibaba/pouch/pkg/nsexec" | ||
) | ||
|
||
//reexecRunNsExec will run nsenter in go init and join to container namespace | ||
func reexecRunNsExec() { | ||
logrus.Infof("run reexecRunNsExec") | ||
initPipe := os.Getenv("_LIBCONTAINER_INITPIPE") | ||
pipeFd, err := strconv.Atoi(initPipe) | ||
if err != nil { | ||
panic("init pipe get failed") | ||
} | ||
|
||
f := os.NewFile(uintptr(pipeFd), "init") | ||
defer f.Close() | ||
|
||
nsexecOp := nsexec.Op{} | ||
err = json.NewDecoder(f).Decode(&nsexecOp) | ||
if err != nil { | ||
panic("op failed") | ||
} | ||
|
||
res := nsexec.Dispatch(nsexecOp) | ||
err = json.NewEncoder(f).Encode(res) | ||
if err != nil { | ||
panic("send data failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package nsexec | ||
|
||
import "fmt" | ||
|
||
// Dispatch should be called by pouchd nsexec to do function in container namespace | ||
func Dispatch(op Op) *Result { | ||
f, exist := regigsterFuncMap[op.OpCode] | ||
if !exist { | ||
return &Result{ | ||
Finish: true, | ||
ErrMsg: fmt.Sprintf("not register nsexec op %s", op.OpCode), | ||
} | ||
} | ||
|
||
res, err := f(op.Data) | ||
errMsg := "" | ||
if err != nil { | ||
errMsg = err.Error() | ||
} | ||
|
||
return &Result{ | ||
Finish: true, | ||
ErrMsg: errMsg, | ||
Data: res, | ||
} | ||
} |
Oops, something went wrong.