-
Notifications
You must be signed in to change notification settings - Fork 28
/
ssh-session-cli
92 lines (79 loc) · 2.11 KB
/
ssh-session-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env ruby
require "json"
def bold(string)
"\e[33m#{string}\e[0m"
end
def load_commands(type)
setup_commands = JSON.parse(File.read("/var/run/commands/setup_commands"))
job_commands = JSON.parse(File.read("/var/run/commands/job_commands"))
post_job_commands = JSON.parse(File.read("/var/run/commands/post_job_commands"))
case type
when :setup
setup_commands
when :job
job_commands
when :post_job
post_job_commands
when :all
setup_commands + job_commands + post_job_commands
else
raise "Unrecognized command type: #{type}"
end
end
def display_help
puts "Semaphore CLI for SSH session."
puts ""
puts " #{bold "run:setup"} - Runs setup commands for this job"
puts " #{bold "run:job"} - Runs commands specific for this job"
puts " #{bold "run:post_job"} - Runs post_job commands for this job"
puts " #{bold "run:all"} - Runs all commands for this job"
puts ""
puts " #{bold "show:setup"} - Show setup commands for this job"
puts " #{bold "show:job"} - Show commands specific for this job"
puts " #{bold "show:post_job"} - Show post_job commands for this job"
puts " #{bold "show:all"} - Show all commands for this job"
puts ""
end
def show(type)
load_commands(type).each.with_index do |cmd, index|
puts "#{bold (index + 1).to_s.rjust(2)}: #{cmd["command_string"]}"
end
end
def run(type)
load_commands(type).each do |cmd|
puts bold("$ #{cmd["command_string"]}")
# we need to use login shell if we want 'nvm' and 'rvm' to be available
system("bash", "-c", "-l", cmd["command_string"])
status = $?.exitstatus
if status != 0
puts ""
puts bold("Command Failed. Exit Status: #{status}")
exit(status)
end
puts ""
end
end
if ARGV.size == 0
display_help
exit
end
case ARGV[0]
when "run:setup"
run(:setup)
when "run:job"
run(:job)
when "run:post_job"
run(:post_job)
when "run:all"
run(:all)
when "show:setup"
show(:setup)
when "show:job"
show(:job)
when "show:post_job"
show(:post_job)
when "show:all"
show(:all)
else
abort "[ERROR] Unrecognized command"
end