本文整理汇总了Golang中github.com/juju/utils.AtomicWriteFile函数的典型用法代码示例。如果您正苦于以下问题:Golang AtomicWriteFile函数的具体用法?Golang AtomicWriteFile怎么用?Golang AtomicWriteFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了AtomicWriteFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestReadConfReadsLegacyFormatAndWritesNew
func (*format_1_16Suite) TestReadConfReadsLegacyFormatAndWritesNew(c *gc.C) {
dataDir := c.MkDir()
formatPath := filepath.Join(dataDir, legacyFormatFilename)
err := utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600)
c.Assert(err, gc.IsNil)
configPath := filepath.Join(dataDir, agentConfigFilename)
err = utils.AtomicWriteFile(configPath, []byte(agentConfig1_16Contents), 0600)
c.Assert(err, gc.IsNil)
config, err := ReadConfig(configPath)
c.Assert(err, gc.IsNil)
c.Assert(config, gc.NotNil)
// Test we wrote a currently valid config.
config, err = ReadConfig(configPath)
c.Assert(err, gc.IsNil)
c.Assert(config, gc.NotNil)
c.Assert(config.UpgradedToVersion(), jc.DeepEquals, version.MustParse("1.16.0"))
c.Assert(config.Jobs(), gc.HasLen, 0)
// Old format was deleted.
assertFileNotExist(c, formatPath)
// And new contents were written.
data, err := ioutil.ReadFile(configPath)
c.Assert(err, gc.IsNil)
c.Assert(string(data), gc.Not(gc.Equals), agentConfig1_16Contents)
}
示例2: TestMissingAttributes
func (s *format_1_16Suite) TestMissingAttributes(c *gc.C) {
logDir, err := paths.LogDir(series.HostSeries())
c.Assert(err, jc.ErrorIsNil)
realDataDir, err := paths.DataDir(series.HostSeries())
c.Assert(err, jc.ErrorIsNil)
realDataDir = filepath.FromSlash(realDataDir)
logPath := filepath.Join(logDir, "juju")
logPath = filepath.FromSlash(logPath)
dataDir := c.MkDir()
formatPath := filepath.Join(dataDir, legacyFormatFilename)
err = utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600)
c.Assert(err, jc.ErrorIsNil)
configPath := filepath.Join(dataDir, agentConfigFilename)
err = utils.AtomicWriteFile(configPath, []byte(configDataWithoutNewAttributes), 0600)
c.Assert(err, jc.ErrorIsNil)
readConfig, err := ReadConfig(configPath)
c.Assert(err, jc.ErrorIsNil)
c.Assert(readConfig.UpgradedToVersion(), gc.Equals, version.MustParse("1.16.0"))
configLogDir := filepath.FromSlash(readConfig.LogDir())
configDataDir := filepath.FromSlash(readConfig.DataDir())
c.Assert(configLogDir, gc.Equals, logPath)
c.Assert(configDataDir, gc.Equals, realDataDir)
// Test data doesn't include a StateServerKey so StateServingInfo
// should *not* be available
_, available := readConfig.StateServingInfo()
c.Assert(available, jc.IsFalse)
}
示例3: EnsureServer
// EnsureMongoServer ensures that the correct mongo upstart script is installed
// and running.
//
// This method will remove old versions of the mongo upstart script as necessary
// before installing the new version.
//
// The namespace is a unique identifier to prevent multiple instances of mongo
// on this machine from colliding. This should be empty unless using
// the local provider.
func EnsureServer(dataDir string, namespace string, info params.StateServingInfo) error {
logger.Infof("Ensuring mongo server is running; data directory %s; port %d", dataDir, info.StatePort)
dbDir := filepath.Join(dataDir, "db")
if err := os.MkdirAll(dbDir, 0700); err != nil {
return fmt.Errorf("cannot create mongo database directory: %v", err)
}
certKey := info.Cert + "\n" + info.PrivateKey
err := utils.AtomicWriteFile(sslKeyPath(dataDir), []byte(certKey), 0600)
if err != nil {
return fmt.Errorf("cannot write SSL key: %v", err)
}
err = utils.AtomicWriteFile(sharedSecretPath(dataDir), []byte(info.SharedSecret), 0600)
if err != nil {
return fmt.Errorf("cannot write mongod shared secret: %v", err)
}
// Disable the default mongodb installed by the mongodb-server package.
// Only do this if the file doesn't exist already, so users can run
// their own mongodb server if they wish to.
if _, err := os.Stat(mongoConfigPath); os.IsNotExist(err) {
err = utils.AtomicWriteFile(
mongoConfigPath,
[]byte("ENABLE_MONGODB=no"),
0644,
)
if err != nil {
return err
}
}
if err := aptGetInstallMongod(); err != nil {
return fmt.Errorf("cannot install mongod: %v", err)
}
upstartConf, mongoPath, err := upstartService(namespace, dataDir, dbDir, info.StatePort)
if err != nil {
return err
}
logVersion(mongoPath)
if err := upstartServiceStop(&upstartConf.Service); err != nil {
return fmt.Errorf("failed to stop mongo: %v", err)
}
if err := makeJournalDirs(dbDir); err != nil {
return fmt.Errorf("error creating journal directories: %v", err)
}
if err := preallocOplog(dbDir); err != nil {
return fmt.Errorf("error creating oplog files: %v", err)
}
return upstartConfInstall(upstartConf)
}
示例4: TestStatePortParsed
func (*format_1_16Suite) TestStatePortParsed(c *gc.C) {
dataDir := c.MkDir()
formatPath := filepath.Join(dataDir, legacyFormatFilename)
err := utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600)
c.Assert(err, gc.IsNil)
configPath := filepath.Join(dataDir, agentConfigFilename)
err = utils.AtomicWriteFile(configPath, []byte(stateMachineConfigData), 0600)
c.Assert(err, gc.IsNil)
readConfig, err := ReadConfig(configPath)
c.Assert(err, gc.IsNil)
info, available := readConfig.StateServingInfo()
c.Assert(available, gc.Equals, true)
c.Assert(info.StatePort, gc.Equals, 37017)
}
示例5: WriteCredentialsFile
// WriteCredentialsFile marshals to YAML details of the given credentials
// and writes it to the credentials file.
func WriteCredentialsFile(credentials map[string]cloud.CloudCredential) error {
data, err := yaml.Marshal(credentialsCollection{credentials})
if err != nil {
return errors.Annotate(err, "cannot marshal yaml credentials")
}
return utils.AtomicWriteFile(JujuCredentialsPath(), data, os.FileMode(0600))
}
示例6: writeOrRemove
// writeOrRemove writes the new content of the file or removes it according to Op field.
func (cf ConfigFiles) writeOrRemove() error {
// Create /etc/network/interfaces.d directory if absent.
if _, err := os.Stat(configSubDirName); err != nil {
err := os.Mkdir(configSubDirName, 0755)
if err != nil {
logger.Errorf("failed to create directory %q: %v", configSubDirName, err)
return err
}
}
for fileName, f := range cf {
if f.Op == doRemove {
err := os.Remove(fileName)
if err != nil {
logger.Errorf("failed to remove file %q: %v", fileName, err)
return err
}
} else if f.Op == doWrite {
err := utils.AtomicWriteFile(fileName, []byte(f.Data), 0644)
if err != nil {
logger.Errorf("failed to white file %q: %v", fileName, err)
return err
}
}
}
return nil
}
示例7: WriteControllersFile
// WriteControllersFile marshals to YAML details of the given controllers
// and writes it to the controllers file.
func WriteControllersFile(controllers *Controllers) error {
data, err := yaml.Marshal(controllers)
if err != nil {
return errors.Annotate(err, "cannot marshal yaml controllers")
}
return utils.AtomicWriteFile(JujuControllersPath(), data, os.FileMode(0600))
}
示例8: WriteControllersFile
// WriteControllersFile marshals to YAML details of the given controllers
// and writes it to the controllers file.
func WriteControllersFile(controllers map[string]ControllerDetails) error {
data, err := yaml.Marshal(controllersCollection{controllers})
if err != nil {
return errors.Annotate(err, "cannot marshal yaml controllers")
}
return utils.AtomicWriteFile(JujuControllersPath(), data, os.FileMode(0600))
}
示例9: WriteBootstrapConfigFile
// WriteBootstrapConfigFile marshals to YAML details of the given bootstrap
// configurations and writes it to the bootstrap config file.
func WriteBootstrapConfigFile(configs map[string]BootstrapConfig) error {
data, err := yaml.Marshal(bootstrapConfigCollection{configs})
if err != nil {
return errors.Annotate(err, "cannot marshal bootstrap configurations")
}
return utils.AtomicWriteFile(JujuBootstrapConfigPath(), data, os.FileMode(0600))
}
示例10: Apply
// Apply implements ConfigFile.Apply().
func (f *configFile) Apply() error {
if f.needsUpdating {
err := utils.AtomicWriteFile(f.fileName, f.data, 0644)
if err != nil {
logger.Errorf("failed to write file %q: %v", f.fileName, err)
return err
}
if f.interfaceName == "" {
logger.Debugf("updated main network config %q", f.fileName)
} else {
logger.Debugf("updated network config %q for %q", f.fileName, f.interfaceName)
}
f.needsUpdating = false
}
if f.pendingRemoval {
err := os.Remove(f.fileName)
if err != nil {
logger.Errorf("failed to remove file %q: %v", f.fileName, err)
return err
}
logger.Debugf("removed config %q for %q", f.fileName, f.interfaceName)
f.pendingRemoval = false
}
return nil
}
示例11: WriteAccountsFile
// WriteAccountsFile marshals to YAML details of the given accounts
// and writes it to the accounts file.
func WriteAccountsFile(controllerAccounts map[string]AccountDetails) error {
data, err := yaml.Marshal(accountsCollection{controllerAccounts})
if err != nil {
return errors.Annotate(err, "cannot marshal accounts")
}
return utils.AtomicWriteFile(JujuAccountsPath(), data, os.FileMode(0600))
}
示例12: TestMissingAttributes
func (s *format_1_18Suite) TestMissingAttributes(c *gc.C) {
logDir, err := paths.LogDir(series.HostSeries())
c.Assert(err, jc.ErrorIsNil)
realDataDir, err := paths.DataDir(series.HostSeries())
c.Assert(err, jc.ErrorIsNil)
realDataDir = filepath.FromSlash(realDataDir)
logPath := filepath.Join(logDir, "juju")
logPath = filepath.FromSlash(logPath)
dataDir := c.MkDir()
configPath := filepath.Join(dataDir, agentConfigFilename)
err = utils.AtomicWriteFile(configPath, []byte(configData1_18WithoutUpgradedToVersion), 0600)
c.Assert(err, jc.ErrorIsNil)
readConfig, err := ReadConfig(configPath)
c.Assert(err, jc.ErrorIsNil)
c.Assert(readConfig.UpgradedToVersion(), gc.Equals, version.MustParse("1.16.0"))
configLogDir := filepath.FromSlash(readConfig.LogDir())
configDataDir := filepath.FromSlash(readConfig.DataDir())
c.Assert(configLogDir, gc.Equals, logPath)
c.Assert(configDataDir, gc.Equals, realDataDir)
c.Assert(readConfig.PreferIPv6(), jc.IsFalse)
// The api info doesn't have the environment tag set.
apiInfo, ok := readConfig.APIInfo()
c.Assert(ok, jc.IsTrue)
c.Assert(apiInfo.EnvironTag.Id(), gc.Equals, "")
}
示例13: WriteModelsFile
// WriteModelsFile marshals to YAML details of the given models
// and writes it to the models file.
func WriteModelsFile(models map[string]ControllerAccountModels) error {
data, err := yaml.Marshal(modelsCollection{models})
if err != nil {
return errors.Annotate(err, "cannot marshal models")
}
return utils.AtomicWriteFile(JujuModelsPath(), data, os.FileMode(0600))
}
示例14: WritePublicCloudMetadata
// WritePublicCloudMetadata marshals to YAML and writes the cloud metadata
// to the public cloud file.
func WritePublicCloudMetadata(cloudsMap map[string]Cloud) error {
data, err := marshalCloudMetadata(cloudsMap)
if err != nil {
return errors.Trace(err)
}
return utils.AtomicWriteFile(JujuPublicCloudsPath(), data, 0600)
}
示例15: TestMissingAttributes
func (s *format_1_16Suite) TestMissingAttributes(c *gc.C) {
dataDir := c.MkDir()
formatPath := filepath.Join(dataDir, legacyFormatFilename)
err := utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600)
c.Assert(err, gc.IsNil)
configPath := filepath.Join(dataDir, agentConfigFilename)
err = utils.AtomicWriteFile(configPath, []byte(configDataWithoutNewAttributes), 0600)
c.Assert(err, gc.IsNil)
readConfig, err := ReadConfig(configPath)
c.Assert(err, gc.IsNil)
c.Assert(readConfig.UpgradedToVersion(), gc.Equals, version.MustParse("1.16.0"))
c.Assert(readConfig.LogDir(), gc.Equals, "/var/log/juju")
c.Assert(readConfig.DataDir(), gc.Equals, "/var/lib/juju")
// Test data doesn't include a StateServerKey so StateServingInfo
// should *not* be available
_, available := readConfig.StateServingInfo()
c.Assert(available, gc.Equals, false)
}