- Replaced package XML structure by JSON.

- Removed Package.build() in favor of the 'zammad-szpm' gem.
This commit is contained in:
Thorsten Eckel 2016-01-31 18:25:31 +01:00
parent f52cf41a35
commit 977cc3d981
2 changed files with 153 additions and 169 deletions

View file

@ -1,48 +1,8 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
require 'rexml/document'
class Package < ApplicationModel class Package < ApplicationModel
@@root = Rails.root.to_s # rubocop:disable Style/ClassVars @@root = Rails.root.to_s # rubocop:disable Style/ClassVars
# build package based on .szpm
# Package.build(
# file: 'package.szpm',
# root: '/path/to/src/extention/',
# output: '/path/to/package_location/'
# )
def self.build(data)
if data[:file]
xml = _read_file(data[:file], data[:root] || true)
package = _parse(xml)
elsif data[:string]
package = _parse(data[:string])
end
build_date = REXML::Element.new('build_date')
build_date.text = Time.zone.now.iso8601
build_host = REXML::Element.new('build_host')
build_host.text = Socket.gethostname
package.root.insert_after('//zpm/description', build_date)
package.root.insert_after('//zpm/description', build_host)
package.elements.each('zpm/filelist/file') do |element|
location = element.attributes['location']
content = _read_file(location, data[:root])
base64 = Base64.encode64(content)
element.text = base64
end
if data[:output]
location = "#{data[:output]}/#{package.elements['zpm/name'].text}-#{package.elements['zpm/version'].text}.zpm"
logger.info "NOTICE: writting package to '#{location}'"
file = File.new(location, 'wb')
file.write(package.to_s)
file.close
return true
end
package.to_s
end
# Package.auto_install # Package.auto_install
# install all packages located under auto_install/*.zpm # install all packages located under auto_install/*.zpm
def self.auto_install def self.auto_install
@ -178,17 +138,17 @@ class Package < ApplicationModel
# Package.install(string: zpm_as_string) # Package.install(string: zpm_as_string)
def self.install(data) def self.install(data)
if data[:file] if data[:file]
xml = _read_file(data[:file], true) json = _read_file(data[:file], true)
package = _parse(xml) package = JSON.parse(json)
elsif data[:string] elsif data[:string]
package = _parse(data[:string]) package = JSON.parse(data[:string])
end end
# package meta data # package meta data
meta = { meta = {
name: package.elements['zpm/name'].text, name: package['name'],
version: package.elements['zpm/version'].text, version: package['version'],
vendor: package.elements['zpm/vendor'].text, vendor: package['vendor'],
state: 'uninstalled', state: 'uninstalled',
created_by_id: 1, created_by_id: 1,
updated_by_id: 1, updated_by_id: 1,
@ -220,7 +180,7 @@ class Package < ApplicationModel
Store.add( Store.add(
object: 'Package', object: 'Package',
o_id: record.id, o_id: record.id,
data: package.to_s, data: package.to_json,
filename: "#{meta[:name]}-#{meta[:version]}.zpm", filename: "#{meta[:name]}-#{meta[:version]}.zpm",
preferences: {}, preferences: {},
created_by_id: UserInfo.current_user_id || 1, created_by_id: UserInfo.current_user_id || 1,
@ -228,13 +188,11 @@ class Package < ApplicationModel
end end
# write files # write files
package.elements.each('zpm/filelist/file') do |element| package['files'].each { |file|
location = element.attributes['location'] permission = file['permission'] || '644'
permission = element.attributes['permission'] || '644' content = Base64.decode64(file['content'])
base64 = element.text _write_file(file['location'], permission, content)
content = Base64.decode64(base64) }
content = _write_file(location, permission, content)
end
# update package state # update package state
record.state = 'installed' record.state = 'installed'
@ -264,55 +222,33 @@ class Package < ApplicationModel
def self.uninstall(data) def self.uninstall(data)
if data[:string] if data[:string]
package = _parse(data[:string]) package = JSON.parse(data[:string])
else else
file = _get_bin(data[:name], data[:version]) json_file = _get_bin(data[:name], data[:version])
package = _parse(file) package = JSON.parse(json_file)
end end
# package meta data
meta = {
name: package.elements['zpm/name'].text,
version: package.elements['zpm/version'].text,
}
# down migrations # down migrations
if !data[:migration_not_down] if !data[:migration_not_down]
Package::Migration.migrate(meta[:name], 'reverse') Package::Migration.migrate(package['name'], 'reverse')
end end
package.elements.each('zpm/filelist/file') do |element| package['files'].each { |file|
location = element.attributes['location'] permission = file['permission'] || '644'
permission = element.attributes['permission'] || '644' content = Base64.decode64(file['content'])
base64 = element.text _delete_file(file['location'], permission, content)
content = Base64.decode64(base64) }
content = _delete_file(location, permission, content)
end
# prebuild assets
# delete package # delete package
record = Package.find_by( record = Package.find_by(
name: meta[:name], name: package['name'],
version: meta[:version], version: package['version'],
) )
record.destroy record.destroy
true true
end end
def self._parse(xml)
logger.debug xml.inspect
begin
package = REXML::Document.new(xml)
rescue => e
logger.error 'ERROR: ' + e.inspect
return
end
logger.debug package.inspect
package
end
def self._get_bin(name, version) def self._get_bin(name, version)
package = Package.find_by( package = Package.find_by(
name: name, name: name,
@ -470,7 +406,6 @@ class Package < ApplicationModel
Kernel.const_get(classname).up Kernel.const_get(classname).up
Package::Migration.create(name: package.underscore, version: version) Package::Migration.create(name: package.underscore, version: version)
end end
} }
end end
end end

View file

@ -7,25 +7,36 @@ class PackageTest < ActiveSupport::TestCase
# test 1 - normal install # test 1 - normal install
{ {
zpm: '<?xml version="1.0"?> zpm: '{
<zpm version="1.0"> "name": "UnitTestSample",
<name>UnitTestSample</name> "version": "1.0.1",
<version>1.0.1</version> "vendor": "Znuny GmbH",
<vendor>Znuny GmbH</vendor> "license": "ABC",
<url>http://znuny.org/</url> "url": "http://znuny.org/",
<license>ABC</license> "description": [
<description lang="en">some description</description> {
<filelist> "language": "en",
<file permission="644" location="test.txt">YWJjw6TDtsO8w58=</file> "text": "some description"
<file permission="644" location="some/dir/test.txt">YWJjw6TDtsO8w58=</file> }
<file permission="644" location="db/addon/unit_test_sample/20121212000001_create_base.rb">Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw ],
DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6 "files": [
bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog {
ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6 "permission": "644",
bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w "location": "test.txt",
X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file> "content": "YWJjw6TDtsO8w58="
</filelist> },
</zpm>', {
"permission": "644",
"location": "some/dir/test.txt",
"content": "YWJjw6TDtsO8w58="
},
{
"permission": "644",
"location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
"content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
}
]
}',
action: 'install', action: 'install',
result: true, result: true,
verify: { verify: {
@ -52,61 +63,88 @@ X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
# test 2 - try to install same package again / should not work # test 2 - try to install same package again / should not work
{ {
zpm: '<?xml version="1.0"?> zpm: '{
<zpm version="1.0"> "name": "UnitTestSample",
<name>UnitTestSample</name> "version": "1.0.1",
<version>1.0.1</version> "vendor": "Znuny GmbH",
<vendor>Znuny GmbH</vendor> "license": "ABC",
<url>http://znuny.org/</url> "url": "http://znuny.org/",
<license>ABC</license> "description": [
<description lang="en">some description</description> {
<filelist> "language": "en",
<file permission="644" location="test.txt">YWJjw6TDtsO8w58=</file> "text": "some description"
</filelist> }
</zpm>', ],
"files": [
{
"permission": "644",
"location": "test.txt",
"content": "YWJjw6TDtsO8w58="
}
]
}',
action: 'install', action: 'install',
result: false, result: false,
}, },
# test 3 - try to install lower version / should not work # test 3 - try to install lower version / should not work
{ {
zpm: '<?xml version="1.0"?> zpm: '{
<zpm version="1.0"> "name": "UnitTestSample",
<name>UnitTestSample</name> "version": "1.0.0",
<version>1.0.0</version> "vendor": "Znuny GmbH",
<vendor>Znuny GmbH</vendor> "license": "ABC",
<url>http://znuny.org/</url> "url": "http://znuny.org/",
<license>ABC</license> "description": [
<description lang="en">some description</description> {
<filelist> "language": "en",
<file permission="644" location="test.txt">YWJjw6TDtsO8w58=</file> "text": "some description"
</filelist> }
</zpm>', ],
"files": [
{
"permission": "644",
"location": "test.txt",
"content": "YWJjw6TDtsO8w58="
}
]
}',
action: 'install', action: 'install',
result: false, result: false,
}, },
# test 4 - upgrade 7 should work # test 4 - upgrade 7 should work
{ {
zpm: '<?xml version="1.0"?> zpm: '{
<zpm version="1.0"> "name": "UnitTestSample",
<name>UnitTestSample</name> "version": "1.0.2",
<version>1.0.2</version> "vendor": "Znuny GmbH",
<vendor>Znuny GmbH</vendor> "license": "ABC",
<url>http://znuny.org/</url> "url": "http://znuny.org/",
<license>ABC</license> "description": [
<description lang="en">some description</description> {
<filelist> "language": "en",
<file permission="644" location="test.txt2">YWJjw6TDtsO8w58=</file> "text": "some description"
<file permission="644" location="some/dir/test.txt2">YWJjw6TDtsO8w58=</file> }
<file permission="644" location="db/addon/unit_test_sample/20121212000001_create_base.rb">Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw ],
DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6 "files": [
bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog {
ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6 "permission": "644",
bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w "location": "test.txt2",
X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file> "content": "YWJjw6TDtsO8w58="
</filelist> },
</zpm>', {
"permission": "644",
"location": "some/dir/test.txt2",
"content": "YWJjw6TDtsO8w58="
},
{
"permission": "644",
"location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
"content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
}
]
}',
action: 'install', action: 'install',
result: true, result: true,
verify: { verify: {
@ -157,25 +195,36 @@ X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file>
# test 5 - check auto_install mechanism # test 5 - check auto_install mechanism
{ {
zpm: '<?xml version="1.0"?> zpm: '{
<zpm version="1.0"> "name": "UnitTestSample",
<name>UnitTestSample</name> "version": "1.0.2",
<version>1.0.2</version> "vendor": "Znuny GmbH",
<vendor>Znuny GmbH</vendor> "license": "ABC",
<url>http://znuny.org/</url> "url": "http://znuny.org/",
<license>ABC</license> "description": [
<description lang="en">some description</description> {
<filelist> "language": "en",
<file permission="644" location="test.txt2">YWJjw6TDtsO8w58=</file> "text": "some description"
<file permission="644" location="some/dir/test.txt2">YWJjw6TDtsO8w58=</file> }
<file permission="644" location="db/addon/unit_test_sample/20121212000001_create_base.rb">Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw ],
DQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6 "files": [
bmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog {
ICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6 "permission": "644",
bnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w "location": "test.txt2",
X3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k</file> "content": "YWJjw6TDtsO8w58="
</filelist> },
</zpm>', {
"permission": "644",
"location": "some/dir/test.txt2",
"content": "YWJjw6TDtsO8w58="
},
{
"permission": "644",
"location": "db/addon/unit_test_sample/20121212000001_create_base.rb",
"content": "Y2xhc3MgQ3JlYXRlQmFzZSA8IEFjdGl2ZVJlY29yZDo6TWlncmF0aW9uDQogIGRlZiBzZWxmLnVw\nDQogICBjcmVhdGVfdGFibGUgOnNhbXBsZV90YWJsZXMgZG8gfHR8DQogICAgICB0LmNvbHVtbiA6\nbmFtZSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiAxNTAsICA6bnVsbCA9PiB0cnVlDQog\nICAgICB0LmNvbHVtbiA6ZGF0YSwgICAgICAgICAgIDpzdHJpbmcsIDpsaW1pdCA9PiA1MDAwLCA6\nbnVsbCA9PiB0cnVlDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzZWxmLmRvd24NCiAgICBkcm9w\nX3RhYmxlIDpzYW1wbGVfdGFibGVzDQogIGVuZA0KZW5k"
}
]
}',
action: 'auto_install', action: 'auto_install',
result: true, result: true,
verify: { verify: {