Class: Escualo::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/escualo/session.rb,
lib/escualo/session/within.rb,
lib/escualo/session/environment.rb

Direct Known Subclasses

Docker, Local, Remote

Defined Under Namespace

Classes: Docker, Local, Remote

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = struct) ⇒ Session

Returns a new instance of Session.



6
7
8
# File 'lib/escualo/session.rb', line 6

def initialize(options=struct)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/escualo/session.rb', line 4

def options
  @options
end

Class Method Details

.parse_session_options(options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/escualo/session/within.rb', line 2

def self.parse_session_options(options)
  struct username: options.username || 'root',
         hostname: options.hostname || 'localhost',
         ssh_options: {
             keys: [options.ssh_key].compact,
             port: options.ssh_port || 22
         },
         verbose: options.verbose,
         local: options.hostname.blank? && options.username.blank? && options.ssh_key.blank? && options.ssh_port.blank?,
         dockerized: options.dockerized,
         unoptimized_dockerfile: options.unoptimized_dockerfile

end

.set_command(key, value) ⇒ Object



32
33
34
# File 'lib/escualo/session.rb', line 32

def self.set_command(key, value)
  "echo export #{key}=#{value} > ~/.escualo/vars/#{key}"
end

.within(options, force_local = false, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/escualo/session/within.rb', line 16

def self.within(options, force_local=false, &block)
  session_options = parse_session_options options

  if session_options.dockerized
    within_dockerized_session session_options, options, &block
  elsif session_options.local || force_local
    block.call(Escualo::Session::Local.new session_options)
  else
    within_ssh_session(session_options, &block)
  end
end

.within_dockerized_session(session_options, options, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/escualo/session/within.rb', line 28

def self.within_dockerized_session(session_options, options, &block)
  session = Escualo::Session::Docker.new session_options
  session.start! options
  block.call(session)
  session.finish! options
end

.within_ssh_session(session_options, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/escualo/session/within.rb', line 35

def self.within_ssh_session(session_options, &block)
  Net::SSH.start(
      session_options.hostname,
      session_options.username,
      session_options.ssh_options) do |ssh|
    block.call(Escualo::Session::Remote.new ssh, session_options)
  end
end

Instance Method Details

#check?(command, include) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/escualo/session.rb', line 10

def check?(command, include)
  ask(command).include? include rescue false
end

#clean_environment_variables!Object



3
4
5
# File 'lib/escualo/session/environment.rb', line 3

def clean_environment_variables!
  tell! Escualo::Env.clean_command
end

#set_environment_variables!(variables) ⇒ Object



15
16
17
# File 'lib/escualo/session/environment.rb', line 15

def set_environment_variables!(variables)
  tell_all! *variables.map { |key, value| Escualo::Env.set_command key, value }
end

#setup_environment_variables!Object



7
8
9
10
11
12
13
# File 'lib/escualo/session/environment.rb', line 7

def setup_environment_variables!
  source_escualorc = "'source ~/.escualorc'"
  tell_all! 'mkdir -p ~/.escualo/vars',
            %q{echo 'for var in ~/.escualo/vars/*; do source $var; done' > ~/.escualorc},
            %q{chmod u+x ~/.escualorc},
            "grep -q #{source_escualorc} ~/.bashrc || echo #{source_escualorc} >> ~/.bashrc"
end

#tell!(command) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/escualo/session.rb', line 18

def tell!(command)
  if options.verbose
    stream! command
  else
    exec! command
  end
end

#tell_all!(*commands) ⇒ Object



14
15
16
# File 'lib/escualo/session.rb', line 14

def tell_all!(*commands)
  tell! commands.compact.join(' && ')
end

#unset_environment_variables!(variable_names) ⇒ Object



19
20
21
# File 'lib/escualo/session/environment.rb', line 19

def unset_environment_variables!(variable_names)
  tell_all! *variable_names.map { |name| Escualo::Env.unset_command name }
end

#upload_template!(destination, name, bindings) ⇒ Object



26
27
28
29
30
# File 'lib/escualo/session.rb', line 26

def upload_template!(destination, name, bindings)
  write_template! name, Mumukit::Core::Template.new(File.join(__dir__, '..', 'templates', "#{name}.erb"), bindings) do |file|
    upload! file, destination
  end
end

#write_template!(name, template, &block) ⇒ Object



36
37
38
# File 'lib/escualo/session.rb', line 36

def write_template!(name, template, &block)
  template.with_tempfile!('template', &block)
end