// Render updates the given destinationPath according to the given template and options. // Returns true if the file was created or changed, false if nothing has changed. func Render(templateName, destinationPath string, options interface{}, destinationFileMode os.FileMode) (bool, error) { asset, err := Asset(templateName) if err != nil { return false, maskAny(err) } // parse template var tmpl *template.Template tmpl = template.New(templateName) funcMap := template.FuncMap{ "escape": escape, "quote": strconv.Quote, } tmpl.Funcs(funcMap) _, err = tmpl.Parse(string(asset)) if err != nil { return false, maskAny(err) } // execute template to buffer buf := &bytes.Buffer{} tmpl.Funcs(funcMap) err = tmpl.Execute(buf, options) if err != nil { return false, maskAny(err) } // Update file changed, err := util.UpdateFile(destinationPath, buf.Bytes(), destinationFileMode) return changed, maskAny(err) }
func createDockerConfig(rootConfigPath string, deps service.ServiceDependencies, flags *service.ServiceFlags) (bool, error) { if flags.Docker.PrivateRegistryPassword != "" && flags.Docker.PrivateRegistryUrl != "" && flags.Docker.PrivateRegistryUserName != "" { deps.Logger.Info("creating %s", rootConfigPath) // Load config file cf := ConfigFile{ AuthConfigs: make(map[string]AuthConfig), } // Set authentication entries cf.AuthConfigs[flags.Docker.PrivateRegistryUrl] = AuthConfig{ Auth: encodeAuth(AuthConfig{ Username: flags.Docker.PrivateRegistryUserName, Password: flags.Docker.PrivateRegistryPassword, Email: "", }), } // Save os.MkdirAll(filepath.Dir(rootConfigPath), 0700) raw, err := json.MarshalIndent(cf, "", "\t") if err != nil { return false, maskAny(err) } changed, err := util.UpdateFile(rootConfigPath, raw, 0600) return changed, maskAny(err) } else { deps.Logger.Warning("Skip creating .docker config") } return false, nil }
func createFleetConf(deps service.ServiceDependencies, flags *service.ServiceFlags) (bool, error) { proxy, err := isEtcdProxy(deps, flags) if err != nil { return false, maskAny(err) } members, err := flags.GetClusterMembers(deps.Logger) if err != nil { deps.Logger.Warning("GetClusterMembers failed: %v", err) } etcdServers := []string{} for _, cm := range members { if !cm.EtcdProxy { etcdServers = append(etcdServers, fmt.Sprintf("http://%s:2379", cm.ClusterIP)) } } lines := []string{ "[Service]", fmt.Sprintf("Environment=FLEET_METADATA=%s", flags.Fleet.Metadata), fmt.Sprintf("Environment=FLEET_ETCD_SERVERS=%s", strings.Join(etcdServers, ",")), fmt.Sprintf("Environment=FLEET_PUBLIC_IP=%s", flags.Network.ClusterIP), fmt.Sprintf("Environment=FLEET_AGENT_TTL=%v", flags.Fleet.AgentTTL), fmt.Sprintf("Environment=FLEET_DISABLE_ENGINE=%v", proxy || flags.Fleet.DisableEngine), fmt.Sprintf("Environment=FLEET_DISABLE_WATCHES=%v", flags.Fleet.DisableWatches), fmt.Sprintf("Environment=FLEET_ENGINE_RECONCILE_INTERVAL=%d", flags.Fleet.EngineReconcileInterval), fmt.Sprintf("Environment=FLEET_TOKEN_LIMIT=%d", flags.Fleet.TokenLimit), } changed, err := util.UpdateFile(confPath, []byte(strings.Join(lines, "\n")), configFileMode) return changed, maskAny(err) }
func updateNoProxy(path, noProxy string, deps service.ServiceDependencies, flags *service.ServiceFlags) error { deps.Logger.Info("updating %s", path) if _, err := os.Stat(path); os.IsNotExist(err) { return nil } origContent, err := ioutil.ReadFile(path) if err != nil { return maskAny(err) } lines := strings.Split(string(origContent), "\n") updatedLines := []string{} for _, line := range lines { line = strings.TrimSpace(line) if !strings.HasPrefix(strings.ToUpper(line), noProxyPrefix) && line != "" { updatedLines = append(updatedLines, line) } } updatedLines = append(updatedLines, noProxyPrefix+noProxy) newContent := strings.Join(updatedLines, "\n") if _, err := util.UpdateFile(path, []byte(newContent), fileMode); err != nil { return maskAny(err) } return nil }
func createEtcd2Conf(deps service.ServiceDependencies, cfg etcdConfig) (bool, error) { if cfg.ClusterIP == "" { return false, maskAny(fmt.Errorf("ClusterIP empty")) } deps.Logger.Info("creating %s", confPath) lines := []string{ "[Service]", "Environment=ETCD_PEER_AUTO_TLS=true", "Environment=ETCD_LISTEN_PEER_URLS=" + cfg.ListenPeerURLs, "Environment=ETCD_LISTEN_CLIENT_URLS=" + cfg.ListenClientURLs, "Environment=ETCD_INITIAL_CLUSTER=" + cfg.InitialCluster, "Environment=ETCD_INITIAL_CLUSTER_STATE=" + cfg.ClusterState, "Environment=ETCD_INITIAL_ADVERTISE_PEER_URLS=" + cfg.AdvertisePeerURLs, "Environment=ETCD_ADVERTISE_CLIENT_URLS=" + cfg.AdvertiseClientURLs, } if cfg.Name != "" { lines = append(lines, "Environment=ETCD_NAME="+cfg.Name) } if cfg.IsProxy { lines = append(lines, "Environment=ETCD_PROXY=on") } changed, err := util.UpdateFile(confPath, []byte(strings.Join(lines, "\n")), configFileMode) return changed, maskAny(err) }
// create/update /home/core/bin/docker-cleanup.sh func createDockerCleanup(deps service.ServiceDependencies, flags *service.ServiceFlags) (bool, error) { deps.Logger.Info("creating %s", cleanupPath) asset, err := templates.Asset(cleanupSource) if err != nil { return false, maskAny(err) } changed, err := util.UpdateFile(cleanupPath, asset, scriptFileMode) return changed, maskAny(err) }
func createTmpFilesConf(deps service.ServiceDependencies, flags *service.ServiceFlags) (bool, error) { deps.Logger.Info("creating %s", tmpFilesConfPath) asset, err := templates.Asset(tmpFilesConfSource) if err != nil { return false, maskAny(err) } changed, err := util.UpdateFile(tmpFilesConfPath, asset, 0644) return changed, maskAny(err) }
func createJournalConf(deps service.ServiceDependencies, flags *service.ServiceFlags) (bool, error) { lines := []string{ "[Unit]", "Description=Journal Gateway Service Socket", "Documentation=man:systemd-journal-gatewayd(8)", "", "[Socket]", "ListenStream=[::1]:19531", "", "[Install]", "WantedBy=sockets.target", } changed, err := util.UpdateFile(socketPath, []byte(strings.Join(lines, "\n")), configFileMode) return changed, maskAny(err) }
func createPrivateRegistryAuthConf(deps service.ServiceDependencies, flags *service.ServiceFlags) (bool, error) { if flags.Docker.PrivateRegistryPassword != "" && flags.Docker.PrivateRegistryUrl != "" && flags.Docker.PrivateRegistryUserName != "" { deps.Logger.Info("creating %s", privateRegistryAuthConfPath) // Create config /* { "rktKind": "auth", "rktVersion": "v1", "domains": ["coreos.com", "tectonic.com"], "type": "basic", "credentials": { "user": "******", "password": "******" } }*/ cf := struct { Kind string `json:"rktKind"` Version string `json:"rktVersion"` Registries []string `json:"registries"` Type string `json:"type"` Credentials struct { User string `json:"user"` Password string `json:"password"` } `json:"credentials"` }{ Kind: "dockerAuth", Version: "v1", Registries: []string{flags.Docker.PrivateRegistryUrl}, Type: "basic", } cf.Credentials.User = flags.Docker.PrivateRegistryUserName cf.Credentials.Password = flags.Docker.PrivateRegistryPassword // Save os.MkdirAll(filepath.Dir(privateRegistryAuthConfPath), 0700) raw, err := json.MarshalIndent(cf, "", "\t") if err != nil { return false, maskAny(err) } changed, err := util.UpdateFile(privateRegistryAuthConfPath, raw, 0600) return changed, maskAny(err) } else { deps.Logger.Warningf("Skip creating %s", privateRegistryAuthConfPath) } return false, nil }
func updateContent(path, content string, fileMode os.FileMode) (bool, error) { content = strings.TrimSpace(content) os.MkdirAll(filepath.Dir(path), 0755) changed, err := util.UpdateFile(path, []byte(content), fileMode) return changed, maskAny(err) }