File tree 1 file changed +92
-0
lines changed 1 file changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ require "json"
4
+
5
+ def bold ( string )
6
+ "\e [33m#{ string } \e [0m"
7
+ end
8
+
9
+ def load_commands ( type )
10
+ setup_commands = JSON . parse ( File . read ( "/var/run/commands/setup_commands" ) )
11
+ job_commands = JSON . parse ( File . read ( "/var/run/commands/job_commands" ) )
12
+ post_job_commands = JSON . parse ( File . read ( "/var/run/commands/post_job_commands" ) )
13
+
14
+ case type
15
+ when :setup
16
+ setup_commands
17
+ when :job
18
+ job_commands
19
+ when :post_job
20
+ post_job_commands
21
+ when :all
22
+ setup_commands + job_commands + post_job_commands
23
+ else
24
+ raise "Unrecognized command type: #{ type } "
25
+ end
26
+ end
27
+
28
+ def display_help
29
+ puts "Semaphore CLI for SSH session."
30
+ puts ""
31
+ puts " #{ bold "run:setup" } - Runs setup commands for this job"
32
+ puts " #{ bold "run:job" } - Runs commands specific for this job"
33
+ puts " #{ bold "run:post_job" } - Runs post_job commands for this job"
34
+ puts " #{ bold "run:all" } - Runs all commands for this job"
35
+ puts ""
36
+ puts " #{ bold "show:setup" } - Show setup commands for this job"
37
+ puts " #{ bold "show:job" } - Show commands specific for this job"
38
+ puts " #{ bold "show:post_job" } - Show post_job commands for this job"
39
+ puts " #{ bold "show:all" } - Show all commands for this job"
40
+ puts ""
41
+ end
42
+
43
+ def show ( type )
44
+ load_commands ( type ) . each . with_index do |cmd , index |
45
+ puts "#{ bold ( index + 1 ) . to_s . rjust ( 2 ) } : #{ cmd [ "command_string" ] } "
46
+ end
47
+ end
48
+
49
+ def run ( type )
50
+ load_commands ( type ) . each do |cmd |
51
+ puts bold ( "$ #{ cmd [ "command_string" ] } " )
52
+
53
+ # we need to use login shell if we want 'nvm' and 'rvm' to be available
54
+ system ( "bash" , "-c" , "-l" , cmd [ "command_string" ] )
55
+
56
+ status = $?. exitstatus
57
+
58
+ if status != 0
59
+ puts ""
60
+ puts bold ( "Command Failed. Exit Status: #{ status } " )
61
+ exit ( status )
62
+ end
63
+
64
+ puts ""
65
+ end
66
+ end
67
+
68
+ if ARGV . size == 0
69
+ display_help
70
+ exit
71
+ end
72
+
73
+ case ARGV [ 0 ]
74
+ when "run:setup"
75
+ run ( :setup )
76
+ when "run:job"
77
+ run ( :job )
78
+ when "run:post_job"
79
+ run ( :post_job )
80
+ when "run:all"
81
+ run ( :all )
82
+ when "show:setup"
83
+ show ( :setup )
84
+ when "show:job"
85
+ show ( :job )
86
+ when "show:post_job"
87
+ show ( :post_job )
88
+ when "show:all"
89
+ show ( :all )
90
+ else
91
+ abort "[ERROR] Unrecognized command"
92
+ end
You can’t perform that action at this time.
0 commit comments