mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-21 12:05:00 +00:00
Merge pull request #64 from resin-io/63-write-configjson-atomically
Make config.json writes atomic
This commit is contained in:
commit
6d3abe1e34
@ -1,3 +1,5 @@
|
||||
* Make config.json writes atomic [Pablo]
|
||||
|
||||
# v1.3.2
|
||||
|
||||
* Do not report the tun addresses to API [Praneeth]
|
||||
|
@ -6,6 +6,7 @@ deviceRegister = require 'resin-register-device'
|
||||
{ resinApi } = require './request'
|
||||
fs = Promise.promisifyAll(require('fs'))
|
||||
EventEmitter = require('events').EventEmitter
|
||||
execAsync = Promise.promisify(require('child_process').exec)
|
||||
config = require './config'
|
||||
configPath = '/boot/config.json'
|
||||
appsPath = '/boot/apps.json'
|
||||
@ -48,7 +49,7 @@ bootstrap = ->
|
||||
.then (device) ->
|
||||
userConfig.registered_at = Date.now()
|
||||
userConfig.deviceId = device.id
|
||||
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
|
||||
writeConfigFile(userConfig)
|
||||
.return(userConfig)
|
||||
.then (userConfig) ->
|
||||
console.log('Finishing bootstrapping')
|
||||
@ -66,6 +67,14 @@ bootstrap = ->
|
||||
.tap ->
|
||||
bootstrapper.doneBootstrapping()
|
||||
|
||||
|
||||
writeConfigFile = (config) ->
|
||||
fs.writeFileAsync(configPath + '.new', JSON.stringify(config))
|
||||
.then ->
|
||||
fs.renameAsync(configPath + '.new', configPath)
|
||||
.then ->
|
||||
execAsync('sync')
|
||||
|
||||
readConfigAndEnsureUUID = ->
|
||||
# Load config file
|
||||
fs.readFileAsync(configPath, 'utf8')
|
||||
@ -76,7 +85,7 @@ readConfigAndEnsureUUID = ->
|
||||
deviceRegister.generateUUID()
|
||||
.then (uuid) ->
|
||||
userConfig.uuid = uuid
|
||||
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
|
||||
writeConfigFile(userConfig)
|
||||
.return(uuid)
|
||||
.catch (err) ->
|
||||
console.log('Error generating and saving UUID: ', err)
|
||||
|
Loading…
Reference in New Issue
Block a user