class Serverkit::Resources::Base

Attributes

abstract_class[W]
attributes[R]
backend[RW]
check_result[R]
recheck_result[R]
recipe[R]

Public Class Methods

abstract_class?() click to toggle source
# File lib/serverkit/resources/base.rb, line 17
def abstract_class?
  !!@abstract_class
end
attribute(name, options = {}) click to toggle source

@note DSL method to define attribute with its validations

# File lib/serverkit/resources/base.rb, line 22
def attribute(name, options = {})
  default = options.delete(:default)
  define_method(name) do
    @attributes[name.to_s] || default
  end
  validates name, options unless options.empty?
end
new(recipe, attributes) click to toggle source

@param [Serverkit::Recipe] recipe @param [Hash] attributes

# File lib/serverkit/resources/base.rb, line 54
def initialize(recipe, attributes)
  @attributes = attributes
  @recipe = recipe
end

Public Instance Methods

all_errors() click to toggle source

@note For override @return [Array<Serverkit::Errors::Base>]

# File lib/serverkit/resources/base.rb, line 61
def all_errors
  attribute_validation_errors
end
check_command(*args) click to toggle source

@return [true, false]

# File lib/serverkit/resources/base.rb, line 66
def check_command(*args)
  run_command(*args).success?
end
check_command_from_identifier(*args) click to toggle source

@return [true, false]

# File lib/serverkit/resources/base.rb, line 71
def check_command_from_identifier(*args)
  run_command_from_identifier(*args).success?
end
get_command_from_identifier(*args) click to toggle source

@return [String]

# File lib/serverkit/resources/base.rb, line 76
def get_command_from_identifier(*args)
  backend.command.get(*args)
end
handlers() click to toggle source

@return [Array<Serverkit::Resource>]

# File lib/serverkit/resources/base.rb, line 81
def handlers
  @handlers ||= Array(notify).map do |id|
    recipe.handlers.find do |handler|
      handler.id == id
    end
  end.compact
end
id() click to toggle source

@note For logging and notifying @return [String]

# File lib/serverkit/resources/base.rb, line 91
def id
  @attributes["id"] || default_id
end
notifiable?() click to toggle source

@return [true, false] True if this resource should call any handler

# File lib/serverkit/resources/base.rb, line 96
def notifiable?
  @recheck_result == true && !handlers.nil?
end
run_apply() click to toggle source

@note check and apply wrapper

# File lib/serverkit/resources/base.rb, line 101
def run_apply
  unless run_check
    apply
    @recheck_result = !!recheck_with_script
  end
end
run_check() click to toggle source

@note check wrapper @return [true, false]

# File lib/serverkit/resources/base.rb, line 110
def run_check
  @check_result = !!check_with_script
end
successful?() click to toggle source
# File lib/serverkit/resources/base.rb, line 114
def successful?
  successful_on_check? || successful_on_recheck?
end
successful_on_check?() click to toggle source
# File lib/serverkit/resources/base.rb, line 118
def successful_on_check?
  @check_result == true
end
successful_on_recheck?() click to toggle source
# File lib/serverkit/resources/base.rb, line 122
def successful_on_recheck?
  @recheck_result == true
end
to_a() click to toggle source

@note recipe resource will override to replace itself with multiple resources @return [Array<Serverkit::Resources::Base>]

# File lib/serverkit/resources/base.rb, line 128
def to_a
  [self]
end

Private Instance Methods

attribute_validation_errors() click to toggle source

@return [Array<Serverkit::Errors::AttributeValidationError>]

# File lib/serverkit/resources/base.rb, line 135
def attribute_validation_errors
  valid?
  errors.map do |attribute_name, message|
    Serverkit::Errors::AttributeValidationError.new(self, attribute_name, message)
  end
end
check_with_script() click to toggle source

@note Prior check_script attribute to resource’s check implementation

# File lib/serverkit/resources/base.rb, line 143
def check_with_script
  if check_script
    check_command(check_script)
  else
    check
  end
end
create_remote_file(content: nil, path: nil) click to toggle source

Create a file on remote side @param [String] content @param [String] path

# File lib/serverkit/resources/base.rb, line 154
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_remote_temp_file(content) click to toggle source

Create temp file on remote side with given content @param [String] content @return [String] Path to remote temp file

# File lib/serverkit/resources/base.rb, line 165
def create_remote_temp_file(content)
  make_remote_temp_path.tap do |path|
    update_remote_file_content(content: content, path: path)
  end
end
default_id() click to toggle source

@note For override @return [String]

# File lib/serverkit/resources/base.rb, line 173
def default_id
  required_values.join(" ")
end
make_remote_temp_path() click to toggle source

@note GNU mktemp doesn’t require -t option, but BSD mktemp does @return [String]

# File lib/serverkit/resources/base.rb, line 179
def make_remote_temp_path
  run_command("mktemp -u 2>/dev/null || mktemp -u -t tmp").stdout.rstrip
end
move_remote_file_keeping_destination_metadata(source, destination) click to toggle source

@note “metadata” means a set of group, mode, and owner @param [String] destination @param [String] source

# File lib/serverkit/resources/base.rb, line 186
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
recheck() click to toggle source

@note For override @return [true, false]

# File lib/serverkit/resources/base.rb, line 198
def recheck
  check
end
recheck_with_script() click to toggle source

@note Prior recheck_script attribute to resource’s recheck implementation

# File lib/serverkit/resources/base.rb, line 203
def recheck_with_script
  if recheck_script
    check_command(recheck_script)
  else
    recheck
  end
end
required_attribute_names() click to toggle source

@return [Array<Symbol>]

# File lib/serverkit/resources/base.rb, line 212
def required_attribute_names
  _validators.select do |key, validators|
    validators.grep(::RequiredValidator).any?
  end.keys
end
required_values() click to toggle source

@return [Array]

# File lib/serverkit/resources/base.rb, line 219
def required_values
  required_attribute_names.map do |required_attribute_name|
    send(required_attribute_name)
  end
end
run_command(command) click to toggle source

@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]

# File lib/serverkit/resources/base.rb, line 228
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
run_command_from_identifier(*args) click to toggle source

@return [Specinfra::CommandResult]

# File lib/serverkit/resources/base.rb, line 241
def run_command_from_identifier(*args)
  run_command(get_command_from_identifier(*args))
end
update_remote_file_content(content: nil, path: nil) click to toggle source

Update remote file content on given path with given content @param [String] content @param [String] path

# File lib/serverkit/resources/base.rb, line 248
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