class Serverkit::Resources::Base
Attributes
Public Class Methods
Source
# File lib/serverkit/resources/base.rb, line 19 def abstract_class? !!@abstract_class end
Source
# File lib/serverkit/resources/base.rb, line 24 def attribute(name, options = {}) default = options.delete(:default) define_method(name) do @attributes.fetch(name.to_s, default) end validates name, options unless options.empty? end
@note DSL method to define attribute with its validations
Source
# File lib/serverkit/resources/base.rb, line 56 def initialize(recipe, attributes) @attributes = attributes @recipe = recipe end
@param [Serverkit::Recipe] recipe @param [Hash] attributes
Public Instance Methods
Source
# File lib/serverkit/resources/base.rb, line 63 def all_errors attribute_validation_errors end
@note For override @return [Array<Serverkit::Errors::Base>]
Source
# File lib/serverkit/resources/base.rb, line 68 def check_command(*args) run_command(*args).success? end
@return [true, false]
Source
# File lib/serverkit/resources/base.rb, line 73 def check_command_from_identifier(*args) run_command_from_identifier(*args).success? end
@return [true, false]
Source
# File lib/serverkit/resources/base.rb, line 78 def get_command_from_identifier(*args) backend.command.get(*args) end
@return [String]
Source
# File lib/serverkit/resources/base.rb, line 83 def handlers @handlers ||= Array(notify).map do |id| recipe.handlers.find do |handler| handler.id == id end end.compact end
@return [Array<Serverkit::Resource>]
Source
# File lib/serverkit/resources/base.rb, line 93 def id @attributes["id"] || default_id end
@note For logging and notifying @return [String]
Source
# File lib/serverkit/resources/base.rb, line 98 def notifiable? @recheck_result == true && !handlers.nil? end
@return [true, false] True if this resource should call any handler
Source
# File lib/serverkit/resources/base.rb, line 103 def run_apply unless run_check apply @recheck_result = !!recheck_with_script end end
@note check and apply wrapper
Source
# File lib/serverkit/resources/base.rb, line 112 def run_check @check_result = !!check_with_script end
@note check wrapper @return [true, false]
Source
# File lib/serverkit/resources/base.rb, line 116 def successful? successful_on_check? || successful_on_recheck? end
Source
# File lib/serverkit/resources/base.rb, line 120 def successful_on_check? @check_result == true end
Source
# File lib/serverkit/resources/base.rb, line 124 def successful_on_recheck? @recheck_result == true end
Source
# File lib/serverkit/resources/base.rb, line 130 def to_a [self] end
@note recipe resource will override to replace itself with multiple resources @return [Array<Serverkit::Resources::Base>]
Private Instance Methods
Source
# File lib/serverkit/resources/base.rb, line 137 def attribute_validation_errors valid? errors.map do |attribute_name, message| Serverkit::Errors::AttributeValidationError.new(self, attribute_name, message) end end
@return [Array<Serverkit::Errors::AttributeValidationError>]
Source
# File lib/serverkit/resources/base.rb, line 145 def check_with_script if check_script check_command(check_script) else check end end
@note Prior check_script attribute to resource’s check implementation
Source
# File lib/serverkit/resources/base.rb, line 156 def create_remote_file(content: nil, path: nil) ::Tempfile.open("") do |file| file.write(content) file.close backend.send_file(file.path, path) end end
Create a file on remote side @param [String] content @param [String] path
Source
# File lib/serverkit/resources/base.rb, line 167 def create_remote_temp_file(content) make_remote_temp_path.tap do |path| update_remote_file_content(content: content, path: path) end end
Create temp file on remote side with given content @param [String] content @return [String] Path to remote temp file
Source
# File lib/serverkit/resources/base.rb, line 175 def default_id required_values.join(" ") end
@note For override @return [String]
Source
# File lib/serverkit/resources/base.rb, line 181 def make_remote_temp_path run_command("mktemp -u 2>/dev/null || mktemp -u -t tmp").stdout.rstrip end
@note GNU mktemp doesn’t require -t option, but BSD mktemp does @return [String]
Source
# File lib/serverkit/resources/base.rb, line 188 def move_remote_file_keeping_destination_metadata(source, destination) group = run_command_from_identifier(:get_file_owner_group, destination).stdout.rstrip mode = run_command_from_identifier(:get_file_mode, destination).stdout.rstrip owner = run_command_from_identifier(:get_file_owner_user, destination).stdout.rstrip run_command_from_identifier(:change_file_group, source, group) unless group.empty? run_command_from_identifier(:change_file_mode, source, mode) unless mode.empty? run_command_from_identifier(:change_file_owner, source, owner) unless owner.empty? run_command_from_identifier(:move_file, source, destination) end
@note “metadata” means a set of group, mode, and owner @param [String] destination @param [String] source
Source
# File lib/serverkit/resources/base.rb, line 200 def recheck check end
@note For override @return [true, false]
Source
# File lib/serverkit/resources/base.rb, line 205 def recheck_with_script if recheck_script check_command(recheck_script) else recheck end end
@note Prior recheck_script attribute to resource’s recheck
implementation
Source
# File lib/serverkit/resources/base.rb, line 214 def required_attribute_names _validators.select do |key, validators| validators.grep(::RequiredValidator).any? end.keys end
@return [Array<Symbol>]
Source
# File lib/serverkit/resources/base.rb, line 221 def required_values required_attribute_names.map do |required_attribute_name| send(required_attribute_name) end end
@return [Array]
Source
# File lib/serverkit/resources/base.rb, line 230 def run_command(command) if cwd command = "cd #{Shellwords.escape(cwd)} && #{command}" elsif user command = "cd && #{command}" end unless user.nil? command = "sudo -H -u #{user} -i -- /bin/bash -i -c #{Shellwords.escape(command)}" end backend.run_command(command) end
@note Wraps backend.run_command for ‘cwd` and `user` attributes @param [String] command one-line shell script to be executed on remote machine @return [Specinfra::CommandResult]
Source
# File lib/serverkit/resources/base.rb, line 243 def run_command_from_identifier(*args) run_command(get_command_from_identifier(*args)) end
@return [Specinfra::CommandResult]
Source
# File lib/serverkit/resources/base.rb, line 250 def update_remote_file_content(content: nil, path: nil) group = run_command_from_identifier(:get_file_owner_group, path).stdout.rstrip mode = run_command_from_identifier(:get_file_mode, path).stdout.rstrip owner = run_command_from_identifier(:get_file_owner_user, path).stdout.rstrip create_remote_file(content: content, path: path) run_command_from_identifier(:change_file_group, path, group) unless group.empty? run_command_from_identifier(:change_file_mode, path, mode) unless mode.empty? run_command_from_identifier(:change_file_owner, path, owner) unless owner.empty? end
Update remote file content on given path with given content @param [String] content @param [String] path