trent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd76ca7ec62253cf51678cf861497c9b7e994d2e
4
+ data.tar.gz: 494bf1d60e7bed0be13746a83102359fe5e1aa0d
5
+ SHA512:
6
+ metadata.gz: aedc27c0447919ba248ed0b883a2dc5d7f327422be206894dfdba9de9abdece1799e4096bd174cf1482bcb253725d5a4de06ffc26bdf9f2e763a4f89f48870c7
7
+ data.tar.gz: 618011c76aee3f36ae61019f7614de36145db8f1ce836d7bf1f30e1f4bf9dce772bbcf0577e5d3349f58969e56a0f0b4f09b63bee88514c89bf3f782c42f1f79
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ### [0.1.0] 2018-08-07
2
+
3
+ First release!
4
+
5
+ ### Added
6
+ - Ability to run local shell commands on Travis CI build.
7
+ - Ability to run remote ssh commands on Travis CI builds.
8
+ - Ability to send a comment on the GitHub pull request for the Travis CI build.
9
+ - Trent only works with Travis-CI at this time.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Levi Bostian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # Trent
2
+
3
+ Run and debug bash commands on Travis-CI much easier.
4
+
5
+ ...Yes, Trent is open to working with more then Travis-CI. At this time, Travis is what I use so it's what's supported. Make a pull request to add more.
6
+
7
+ ## What is Trent?
8
+
9
+ I have been using Travis-CI for a few years to build, test, and deploy my apps. Bash is great, but using a higher level language for interacting with Travis and the build machine would be very beneficial.
10
+
11
+ Trent is a convenient ruby gem that helps you execute system shell scripts with as little code *and pain* as possible.
12
+
13
+ ## How can Trent help you?
14
+
15
+ Have you encountered any of the following pains while working with Travis-CI?
16
+
17
+ 1. If you run shell commands from a bash *file*, Travis does not echo each command before it executes in the bash file.
18
+
19
+ ![](misc/example_3commands.png)
20
+
21
+ This makes it difficult to debug when things go wrong. Knowing what commands were executed would be very helpful.
22
+
23
+ 2. At times where I forget to set `set -e` in my bash script, commands that fail allow my Travis build to continue. Commands should fail by default to help keep Travis builds reliable.
24
+
25
+ 3. Some commands are ok if they fail. Make it easy to flag these commands as ok to fail.
26
+
27
+ 4. Bash is difficult to write and maintain if you do anything advanced. Parsing output from an executed command, for example, has always caused me trouble getting it to work. Using a high level language such as Python or Ruby is much easier.
28
+
29
+ 5. Misc tasks such as commenting on a GitHub pull request should be 1 line of code: `comment("Tests successful!")`.
30
+
31
+ # Getting started
32
+
33
+ * Install the gem via Bundler.
34
+
35
+ ```
36
+ $> bundle add trent
37
+ ```
38
+
39
+ * Create a new Ruby file for the scripts you want to run and create an instance of `Trent`.
40
+
41
+ ```
42
+ require 'trent'
43
+
44
+ ci = Trent.new()
45
+ ```
46
+
47
+ * Tell Travis to run your scripts. In your `.travis.yml` file, add `ruby name-of-file.rb` for a job.
48
+
49
+ Done! Continue reading for what you can do with Trent.
50
+
51
+ # Run local shell commmands
52
+
53
+ ```ruby
54
+ require 'trent'
55
+
56
+ ci = Trent.new()
57
+
58
+ result = ci.sh("echo 'foo'")
59
+
60
+ puts result
61
+ ```
62
+
63
+ When you run the script, you will see:
64
+
65
+ ```
66
+ echo 'foo'
67
+ foo
68
+ {:output=>"foo\n", :result=>true}
69
+ ```
70
+
71
+ Let's break this down.
72
+
73
+ * `echo 'foo'`: Trent always prints to the console each command that is evaluated to help you debug.
74
+ * `foo`: The output of the command that is executed. This includes both STDOUT and STDERR.
75
+ * `{:output=>"foo\n", :result=>true} `: The result of `ci.sh()` returns a Hash with the output string you can parse if you wish and the result of the command. `true` for exit status code 0 and `false` otherwise.
76
+
77
+ ### Advanced tips for running local shell commands:
78
+
79
+ ```ruby
80
+ # Allow a command to fail and not fail the Travis build.
81
+ ci.sh("false", :fail_non_success => false)
82
+ ```
83
+
84
+ # Run remote SSH shell commmands
85
+
86
+ ```ruby
87
+ require 'trent'
88
+
89
+ ci = Trent.new()
90
+ ci.config_ssh('username', '111.111.234.332')
91
+
92
+ result = ci.ssh("echo 'foo'")
93
+
94
+ puts result
95
+ ```
96
+
97
+ When you run the script, you will see:
98
+
99
+ ```
100
+ echo 'foo'
101
+ foo
102
+ {:output=>"foo\n", :result=>true}
103
+ ```
104
+
105
+ Let's break this down.
106
+
107
+ * `echo 'foo'`: Trent always prints to the console each command that is evaluated to help you debug.
108
+ * `foo`: The output of the command that is executed. This includes both STDOUT and STDERR.
109
+ * `{:output=>"foo\n", :result=>true} `: The result of `ci.ssh()` returns a Hash with the output string you can parse if you wish and the result of the command. `true` for exit status code 0 and `false` otherwise.
110
+
111
+ ### Advanced tips for running remote SSH shell commands
112
+
113
+ ```ruby
114
+ # Allow a command to fail and not fail the Travis build.
115
+ ci.ssh("false", :fail_non_success => false)
116
+ ```
117
+
118
+ # GitHub
119
+
120
+ Trent comes with some handy commands that allow you to work with Travis easily. This would include interacting with GitHub.
121
+
122
+ ### Comment on a pull request
123
+
124
+ ```ruby
125
+ require 'trent'
126
+
127
+ ci = Trent.new()
128
+ # Set GitHub private access key to authenticate user account.
129
+ # Here is a guide on how to create tokens:
130
+ # https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/#creating-a-token
131
+ # Tokens for OSS Projects
132
+ # We recommend giving the token the smallest scope possible. This means just public_repo.
133
+ # Tokens for Closed Source Projects
134
+ # We recommend giving access to the whole repo scope, and its children.
135
+ ci.config_github('wfoweifwoeifjweoijwefowefweoif')
136
+
137
+ ci.github.comment("Tests pass!") # Send comment to pull request for the Travis build. The pull request information is automatically retrieved from the Travis virtual machine the build is running on.
138
+ ```
139
+
140
+ # Advanced Trent configuration
141
+
142
+ ```ruby
143
+ # Set the color Trent uses to print the command about to run to the console.
144
+ # Available colors are located here: https://github.com/fazibear/colorize/blob/master/lib/colorize/class_methods.rb#L61
145
+ ci = Trent.new(:green)
146
+ ```
147
+
148
+ ## Author
149
+
150
+ * Levi Bostian - [GitHub](https://github.com/levibostian), [Twitter](https://twitter.com/levibostian), [Website/blog](http://levibostian.com)
151
+
152
+ ![Levi Bostian image](https://gravatar.com/avatar/22355580305146b21508c74ff6b44bc5?s=250)
153
+
154
+ ## Contribute
155
+
156
+ Trent is open for pull requests. Check out the [list of issues](https://github.com/levibostian/trent/issues) for tasks I am planning on working on. Check them out if you wish to contribute in that way.
157
+
158
+ **Want to add features to Trent?** Before you decide to take a bunch of time and add functionality to the library, please, [create an issue](https://github.com/levibostian/trent/issues/new) stating what you wish to add. This might save you some time in case your purpose does not fit well in the use cases of Trent.
159
+
160
+ ### Building Trent
161
+
162
+ ```
163
+ $> gem build trent.gemspec
164
+ $> gem install --dev ./trent-X.X.X.gem
165
+ ```
166
+
167
+ This will install all dependencies including for development. You are ready to write some code.
168
+
169
+ While working on Trent, make sure to lint it:
170
+
171
+ ```
172
+ $> rubocop --auto-correct
173
+ ```
174
+
175
+ To test out Trent on your machine:
176
+
177
+ ```
178
+ $> gem build trent.gemspec
179
+ $> gem install ./trent-X.X.X.gem
180
+ ```
181
+
182
+ Then, you can use it on your own machine using `require 'trent'` in your ruby scripts.
183
+
184
+ ### Where did the name come from?
185
+
186
+ I built this gem to work well with Travis-CI. I simply looked up similar names as "Travis". I liked how "Travis" and "Trent" share the same beginning couple of letters.
@@ -0,0 +1,25 @@
1
+ require 'open3'
2
+
3
+ # Run bash commands locally on machine.
4
+ class Sh
5
+ def initialize; end
6
+
7
+ # Run a bash command locally.
8
+ def run(command)
9
+ output = ''
10
+ exit_status = nil
11
+ Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
12
+ stdin.close
13
+ stdout.each_line do |line|
14
+ puts line
15
+ output += line
16
+ end
17
+ stderr.each_line do |line|
18
+ puts line
19
+ output += line
20
+ end
21
+ exit_status = wait_thr.value
22
+ return { output: output, result: exit_status.success? }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ require 'net/ssh'
2
+
3
+ # Run SSH commands remotely on a server.
4
+ class SSH
5
+ def initialize(username, host, password = nil)
6
+ @username = username
7
+ @host = host
8
+ @password = password
9
+ end
10
+
11
+ # Run the SSH command on the server
12
+ def run(command)
13
+ Net::SSH.start(@host, @username, password: @password) do |ssh|
14
+ output = ''
15
+ exit_code = nil
16
+ ssh.open_channel do |channel|
17
+ channel.exec(command) do |_ch, success|
18
+ abort "FAILED: Couldn't execute command #{command}" unless success
19
+
20
+ channel.on_data do |_ch, data|
21
+ puts data
22
+ output += data
23
+ end
24
+
25
+ channel.on_extended_data do |_ch, _type, data|
26
+ puts data
27
+ output += data
28
+ end
29
+
30
+ channel.on_request('exit-status') { |_ch, data| exit_code = data.read_long }
31
+ end
32
+ end
33
+ ssh.loop
34
+ return { output: output, result: exit_code <= 0 }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require_relative '../log'
4
+
5
+ # Functions to run on GitHub
6
+ class GitHub
7
+ def initialize(api_key)
8
+ @api_key = api_key
9
+ end
10
+
11
+ # Comment on Pull Request
12
+ def comment(message)
13
+ result = comment_on_pull_request(message)
14
+
15
+ if !result[:successful]
16
+ puts "Status code: #{result[:status_code]}"
17
+ puts "Body: #{result[:body]}"
18
+ Log.fatal('Commenting on GitHub pull request failed.')
19
+ else
20
+ Log.success('Successfully commented on GitHub pull request.')
21
+ end
22
+ end
23
+
24
+ def comment_on_pull_request(message)
25
+ puts "Commenting on GitHub pull request: #{ENV['TRAVIS_REPO_SLUG']}/#{ENV['TRAVIS_PULL_REQUEST']}"
26
+ uri = URI("https://api.github.com/repos/#{ENV['TRAVIS_REPO_SLUG']}/issues/#{ENV['TRAVIS_PULL_REQUEST']}/comments")
27
+
28
+ req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
29
+ req['Authorization'] = "token #{@api_key}"
30
+ req.body = { body: message }.to_json
31
+
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+ http.use_ssl = true
34
+ res = http.request(req)
35
+ status_code = res.code.to_i
36
+ { status_code: status_code, body: res.body, successful: res.is_a?(Net::HTTPSuccess) && status_code < 400 }
37
+ end
38
+ end
data/lib/log.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'colorize'
2
+
3
+ # Convenient class to log to console in certain ways
4
+ class Log
5
+ # Fatal error. We should not move forward.
6
+ def self.fatal(message)
7
+ abort("FATAL: #{message}".colorize(:red))
8
+ end
9
+
10
+ # Warning we want to show to the user.
11
+ def self.warning(message)
12
+ puts "Warning: #{message}".colorize(:yellow)
13
+ end
14
+
15
+ def self.success(message)
16
+ puts "Success: #{message}".colorize(:green)
17
+ end
18
+ end
data/lib/trent.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'command/ssh/ssh'
2
+ require 'github/github'
3
+ require 'command/sh/sh'
4
+ require 'colorize'
5
+ require_relative './log'
6
+
7
+ # Communicate with all of the Trent library with this class.
8
+ class Trent
9
+ def initialize(color = :blue)
10
+ Log.fatal('Trent is designed to run on Travis-CI builds. Run it on Travis-CI.') unless ENV['HAS_JOSH_K_SEAL_OF_APPROVAL']
11
+
12
+ @color = color
13
+ @sh = Sh.new
14
+ end
15
+
16
+ # Configure how to run remote SSH commmands on server.
17
+ def config_ssh(username, host, password = nil)
18
+ @ssh = SSH.new(username, host, password: password)
19
+ end
20
+
21
+ # Configure how to communicate with GitHub
22
+ def config_github(api_key)
23
+ @github = GitHub.new(api_key)
24
+ end
25
+
26
+ ## Run ssh command
27
+ def ssh(command, fail_non_success = true)
28
+ Log.fatal('You did not configure SSH yet.') unless @ssh
29
+
30
+ puts command.colorize(@color)
31
+ result = @ssh.run(command)
32
+
33
+ Log.fatal('Command failed with a non 0 exit status.') if !result[:result] && fail_non_success
34
+
35
+ result
36
+ end
37
+
38
+ # Run local bash command
39
+ def sh(command, fail_non_success = true)
40
+ puts command.colorize(@color)
41
+
42
+ result = @sh.run(command)
43
+
44
+ unless result[:result]
45
+ if fail_non_success
46
+ Log.fatal('Command failed with a non 0 exit status.')
47
+ else
48
+ Log.warning('Command failed with a non 0 exit status.')
49
+ end
50
+ end
51
+
52
+ result
53
+ end
54
+
55
+ # Get instance of GitHub class to run commands against GitHub
56
+ def github
57
+ Log.fatal('You did not configure GitHub yet.') unless @github
58
+ @github
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Levi Bostian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-ssh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 5.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.58.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.58.2
55
+ description: "I have been using Travis-CI for a few years to build, test, and deploy
56
+ my apps. Bash is great, but using a higher level language for interacting with Travis
57
+ and the build machine would be very beneficial. \n\n Trent is a convenient ruby
58
+ gem that helps you execute system shell scripts with as little code *and pain* as
59
+ possible."
60
+ email: levi.bostian@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - CHANGELOG.md
66
+ - LICENSE
67
+ - README.md
68
+ - lib/command/sh/sh.rb
69
+ - lib/command/ssh/ssh.rb
70
+ - lib/github/github.rb
71
+ - lib/log.rb
72
+ - lib/trent.rb
73
+ homepage: http://github.com/levibostian/trent
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Run and debug bash commands on Travis-CI much easier.
97
+ test_files: []