trabajo-afectivo/app/models/package.rb

489 lines
11 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Package < ApplicationModel
@@root = Rails.root.to_s # rubocop:disable Style/ClassVars
=begin
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 do |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'
end
2017-11-21 14:25:04 +00:00
return nil if issues.blank?
issues
end
=begin
install all packages located under auto_install/*.zpm
Package.auto_install
=end
def self.auto_install
2016-01-21 22:25:05 +00:00
path = "#{@@root}/auto_install/"
return if !File.exist?(path)
data = []
2016-01-21 22:25:05 +00:00
Dir.foreach(path) do |entry|
2014-03-10 15:45:52 +00:00
if entry =~ /\.zpm/ && entry !~ /^\./
data.push entry
end
end
data.each do |file|
2016-01-21 22:25:05 +00:00
install(file: "#{path}/#{file}")
end
data
end
=begin
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
# link files
2016-01-21 22:25:05 +00:00
Dir.glob("#{@@root}/**/*") do |entry|
if File.symlink?(entry)
2014-10-26 12:17:00 +00:00
logger.info "unlink: #{entry}"
2016-01-21 22:25:05 +00:00
File.delete(entry)
end
2013-03-19 15:39:31 +00:00
backup_file = entry + '.link_backup'
2016-01-21 22:25:05 +00:00
if File.exist?(backup_file)
2014-10-26 12:17:00 +00:00
logger.info "Restore backup file of #{backup_file} -> #{entry}."
2016-01-21 22:25:05 +00:00
File.rename(backup_file, entry)
2013-03-19 15:39:31 +00:00
end
end
end
# check if zpm is a package source repo
def self._package_base_dir?(package_base_dir)
package = false
2016-01-21 22:25:05 +00:00
Dir.glob(package_base_dir + '/*.szpm') do |entry|
package = entry.sub(%r{^.*/(.+?)\.szpm$}, '\1')
end
if package == false
2016-03-01 14:26:46 +00:00
raise "Can't link package, '#{package_base_dir}' is no package source directory!"
end
logger.debug { package.inspect }
package
end
=begin
execute migration down + unlink files
Package.unlink('/path/to/src/extension')
=end
def self.unlink(package_base_dir)
# check if zpm is a package source repo
package = _package_base_dir?(package_base_dir)
# migration down
2016-01-21 22:25:05 +00:00
Package::Migration.migrate(package, 'reverse')
# link files
2016-01-21 22:25:05 +00:00
Dir.glob(package_base_dir + '/**/*') do |entry|
entry = entry.sub('//', '/')
file = entry
file = file.sub(/#{package_base_dir}/, '')
dest = @@root + '/' + file
2016-01-21 22:25:05 +00:00
if File.symlink?(dest.to_s)
logger.info "Unlink file: #{dest}"
2016-01-21 22:25:05 +00:00
File.delete(dest.to_s)
end
2013-03-19 15:39:31 +00:00
backup_file = dest.to_s + '.link_backup'
2016-01-21 22:25:05 +00:00
if File.exist?(backup_file)
logger.info "Restore backup file of #{backup_file} -> #{dest}."
2016-01-21 22:25:05 +00:00
File.rename(backup_file, dest.to_s)
2013-03-19 15:39:31 +00:00
end
end
end
=begin
link files + execute migration up
Package.link('/path/to/src/extension')
=end
def self.link(package_base_dir)
# check if zpm is a package source repo
package = _package_base_dir?(package_base_dir)
# link files
2016-01-21 22:25:05 +00:00
Dir.glob(package_base_dir + '/**/*') do |entry|
entry = entry.sub('//', '/')
file = entry
file = file.sub(/#{package_base_dir}/, '')
2016-01-21 22:25:05 +00:00
file = file.sub(%r{^/}, '')
2013-02-02 18:52:31 +00:00
# ignore files
2017-11-23 08:09:44 +00:00
if file.match?(/^README/)
2014-10-26 12:17:00 +00:00
logger.info "NOTICE: Ignore #{file}"
2013-02-02 18:52:31 +00:00
next
end
# get new file destination
dest = @@root + '/' + file
2016-01-21 22:25:05 +00:00
if File.directory?(entry.to_s)
if !File.exist?(dest.to_s)
logger.info "Create dir: #{dest}"
2016-01-21 22:25:05 +00:00
FileUtils.mkdir_p(dest.to_s)
end
end
2016-01-21 22:25:05 +00:00
if File.file?(entry.to_s) && (File.file?(dest.to_s) && !File.symlink?(dest.to_s))
2013-03-19 15:39:31 +00:00
backup_file = dest.to_s + '.link_backup'
2016-01-21 22:25:05 +00:00
if File.exist?(backup_file)
2016-03-01 14:26:46 +00:00
raise "Can't link #{entry} -> #{dest}, destination and .link_backup already exists!"
2013-03-19 15:39:31 +00:00
end
2017-11-23 08:09:44 +00:00
logger.info "Create backup file of #{dest} -> #{backup_file}."
File.rename(dest.to_s, backup_file)
end
2016-01-21 22:25:05 +00:00
if File.file?(entry)
if File.symlink?(dest.to_s)
File.delete(dest.to_s)
end
logger.info "Link file: #{entry} -> #{dest}"
2016-01-21 22:25:05 +00:00
File.symlink(entry.to_s, dest.to_s)
end
end
# migration up
2016-01-21 22:25:05 +00:00
Package::Migration.migrate(package)
end
=begin
install zpm package
package = Package.install(file: '/path/to/package.zpm')
or
package = Package.install(string: zpm_as_string)
returns
package # record of newly created package
=end
2012-12-27 14:41:28 +00:00
def self.install(data)
if data[:file]
json = _read_file(data[:file], true)
package = JSON.parse(json)
2012-12-27 14:41:28 +00:00
elsif data[:string]
package = JSON.parse(data[:string])
2012-12-27 14:41:28 +00:00
end
# package meta data
2012-12-27 14:41:28 +00:00
meta = {
name: package['name'],
version: package['version'],
vendor: package['vendor'],
state: 'uninstalled',
created_by_id: 1,
updated_by_id: 1,
}
# verify if package can get installed
2016-01-21 22:25:05 +00:00
package_db = Package.find_by(name: meta[:name])
if package_db
2013-03-19 12:12:22 +00:00
if !data[:reinstall]
2016-01-21 22:25:05 +00:00
if Gem::Version.new(package_db.version) == Gem::Version.new(meta[:version])
2016-03-01 14:26:46 +00:00
raise "Package '#{meta[:name]}-#{meta[:version]}' already installed!"
2013-03-19 12:12:22 +00:00
end
2016-01-21 22:25:05 +00:00
if Gem::Version.new(package_db.version) > Gem::Version.new(meta[:version])
2016-03-01 14:26:46 +00:00
raise "Newer version (#{package_db.version}) of package '#{meta[:name]}-#{meta[:version]}' already installed!"
2013-03-19 12:12:22 +00:00
end
end
2013-03-22 12:48:47 +00:00
# uninstall files of old package
uninstall(
name: package_db.name,
version: package_db.version,
migration_not_down: true,
reinstall: data[:reinstall],
2015-04-27 15:21:17 +00:00
)
end
# store package
2013-03-19 12:12:22 +00:00
if !data[:reinstall]
2016-02-01 00:42:30 +00:00
package_db = Package.create(meta)
2013-03-19 12:12:22 +00:00
Store.add(
object: 'Package',
o_id: package_db.id,
data: package.to_json,
filename: "#{meta[:name]}-#{meta[:version]}.zpm",
preferences: {},
created_by_id: UserInfo.current_user_id || 1,
2013-03-19 12:12:22 +00:00
)
end
# write files
package['files'].each do |file|
permission = file['permission'] || '644'
content = Base64.decode64(file['content'])
_write_file(file['location'], permission, content)
end
# update package state
2016-02-01 00:42:30 +00:00
package_db.state = 'installed'
package_db.save
# up migrations
2016-01-21 22:25:05 +00:00
Package::Migration.migrate(meta[:name])
# prebuild assets
2016-02-01 00:42:30 +00:00
package_db
end
=begin
reinstall package
package = Package.reinstall(package_name)
returns
package # record of newly created package
=end
2013-03-19 12:12:22 +00:00
def self.reinstall(package_name)
2016-01-21 22:25:05 +00:00
package = Package.find_by(name: package_name)
2013-05-10 21:48:29 +00:00
if !package
2016-03-01 14:26:46 +00:00
raise "No such package '#{package_name}'"
2013-05-10 21:48:29 +00:00
end
2016-01-21 22:25:05 +00:00
file = _get_bin(package.name, package.version)
install(string: file, reinstall: true)
package
2013-03-19 12:12:22 +00:00
end
=begin
uninstall package
package = Package.uninstall(name: 'package', version: '0.1.1')
or
package = Package.uninstall(string: zpm_as_string)
returns
package # record of newly created package
=end
2016-01-21 22:25:05 +00:00
def self.uninstall(data)
2012-12-27 14:41:28 +00:00
if data[:string]
package = JSON.parse(data[:string])
2012-12-27 14:41:28 +00:00
else
json_file = _get_bin(data[:name], data[:version])
package = JSON.parse(json_file)
2012-12-27 14:41:28 +00:00
end
# down migrations
2012-12-27 14:41:28 +00:00
if !data[:migration_not_down]
Package::Migration.migrate(package['name'], 'reverse')
end
package['files'].each do |file|
permission = file['permission'] || '644'
content = Base64.decode64(file['content'])
_delete_file(file['location'], permission, content)
end
# delete package
2016-02-01 00:42:30 +00:00
if !data[:reinstall]
record = Package.find_by(
name: package['name'],
2016-02-01 00:42:30 +00:00
version: package['version'],
)
record.destroy
end
record
end
=begin
execute all pending package migrations at once
Package.migration_execute
=end
def self.migration_execute
Package.all.each do |package|
json_file = Package._get_bin(package.name, package.version)
package = JSON.parse(json_file)
Package::Migration.migrate(package['name'])
end
end
2016-01-21 22:25:05 +00:00
def self._get_bin(name, version)
package = Package.find_by(
name: name,
version: version,
)
if !package
2016-03-01 14:26:46 +00:00
raise "No such package '#{name}' version '#{version}'"
end
list = Store.list(
object: 'Package',
o_id: package.id,
)
# find file
2013-05-10 21:48:29 +00:00
if !list || !list.first
2016-03-01 14:26:46 +00:00
raise "No such file in storage list #{name} #{version}"
2013-05-10 21:48:29 +00:00
end
2014-04-28 07:44:36 +00:00
if !list.first.content
2016-03-01 14:26:46 +00:00
raise "No such file in storage #{name} #{version}"
2013-05-10 21:48:29 +00:00
end
2014-04-28 07:44:36 +00:00
list.first.content
end
def self._read_file(file, fullpath = false)
location = if fullpath == false
@@root + '/' + file
elsif fullpath == true
file
else
fullpath + '/' + file
end
begin
2016-01-21 22:25:05 +00:00
data = File.open(location, 'rb')
contents = data.read
rescue => e
raise e
end
2014-04-28 07:44:36 +00:00
contents
end
def self._write_file(file, permission, data)
2016-01-21 22:25:05 +00:00
location = "#{@@root}/#{file}"
# rename existing file if not already the same file
2016-01-21 22:25:05 +00:00
if File.exist?(location)
content_fs = _read_file(file)
if content_fs == data
logger.debug { "NOTICE: file '#{location}' already exists, skip install" }
return true
end
backup_location = location + '.save'
2014-10-26 12:17:00 +00:00
logger.info "NOTICE: backup old file '#{location}' to #{backup_location}"
2016-01-21 22:25:05 +00:00
File.rename(location, backup_location)
end
# check if directories need to be created
directories = location.split '/'
(0..(directories.length - 2) ).each do |position|
tmp_path = ''
(1..position).each do |count|
2016-01-21 22:25:05 +00:00
tmp_path = "#{tmp_path}/#{directories[count]}"
end
next if tmp_path == ''
next if File.exist?(tmp_path)
2016-06-30 20:04:48 +00:00
Dir.mkdir(tmp_path, 0o755)
end
# install file
begin
2014-10-26 12:17:00 +00:00
logger.info "NOTICE: install '#{location}' (#{permission})"
2016-01-21 22:25:05 +00:00
file = File.new(location, 'wb')
file.write(data)
file.close
File.chmod(permission.to_s.to_i(8), location)
rescue => e
raise e
end
2014-04-28 07:44:36 +00:00
true
end
def self._delete_file(file, _permission, _data)
2016-01-21 22:25:05 +00:00
location = "#{@@root}/#{file}"
# install file
2014-10-26 12:17:00 +00:00
logger.info "NOTICE: uninstall '#{location}'"
2016-01-21 22:25:05 +00:00
if File.exist?(location)
File.delete(location)
end
# rename existing file
backup_location = location + '.save'
2016-01-21 22:25:05 +00:00
if File.exist?(backup_location)
2014-10-26 12:17:00 +00:00
logger.info "NOTICE: restore old file '#{backup_location}' to #{location}"
2016-01-21 22:25:05 +00:00
File.rename(backup_location, location)
end
true
end
end