Add --env-file to ctr
Closes #3517 Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
2ddfc6d2ed
commit
fa11147e5f
@ -77,6 +77,10 @@ var (
|
|||||||
Name: "env",
|
Name: "env",
|
||||||
Usage: "specify additional container environment variables (i.e. FOO=bar)",
|
Usage: "specify additional container environment variables (i.e. FOO=bar)",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "env-file",
|
||||||
|
Usage: "specify additional container environment variables in a file(i.e. FOO=bar, one per line)",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "label",
|
Name: "label",
|
||||||
Usage: "specify additional labels (i.e. foo=bar)",
|
Usage: "specify additional labels (i.e. foo=bar)",
|
||||||
|
@ -64,6 +64,9 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
|||||||
args = context.Args()[2:]
|
args = context.Args()[2:]
|
||||||
)
|
)
|
||||||
opts = append(opts, oci.WithDefaultSpec(), oci.WithDefaultUnixDevices)
|
opts = append(opts, oci.WithDefaultSpec(), oci.WithDefaultUnixDevices)
|
||||||
|
if ef := context.String("env-file"); ef != "" {
|
||||||
|
opts = append(opts, oci.WithEnvFile(ef))
|
||||||
|
}
|
||||||
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
|
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
|
||||||
opts = append(opts, withMounts(context))
|
opts = append(opts, withMounts(context))
|
||||||
|
|
||||||
|
@ -67,6 +67,9 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
|||||||
opts = append(opts, oci.WithWindowNetworksAllowUnqualifiedDNSQuery())
|
opts = append(opts, oci.WithWindowNetworksAllowUnqualifiedDNSQuery())
|
||||||
opts = append(opts, oci.WithWindowsIgnoreFlushesDuringBoot())
|
opts = append(opts, oci.WithWindowsIgnoreFlushesDuringBoot())
|
||||||
}
|
}
|
||||||
|
if ef := context.String("env-file"); ef != "" {
|
||||||
|
opts = append(opts, oci.WithEnvFile(ef))
|
||||||
|
}
|
||||||
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
|
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
|
||||||
opts = append(opts, withMounts(context))
|
opts = append(opts, withMounts(context))
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package oci
|
package oci
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -1200,3 +1201,24 @@ func WithLinuxDevice(path, permissions string) SpecOpts {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithEnvFile adds environment variables from a file to the container's spec
|
||||||
|
func WithEnvFile(path string) SpecOpts {
|
||||||
|
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
|
||||||
|
var vars []string
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
sc := bufio.NewScanner(f)
|
||||||
|
for sc.Scan() {
|
||||||
|
if sc.Err() != nil {
|
||||||
|
return sc.Err()
|
||||||
|
}
|
||||||
|
vars = append(vars, sc.Text())
|
||||||
|
}
|
||||||
|
return WithEnv(vars)(nil, nil, nil, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user