Class: MescalCli::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/mescal-cli/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Cli

Returns a new instance of Cli.



6
7
8
9
10
11
12
13
14
15
# File 'lib/mescal-cli/cli.rb', line 6

def initialize(config)
  @mode = ARGV.first
  usage and System.exit(1) unless !!@mode
  @config = config
  @client = Client.new(config['mescal'])
  @image = config['image']
  @sshCmd = config['sshCmd']
  @cpus = config['cpus']
  @mem = config['mem']
end

Instance Method Details

#kill(id) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/mescal-cli/cli.rb', line 84

def kill(id)
  if(killed = Task.kill(@client, id))
    puts "Killed #{killed}"
  else
    puts "Failed to kill #{id}"
  end
end

#listObject



30
31
32
# File 'lib/mescal-cli/cli.rb', line 30

def list
  tp Task.list(@client), {id: {width: 48}}, :image, :cmd, :state
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mescal-cli/cli.rb', line 34

def run
  @cmd = ARGV[1] || @config['cmd']
  puts "Sending task to Mescal..."
  task = Task.create(@client, @image, @cmd, @cpus, @mem)
  run = true
  threads = []
  pailer_started = false
  while(run) do
    sleep(2)
    state = task.state
    task.update!
    if state != task.state
      puts "State: #{task.state}"
      if task.state != "TASK_PENDING" && !pailer_started
        pailer_started = true
        ["stdout", "stderr"].each do |std|
          threads << Thread.new { Pailer.new(task, @config['mescal'], std).run! }
        end
      end
    end

    run = !task.done?
  end
  sleep(10)
rescue Interrupt
  kill(task.id) if !!task
ensure
  threads.each { |t| t.kill } if !!threads
end

#run!Object



21
22
23
24
25
26
27
28
# File 'lib/mescal-cli/cli.rb', line 21

def run!
  case @mode
  when "run" then run
  when "ssh" then ssh
  when "list" then list
  when "kill" then kill(ARGV[1])
  end
end

#sshObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mescal-cli/cli.rb', line 64

def ssh
  @cmd = ARGV[1] || @config['cmd']
  task = Task.create(@client, @image, @sshCmd, @cpus, @mem)
  run = true
  while(run) do
    sleep(2)
    state = task.state
    task.update!
    if state != task.state
      if task.state == "TASK_RUNNING"
        Ssh.new(task).run!
      elsif task.state != "TASK_PENDING"
        puts "Task exited with state #{task.state} before ssh could connect"
      end
    end

    run = !task.done?
  end
end

#usageObject



17
18
19
# File 'lib/mescal-cli/cli.rb', line 17

def usage
  puts "mescal [run|ssh]"
end