jester 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2fde9b0f05e8ad06038522f9f3224be1960745ca
4
+ data.tar.gz: 77fcfb3eea73ce17961bc9f8220b30e49a90c0a8
5
+ SHA512:
6
+ metadata.gz: 1286e5056bbd3aea98c01a42c998d5ffe710239309fa87112de7163857f3b2ffd142cccc188d88a25be73f1edbfa18e6b81f45d7373bcf3e0938f4f6befce7d0
7
+ data.tar.gz: ecb2aef7079f752437a81f3d763aaaef5792af56c50a379c6e4af52fe9e982132e3e958297a0c9ee0bda4e7db56cf3bc7cd53f31b00a0e70ebec1a8dfa145a6b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ **/*.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.2
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.1
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.11.2
6
+ script:
7
+ - rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jester.gemspec
4
+ gemspec
data/Jenkinsfile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env groovy
2
+
3
+ // this is just a sample Jenkins pipeline script for testing, not for building this project!
4
+
5
+ node {
6
+ print "hello world!"
7
+ print "binding.variables:"
8
+ binding.variables.each{
9
+ print it.key
10
+ print it.value
11
+ }
12
+ }
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 adampats
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Jester
2
+
3
+ [![Build Status](https://travis-ci.org/adampats/jester.svg?branch=master)](https://travis-ci.org/adampats/jester)
4
+
5
+ A command line tool to enable more seamless "local" development of Jenkinsfile groovy pipelines without having to do the typical "commit, push, webhook, pipeline job run" to iterate on a pipeline.
6
+
7
+ Instead, you can use jester, which performs the following:
8
+
9
+ * Creates (or uses existing) pipeline job on Jenkins master
10
+ * Updates the pipeline job's value of the pipeline script with a custom string or local Jenkinsfile
11
+ * Runs the pipeline job with the newly modified script
12
+ * Fetches the job console output and saves it to a local file for review
13
+
14
+ All of this is done from the command line - logging in to the Jenkins UI *not* required.
15
+
16
+ -----
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'jester'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install jester
33
+
34
+ -----
35
+
36
+ ## Usage
37
+
38
+ If you don't have a Jenkins instance running, you can run one easily in Docker:
39
+
40
+ ```
41
+ docker run -d -p 8080:8080 --name localjenkins jenkins/jenkins:lts
42
+ docker logs localjenkins
43
+ open http://localhost:8080
44
+ # perform basic setup wizard steps to bring the new Jenkins master online
45
+ ```
46
+
47
+ Then you should be able to run jester using the defaults for `-s` / `--url`.
48
+
49
+ -----
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/adampats/jester.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jester"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start
data/bin/jester ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'jester'
5
+
6
+ Jester::Cli.start(ARGV)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/jester.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jester/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jester"
8
+ spec.version = Jester::VERSION
9
+ spec.authors = ["adampats"]
10
+ spec.email = ["adampatterson@protonmail.com"]
11
+
12
+ spec.summary = %q{Jenkins pipeline testing iteration tool}
13
+ spec.description = %q{jester makes testing Jenkinsfiles much easier.}
14
+ spec.homepage = "https://github.com/adampats/jester"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency 'thor', "~> 0.20.0"
31
+ spec.add_dependency 'faraday', "~> 0.14.0"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.11"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "pry", "~> 0.11.2"
37
+ end
data/lib/jester/cli.rb ADDED
@@ -0,0 +1,219 @@
1
+ require 'thor'
2
+ require 'pry'
3
+ require 'faraday'
4
+ require 'json'
5
+ require 'uri'
6
+
7
+ module Jester
8
+ class Cli < Thor
9
+
10
+ # Constants
11
+ JOB_RETRY = 2 # seconds to wait between checking build result
12
+
13
+ # Global options
14
+ class_option :url, desc: "URL of Jenkins master",
15
+ aliases: '-s', default: "http://localhost:8080"
16
+ class_option :username, desc: "User to connect with",
17
+ aliases: '-u', default: "admin"
18
+ class_option :password, desc: "Password to connect with",
19
+ aliases: '-p', default: "admin"
20
+ class_option :verbose, desc: "Toggle verbose/debug output",
21
+ aliases: '-v', default: false, type: :boolean
22
+
23
+ #
24
+ desc "test", "Test Jenkins server connectivity"
25
+ def test
26
+ puts "Testing authenticated connectivity to #{@options[:url]}..."
27
+ r = get( @options[:url] )
28
+ version = r['x-jenkins']
29
+ if version.nil?
30
+ puts "Fail"
31
+ else
32
+ puts "Success! Running Jenkins version " + version
33
+ end
34
+ end
35
+
36
+ #
37
+ desc "new", "Create new Jenkins pipeline job"
38
+ method_option :job_name, desc: "Pipeline job name",
39
+ aliases: '-j', default: 'jester-test-job'
40
+ def new
41
+ puts "Checking if job, '#{@options[:job_name]}', already exists..."
42
+ if job_exists?(@options[:job_name])
43
+ puts "Job already exists! Quit."
44
+ else
45
+ puts "Creating new pipeline job named #{@options[:job_name]}..."
46
+ job_params = {
47
+ description: @options[:job_name],
48
+ script: '// empty job created by jester\n node {print "test"}' }
49
+ xml = pipeline_xml(job_params)
50
+ r = post( @options[:url] + "/createItem?name=#{@options[:job_name]}", xml )
51
+ if r.status == 200
52
+ puts "Job successfully created."
53
+ else
54
+ puts "Job creation failed."
55
+ end
56
+ end
57
+ end
58
+
59
+ #
60
+ desc "build", "Build (run) a Jenkins pipeline job"
61
+ method_option :job_name, desc: "Pipeline job name",
62
+ aliases: '-j', default: 'jester-test-job'
63
+ method_option :pipeline_file, desc: "Path to ad-hoc pipeline (Jenkinsfile) file",
64
+ aliases: '-f', default: 'Jenkinsfile'
65
+ def build
66
+ if File.file?(@options[:pipeline_file])
67
+ pipeline = File.read(@options[:pipeline_file])
68
+ else
69
+ puts "File not found: " + @options[:pipeline_file]
70
+ raise 'FileNotFound'
71
+ end
72
+ job_params = {
73
+ description: @options[:job_name],
74
+ script: pipeline }
75
+ xml = pipeline_xml(job_params)
76
+
77
+ if job_exists?(@options[:job_name])
78
+ path = "/job/" + @options[:job_name] + "/config.xml"
79
+ else
80
+ path = "/createItem?name=" + @options[:job_name]
81
+ end
82
+ r = post( @options[:url] + path, xml )
83
+ if r.status != 200
84
+ puts "Job config update failed."
85
+ quit
86
+ else
87
+ puts "Job config update succeeded."
88
+ end
89
+
90
+ build_path = "/job/" + @options[:job_name] + "/build?delay=0sec"
91
+ build_resp = post( @options[:url] + build_path )
92
+ if build_resp.status != 201
93
+ puts "Unable to run build. Quit"
94
+ quit
95
+ else
96
+ puts "Build running - getting output..."
97
+ end
98
+ resp = get(@options[:url] + "/job/" + @options[:job_name] + "/api/json")
99
+ json = JSON.parse(resp.body)
100
+ if json['inQueue'] == false
101
+ build_num = json['lastBuild']['number']
102
+ end
103
+
104
+ build = build_result(@options[:job_name], build_num)
105
+ puts "Job " + build_num.to_s + " result: " + build['result']
106
+ log = log_result(@options[:job_name], build_num)
107
+ puts "DEBUG: " + log.body if debug
108
+ File.write("#{@options[:job_name]}.log", log.body)
109
+ puts "See #{@options[:job_name]}.log for output."
110
+ end
111
+
112
+
113
+ private
114
+
115
+ #
116
+ def debug
117
+ @options[:verbose]
118
+ end
119
+
120
+ #
121
+ def get (url, params = {})
122
+ begin
123
+ c = Faraday.new(url: url) do |conn|
124
+ conn.basic_auth(@options[:username], @options[:password])
125
+ conn.adapter Faraday.default_adapter
126
+ end
127
+ resp = c.get
128
+ rescue Exception => e
129
+ puts e.message
130
+ return e
131
+ end
132
+ end
133
+
134
+ #
135
+ def post (url, body = nil, params = {})
136
+ begin
137
+ crumb = get_crumb(url, @options[:username], @options[:password])
138
+ puts "DEBUG: crumb = " + crumb if @options[:verbose]
139
+ c = Faraday.new(url: url) do |conn|
140
+ conn.basic_auth(@options[:username], @options[:password])
141
+ conn.adapter Faraday.default_adapter
142
+ end
143
+ resp = c.post do |conn|
144
+ conn.headers['Jenkins-Crumb'] = crumb
145
+ conn.headers['Content-Type'] = 'application/xml'
146
+ if body != nil
147
+ conn.body = body
148
+ end
149
+ end
150
+ if @options[:verbose]
151
+ puts "DEBUG: "
152
+ pp resp.to_hash[:response_headers]
153
+ end
154
+ return resp
155
+ rescue Exception => e
156
+ puts e.message
157
+ return e
158
+ end
159
+ end
160
+
161
+ #
162
+ def get_crumb (url, user, pass)
163
+ p_url = URI.parse(url)
164
+ base_url = p_url.scheme + "://" + p_url.host + ":" + p_url.port.to_s
165
+ r = get(base_url +
166
+ '/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' )
167
+ r.body.split(':')[1]
168
+ end
169
+
170
+ #
171
+ # params = { description, script }
172
+ def pipeline_xml (params = {})
173
+ return %Q{<?xml version='1.1' encoding='UTF-8'?>
174
+ <flow-definition plugin="workflow-job@2.17">
175
+ <description>#{params[:description]}</description>
176
+ <keepDependencies>false</keepDependencies>
177
+ <properties/>
178
+ <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.46">
179
+ <script>#{params[:script]}</script>
180
+ <sandbox>true</sandbox>
181
+ </definition>
182
+ <triggers/>
183
+ <disabled>false</disabled>
184
+ </flow-definition>}
185
+ end
186
+
187
+ #
188
+ def job_exists? (job_name)
189
+ job = get( @options[:url] + "/job/" + job_name )
190
+ if job.reason_phrase == "Found"
191
+ true
192
+ else
193
+ false
194
+ end
195
+ end
196
+
197
+ #
198
+ def build_result (job_name, build_number)
199
+ result = nil
200
+ while result.nil?
201
+ resp = JSON.parse(
202
+ get( @options[:url] +
203
+ "/job/" + job_name + "/" + build_number.to_s + "/api/json").body )
204
+ if resp['building'] == true || resp['result'].nil?
205
+ sleep JOB_RETRY
206
+ print "." if debug
207
+ end
208
+ result = resp['result']
209
+ end
210
+ resp
211
+ end
212
+
213
+ #
214
+ def log_result (job_name, build_number)
215
+ resp = get( @options[:url] + "/job/" + job_name + "/" + build_number.to_s + "/consoleText")
216
+ end
217
+
218
+ end
219
+ end
@@ -0,0 +1,3 @@
1
+ module Jester
2
+ VERSION = "0.1.0"
3
+ end
data/lib/jester.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "jester/version"
2
+ require "jester/cli"
3
+
4
+ module Jester
5
+ end
data/temp/config.xml ADDED
@@ -0,0 +1,14 @@
1
+ <?xml version='1.1' encoding='UTF-8'?>
2
+ <flow-definition plugin="workflow-job@2.17">
3
+ <description>test</description>
4
+ <keepDependencies>false</keepDependencies>
5
+ <properties/>
6
+ <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.46">
7
+ <script>node {
8
+ print &quot;hello world&quot;
9
+ }</script>
10
+ <sandbox>true</sandbox>
11
+ </definition>
12
+ <triggers/>
13
+ <disabled>false</disabled>
14
+ </flow-definition>
@@ -0,0 +1,12 @@
1
+ <?xml version='1.1' encoding='UTF-8'?>
2
+ <flow-definition plugin="workflow-job@2.17">
3
+ <description>jester-test-job</description>
4
+ <keepDependencies>false</keepDependencies>
5
+ <properties/>
6
+ <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.46">
7
+ <script>node { print 'hello world!' }</script>
8
+ <sandbox>true</sandbox>
9
+ </definition>
10
+ <triggers/>
11
+ <disabled>false</disabled>
12
+ </flow-definition>
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - adampats
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.11.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.11.2
97
+ description: jester makes testing Jenkinsfiles much easier.
98
+ email:
99
+ - adampatterson@protonmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-version"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Jenkinsfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/jester
115
+ - bin/setup
116
+ - jester.gemspec
117
+ - lib/jester.rb
118
+ - lib/jester/cli.rb
119
+ - lib/jester/version.rb
120
+ - temp/config.xml
121
+ - temp/test_config.xml
122
+ homepage: https://github.com/adampats/jester
123
+ licenses:
124
+ - MIT
125
+ metadata:
126
+ allowed_push_host: https://rubygems.org
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.6.13
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Jenkins pipeline testing iteration tool
147
+ test_files: []