func (h *HookDir) runHooks(dirpath string, hType HookType, pod Pod, podManifest manifest.Manifest, logger logging.Logger) error { configFileName, err := podManifest.ConfigFileName() if err != nil { return err } // Write manifest to a file so hooks can read it. tmpManifestFile, err := ioutil.TempFile("", fmt.Sprintf("%s-manifest.yaml", podManifest.ID())) if err != nil { logger.WithErrorAndFields(err, logrus.Fields{ "dir": dirpath, }).Warnln("Unable to open manifest file for hooks") return err } defer os.Remove(tmpManifestFile.Name()) err = podManifest.Write(tmpManifestFile) if err != nil { logger.WithErrorAndFields(err, logrus.Fields{ "dir": dirpath, }).Warnln("Unable to write manifest file for hooks") return err } hookEnvironment := []string{ fmt.Sprintf("%s=%s", HOOK_ENV_VAR, path.Base(dirpath)), fmt.Sprintf("%s=%s", HOOK_EVENT_ENV_VAR, hType.String()), fmt.Sprintf("%s=%s", HOOKED_NODE_ENV_VAR, pod.Node()), fmt.Sprintf("%s=%s", HOOKED_POD_ID_ENV_VAR, podManifest.ID()), fmt.Sprintf("%s=%s", HOOKED_POD_HOME_ENV_VAR, pod.Home()), fmt.Sprintf("%s=%s", HOOKED_POD_MANIFEST_ENV_VAR, tmpManifestFile.Name()), fmt.Sprintf("%s=%s", HOOKED_CONFIG_PATH_ENV_VAR, path.Join(pod.ConfigDir(), configFileName)), fmt.Sprintf("%s=%s", HOOKED_ENV_PATH_ENV_VAR, pod.EnvDir()), fmt.Sprintf("%s=%s", HOOKED_CONFIG_DIR_PATH_ENV_VAR, pod.ConfigDir()), fmt.Sprintf("%s=%s", HOOKED_SYSTEM_POD_ROOT_ENV_VAR, h.podRoot), } return runDirectory(dirpath, hookEnvironment, logger) }
// setupConfig does the following: // // 1) creates a directory in the pod's home directory called "config" which // contains YAML configuration files (named with pod's ID and the SHA of its // manifest's content) the path to which will be exported to a pods launchables // via the CONFIG_PATH environment variable // // 2) writes an "env" directory in the pod's home directory called "env" which // contains environment variables written as files that will be exported to all // processes started by all launchables (as described in // http://smarden.org/runit/chpst.8.html, with the -e option), including // CONFIG_PATH // // 3) writes an "env" directory for each launchable. The "env" directory // contains environment files specific to a launchable (such as // LAUNCHABLE_ROOT) // // We may wish to provide a "config" directory per launchable at some point as // well, so that launchables can have different config namespaces func (pod *Pod) setupConfig(manifest manifest.Manifest, launchables []launch.Launchable) error { uid, gid, err := user.IDs(manifest.RunAsUser()) if err != nil { return util.Errorf("Could not determine pod UID/GID: %s", err) } var configData bytes.Buffer err = manifest.WriteConfig(&configData) if err != nil { return err } var platConfigData bytes.Buffer err = manifest.WritePlatformConfig(&platConfigData) if err != nil { return err } err = util.MkdirChownAll(pod.ConfigDir(), uid, gid, 0755) if err != nil { return util.Errorf("Could not create config directory for pod %s: %s", manifest.ID(), err) } configFileName, err := manifest.ConfigFileName() if err != nil { return err } configPath := filepath.Join(pod.ConfigDir(), configFileName) err = writeFileChown(configPath, configData.Bytes(), uid, gid) if err != nil { return util.Errorf("Error writing config file for pod %s: %s", manifest.ID(), err) } platConfigFileName, err := manifest.PlatformConfigFileName() if err != nil { return err } platConfigPath := filepath.Join(pod.ConfigDir(), platConfigFileName) err = writeFileChown(platConfigPath, platConfigData.Bytes(), uid, gid) if err != nil { return util.Errorf("Error writing platform config file for pod %s: %s", manifest.ID(), err) } err = util.MkdirChownAll(pod.EnvDir(), uid, gid, 0755) if err != nil { return util.Errorf("Could not create the environment dir for pod %s: %s", manifest.ID(), err) } err = writeEnvFile(pod.EnvDir(), ConfigPathEnvVar, configPath, uid, gid) if err != nil { return err } err = writeEnvFile(pod.EnvDir(), PlatformConfigPathEnvVar, platConfigPath, uid, gid) if err != nil { return err } err = writeEnvFile(pod.EnvDir(), PodHomeEnvVar, pod.Home(), uid, gid) if err != nil { return err } err = writeEnvFile(pod.EnvDir(), PodIDEnvVar, pod.Id.String(), uid, gid) if err != nil { return err } err = writeEnvFile(pod.EnvDir(), PodUniqueKeyEnvVar, pod.uniqueKey.String(), uid, gid) if err != nil { return err } for _, launchable := range launchables { // we need to remove any unset env vars from a previous pod err = os.RemoveAll(launchable.EnvDir()) if err != nil { return err } err = util.MkdirChownAll(launchable.EnvDir(), uid, gid, 0755) if err != nil { return util.Errorf("Could not create the environment dir for pod %s launchable %s: %s", manifest.ID(), launchable.ServiceID(), err) } err = writeEnvFile(launchable.EnvDir(), LaunchableIDEnvVar, launchable.ID().String(), uid, gid) if err != nil { return err } err = writeEnvFile(launchable.EnvDir(), "LAUNCHABLE_ROOT", launchable.InstallDir(), uid, gid) if err != nil { return err } // last, write the user-supplied env variables to ensure priority of user-supplied values for envName, value := range launchable.EnvVars() { err = writeEnvFile(launchable.EnvDir(), envName, fmt.Sprint(value), uid, gid) if err != nil { return err } } } return nil }