-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathlocal.rb
More file actions
86 lines (74 loc) · 3.63 KB
/
local.rb
File metadata and controls
86 lines (74 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require "pluginmanager/ui"
require "pluginmanager/bundler/logstash_injector"
require "pluginmanager/gem_installer"
require "pluginmanager/errors"
require "pluginmanager/pack_installer/pack"
require "bootstrap/util/compress"
module LogStash module PluginManager module PackInstaller
class Local
PACK_EXTENSION = ".zip"
LOGSTASH_PATTERN_RE = /logstash\/?/
attr_reader :local_file
def initialize(local_file)
@local_file = local_file
end
def execute
raise PluginManager::FileNotFoundError, "Can't file local file #{local_file}" unless ::File.exist?(local_file)
raise PluginManager::InvalidPackError, "Invalid format, the pack must be in zip format" unless valid_format?(local_file)
PluginManager.ui.info("Installing file: #{local_file}")
uncompressed_path = uncompress(local_file)
PluginManager.ui.debug("Pack uncompressed to #{uncompressed_path}")
pack = LogStash::PluginManager::PackInstaller::Pack.new(uncompressed_path)
raise PluginManager::InvalidPackError, "The pack must contains at least one plugin" unless pack.valid?
# Install the gems to make them available locally when bundler does his local resolution
post_install_messages = []
pack.gems.each do |packed_gem|
PluginManager.ui.debug("Installing, #{packed_gem.name}, version: #{packed_gem.version} file: #{packed_gem.file}")
post_install_messages << LogStash::PluginManager::GemInstaller::install(packed_gem.file, packed_gem.plugin?)
end
# Try to add the gems to the current gemfile and lock file, if successful
# both of them will be updated. This injector is similar to Bundler's own injector class
# minus the support for additionals source and doing local resolution only.
::Bundler::LogstashInjector.inject!(pack)
post_install_messages.compact.each do |message|
PluginManager.ui.info(message)
end
PluginManager.ui.info("Install successful")
rescue ::Bundler::BundlerError => e
raise PluginManager::InstallError.new(e), "An error occurred went installing plugins"
ensure
FileUtils.rm_rf(uncompressed_path) if uncompressed_path && Dir.exist?(uncompressed_path)
end
private
def uncompress(source)
temporary_directory = Stud::Temporary.pathname
LogStash::Util::Zip.extract(source, temporary_directory, LOGSTASH_PATTERN_RE)
temporary_directory
rescue Zip::Error => e
# OK Zip's handling of file is bit weird, if the file exist but is not a valid zip, it will raise
# a `Zip::Error` exception with a file not found message...
raise InvalidPackError, "Cannot uncompress the zip: #{source}"
rescue LogStash::CompressError => e
raise InvalidPackError, "Cannot uncompress the archive: #{e.message}"
end
def valid_format?(local_file)
::File.extname(local_file).downcase == PACK_EXTENSION
end
end
end end end