Improved do, added package verify (check if package is deployed correctly) method.

This commit is contained in:
Martin Edenhofer 2016-02-01 00:20:14 +01:00
parent 4503ba4d16
commit c4206539d1
2 changed files with 137 additions and 26 deletions

View file

@ -3,8 +3,59 @@
class Package < ApplicationModel class Package < ApplicationModel
@@root = Rails.root.to_s # rubocop:disable Style/ClassVars @@root = Rails.root.to_s # rubocop:disable Style/ClassVars
# Package.auto_install =begin
# install all packages located under auto_install/*.zpm
verify if package is installed correctly
package = Package.find(123)
issues = package.verify
returns:
# if no issue exists
nil
# list of issues
{
'path/to/file' => 'missing',
'path/to/file' => 'changed',
}
=end
def verify
# get package
json_file = self.class._get_bin(name, version)
package = JSON.parse(json_file)
# verify installed files
issues = {}
package['files'].each { |file|
if !File.exist?(file['location'])
logger.error "File #{file['location']} is missing"
issues[file['location']] = 'missing'
next
end
content_package = Base64.decode64(file['content'])
content_fs = self.class._read_file(file['location'])
next if content_package == content_fs
logger.error "File #{file['location']} is different"
issues[file['location']] = 'changed'
}
return nil if issues.empty?
issues
end
=begin
install all packages located under auto_install/*.zpm
Package.auto_install
=end
def self.auto_install def self.auto_install
path = "#{@@root}/auto_install/" path = "#{@@root}/auto_install/"
return if !File.exist?(path) return if !File.exist?(path)
@ -20,9 +71,16 @@ class Package < ApplicationModel
data data
end end
# Package.unlink_all =begin
# remove all linked files in application
# note: will not take down package migrations, use Package.unlink instead remove all linked files in application
note: will not take down package migrations, use Package.unlink instead
Package.unlink_all
=end
def self.unlink_all def self.unlink_all
# link files # link files
Dir.glob("#{@@root}/**/*") do |entry| Dir.glob("#{@@root}/**/*") do |entry|
@ -51,8 +109,14 @@ class Package < ApplicationModel
package package
end end
# Package.unlink('/path/to/src/extention') =begin
# execute migration down + unlink files
execute migration down + unlink files
Package.unlink('/path/to/src/extention')
=end
def self.unlink(package_base_dir) def self.unlink(package_base_dir)
# check if zpm is a package source repo # check if zpm is a package source repo
@ -81,8 +145,14 @@ class Package < ApplicationModel
end end
end end
# Package.link('/path/to/src/extention') =begin
# link files + execute migration up
link files + execute migration up
Package.link('/path/to/src/extention')
=end
def self.link(package_base_dir) def self.link(package_base_dir)
# check if zpm is a package source repo # check if zpm is a package source repo
@ -134,8 +204,22 @@ class Package < ApplicationModel
Package::Migration.migrate(package) Package::Migration.migrate(package)
end end
# Package.install(file: '/path/to/package.zpm') =begin
# Package.install(string: zpm_as_string)
install zpm package
package = Package.install(file: '/path/to/package.zpm')
or
package = Package.install(string: zpm_as_string)
returns
package # record of new created packae
=end
def self.install(data) def self.install(data)
if data[:file] if data[:file]
json = _read_file(data[:file], true) json = _read_file(data[:file], true)
@ -203,10 +287,21 @@ class Package < ApplicationModel
# prebuild assets # prebuild assets
true record
end end
# Package.reinstall(package_name) =begin
reinstall package
package = Package.reinstall(package_name)
returns
package # record of new created packae
=end
def self.reinstall(package_name) def self.reinstall(package_name)
package = Package.find_by(name: package_name) package = Package.find_by(name: package_name)
if !package if !package
@ -215,10 +310,25 @@ class Package < ApplicationModel
file = _get_bin(package.name, package.version) file = _get_bin(package.name, package.version)
install(string: file, reinstall: true) install(string: file, reinstall: true)
package
end end
# Package.uninstall(name: 'package', version: '0.1.1') =begin
# Package.uninstall(string: zpm_as_string)
uninstall package
package = Package.uninstall(name: 'package', version: '0.1.1')
or
package = Package.uninstall(string: zpm_as_string)
returns
package # record of new created packae
=end
def self.uninstall(data) def self.uninstall(data)
if data[:string] if data[:string]
@ -246,7 +356,7 @@ class Package < ApplicationModel
) )
record.destroy record.destroy
true record
end end
def self._get_bin(name, version) def self._get_bin(name, version)

View file

@ -277,34 +277,35 @@ class PackageTest < ActiveSupport::TestCase
tests.each { |test| tests.each { |test|
if test[:action] == 'install' if test[:action] == 'install'
begin begin
success = Package.install( string: test[:zpm] ) package = Package.install( string: test[:zpm] )
rescue => e rescue => e
puts 'ERROR: ' + e.inspect puts 'ERROR: ' + e.inspect
success = false
end end
if test[:result] if test[:result]
assert( success, 'install package not successful' ) assert( package, 'install package not successful' )
issues = package.verify
assert( !issues, 'package verify not successful' )
else else
assert( !success, 'install package successful but should not' ) assert( !package, 'install package successful but should not' )
end end
elsif test[:action] == 'uninstall' elsif test[:action] == 'uninstall'
if test[:zpm] if test[:zpm]
begin begin
success = Package.uninstall( string: test[:zpm] ) package = Package.uninstall( string: test[:zpm] )
rescue rescue
success = false package = false
end end
else else
begin begin
success = Package.uninstall( name: test[:name], version: test[:version] ) package = Package.uninstall( name: test[:name], version: test[:version] )
rescue rescue
success = false package = false
end end
end end
if test[:result] if test[:result]
assert( success, 'uninstall package not successful' ) assert( package, 'uninstall package not successful' )
else else
assert( !success, 'uninstall package successful but should not' ) assert( !package, 'uninstall package successful but should not' )
end end
elsif test[:action] == 'auto_install' elsif test[:action] == 'auto_install'
if test[:zpm] if test[:zpm]