Skip to content
This repository has been archived by the owner on May 27, 2023. It is now read-only.

Commit

Permalink
update command lines, add additional commands to support executable maps
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonYangShadow committed May 27, 2021
1 parent 99f40d1 commit 7aa838c
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 36 deletions.
142 changes: 116 additions & 26 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func Update() *Error {
return cerr
}

func List() *Error {
func List(ListName string) *Error {
currdir, err := GetConfigDir()
if err != nil {
return err
Expand All @@ -406,6 +406,10 @@ func List() *Error {
fmt.Println(fmt.Sprintf("%s%15s%15s%15s%15s%15s%15s", "ContainerID", "ContainerName", "Status", "PID", "RPC", "BaseType", "Image"))
for k, v := range sys.Containers {
if cmap, ok := v.(map[string]interface{}); ok {
//filter with name
if cmap["ContainerName"].(string) != "" && ListName != "" && cmap["ContainerName"].(string) != ListName {
continue
}
//get each container location
root := path.Dir(cmap["RootPath"].(string))

Expand Down Expand Up @@ -733,7 +737,7 @@ func Get(id string, name string, mode bool) *Error {
fmt.Println(fmt.Sprintf("|%-s|%-30s|%-20s|%-20s|%-10s|%-20s|", "ContainerID", "PROGRAM", "ALLOW_PRIVILEGES", "DENY_PRIVILEGES", "REMAP", "ExecMap"))
a_val, _ := getPrivilege(id, name, sys.MemcachedPid, true)
d_val, _ := getPrivilege(id, name, sys.MemcachedPid, false)
m_val, _ := getMap(id, name, sys.MemcachedPid)
m_val, _ := getMap(id, name, sys.MemcachedPid, mode)
e_val, _ := getExec(id, name, sys.MemcachedPid, mode)
fmt.Println(fmt.Sprintf("|%-s|%-30s|%-20s|%-20s|%-10s|%-20s|", id, name, a_val, d_val, m_val, e_val))
} else {
Expand Down Expand Up @@ -771,7 +775,7 @@ func Set(id string, tp string, name string, value string, mode bool) *Error {
}
case ELFOP[4], ELFOP[5]:
{
err := setMap(id, tp, name, value, sys.MemcachedPid)
err := setMap(id, tp, name, value, sys.MemcachedPid, mode)
if err != nil {
return err
}
Expand Down Expand Up @@ -2241,12 +2245,20 @@ func DockerReset(name string) *Error {
return err
}

func CommonFastRun(name, volume_map, command, engine string) *Error {
func CommonFastRun(name, volume_map, command, engine, execmaps string) *Error {
configmap, err := generateContainer(name, "", volume_map, engine)
if err != nil {
return err
}
id := (*configmap)["id"].(string)
if len(execmaps) > 0 {
for _, s := range strings.Split(execmaps, ":") {
k, v := strings.Split(s, "=")[0], strings.Split(s, "=")[1]
if len(k) > 0 && len(v) > 0 {
setExec(id, ELFOP[6], k, v, "", false)
}
}
}
err = Run(configmap, command)
//remove container
if err != nil {
Expand All @@ -2258,11 +2270,19 @@ func CommonFastRun(name, volume_map, command, engine string) *Error {
}

//create container based on images
func CommonCreate(name, container_name, volume_map, engine string) *Error {
func CommonCreate(name, container_name, volume_map, engine, execmaps string) *Error {
configmap, err := generateContainer(name, container_name, volume_map, engine)
if err != nil {
return err
}
if len(execmaps) > 0 {
for _, s := range strings.Split(execmaps, ":") {
k, v := strings.Split(s, "=")[0], strings.Split(s, "=")[1]
if len(k) > 0 && len(v) > 0 {
setExec((*configmap)["id"].(string), ELFOP[6], k, v, "", false)
}
}
}
err = Run(configmap)
return err
}
Expand Down Expand Up @@ -3679,7 +3699,7 @@ func getPrivilege(id string, name string, server string, allow bool) (string, *E
}

func setExec(id, tp, name, value, server string, mode bool) *Error {
if mode {
if !mode {
//read config location
currdir, err := GetConfigDir()
if err != nil {
Expand Down Expand Up @@ -3784,37 +3804,107 @@ func getExec(id, name, server string, mode bool) (string, *Error) {
return "", nil
}

func setMap(id string, tp string, name string, value string, server string) *Error {
mem, err := MInitServers(server)
if err != nil {
return err
}

if tp == ELFOP[4] {
err := mem.MUpdateStrValue(fmt.Sprintf("map:%s:%s", id, name), value)
func setMap(id string, tp string, name string, value string, server string, mode bool) *Error {
if !mode {
//read config location
currdir, err := GetConfigDir()
if err != nil {
return err
}
}
var sys Sys
rootdir := fmt.Sprintf("%s/.lpmxsys", currdir)
err = unmarshalObj(rootdir, &sys)
if err != nil {
return err
}
if v, ok := sys.Containers[id]; ok {
if vval, vok := v.(map[string]interface{}); vok {
configpath := vval["ConfigPath"].(string)
filepath := fmt.Sprintf("%s/.mapmap", configpath)
fc, ferr := FInitServer(filepath)
if ferr != nil {
return ferr
}

if tp == ELFOP[5] {
err := mem.MDeleteByKey(fmt.Sprintf("map:%s:%s", id, name))
if tp == ELFOP[4] {
ferr = fc.FSetValue(name, value)
if ferr != nil {
return ferr
}
}

if tp == ELFOP[5] {
ferr = fc.FDeleteByKey(name)
if ferr != nil {
return ferr
}
}

}
}
} else {
mem, err := MInitServers(server)
if err != nil {
return err
}

if tp == ELFOP[4] {
err := mem.MUpdateStrValue(fmt.Sprintf("map:%s:%s", id, name), value)
if err != nil {
return err
}
}

if tp == ELFOP[5] {
err := mem.MDeleteByKey(fmt.Sprintf("map:%s:%s", id, name))
if err != nil {
return err
}
}
}
return nil
}

func getMap(id string, name string, server string) (string, *Error) {
mem, err := MInitServers(server)
if err != nil {
return "", err
}
func getMap(id string, name string, server string, mode bool) (string, *Error) {
if mode {
//read config location
currdir, err := GetConfigDir()
if err != nil {
return "", err
}
var sys Sys
rootdir := fmt.Sprintf("%s/.lpmxsys", currdir)
err = unmarshalObj(rootdir, &sys)
if err != nil {
return "", err
}
if v, ok := sys.Containers[id]; ok {
if vval, vok := v.(map[string]interface{}); vok {
configpath := vval["ConfigPath"].(string)
filepath := fmt.Sprintf("%s/.mapmap", configpath)
fc, ferr := FInitServer(filepath)
if ferr != nil {
return "", ferr
}

str, err := mem.MGetStrValue(fmt.Sprintf("map:%s:%s", id, name))
if err != nil {
return "", err
str, serr := fc.FGetStrValue(name)
if serr != nil {
return "", serr
}
return str, nil
}
}
} else {
mem, err := MInitServers(server)
if err != nil {
return "", err
}

str, err := mem.MGetStrValue(fmt.Sprintf("map:%s:%s", id, name))
if err != nil {
return "", err
}
return str, nil
}
return str, nil
return "", nil
}
30 changes: 20 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
)

const (
VERSION = "alpha-1.7"
VERSION = "alpha-1.8"
)

func checkCompleteness() *Error {
Expand Down Expand Up @@ -70,6 +70,7 @@ func main() {
initCmd.Flags().BoolVarP(&InitReset, "reset", "r", false, "initialize by force(optional)")
initCmd.Flags().StringVarP(&InitDep, "dependency", "d", "", "dependency tar ball(optional)")

var ListName string
var listCmd = &cobra.Command{
Use: "list",
Short: "list the containers in lpmx system",
Expand All @@ -83,21 +84,22 @@ func main() {
}
},
Run: func(cmd *cobra.Command, args []string) {
err := List()
err := List(ListName)
if err != nil {
LOGGER.Fatal(err.Error())
return
}
},
}
listCmd.Flags().StringVarP(&ListName, "name", "n", "", "container name(optional)")

var GetId string
var GetName string
var GetMode bool
var getCmd = &cobra.Command{
Use: "get",
Short: "get settings from memcache server",
Long: "get command is the basic command of lpmx, which is used for getting settings from cache server",
Short: "get settings",
Long: "get command is the basic command of lpmx, which is used for getting settings",
Args: cobra.ExactArgs(0),
PreRun: func(cmd *cobra.Command, args []string) {
err := checkCompleteness()
Expand All @@ -124,7 +126,7 @@ func main() {
getCmd.MarkFlagRequired("id")
getCmd.Flags().StringVarP(&GetName, "name", "n", "", "required")
getCmd.MarkFlagRequired("name")
getCmd.Flags().BoolVarP(&GetMode, "filecache", "c", false, "use FileCaache rather than Memcached(optional)")
getCmd.Flags().BoolVarP(&GetMode, "filecache", "c", false, "use FileCache rather than Memcached(optional)")

var DownloadSource string
var downloadCmd = &cobra.Command{
Expand Down Expand Up @@ -414,6 +416,7 @@ func main() {
var DockerCreateName string
var DockerCreateVolume string
var DockerCreateEngine string
var DockerCreateExecMap string
var dockerCreateCmd = &cobra.Command{
Use: "create",
Short: "initialize the local docker images",
Expand All @@ -437,7 +440,7 @@ func main() {
},

Run: func(cmd *cobra.Command, args []string) {
err := CommonCreate(args[0], DockerCreateName, DockerCreateVolume, DockerCreateEngine)
err := CommonCreate(args[0], DockerCreateName, DockerCreateVolume, DockerCreateEngine, DockerCreateExecMap)
if err != nil {
LOGGER.Fatal(err.Error())
return
Expand All @@ -447,9 +450,11 @@ func main() {
dockerCreateCmd.Flags().StringVarP(&DockerCreateName, "name", "n", "", "optional")
dockerCreateCmd.Flags().StringVarP(&DockerCreateVolume, "volume", "v", "", "optional")
dockerCreateCmd.Flags().StringVarP(&DockerCreateEngine, "engine", "e", "", "use engine(optional)")
dockerCreateCmd.Flags().StringVarP(&DockerCreateExecMap, "map", "m", "", "executables map, host_exec1=container_exec1:host_exec2=container_exec2(optional)")

var DockerRunVolume string
var DockerRunMode string
var DockerRunExecMap string
var dockerRunCmd = &cobra.Command{
Use: "fastrun",
Short: "run container in a fast way without switching into shell",
Expand All @@ -471,7 +476,7 @@ func main() {
}
},
Run: func(cmd *cobra.Command, args []string) {
err := CommonFastRun(args[0], DockerRunVolume, args[1], DockerRunMode)
err := CommonFastRun(args[0], DockerRunVolume, args[1], DockerRunMode, DockerRunExecMap)
if err != nil {
LOGGER.Fatal(err.Error())
return
Expand All @@ -480,6 +485,7 @@ func main() {
}
dockerRunCmd.Flags().StringVarP(&DockerRunVolume, "volume", "v", "", "optional")
dockerRunCmd.Flags().StringVarP(&DockerRunMode, "engine", "e", "", "use engine(optional)")
dockerRunCmd.Flags().StringVarP(&DockerRunExecMap, "map", "m", "", "executables map, host_exec1=container_exec1:host_exec2=container_exec2(optional)")

var DockerDeletePermernant bool
var dockerDeleteCmd = &cobra.Command{
Expand Down Expand Up @@ -675,6 +681,7 @@ func main() {
var SingularityCreateName string
var SingularityCreateVolume string
var SingularityCreateEngine string
var SingularityCreateExecMap string
var singularityCreateCmd = &cobra.Command{
Use: "create",
Short: "initialize the local singularity images",
Expand All @@ -698,7 +705,7 @@ func main() {
},

Run: func(cmd *cobra.Command, args []string) {
err := CommonCreate(args[0], SingularityCreateName, SingularityCreateVolume, SingularityCreateEngine)
err := CommonCreate(args[0], SingularityCreateName, SingularityCreateVolume, SingularityCreateEngine, SingularityCreateExecMap)
if err != nil {
LOGGER.Fatal(err.Error())
return
Expand All @@ -708,6 +715,7 @@ func main() {
singularityCreateCmd.Flags().StringVarP(&SingularityCreateName, "name", "n", "", "optional")
singularityCreateCmd.Flags().StringVarP(&SingularityCreateVolume, "volume", "v", "", "optional")
singularityCreateCmd.Flags().StringVarP(&SingularityCreateEngine, "engine", "e", "", "use engine(optional)")
singularityCreateCmd.Flags().StringVarP(&SingularityCreateExecMap, "map", "m", "", "executables map, host_exec1=container_exec1:host_exec2=container_exec2(optional)")

var SingularityDeletePermernant bool
var singularityDeleteCmd = &cobra.Command{
Expand Down Expand Up @@ -758,6 +766,7 @@ func main() {

var SingularityRunVolume string
var SingularityRunMode string
var SingularityRunExecMap string
var singularityRunCmd = &cobra.Command{
Use: "fastrun",
Short: "run container in a fast way without switching into shell",
Expand All @@ -779,7 +788,7 @@ func main() {
}
},
Run: func(cmd *cobra.Command, args []string) {
err := CommonFastRun(args[0], DockerRunVolume, args[1], SingularityRunMode)
err := CommonFastRun(args[0], DockerRunVolume, args[1], SingularityRunMode, SingularityRunExecMap)
if err != nil {
LOGGER.Fatal(err.Error())
return
Expand All @@ -788,6 +797,7 @@ func main() {
}
singularityRunCmd.Flags().StringVarP(&SingularityRunVolume, "volume", "v", "", "optional")
singularityRunCmd.Flags().StringVarP(&SingularityRunMode, "engine", "e", "", "use engine(optional)")
singularityRunCmd.Flags().StringVarP(&SingularityRunExecMap, "map", "m", "", "executables map, host_exec1=container_exec1:host_exec2=container_exec2(optional)")

var singularityCmd = &cobra.Command{
Use: "singularity",
Expand Down Expand Up @@ -931,7 +941,7 @@ func main() {
setCmd.Flags().StringVarP(&SetProg, "name", "n", "", "required(should be the name of libc 'system calls wrapper' or mapped program path)")
setCmd.MarkFlagRequired("name")
setCmd.Flags().StringVarP(&SetVal, "value", "v", "", "required in add mode(value(file1:replace_file1;file2:repalce_file2;) or a mapped path) while optional in remove mode")
setCmd.Flags().BoolVarP(&SetMode, "filecache", "c", false, "use FileCache rather than Memcached(optional)")
setCmd.Flags().BoolVarP(&SetMode, "memcached", "c", false, "use Memcached rather than Filecache(optional)")

var uninstallCmd = &cobra.Command{
Use: "uninstall",
Expand Down

0 comments on commit 7aa838c

Please sign in to comment.