Class: Kontena::Plugin::Shell::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/plugin/shell/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Session

Returns a new instance of Session.



13
14
15
16
# File 'lib/kontena/plugin/shell/session.rb', line 13

def initialize(context)
  @context = Context.new(context)
  read_history
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/kontena/plugin/shell/session.rb', line 11

def context
  @context
end

Instance Method Details

#caretObject



167
168
169
# File 'lib/kontena/plugin/shell/session.rb', line 167

def caret
  pastel.white('>')
end

#configObject



146
147
148
# File 'lib/kontena/plugin/shell/session.rb', line 146

def config
  Kontena::Cli::Config
end

#config_file_modified_since?(time) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/kontena/plugin/shell/session.rb', line 79

def config_file_modified_since?(time)
  return false unless config.config_file_available?
  return true if File.mtime(config.config_filename) >= time
end

#execute_with_fork(command) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kontena/plugin/shell/session.rb', line 57

def execute_with_fork(command)
  start = Time.now
  pid = fork do
    Process.setproctitle("kosh-runner")
    command.run
  end
  trap('INT') {
    begin
      Process.kill('TERM', pid)
    rescue Errno::ESRCH
      raise SignalException, 'SIGINT'
    end
  }
  Process.waitpid(pid)
  if config_file_modified_since?(start)
    puts ""
    puts pastel.yellow("Config file has been modified, reloading configuration")
    puts ""
    config.reset_instance
  end
end

#execute_with_thread(command) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/kontena/plugin/shell/session.rb', line 48

def execute_with_thread(command)
  old_trap = trap('INT', Proc.new { Thread.main[:command_thread] && Thread.main[:command_thread].kill })
  Thread.main[:command_thread] = Thread.new do
    command.run
  end
  Thread.main[:command_thread].join
  trap('INT', old_trap)
end

#fork_supported?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/kontena/plugin/shell/session.rb', line 44

def fork_supported?
  Process.respond_to?(:fork)
end

#forkable_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/kontena/plugin/shell/session.rb', line 37

def forkable_command?(command)
  return false if !command.is_a?(Kontena::Plugin::Shell::KontenaCommand)
  return false if command.subcommand_class.has_subcommands?

  true
end

#grid_nameObject



175
176
177
# File 'lib/kontena/plugin/shell/session.rb', line 175

def grid_name
  config.current_grid
end

#history_fileObject



18
19
20
# File 'lib/kontena/plugin/shell/session.rb', line 18

def history_file
  ENV['KOSH_HISTORY'] || File.join(Dir.home, '.kosh_history')
end

#master_nameObject



171
172
173
# File 'lib/kontena/plugin/shell/session.rb', line 171

def master_name
  config.current_master.name if config.current_master
end

#pastelObject



142
143
144
# File 'lib/kontena/plugin/shell/session.rb', line 142

def pastel
  Kontena.pastel
end

#promptObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/kontena/plugin/shell/session.rb', line 150

def prompt
  if master_name && master_name.include?('/'.freeze)
    org, name = master_name.split('/')
    "#{pastel.bright_cyan(org)} / #{pastel.cyan(name)} #{pastel.yellow(context)} #{caret} "
  elsif master_name && grid_name
    "#{pastel.bright_cyan(master_name)} / #{pastel.cyan(grid_name)} #{pastel.yellow(context)} #{caret} "
  elsif master_name
    "#{pastel.bright_cyan(master_name)} / #{pastel.red('<no grid>')} #{pastel.yellow(context)} #{caret} "
  else
    if org = ENV['KONTENA_ORGANIZATION']
      "#{pastel.bright_cyan(org)} #{pastel.yellow(context)} #{caret} "
    else
      "#{pastel.yellow(context)} #{caret} "
    end
  end
end

#read_historyObject



22
23
24
# File 'lib/kontena/plugin/shell/session.rb', line 22

def read_history
  File.readlines(history_file).each { |line| Readline::HISTORY.push(line.chomp) } if File.exist?(history_file)
end

#runObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kontena/plugin/shell/session.rb', line 84

def run
  puts File.read(__FILE__)[/__END__$(.*)/m, 1]
  puts "Kontena Shell v#{Kontena::Plugin::Shell::VERSION} (c) 2017 Kontena"
  puts pastel.green("Enter 'help' to see a list of commands or 'help <command>' to get help on a specific command.")

  stty_save = `stty -g`.chomp rescue nil
  at_exit do
    File.write(history_file, Readline::HISTORY.to_a.uniq.last(100).join("\n"))
    system('stty', stty_save) if stty_save
  end

  # Hook stack command kontena.yml content prompting
  if Gem::Version.new(Kontena::Cli::VERSION) >= Gem::Version.new('1.4.0')
    require 'kontena/plugin/shell/prompt_loader'
  else
    require 'kontena/cli/stacks/validate_command'
    require 'kontena/cli/stacks/install_command'
    require 'kontena/cli/stacks/upgrade_command'
    require 'kontena/plugin/shell/stacks_common_ext'
    Kontena::Cli::Stacks::ValidateCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
    Kontena::Cli::Stacks::InstallCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
    Kontena::Cli::Stacks::UpgradeCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
  end

  Readline.completion_proc = Proc.new do |word|
    line = Readline.line_buffer
    tokens = line.shellsplit
    tokens.pop unless word.empty?

    if context.empty? && tokens.empty?
      completions = Kontena::MainCommand.recognised_subcommands.flat_map(&:names) + Shell.commands.keys
    else
      command = Shell.command(context.first || tokens.first || 'kontena')
      if command
        if command.completions.first.respond_to?(:call)
          completions = command.completions.first.call(context, tokens, word)
        else
          completions = Array(command.completions)
        end
      else
        completions = []
      end
    end

    word.empty? ? completions : completions.select { |c| c.start_with?(word) }
  end

  while buf = Readline.readline(prompt, true)
    if buf.strip.empty?
      Readline::HISTORY.pop
    else
      run_command(buf)
    end
  end
  puts
  puts pastel.green("Bye!")
end

#run_command(buf) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/kontena/plugin/shell/session.rb', line 26

def run_command(buf)
  tokens = buf.split(/\s(?=(?:[^"]|"[^"]*")*$)/).map(&:strip)
  runner = Shell.command(tokens.first) || Shell.command(context.first) || Kontena::Plugin::Shell::KontenaCommand
  command = runner.new(context, tokens, self)
  if fork_supported? && forkable_command?(command)
    execute_with_fork(command)
  else
    execute_with_thread(command)
  end
end