2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-12-26 22:02:16 +00:00
|
|
|
class Package < ApplicationModel
|
2015-05-08 13:47:27 +00:00
|
|
|
@@root = Rails.root.to_s # rubocop:disable Style/ClassVars
|
2012-12-26 22:02:16 +00:00
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=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 = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
package['files'].each do |file|
|
2016-01-31 23:20:14 +00:00
|
|
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
logger.error "File #{file['location']} is different"
|
|
|
|
issues[file['location']] = 'changed'
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-11-21 14:25:04 +00:00
|
|
|
return nil if issues.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
issues
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
install all packages located under auto_install/*.zpm
|
|
|
|
|
|
|
|
Package.auto_install
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-07 08:32:48 +00:00
|
|
|
def self.auto_install
|
2016-01-21 22:25:05 +00:00
|
|
|
path = "#{@@root}/auto_install/"
|
|
|
|
return if !File.exist?(path)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2013-01-07 08:32:48 +00:00
|
|
|
data = []
|
2016-01-21 22:25:05 +00:00
|
|
|
Dir.foreach(path) do |entry|
|
2021-05-12 11:37:44 +00:00
|
|
|
if entry.include?('.zpm') && entry !~ %r{^\.}
|
2013-01-07 08:32:48 +00:00
|
|
|
data.push entry
|
|
|
|
end
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
data.each do |file|
|
2016-01-21 22:25:05 +00:00
|
|
|
install(file: "#{path}/#{file}")
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-01-10 10:13:20 +00:00
|
|
|
data
|
2013-01-07 08:32:48 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
remove all linked files in application
|
|
|
|
|
|
|
|
note: will not take down package migrations, use Package.unlink instead
|
|
|
|
|
|
|
|
Package.unlink_all
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-16 20:09:27 +00:00
|
|
|
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)
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
2020-09-30 09:07:01 +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
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# check if zpm is a package source repo
|
2013-01-16 20:09:27 +00:00
|
|
|
def self._package_base_dir?(package_base_dir)
|
|
|
|
package = false
|
2020-09-30 09:07:01 +00:00
|
|
|
Dir.glob("#{package_base_dir}/*.szpm") do |entry|
|
2016-01-21 22:25:05 +00:00
|
|
|
package = entry.sub(%r{^.*/(.+?)\.szpm$}, '\1')
|
2013-01-16 20:09:27 +00:00
|
|
|
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!"
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { package.inspect }
|
2015-01-10 10:13:20 +00:00
|
|
|
package
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
execute migration down + unlink files
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
Package.unlink('/path/to/src/extension')
|
2016-01-31 23:20:14 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-16 20:09:27 +00:00
|
|
|
def self.unlink(package_base_dir)
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# check if zpm is a package source repo
|
2016-01-15 17:22:57 +00:00
|
|
|
package = _package_base_dir?(package_base_dir)
|
2013-01-16 20:09:27 +00:00
|
|
|
|
|
|
|
# migration down
|
2016-01-21 22:25:05 +00:00
|
|
|
Package::Migration.migrate(package, 'reverse')
|
2013-01-16 20:09:27 +00:00
|
|
|
|
|
|
|
# link files
|
2020-09-30 09:07:01 +00:00
|
|
|
Dir.glob("#{package_base_dir}/**/*") do |entry|
|
2016-01-21 22:25:05 +00:00
|
|
|
entry = entry.sub('//', '/')
|
2013-01-16 20:09:27 +00:00
|
|
|
file = entry
|
2021-05-12 11:37:44 +00:00
|
|
|
file = file.sub(%r{#{package_base_dir}}, '')
|
2020-09-30 09:07:01 +00:00
|
|
|
dest = "#{@@root}/#{file}"
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2016-01-21 22:25:05 +00:00
|
|
|
if File.symlink?(dest.to_s)
|
2015-05-06 09:30:39 +00:00
|
|
|
logger.info "Unlink file: #{dest}"
|
2016-01-21 22:25:05 +00:00
|
|
|
File.delete(dest.to_s)
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
2013-03-19 15:39:31 +00:00
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
backup_file = "#{dest}.link_backup"
|
2016-01-21 22:25:05 +00:00
|
|
|
if File.exist?(backup_file)
|
2015-05-06 09:30:39 +00:00
|
|
|
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
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=begin
|
|
|
|
|
2021-05-31 13:51:29 +00:00
|
|
|
link files
|
2016-01-31 23:20:14 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
Package.link('/path/to/src/extension')
|
2016-01-31 23:20:14 +00:00
|
|
|
|
2021-05-31 13:51:29 +00:00
|
|
|
Migrations will not be executed because the the codebase was modified
|
|
|
|
in the current process and is therefore inconsistent. This must be done
|
|
|
|
subsequently in a separate step.
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=end
|
|
|
|
|
2013-01-16 20:09:27 +00:00
|
|
|
def self.link(package_base_dir)
|
|
|
|
|
|
|
|
# link files
|
2020-09-30 09:07:01 +00:00
|
|
|
Dir.glob("#{package_base_dir}/**/*") do |entry|
|
2016-01-21 22:25:05 +00:00
|
|
|
entry = entry.sub('//', '/')
|
2013-01-16 20:09:27 +00:00
|
|
|
file = entry
|
2021-05-12 11:37:44 +00:00
|
|
|
file = file.sub(%r{#{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
|
2020-05-25 07:05:17 +00:00
|
|
|
if file.start_with?('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
|
2020-09-30 09:07:01 +00:00
|
|
|
dest = "#{@@root}/#{file}"
|
2013-01-16 20:09:27 +00:00
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
if File.directory?(entry.to_s) && !File.exist?(dest.to_s)
|
|
|
|
logger.info "Create dir: #{dest}"
|
|
|
|
FileUtils.mkdir_p(dest.to_s)
|
2013-01-16 20:09:27 +00:00
|
|
|
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))
|
2020-09-30 09:07:01 +00:00
|
|
|
backup_file = "#{dest}.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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
logger.info "Create backup file of #{dest} -> #{backup_file}."
|
|
|
|
File.rename(dest.to_s, backup_file)
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
|
|
|
|
2016-01-21 22:25:05 +00:00
|
|
|
if File.file?(entry)
|
|
|
|
if File.symlink?(dest.to_s)
|
|
|
|
File.delete(dest.to_s)
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
2015-05-06 09:30:39 +00:00
|
|
|
logger.info "Link file: #{entry} -> #{dest}"
|
2016-01-21 22:25:05 +00:00
|
|
|
File.symlink(entry.to_s, dest.to_s)
|
2013-01-16 20:09:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
install zpm package
|
|
|
|
|
|
|
|
package = Package.install(file: '/path/to/package.zpm')
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
package = Package.install(string: zpm_as_string)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
package # record of newly created package
|
2016-01-31 23:20:14 +00:00
|
|
|
|
2021-05-31 13:51:29 +00:00
|
|
|
Migrations will not be executed because the the codebase was modified
|
|
|
|
in the current process and is therefore inconsistent. This must be done
|
|
|
|
subsequently in a separate step.
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=end
|
|
|
|
|
2012-12-27 14:41:28 +00:00
|
|
|
def self.install(data)
|
|
|
|
if data[:file]
|
2016-01-31 17:25:31 +00:00
|
|
|
json = _read_file(data[:file], true)
|
|
|
|
package = JSON.parse(json)
|
2012-12-27 14:41:28 +00:00
|
|
|
elsif data[:string]
|
2016-01-31 17:25:31 +00:00
|
|
|
package = JSON.parse(data[:string])
|
2012-12-27 14:41:28 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# package meta data
|
2012-12-27 14:41:28 +00:00
|
|
|
meta = {
|
2018-12-19 17:31:51 +00:00
|
|
|
name: package['name'],
|
|
|
|
version: package['version'],
|
|
|
|
vendor: package['vendor'],
|
|
|
|
state: 'uninstalled',
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
2012-12-26 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# verify if package can get installed
|
2016-01-21 22:25:05 +00:00
|
|
|
package_db = Package.find_by(name: meta[:name])
|
2012-12-26 22:02:16 +00:00
|
|
|
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
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
2013-03-22 12:48:47 +00:00
|
|
|
# uninstall files of old package
|
2015-05-07 12:10:38 +00:00
|
|
|
uninstall(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: package_db.name,
|
|
|
|
version: package_db.version,
|
2015-04-27 13:42:53 +00:00
|
|
|
migration_not_down: true,
|
2018-12-19 17:31:51 +00:00
|
|
|
reinstall: data[:reinstall],
|
2015-04-27 15:21:17 +00:00
|
|
|
)
|
2012-12-26 22:02:16 +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(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'Package',
|
|
|
|
o_id: package_db.id,
|
|
|
|
data: package.to_json,
|
|
|
|
filename: "#{meta[:name]}-#{meta[:version]}.zpm",
|
|
|
|
preferences: {},
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: UserInfo.current_user_id || 1,
|
2013-03-19 12:12:22 +00:00
|
|
|
)
|
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# write files
|
2017-10-01 12:25:52 +00:00
|
|
|
package['files'].each do |file|
|
2016-01-31 17:25:31 +00:00
|
|
|
permission = file['permission'] || '644'
|
|
|
|
content = Base64.decode64(file['content'])
|
|
|
|
_write_file(file['location'], permission, content)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# update package state
|
2016-02-01 00:42:30 +00:00
|
|
|
package_db.state = 'installed'
|
|
|
|
package_db.save
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# prebuild assets
|
|
|
|
|
2016-02-01 00:42:30 +00:00
|
|
|
package_db
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
reinstall package
|
|
|
|
|
|
|
|
package = Package.reinstall(package_name)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
package # record of newly created package
|
2016-01-31 23:20:14 +00:00
|
|
|
|
|
|
|
=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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-21 22:25:05 +00:00
|
|
|
file = _get_bin(package.name, package.version)
|
|
|
|
install(string: file, reinstall: true)
|
2016-01-31 23:20:14 +00:00
|
|
|
package
|
2013-03-19 12:12:22 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
uninstall package
|
|
|
|
|
|
|
|
package = Package.uninstall(name: 'package', version: '0.1.1')
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
package = Package.uninstall(string: zpm_as_string)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
package # record of newly created package
|
2016-01-31 23:20:14 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-21 22:25:05 +00:00
|
|
|
def self.uninstall(data)
|
2012-12-26 22:02:16 +00:00
|
|
|
|
2012-12-27 14:41:28 +00:00
|
|
|
if data[:string]
|
2016-01-31 17:25:31 +00:00
|
|
|
package = JSON.parse(data[:string])
|
2012-12-27 14:41:28 +00:00
|
|
|
else
|
2016-01-31 17:25:31 +00:00
|
|
|
json_file = _get_bin(data[:name], data[:version])
|
|
|
|
package = JSON.parse(json_file)
|
2012-12-27 14:41:28 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# down migrations
|
2012-12-27 14:41:28 +00:00
|
|
|
if !data[:migration_not_down]
|
2016-01-31 17:25:31 +00:00
|
|
|
Package::Migration.migrate(package['name'], 'reverse')
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
package['files'].each do |file|
|
2016-01-31 17:25:31 +00:00
|
|
|
permission = file['permission'] || '644'
|
|
|
|
content = Base64.decode64(file['content'])
|
|
|
|
_delete_file(file['location'], permission, content)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# delete package
|
2016-02-01 00:42:30 +00:00
|
|
|
if !data[:reinstall]
|
|
|
|
record = Package.find_by(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: package['name'],
|
2016-02-01 00:42:30 +00:00
|
|
|
version: package['version'],
|
|
|
|
)
|
|
|
|
record.destroy
|
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
2016-01-31 23:20:14 +00:00
|
|
|
record
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
2016-06-01 08:32:13 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
execute all pending package migrations at once
|
|
|
|
|
|
|
|
Package.migration_execute
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.migration_execute
|
2017-10-01 12:25:52 +00:00
|
|
|
Package.all.each do |package|
|
2016-06-01 08:32:13 +00:00
|
|
|
json_file = Package._get_bin(package.name, package.version)
|
|
|
|
package = JSON.parse(json_file)
|
|
|
|
Package::Migration.migrate(package['name'])
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-01 08:32:13 +00:00
|
|
|
end
|
|
|
|
|
2016-01-21 22:25:05 +00:00
|
|
|
def self._get_bin(name, version)
|
2015-05-07 10:15:40 +00:00
|
|
|
package = Package.find_by(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: name,
|
2015-04-27 13:42:53 +00:00
|
|
|
version: version,
|
2015-05-07 10:15:40 +00:00
|
|
|
)
|
2012-12-26 22:02:16 +00:00
|
|
|
if !package
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "No such package '#{name}' version '#{version}'"
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2012-12-26 22:02:16 +00:00
|
|
|
list = Store.list(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'Package',
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: package.id,
|
2012-12-26 22:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2014-04-28 07:44:36 +00:00
|
|
|
list.first.content
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self._read_file(file, fullpath = false)
|
2020-07-13 12:46:08 +00:00
|
|
|
location = case fullpath
|
|
|
|
when false
|
2020-09-30 09:07:01 +00:00
|
|
|
"#{@@root}/#{file}"
|
2020-07-13 12:46:08 +00:00
|
|
|
when true
|
2016-01-15 17:22:57 +00:00
|
|
|
file
|
|
|
|
else
|
2020-09-30 09:07:01 +00:00
|
|
|
"#{fullpath}/#{file}"
|
2016-01-15 17:22:57 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
begin
|
2016-01-21 22:25:05 +00:00
|
|
|
data = File.open(location, 'rb')
|
2012-12-26 22:02:16 +00:00
|
|
|
contents = data.read
|
|
|
|
rescue => e
|
2020-03-12 08:23:19 +00:00
|
|
|
raise e
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
2014-04-28 07:44:36 +00:00
|
|
|
contents
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self._write_file(file, permission, data)
|
2016-01-21 22:25:05 +00:00
|
|
|
location = "#{@@root}/#{file}"
|
2012-12-26 22:02:16 +00:00
|
|
|
|
2016-01-31 23:48:43 +00:00
|
|
|
# rename existing file if not already the same file
|
2016-01-21 22:25:05 +00:00
|
|
|
if File.exist?(location)
|
2016-01-31 23:48:43 +00:00
|
|
|
content_fs = _read_file(file)
|
|
|
|
if content_fs == data
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "NOTICE: file '#{location}' already exists, skip install" }
|
2016-01-31 23:48:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2020-09-30 09:07:01 +00:00
|
|
|
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)
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check if directories need to be created
|
|
|
|
directories = location.split '/'
|
2017-10-01 12:25:52 +00:00
|
|
|
(0..(directories.length - 2) ).each do |position|
|
2012-12-26 22:02:16 +00:00
|
|
|
tmp_path = ''
|
2017-10-01 12:25:52 +00:00
|
|
|
(1..position).each do |count|
|
2016-01-21 22:25:05 +00:00
|
|
|
tmp_path = "#{tmp_path}/#{directories[count]}"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if tmp_path == ''
|
|
|
|
next if File.exist?(tmp_path)
|
|
|
|
|
2016-06-30 20:04:48 +00:00
|
|
|
Dir.mkdir(tmp_path, 0o755)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# 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)
|
2012-12-26 22:02:16 +00:00
|
|
|
file.close
|
2016-01-31 23:54:54 +00:00
|
|
|
File.chmod(permission.to_s.to_i(8), location)
|
2012-12-26 22:02:16 +00:00
|
|
|
rescue => e
|
2020-03-12 08:23:19 +00:00
|
|
|
raise e
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
2014-04-28 07:44:36 +00:00
|
|
|
true
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 08:01:35 +00:00
|
|
|
def self._delete_file(file, _permission, _data)
|
2016-01-21 22:25:05 +00:00
|
|
|
location = "#{@@root}/#{file}"
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# 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)
|
2013-03-05 16:16:33 +00:00
|
|
|
end
|
2012-12-26 22:02:16 +00:00
|
|
|
|
|
|
|
# rename existing file
|
2020-09-30 09:07:01 +00:00
|
|
|
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)
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
|
|
|
|
2015-01-10 10:13:20 +00:00
|
|
|
true
|
2012-12-26 22:02:16 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|