Module: Kleiber::Commands

Included in:
Project
Defined in:
lib/kleiber/commands.rb

Instance Method Summary collapse

Instance Method Details

#apply_env_line(env_to_app) ⇒ String

Returns string of environment variable definitions

Examples:

'FIRST_HOST=192.168.22.20 FIRST_PORT=3030 SECOND_HOST=192.168.22.21 SECOND_PORT=3030'

Parameters:

  • env_to_app (Hash)

    final defined environment variables list

Returns:

  • (String)

    environment definition string



148
149
150
# File 'lib/kleiber/commands.rb', line 148

def apply_env_line(env_to_app)
  env_to_app.map { |e| e.join('=') }.join(' ')
end

#handle_halt(_params) ⇒ String

Returns command line for ‘vagrant halt` command.

Parameters:

  • _params (Hash)

    this handler doesn’t use params

Returns:

  • (String)

    line for vagrant halt execution



107
108
109
# File 'lib/kleiber/commands.rb', line 107

def handle_halt(_params)
  vagrant_(:halt)
end

#handle_reload(params) ⇒ String

Returns command line for ‘vagrant reload` command. Evaluates tasks executions line by taken parameters

Parameters:

  • params (Hash)

    params hash with values to execute with

Returns:

  • (String)

    line for vagrant reload execution



98
99
100
101
102
# File 'lib/kleiber/commands.rb', line 98

def handle_reload(params)
  line = [vagrant_(:reload)]
  line << handle_ssh(params) unless params[:tasks].empty?
  line.join(' && ')
end

#handle_ssh(params) ⇒ String

Returns command line for ‘vagrant ssh` command. Evaluates tasks executions line by taken parameters

Parameters:

  • params (Hash)

    params hash with values to execute with

Returns:

  • (String)

    line for vagrant ssh execution



88
89
90
91
92
# File 'lib/kleiber/commands.rb', line 88

def handle_ssh(params)
  line = [vagrant_(:ssh)]
  line << ssh_exec_line(params) unless params[:tasks].empty?
  line.join(' ')
end

#handle_up(params) ⇒ String

Returns command line for ‘vagrant up` command. Evaluates tasks executions line by taken parameters

Parameters:

  • params (Hash)

    params hash with values to execute with

Returns:

  • (String)

    line for vagrant up execution



78
79
80
81
82
# File 'lib/kleiber/commands.rb', line 78

def handle_up(params)
  line = [vagrant_(:up)]
  line << handle_ssh(params) unless params[:tasks].empty?
  line.join(' && ')
end

#in_machine(params) ⇒ String

Returns commandline need to execute in vagrant machine. Actually, it’s an environment settings and chain of tasks

Examples:

'FIRST_HOST=192.168.22.20 cd /vagrant && bundle exec rails server'

Parameters:

  • params (Hash)

    params hash with values to execute with

Returns:

  • (String)


126
127
128
# File 'lib/kleiber/commands.rb', line 126

def in_machine(params)
  [apply_env_line(params[:env]), tasks_line(params[:tasks])].join(' ')
end

#scriptify(command) ⇒ String

Returns text of bashscript file with command

Parameters:

  • command (String)

    command line to execute

Returns:

  • (String)

    scriptlike string



70
71
72
# File 'lib/kleiber/commands.rb', line 70

def scriptify(command)
  ['#!/bin/bash', 'unset RUBYLIB', command].join("\n")
end

#ssh_exec_line(params) ⇒ String

Returns option which executes tasks in vagrant

Examples:

"-c 'FIRST_HOST=192.168.22.20 cd /vagrant && bundle exec rails server'"

Parameters:

  • params (Hash)

    params hash with values to execute with

Returns:

  • (String)

    option



116
117
118
# File 'lib/kleiber/commands.rb', line 116

def ssh_exec_line(params)
  Cocaine::CommandLine.new('', '-c :in_machine').command(in_machine: in_machine(params))
end

#tasks_line(tasks_to_run) ⇒ String

Returns tasks chain command line

Examples:

'cd /vagrant && bundle install --binstubs && bundle exec rackup'

Parameters:

  • tasks_to_run (Hash)

    final defined tasks hash

Returns:

  • (String)

    tasks commandline



135
136
137
138
139
140
141
# File 'lib/kleiber/commands.rb', line 135

def tasks_line(tasks_to_run)
  line = ['cd /vagrant']
  line += tasks_to_run.reduce({}) do |result, (key, value)|
    result.merge(key => tasks[key] || value)
  end.values
  line.join(' && ')
end

#terminal_execute(command) ⇒ Object

Executes command in new tab

Parameters:

  • command (String)

    command line to run



49
50
51
52
53
# File 'lib/kleiber/commands.rb', line 49

def terminal_execute(command)
  with_tmpfile_script(command) do |file|
    Kleiber.terminal.execute(file)
  end
end

#vagrantObject



7
8
9
# File 'lib/kleiber/commands.rb', line 7

def vagrant
  Cocaine::CommandLine.new('/usr/bin/vagrant')
end

#vagrant_(command) ⇒ String

Returns vagrant command

Parameters:

  • command (Symbol, String)

    vagrant command

Returns:

  • (String)

    command line



155
156
157
# File 'lib/kleiber/commands.rb', line 155

def vagrant_(command)
  send("vagrant_#{command}".to_sym).command
end

#vagrant_destroyCocaine::CommandLine

Returns vagrant destroy command line

Returns:

  • (Cocaine::CommandLine)

    vagrant destroy command



37
38
39
# File 'lib/kleiber/commands.rb', line 37

def vagrant_destroy
  Cocaine::CommandLine.new(vagrant.command, 'destroy')
end

#vagrant_haltCocaine::CommandLine

Returns vagrant halt command line

Returns:

  • (Cocaine::CommandLine)

    vagrant halt command



31
32
33
# File 'lib/kleiber/commands.rb', line 31

def vagrant_halt
  Cocaine::CommandLine.new(vagrant.command, 'halt')
end

#vagrant_provisionCocaine::CommandLine

Returns vagrant provision command line

Returns:

  • (Cocaine::CommandLine)

    vagrant provision command



43
44
45
# File 'lib/kleiber/commands.rb', line 43

def vagrant_provision
  Cocaine::CommandLine.new(vagrant.command, 'provision')
end

#vagrant_reloadCocaine::CommandLine

Returns vagrant reload command line

Returns:

  • (Cocaine::CommandLine)

    vagrant reload command



25
26
27
# File 'lib/kleiber/commands.rb', line 25

def vagrant_reload
  Cocaine::CommandLine.new(vagrant.command, 'reload')
end

#vagrant_sshCocaine::CommandLine

Returns vagrant ssh command line

Returns:

  • (Cocaine::CommandLine)

    vagrant ssh command



19
20
21
# File 'lib/kleiber/commands.rb', line 19

def vagrant_ssh
  Cocaine::CommandLine.new(vagrant.command, 'ssh')
end

#vagrant_upCocaine::CommandLine

Returns vagrant up command line

Returns:

  • (Cocaine::CommandLine)

    vagrant up command



13
14
15
# File 'lib/kleiber/commands.rb', line 13

def vagrant_up
  Cocaine::CommandLine.new(vagrant.command, 'up')
end

#with_tmpfile_script(command) {|file| ... } ⇒ Object

Provides command to execute within temporary bashscript file

Parameters:

  • command (String)

    to execute

Yield Parameters:

  • file (File)

    bashcript to use in terminal instance



58
59
60
61
62
63
64
65
# File 'lib/kleiber/commands.rb', line 58

def with_tmpfile_script(command)
  Tempfile.create([name, '.sh'], '/tmp') do |file|
    file.chmod(0_755)
    file.write(scriptify(command))
    file.close
    yield file
  end
end