module Kleiber::Commands
Public Instance Methods
Returns string of environment variable definitions @param [Hash] env_to_app final defined environment variables list @example
'FIRST_HOST=192.168.22.20 FIRST_PORT=3030 SECOND_HOST=192.168.22.21 SECOND_PORT=3030'
@return [String] environment definition string
# File lib/kleiber/commands.rb, line 148 def apply_env_line(env_to_app) env_to_app.map { |e| e.join('=') }.join(' ') end
Returns command line for `vagrant halt` command. @param [Hash] _params this handler doesn't use params @return [String] line for vagrant halt execution
# File lib/kleiber/commands.rb, line 107 def handle_halt(_params) vagrant_(:halt) end
Returns command line for `vagrant reload` command. Evaluates tasks executions line by taken parameters @param [Hash] params params hash with values to execute with @return [String] line for vagrant reload execution
# 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
Returns command line for `vagrant ssh` command. Evaluates tasks executions line by taken parameters @param [Hash] params params hash with values to execute with @return [String] line for vagrant ssh execution
# 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
Returns command line for `vagrant up` command. Evaluates tasks executions line by taken parameters @param [Hash] params params hash with values to execute with @return [String] line for vagrant up execution
# 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
Returns commandline need to execute in vagrant machine. Actually, it's an environment settings and chain of tasks @example
'FIRST_HOST=192.168.22.20 cd /vagrant && bundle exec rails server'
@param [Hash] params params hash with values to execute with @return [String]
# File lib/kleiber/commands.rb, line 126 def in_machine(params) [apply_env_line(params[:env]), tasks_line(params[:tasks])].join(' ') end
Returns text of bashscript file with command @param [String] command command line to execute @return [String] scriptlike string
# File lib/kleiber/commands.rb, line 70 def scriptify(command) ['#!/bin/bash', 'unset RUBYLIB', command].join("\n") end
Returns option which executes tasks in vagrant @example
"-c 'FIRST_HOST=192.168.22.20 cd /vagrant && bundle exec rails server'"
@param [Hash] params params hash with values to execute with @return [String] option
# 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
Returns tasks chain command line @param [Hash] tasks_to_run final defined tasks hash @example
'cd /vagrant && bundle install --binstubs && bundle exec rackup'
@return [String] tasks commandline
# 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
Executes command in new tab @param [String] command command line to run
# File lib/kleiber/commands.rb, line 49 def terminal_execute(command) with_tmpfile_script(command) do |file| Kleiber.terminal.execute(file) end end
# File lib/kleiber/commands.rb, line 7 def vagrant Cocaine::CommandLine.new('/usr/bin/vagrant') end
Returns vagrant command @param [Symbol, String] command vagrant command @return [String] command line
# File lib/kleiber/commands.rb, line 155 def vagrant_(command) send("vagrant_#{command}".to_sym).command end
Returns vagrant destroy command line @return [Cocaine::CommandLine] vagrant destroy command
# File lib/kleiber/commands.rb, line 37 def vagrant_destroy Cocaine::CommandLine.new(vagrant.command, 'destroy') end
Returns vagrant halt command line @return [Cocaine::CommandLine] vagrant halt command
# File lib/kleiber/commands.rb, line 31 def vagrant_halt Cocaine::CommandLine.new(vagrant.command, 'halt') end
Returns vagrant provision command line @return [Cocaine::CommandLine] vagrant provision command
# File lib/kleiber/commands.rb, line 43 def vagrant_provision Cocaine::CommandLine.new(vagrant.command, 'provision') end
Returns vagrant reload command line @return [Cocaine::CommandLine] vagrant reload command
# File lib/kleiber/commands.rb, line 25 def vagrant_reload Cocaine::CommandLine.new(vagrant.command, 'reload') end
Returns vagrant ssh command line @return [Cocaine::CommandLine] vagrant ssh command
# File lib/kleiber/commands.rb, line 19 def vagrant_ssh Cocaine::CommandLine.new(vagrant.command, 'ssh') end
Returns vagrant up command line @return [Cocaine::CommandLine] vagrant up command
# File lib/kleiber/commands.rb, line 13 def vagrant_up Cocaine::CommandLine.new(vagrant.command, 'up') end
Provides command to execute within temporary bashscript file @param [String] command to execute @yieldparam [File] file bashcript to use in terminal instance
# 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