Skip to content

Commit

Permalink
auto update now executes all writes only when successful
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoates committed Mar 2, 2016
1 parent c333ed5 commit 851de1c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
59 changes: 36 additions & 23 deletions MonolithStarterProject/Scripts/Classes/Assistant.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
STDOUT.sync = true

require 'json'

class Assistant
def initialize()
@classesDirectory = File.expand_path(File.dirname(__FILE__))
Expand All @@ -14,35 +16,35 @@ def readConfig()
# read config.json
configFilename = "config.json"
@configFilepath = File.join(@scriptsDirectory, configFilename)
config = JSON.parse(File.read(configFilepath))
config = JSON.parse(File.read(@configFilepath))
@config = config
end

def runAutoUpdateAsNeeded()
shouldAutoUpdateScript = config['shouldAutoUpdateScript']
shouldAutoUpdateScript = @config['shouldAutoUpdateScript']
if !shouldAutoUpdateScript
return
end

currentTimestamp = Time.now
lastUpdateCheck = config['lastUpdateCheck']
checkUpdatesEveryXHours = config['checkUpdatesEveryXHours']
currentTimestamp = Time.now.to_i
lastUpdateCheck = @config['lastUpdateCheck']
checkUpdatesEveryXHours = @config['checkUpdatesEveryXHours']
checkUpdatesEveryXSeconds = checkUpdatesEveryXHours * 3600
secondsSinceLastUpdateCheck = currentTimestamp - lastUpdateCheck

if secondsSinceLastUpdateCheck > checkUpdatesEveryXSeconds
@config['lastUpdateCheck'] = Time.now
saveConfig()
ensureScriptsAreUpdated()
@config['lastUpdateCheck'] = currentTimestamp
saveConfig()
end
end
def saveConfig()
File.open(@configFilepath, "w") do |fileHandle|
fileHandle.write(JSON.pretty_generate(@config))
prettyOutput = JSON.pretty_generate(@config)
fileHandle.write(prettyOutput)
end
end
def ensureScriptsAreUpdated()
require 'json'
scriptsJSON = JSON.parse(File.read(@scriptsJSONpath))
currentVersion = scriptsJSON['currentScriptsVersion']

Expand All @@ -53,23 +55,28 @@ def ensureScriptsAreUpdated()
if Gem::Version.new(remoteVersion) > Gem::Version.new(currentVersion)
puts "Version #{remoteVersion} of Monolith Scripts are available. You have #{currentVersion}"
puts "Installing new version"
downloadNewScripts(scriptsJSON, remoteScriptsJSON)
if downloadNewScripts(scriptsJSON, remoteScriptsJSON)
buildCRC32sForScripts(scriptsJSON)
end
end

buildCRC32sForScripts(scriptsJSON)
end
def downloadNewScripts(scriptsJSON, remoteScriptsJSON)
remoteScripts = remoteScriptsJSON['scripts']
remoteCRC32Hashes = remoteScriptsJSON['crc32']
localCRC32Hashes = scriptsJSON['crc32']

# buffer writes so that scripts are left
# in a half updated state
pendingWrites = {}

remoteScripts.each do |scriptPath|
localPath = File.join(@projectDirectory, scriptPath)
remotePath = "#{@remoteProjectDirectory}/#{scriptPath}"

if File.exists?(localPath) == false
remoteContents = contentsOfURL(remotePath)
puts "Writing script #{scriptPath}"
pendingWrites[localPath] = remoteContents
puts "New script: #{scriptPath}"
else
# puts "Checking CRC for #{scriptPath}"
localCRC32 = localCRC32Hashes[scriptPath]
Expand Down Expand Up @@ -99,22 +106,31 @@ def downloadNewScripts(scriptsJSON, remoteScriptsJSON)
puts "Update cancelled, continuing"
return false
else

testUpdatedFile = "#{localPath}.updated"
#pendingWrites[testUpdatedFile] = remoteContents
pendingWrites[localPath] = remoteContents
end
end
end
# puts "local vs remote: #{localCRC32}, #{remoteCRC32}"
else # if file is unchanged (crc32 matches)
remoteContents = contentsOfURL(remotePath)
pendingWrites[localPath] = remoteContents
end # currentCRC32 != localCRC32
end # localRC32 != remoteCRC32

end
end
end
end # if File.exists?(localPath) == false
end # remoteScripts.each do |scriptPath|

pendingWrites.each do |filePath, contents|
puts "Writing #{filePath} update."
end
puts "Finished updating, continuing"
end # downloadNewScripts

def overwriteFile(filePath, contents)
File.open(filePath, "w") do |fileHandle|
fileHandle.write contents
end
end

def contentsOfURL(url)
require 'net/http'
uri = URI(url)
Expand Down Expand Up @@ -344,6 +360,3 @@ def ensureSSHKeysAreGenerated()
end
end
end

assistant = Assistant.new()
assistant.ensureScriptsAreUpdated()
2 changes: 0 additions & 2 deletions MonolithStarterProject/Scripts/build.tweak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
# get updates
assistant.runAutoUpdateAsNeeded()

exit;

deviceIP = config['deviceIP']
deviceName = config['deviceName']

Expand Down

0 comments on commit 851de1c

Please sign in to comment.