Class: HaveAPI::Fs::Components::Executable
- Inherits:
-
File
- Object
- HaveAPI::Fs::Component
- File
- HaveAPI::Fs::Components::Executable
- Defined in:
- lib/haveapi/fs/components/executable.rb
Overview
Base class for all executables. Executables can be executed either by
writing 1
into them or by running them, as they contain a Ruby script.
The Ruby script communicates with the file system using RemoteControlFile.
In short, it tells the file system to execute a component at a certain path,
which results in the same action as when 1
is written to the file.
In both cases, the file system itself does the operation, the outer process only signals it to do so and then waits for a reply.
When the component is to be executed, method #exec is called.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from HaveAPI::Fs::Component
#atime, #context, #ctime, #mtime
Instance Method Summary collapse
Methods inherited from File
#file?, #raw_close, #raw_open, #raw_read, #raw_sync, #raw_truncate, #raw_write, #size
Methods inherited from HaveAPI::Fs::Component
#abspath, #bound=, #bound?, children_reader, component, #contents, #directory?, #file?, #find, inherited, #initialize, #invalid?, #invalidate, #parent, #path, #readable?, #reset, #setup, #times, #title, #unsaved?, #use
Constructor Details
This class inherits a constructor from HaveAPI::Fs::Component
Instance Method Details
#exec ⇒ Object
71 72 73 |
# File 'lib/haveapi/fs/components/executable.rb', line 71 def exec raise NotImplementedError end |
#executable? ⇒ Boolean
14 15 16 |
# File 'lib/haveapi/fs/components/executable.rb', line 14 def executable? true end |
#read ⇒ Object
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 |
# File 'lib/haveapi/fs/components/executable.rb', line 22 def read abs_path = ::File.join(context.mountpoint, path) <<END #!#{RbConfig.ruby} # # This action can be executed either by running this file or by writing "1" to # it, e.g.: # # .#{abs_path} # # or # # echo 1 > #{abs_path} # require 'yaml' f = ::File.open( '#{::File.join(context.mountpoint, '.remote_control')}', 'w+' ) f.write(YAML.dump({ action: :execute, path: '/#{path}', })) f.write(#{RemoteControlFile::MSG_DELIMITER.dump}) f.flush f.seek(0) ret = YAML.load(f.read) f.close unless ret[:status] warn "Action failed: \#{ret[:message]}" if ret[:errors] ret[:errors].each do |k, v| warn " \#{k}: \#{v.join('; ')}" end end end exit(ret[:status]) END end |
#writable? ⇒ Boolean
18 19 20 |
# File 'lib/haveapi/fs/components/executable.rb', line 18 def writable? true end |
#write(str) ⇒ Object
67 68 69 |
# File 'lib/haveapi/fs/components/executable.rb', line 67 def write(str) exec if str.strip == '1' end |