Class: Dkdeploy::TestEnvironment::Remote
- Inherits:
-
Object
- Object
- Dkdeploy::TestEnvironment::Remote
- Includes:
- Constants, SSHKit::DSL
- Defined in:
- lib/dkdeploy/test_environment/remote.rb
Overview
Class for remote actions
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#last_command_result ⇒ Object
Returns the value of attribute last_command_result.
-
#ssh_config ⇒ Object
Returns the value of attribute ssh_config.
Instance Method Summary collapse
-
#create_directory(remote_directory_name) ⇒ Boolean
Creates the remote directory.
-
#create_file(file_path, content) ⇒ Boolean
Creates the remote file with the given content.
-
#directory_exists?(path) ⇒ Boolean
Check if remote directory exists.
-
#exists?(type, path) ⇒ Boolean
Execute remote if condition.
-
#file_exists?(path) ⇒ Boolean
Check if remote file exists.
-
#get_file_content(file_path) ⇒ Boolean
Gets the remote file content.
-
#get_resource_property(path, property) ⇒ String
Gets one of the properties of resource.
-
#group_has_permission?(all_permissions, examined_group_permission) ⇒ Boolean
Checks, if the actually permissions include the expected group permission.
-
#initialize(hostname, ssh_config = {}) ⇒ Remote
constructor
A new instance of Remote.
-
#others_has_permission?(all_permissions, examined_others_permission) ⇒ Boolean
Checks, if the actually permissions include the expected others permission.
-
#remove_deploy_to_path ⇒ Boolean
Remove the deploy_to path.
-
#remove_resource(resource_path) ⇒ Boolean
Removes the remote file or directory.
-
#run(command) ⇒ Boolean
Execute command on server.
-
#source_of_symlink(target_symlink_path) ⇒ String
Remove the source of a symlink.
-
#symlink_exists?(path) ⇒ Boolean
Check if remote symlink exists.
-
#user_has_permission?(all_permissions, examined_user_permission) ⇒ Boolean
Checks, if the actually permissions include the expected user permission.
Methods included from Constants
#assets_path, #capistrano_configuration_fixtures_path, #config_dev_path, #current_path, #deploy_to, #gem_root, #maintenance_config_file_path, #releases_path, #remote_tmp_path, #server_url, #shared_path, #stage, #template_path, #test_app_path, #tmp_path
Constructor Details
#initialize(hostname, ssh_config = {}) ⇒ Remote
Returns a new instance of Remote.
16 17 18 19 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 16 def initialize(hostname, ssh_config = {}) ssh_config[:compression] = 'none' @host = SSHKit::Host.new hostname: hostname, ssh_options: ssh_config end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
14 15 16 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 14 def host @host end |
#last_command_result ⇒ Object
Returns the value of attribute last_command_result.
14 15 16 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 14 def last_command_result @last_command_result end |
#ssh_config ⇒ Object
Returns the value of attribute ssh_config.
14 15 16 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 14 def ssh_config @ssh_config end |
Instance Method Details
#create_directory(remote_directory_name) ⇒ Boolean
Creates the remote directory
58 59 60 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 58 def create_directory(remote_directory_name) run %(mkdir -p "#{remote_directory_name}") end |
#create_file(file_path, content) ⇒ Boolean
Creates the remote file with the given content
67 68 69 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 67 def create_file(file_path, content) run %(echo "#{content}" > "#{file_path}") end |
#directory_exists?(path) ⇒ Boolean
Check if remote directory exists
25 26 27 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 25 def directory_exists?(path) exists? 'd', path end |
#exists?(type, path) ⇒ Boolean
Execute remote if condition
50 51 52 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 50 def exists?(type, path) run %([ -#{type} "#{path}" ] && exit 0 || exit 1) end |
#file_exists?(path) ⇒ Boolean
Check if remote file exists
41 42 43 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 41 def file_exists?(path) exists? 'f', path end |
#get_file_content(file_path) ⇒ Boolean
Gets the remote file content
116 117 118 119 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 116 def get_file_content(file_path) raise 'Internal server error.' unless run %(less #{file_path}) last_command_result end |
#get_resource_property(path, property) ⇒ String
Gets one of the properties of resource
Examples:
get_resource_property('/a/b/c/', 'group') -> www-data
get_resource_property('/a/b/c/', 'group') -> -rwxr-xr--
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 130 def get_resource_property(path, property) # run the ls command raise 'Internal server error.' unless run %(ls -lad "#{path}") # Parses the returned string value and converts it to an array # Example: Array[drwxrwxr-x, 3, vagrant, www-data, 4096, Apr, 25, 14:12] properties = (last_command_result || '').split(' ') # property mapping to the number of the array element case property when 'permissions' property = 0 when 'owner' property = 2 when 'group' property = 3 else raise ArgumentError, 'The given property can not be mapped.' end properties.at(property) if property.between?(0, properties.count) end |
#group_has_permission?(all_permissions, examined_group_permission) ⇒ Boolean
Checks, if the actually permissions include the expected group permission
166 167 168 169 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 166 def (, ) = [4, 3].delete('-') # Example -rwxr-xr-- -> rx .include?() end |
#others_has_permission?(all_permissions, examined_others_permission) ⇒ Boolean
Checks, if the actually permissions include the expected others permission
176 177 178 179 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 176 def (, ) = [7, 3].delete('-') # Example -rwxr-xr-- -> r .include?() end |
#remove_deploy_to_path ⇒ Boolean
Remove the deploy_to path
82 83 84 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 82 def remove_deploy_to_path run %(sudo rm -rf -R "#{deploy_to}") end |
#remove_resource(resource_path) ⇒ Boolean
Removes the remote file or directory
75 76 77 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 75 def remove_resource(resource_path) run %(rm -rf "#{resource_path}") end |
#run(command) ⇒ Boolean
Execute command on server
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 97 def run(command) success = false result = '' # we need a local variable to capture result from sshkit block begin on @host do result = capture command end @last_command_result = result success = true rescue SSHKit::Command::Failed, SSHKit::Runner::ExecuteError success = false end success end |
#source_of_symlink(target_symlink_path) ⇒ String
Remove the source of a symlink
89 90 91 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 89 def source_of_symlink(target_symlink_path) run %(readlink "#{target_symlink_path}") end |
#symlink_exists?(path) ⇒ Boolean
Check if remote symlink exists
33 34 35 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 33 def symlink_exists?(path) exists? 'L', path end |
#user_has_permission?(all_permissions, examined_user_permission) ⇒ Boolean
Checks, if the actually permissions include the expected user permission
156 157 158 159 |
# File 'lib/dkdeploy/test_environment/remote.rb', line 156 def (, ) = [1, 3].delete('-') # Example -rwxr-xr-- -> rwx .include?() end |