package process_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "os" "path/filepath" "github.com/cloudfoundry-incubator/garden-linux/process" ) var _ = Describe("Environment", func() { Context("with an empty environment", func() { It("turns into an empty array", func() { env := process.Env{} Expect(env.Array()).To(BeEmpty()) }) }) Context("with a non empty environment", func() { It("converts the environment into the corresponding array", func() { env := process.Env{ "HOME": "/home/alice", "USER": "******", } Expect(env.Array()).To(ConsistOf( "HOME=/home/alice", "USER=alice", )) })
func (p *LinuxResourcePool) acquireSystemResources(spec garden.ContainerSpec, id string, resources *linux_backend.Resources, pLog lager.Logger) (string, process.Env, error) { containerPath := path.Join(p.depotPath, id) if err := os.MkdirAll(containerPath, 0755); err != nil { return "", nil, fmt.Errorf("resource_pool: creating container directory: %v", err) } rootFSPath, rootFSEnvVars, err := p.setupContainerDirectories(spec, id, resources, pLog) if err != nil { os.RemoveAll(containerPath) return "", nil, err } createCmd := path.Join(p.binPath, "create.sh") create := exec.Command(createCmd, containerPath) suff, _ := resources.Network.Subnet.Mask.Size() env := process.Env{ "id": id, "rootfs_path": rootFSPath, "network_host_ip": subnets.GatewayIP(resources.Network.Subnet).String(), "network_container_ip": resources.Network.IP.String(), "network_cidr_suffix": strconv.Itoa(suff), "network_cidr": resources.Network.Subnet.String(), "external_ip": p.externalIP.String(), "container_iface_mtu": fmt.Sprintf("%d", p.mtu), "bridge_iface": resources.Bridge, "root_uid": strconv.FormatUint(uint64(resources.RootUID), 10), "PATH": os.Getenv("PATH"), } create.Env = env.Array() pRunner := logging.Runner{ CommandRunner: p.runner, Logger: pLog.Session("create-script"), } err = pRunner.Run(create) defer cleanup(&err, func() { p.tryReleaseSystemResources(pLog, id) }) if err != nil { pLog.Error("create-command-failed", err, lager.Data{ "CreateCmd": createCmd, "Env": create.Env, }) return "", nil, err } err = p.saveRootFSProvider(id, "docker-composite") if err != nil { pLog.Error("save-rootfs-provider-failed", err, lager.Data{ "Id": id, "rootfs": spec.RootFSPath, }) return "", nil, err } err = p.saveContainerVersion(id) if err != nil { pLog.Error("save-container-version-failed", err, lager.Data{ "Id": id, "ContainerPath": containerPath, }) return "", nil, err } err = p.writeBindMounts(containerPath, rootFSPath, spec.BindMounts, resources.RootUID) if err != nil { pLog.Error("bind-mounts-failed", err) return "", nil, err } return rootFSPath, rootFSEnvVars, nil }
func (p *LinuxResourcePool) acquireSystemResources(id, handle, containerPath, rootFSPath string, resources *linux_backend.Resources, bindMounts []garden.BindMount, diskQuota int64, pLog lager.Logger) (string, process.Env, error) { if err := os.MkdirAll(containerPath, 0755); err != nil { return "", nil, fmt.Errorf("containerpool: creating container directory: %v", err) } rootfsURL, err := url.Parse(rootFSPath) if err != nil { pLog.Error("parse-rootfs-path-failed", err, lager.Data{ "RootFSPath": rootFSPath, }) return "", nil, err } provider, found := p.rootfsProviders[rootfsURL.Scheme] if !found { pLog.Error("unknown-rootfs-provider", nil, lager.Data{ "provider": rootfsURL.Scheme, }) return "", nil, ErrUnknownRootFSProvider } rootfsPath, rootFSEnvVars, err := provider.ProvideRootFS(pLog.Session("create-rootfs"), id, rootfsURL, resources.RootUID != 0, diskQuota) if err != nil { pLog.Error("provide-rootfs-failed", err) return "", nil, err } if resources.Bridge, err = p.bridges.Reserve(resources.Network.Subnet, id); err != nil { pLog.Error("reserve-bridge-failed", err, lager.Data{ "Id": id, "Subnet": resources.Network.Subnet, "Bridge": resources.Bridge, }) p.rootfsRemover.Remove(layercake.ContainerID(rootfsPath)) return "", nil, err } if err = p.saveBridgeName(id, resources.Bridge); err != nil { pLog.Error("save-bridge-name-failed", err, lager.Data{ "Id": id, "Bridge": resources.Bridge, }) p.rootfsRemover.Remove(layercake.ContainerID(rootfsPath)) return "", nil, err } createCmd := path.Join(p.binPath, "create.sh") create := exec.Command(createCmd, containerPath) suff, _ := resources.Network.Subnet.Mask.Size() env := process.Env{ "id": id, "rootfs_path": rootfsPath, "network_host_ip": subnets.GatewayIP(resources.Network.Subnet).String(), "network_container_ip": resources.Network.IP.String(), "network_cidr_suffix": strconv.Itoa(suff), "network_cidr": resources.Network.Subnet.String(), "external_ip": p.externalIP.String(), "container_iface_mtu": fmt.Sprintf("%d", p.mtu), "bridge_iface": resources.Bridge, "root_uid": strconv.FormatUint(uint64(resources.RootUID), 10), "PATH": os.Getenv("PATH"), } create.Env = env.Array() pRunner := logging.Runner{ CommandRunner: p.runner, Logger: pLog.Session("create-script"), } err = pRunner.Run(create) defer cleanup(&err, func() { p.tryReleaseSystemResources(p.logger, id) }) if err != nil { p.logger.Error("create-command-failed", err, lager.Data{ "CreateCmd": createCmd, "Env": create.Env, }) return "", nil, err } err = p.saveRootFSProvider(id, provider.Name()) if err != nil { p.logger.Error("save-rootfs-provider-failed", err, lager.Data{ "Id": id, "rootfs": rootfsURL.String(), }) return "", nil, err } err = p.saveContainerVersion(id) if err != nil { p.logger.Error("save-container-version-failed", err, lager.Data{ "Id": id, "ContainerPath": containerPath, }) return "", nil, err } err = p.writeBindMounts(containerPath, rootfsPath, bindMounts) if err != nil { p.logger.Error("bind-mounts-failed", err) return "", nil, err } filterLog := pLog.Session("setup-filter") filterLog.Debug("starting") if err = p.filterProvider.ProvideFilter(id).Setup(handle); err != nil { p.logger.Error("set-up-filter-failed", err) return "", nil, fmt.Errorf("resource_pool: set up filter: %v", err) } filterLog.Debug("finished") return rootfsPath, rootFSEnvVars, nil }