Class: Dkdeploy::TestEnvironment::Remote

Inherits:
Object
  • Object
show all
Includes:
Constants, SSHKit::DSL
Defined in:
lib/dkdeploy/test_environment/remote.rb

Overview

Class for remote actions

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostObject

Returns the value of attribute host.



14
15
16
# File 'lib/dkdeploy/test_environment/remote.rb', line 14

def host
  @host
end

#last_command_resultObject

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_configObject

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

Parameters:

  • remote_directory_name (String)

    the remote directory name

Returns:

  • (Boolean)


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

Parameters:

  • file_path (String)

    the file path to be created

  • content (String)

    the content to be put into the created file

Returns:

  • (Boolean)


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

Parameters:

  • path (String)

    Remote path

Returns:

  • (Boolean)


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

Parameters:

  • type (String)

    Type of if condition

  • path (String)

    Remote path

Returns:

  • (Boolean)


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

Parameters:

  • path (String)

    Remote path

Returns:

  • (Boolean)


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

Parameters:

  • file_path (String)

Returns:

  • (Boolean)


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

Parameters:

  • path (String)

    path to the remote file or directory to be examined

  • property (String)

    the property to be returned

Returns:

  • (String)

    resource property



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

Parameters:

  • all_permissions (String)

    Example: -rwxr-xr–

  • examined_group_permission (String)

    Example: w

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/dkdeploy/test_environment/remote.rb', line 166

def group_has_permission?(all_permissions, examined_group_permission)
  group_permissions = all_permissions[4, 3].delete('-') # Example -rwxr-xr-- -> rx
  group_permissions.include?(examined_group_permission)
end

#others_has_permission?(all_permissions, examined_others_permission) ⇒ Boolean

Checks, if the actually permissions include the expected others permission

Parameters:

  • all_permissions (String)

    Example: -rwxr-xr–

  • examined_others_permission (String)

    Example: r

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/dkdeploy/test_environment/remote.rb', line 176

def others_has_permission?(all_permissions, examined_others_permission)
  others_permissions = all_permissions[7, 3].delete('-') # Example -rwxr-xr-- -> r
  others_permissions.include?(examined_others_permission)
end

#remove_deploy_to_pathBoolean

Remove the deploy_to path

Returns:

  • (Boolean)


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

Parameters:

  • resource_path (String)

    the remote file or directory path

Returns:

  • (Boolean)


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

Parameters:

  • command (String)

Returns:

  • (Boolean)


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

Remove the source of a symlink

Returns:

  • (String)


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

Check if remote symlink exists

Parameters:

  • path (String)

    Remote path

Returns:

  • (Boolean)


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

Parameters:

  • all_permissions (String)

    Example: -rwxr-xr–

  • examined_user_permission (String)

    Example: w

Returns:

  • (Boolean)


156
157
158
159
# File 'lib/dkdeploy/test_environment/remote.rb', line 156

def user_has_permission?(all_permissions, examined_user_permission)
  user_permissions = all_permissions[1, 3].delete('-') # Example -rwxr-xr-- -> rwx
  user_permissions.include?(examined_user_permission)
end