bolt_train_runner 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48d2fd061df535be03f5583b85f96ea40e80fb7b59e9a585c8345e9312c8e169
4
- data.tar.gz: 630d34c786a3e334363f3f4f35b554e25944c2452bd6e0155e0e88369facb47c
3
+ metadata.gz: 435c6852758e4d68c5e11abd028ba1f0cc823075046266a915127f9085c2810e
4
+ data.tar.gz: a4d6d656a704c7375ee271c305cb5c74018c91762e502b20b5af7f7a09a7c2dd
5
5
  SHA512:
6
- metadata.gz: e56fcffbd4f44ad4ca73c1113278717559b78bcb70d3e43253eb0835b9961db03a7fd18cbf7d3d6063f89bcc7be6bf70e8c82f0e808265711f57d32fb3c5269d
7
- data.tar.gz: 27762f3c6a3675f27da754514ac3d7d4ad3697375b64c8dd24a13a599fb2a6ac199c735eab4403766b1938e1d1ec297de66a389b81b042a1e69ed8e5aa028625
6
+ metadata.gz: e5b069eb579346addf1f56c271cf2e23fab609cd211b86d7ee4d71200915b1af2ac993e19ee4f445b676c91d9629179c4da47af574ed403159e7f8a1ca2fb1ff
7
+ data.tar.gz: 0f31dae99a45ebf77fbd7a0af0b02a987733f29ac5031add9d111936dd2bbb283607b3a6310ce86c9590bffc8e6e7b9ec9e3206dfd72e5244c159f2f4cd938a3
@@ -3,6 +3,7 @@
3
3
  require 'rubygems'
4
4
  require 'colorize'
5
5
  require 'bolt_train_runner/comms'
6
+ require 'bolt_train_runner/session_runner'
6
7
 
7
8
  # Load all commands
8
9
  Dir[File.join(File.absolute_path(__dir__) + '/bolt_train_runner/commands') + "/**/*.rb"].each do |file|
@@ -24,6 +25,7 @@ class BoltTrainRunner
24
25
 
25
26
  def run
26
27
  comms = nil
28
+ session_runner = nil
27
29
  puts 'Welcome to the Bolty McBoltTrain Runner! Choo choo!'.cyan
28
30
  puts 'To list all commands, enter "help"'.cyan
29
31
  puts 'For help with a specific command, enter "<command> help"'.cyan
@@ -33,27 +35,36 @@ class BoltTrainRunner
33
35
  input = gets.chomp
34
36
  args = input.split(' ')
35
37
  command = args.shift
38
+
36
39
  case command
37
40
  when /^help$/i
38
41
  help
42
+ when Commands.respond_to?(command)
43
+ puts 'Commands can run this'
39
44
  when /^connect$/i
40
45
  newcomms = Commands.connect(args)
41
46
  comms = newcomms if newcomms
42
47
  when /^disconnect$/i
48
+ session_runner.stop if session_runner
49
+ session_runner = nil
43
50
  Commands.disconnect(comms)
44
51
  comms = nil
52
+ when /^sessions$/i
53
+ session_runner = Commands.sessions(args, comms, session_runner)
45
54
  when /^debug$/i
46
55
  Commands.debug(args)
47
- when /^power$/i
48
- Commands.power(args, comms)
49
- when /^throttle$/i
50
- Commands.throttle(args, comms)
51
- when /^move$/i
52
- Commands.move(args, comms)
53
56
  when /^exit$/i
54
- Commands.exit_program(comms)
57
+ Commands.exit_program(comms, session_runner)
55
58
  else
56
- puts "Unknown command: #{command}".red
59
+ if Commands.respond_to?(command)
60
+ # The commands for directly manipulating the train should all
61
+ # accept "args" and "comms" parameters. Because the CLI will pass
62
+ # in args as an array and the session runner will pass in a hash,
63
+ # these commands must be able to handle both.
64
+ Commands.send(command.to_sym, args, comms)
65
+ else
66
+ puts "Unknown command: #{command}".red
67
+ end
57
68
  end
58
69
  end
59
70
  end
@@ -66,6 +77,7 @@ class BoltTrainRunner
66
77
  puts 'power - Turn power on or off to the train'.cyan
67
78
  puts 'throttle - Set throttle to a value between 0 and 10'.cyan
68
79
  puts 'move - Move the train in the given direction at the given speed for a certain length of time'.cyan
80
+ puts 'stop - Stop the train'.cyan
69
81
  puts 'exit - Exit program'.cyan
70
82
  end
71
83
 
@@ -2,10 +2,9 @@ require 'colorize'
2
2
  require 'bolt_train_runner/comms'
3
3
 
4
4
  module Commands
5
- def self.exit_program(comms)
6
- if comms
7
- comms.disconnect
8
- end
5
+ def self.exit_program(comms, session_runner)
6
+ session_runner.stop if session_runner
7
+ comms.disconnect if comms
9
8
  puts 'Seeya later!'.green
10
9
  exit 0
11
10
  end
@@ -1,4 +1,6 @@
1
1
  require 'bolt_train_runner/comms'
2
+ require 'bolt_train_runner/commands/throttle'
3
+ require 'bolt_train_runner/commands/stop'
2
4
  require 'colorize'
3
5
 
4
6
  module Commands
@@ -15,13 +17,21 @@ module Commands
15
17
  return
16
18
  end
17
19
 
18
- if args.length < 3
20
+
21
+ if args.is_a?(Hash)
22
+ direction = args['direction']
23
+ speed = args['speed']
24
+ time = args['time']
25
+ else
26
+ direction, speed, time = args
27
+ end
28
+
29
+ unless direction && speed && time
19
30
  puts 'Please provide direction, speed, and time'.red
20
31
  return
21
32
  end
22
- direction = args[0]
23
- speed = args[1].to_i
24
- time = args[2].to_i
33
+ speed = speed.to_i
34
+ time = time.to_i
25
35
  unless ['forward','reverse'].include?(direction)
26
36
  puts 'Please provide "forward" or "reverse" for direction'.red
27
37
  return
@@ -35,31 +45,10 @@ module Commands
35
45
  return
36
46
  end
37
47
 
38
- message = {
39
- 'type' => 'throttle',
40
- 'method' => 'put',
41
- 'data' => {
42
- 'throttle' => 'bolttrain',
43
- 'address' => '6871',
44
- 'speed' => "#{speed/10.0}",
45
- 'forward' => "#{direction == 'forward'}"
46
- }
47
- }
48
- comms.send_message(message)
49
- puts "Train moving #{direction} at speed #{speed}".green
50
- puts "Waiting #{time} seconds".green
48
+ puts "Moving train #{direction} direction at speed #{speed} for #{time} seconds...".green
49
+ Commands.throttle([speed,direction],comms)
51
50
  sleep(time)
52
- puts "Stopping train".green
53
- message = {
54
- 'type' => 'throttle',
55
- 'method' => 'post',
56
- 'data' => {
57
- 'throttle' => 'bolttrain',
58
- 'address' => '6871',
59
- 'speed' => '0'
60
- }
61
- }
62
- comms.send_message(message)
51
+ Commands.stop(nil, comms)
63
52
  puts 'Move complete'.green
64
53
  end
65
54
  end
@@ -13,7 +13,13 @@ module Commands
13
13
  puts 'Turns power to the train on or off. Must first be connected.'.cyan
14
14
  return
15
15
  end
16
- state = args[0]
16
+
17
+ if args.is_a?(Hash)
18
+ state = args['state']
19
+ else
20
+ state = args[0]
21
+ end
22
+
17
23
  unless ['on','off'].include?(state)
18
24
  puts 'Please provide either "on" or "off"'.red
19
25
  return
@@ -30,5 +36,6 @@ module Commands
30
36
  }
31
37
  }
32
38
  comms.send_message(message)
39
+ puts "Power #{state}".green
33
40
  end
34
41
  end
@@ -0,0 +1,64 @@
1
+ require 'colorize'
2
+ require 'bolt_train_runner/conf'
3
+ require 'bolt_train_runner/session_runner'
4
+
5
+ module Commands
6
+ def self.sessions(args, comms, session_runner)
7
+ if args.empty? || args[0] =~ /help/i
8
+ puts 'Command: sessions'.cyan
9
+ puts 'Syntax: sessions <start|stop>'.cyan
10
+ puts 'Start or stop the thread that monitors the sessions folder for sessions files. When running, this thread'.cyan
11
+ puts 'will automatically execute the commands contained in sessions files generated by the bolt-train-api server'.cyan
12
+ puts 'as they appear in the folder.'.cyan
13
+ return
14
+ end
15
+
16
+ state = args[0]
17
+ starting = state == 'start'
18
+ if !['start','stop'].include?(state)
19
+ puts 'Please provide either "start" or "stop"'.red
20
+ return
21
+ end
22
+
23
+ unless comms
24
+ puts 'Please connect first'.red
25
+ return
26
+ end
27
+
28
+ if starting and session_runner
29
+ puts 'Session runner thread already started'.yellow
30
+ return session_runner
31
+ end
32
+
33
+ if !starting and !session_runner
34
+ puts 'No session runner thread currently running'.yellow
35
+ return nil
36
+ end
37
+
38
+ if starting
39
+ conf = Conf.load_conf
40
+ if args[1]
41
+ session_dir = args[1]
42
+ conf['session_dir'] = session_dir
43
+ Conf.save_conf(conf)
44
+ else
45
+ session_dir = conf['session_dir']
46
+ unless session_dir
47
+ # This should be changed to pick up BOLT_TRAIN_QUEUE_DIR automatically
48
+ print 'Please enter directory for session files [/tmp/bolt-train-queue] > '
49
+ session_dir = gets.chomp
50
+ session_dir = '/tmp/bolt-train-queue' if session_dir.empty?
51
+ conf['session_dir'] = session_dir
52
+ Conf.save_conf(conf)
53
+ end
54
+ end
55
+
56
+ runner = SessionRunner.new(comms, session_dir)
57
+ runner.start
58
+ return runner
59
+ else
60
+ session_runner.stop
61
+ return nil
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,12 @@
1
+ require 'colorize'
2
+ require 'bolt_train_runner/commands/throttle'
3
+
4
+ module Commands
5
+ def self.stop(_args, comms)
6
+ unless comms
7
+ puts 'Please connect first'.red
8
+ return
9
+ end
10
+ Commands.throttle([0],comms)
11
+ end
12
+ end
@@ -15,18 +15,22 @@ module Commands
15
15
  return
16
16
  end
17
17
 
18
- speed = args[0].to_i
18
+ if args.is_a?(Hash)
19
+ speed = args['speed']
20
+ direction = args['direction']
21
+ else
22
+ speed, direction = args
23
+ end
24
+
25
+ speed = speed.to_i
19
26
  if speed < 0 or speed > 10
20
27
  puts 'Please select a speed between 0 and 10'.red
21
28
  return
22
29
  end
23
- direction = nil
24
- if args.length > 1
25
- direction = args[1]
26
- unless ['forward','reverse'].include?(direction)
27
- puts 'Direction must be either "forward" or "reverse"'.red
28
- return
29
- end
30
+
31
+ unless direction.nil? || ['forward','reverse'].include?(direction)
32
+ puts 'Direction must be either "forward" or "reverse"'.red
33
+ return
30
34
  end
31
35
 
32
36
  message = {
@@ -2,7 +2,6 @@ require 'websocket-client-simple'
2
2
  require 'json'
3
3
  require 'colorize'
4
4
  require 'bolt_train_runner/conf'
5
- require 'pry-byebug'
6
5
 
7
6
  # Sending and receiving responses is a little bit funky
8
7
  # Since we have to receive messages asynchronously, and because
@@ -18,7 +17,6 @@ class Comms
18
17
  @heartbeat_thread = nil
19
18
  @consumer_thread = nil
20
19
  @kill_threads = false
21
- @queue = []
22
20
 
23
21
  def initialize(server)
24
22
  debug = Conf.debug
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+
3
+ # Load all commands
4
+ Dir[File.join(File.absolute_path(__dir__) + '/bolt_train_runner/commands') + "/**/*.rb"].each do |file|
5
+ require file
6
+ end
7
+
8
+ # This class manages a thread which monitors a directory that the bolt-train-api will
9
+ # place session files in. As these files show up, the thread should read them and
10
+ # issue the commands, delete the session file, then move to the next oldest file in the directory.
11
+
12
+ class SessionRunner
13
+
14
+ @session_thread = nil
15
+ @kill_thread = false
16
+
17
+ def initialize(comms, session_dir)
18
+ raise 'comms must not be nil' if comms.nil?
19
+ raise 'session_dir must not be nil' if session_dir.nil?
20
+ raise 'session_dir does not exist' unless File.exist?(session_dir)
21
+
22
+ @session_dir = session_dir
23
+ @comms = comms
24
+ end
25
+
26
+ def start
27
+ @session_thread = Thread.new { run_thread }
28
+ end
29
+
30
+ def stop
31
+ @kill_thread = true
32
+ puts 'Stopping Session Runner'.magenta
33
+ @session_thread.join if @session_thread
34
+ end
35
+
36
+ def run_thread
37
+ while !@kill_thread
38
+ files = Dir["#{@session_dir}/*.json"].sort_by { |f| File.mtime(f) }
39
+ files.each do |f|
40
+ data = JSON.parse(File.read(f))
41
+ session = data['session']
42
+ email = session['email']
43
+ puts "[Session Runner] Starting session for #{email}".magenta
44
+ commands = session['commands']
45
+ commands.each do |args|
46
+ c = args['command']
47
+ args.delete('command')
48
+ puts "[Session Runner] Sending command #{c}".magenta
49
+ puts "[Session Runner] Arguments = #{args}".magenta
50
+ Commands.send(c, args, @comms)
51
+ end
52
+ File.delete(f)
53
+ puts "[Session Runner] Session for #{email} complete".magenta
54
+ end
55
+ end
56
+ end
57
+
58
+ end
@@ -1,3 +1,3 @@
1
1
  module BoltTrainRunner
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bolt_train_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Burgan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-15 00:00:00.000000000 Z
11
+ date: 2019-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,9 +86,12 @@ files:
86
86
  - lib/bolt_train_runner/commands/exit.rb
87
87
  - lib/bolt_train_runner/commands/move.rb
88
88
  - lib/bolt_train_runner/commands/power.rb
89
+ - lib/bolt_train_runner/commands/sessions.rb
90
+ - lib/bolt_train_runner/commands/stop.rb
89
91
  - lib/bolt_train_runner/commands/throttle.rb
90
92
  - lib/bolt_train_runner/comms.rb
91
93
  - lib/bolt_train_runner/conf.rb
94
+ - lib/bolt_train_runner/session_runner.rb
92
95
  - lib/bolt_train_runner/version.rb
93
96
  homepage: https://github.com/puppetlabs/bolt-train-runner
94
97
  licenses: