mock_code_manager 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a409639c1ba2eb5a0a62fe0a1d65e05754a031e8
4
- data.tar.gz: 55e220bbe78b5eb19defef9075d3f53f3cbae9d5
2
+ SHA256:
3
+ metadata.gz: 2c2ec4291712db35ad659a36a6025c3855a64d64699694018f8d02f46fda567d
4
+ data.tar.gz: 4a64b7589f02e67b080aa8fdac119d6d75fa18455692a562268c9470d5e45559
5
5
  SHA512:
6
- metadata.gz: be5e30fe1d7f712d9d155980eb9e5126b5e50c1d4aed9072f584cf320ba2fe98283e1add7be687747a6e8e9f049be13e0b9c9dec9b453c6c351da9970fdc409d
7
- data.tar.gz: 3b7ebf2c08c6f63bc53df858a1c3e478cded59289d5abcdefd082af8d67dd96d11d2d4913c450dc66e03feab76a91d88cc37f69e645cb1a2be430152296104ae
6
+ metadata.gz: d5d371549963a5b22200029ef4fe6225cad173cc7324e8f3931f217d63e92804736bad9a69553141193df30c4d49999701cacbd0c4e56d92df7d723ada021a62
7
+ data.tar.gz: 7551d25e5019a153cf3b51d95e20c0fae342a888cc483c0d34307192d0da8e8f384876b4593e4b0cbc249090baa3dc999abb2b55133289b0375f7f7d0e718245
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .idea
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /Gemfile.lock
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # MockCodeManager
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mock_code_manager`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This is a limited mock version of the [Puppet Code Manager REST API](https://docs.puppet.com/pe/latest/code_mgr_scripts.html).
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ It exists to allow Integration testing of software consuming this service without the need to stand-up a complete working Puppet Enterprise environment
6
6
 
7
7
  ## Installation
8
8
 
@@ -20,16 +20,59 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install mock_code_manager
22
22
 
23
+ ## Requirements
24
+
25
+ * Ruby 2.3+ (use [RVM](https://rvm.io/))
26
+ * openssl
27
+ * Access to `/tmp/mock_code_manager_ssl` for credential storage
28
+ * Hostname as reported by `hostname -f` **must** match the fqdn the service will be accessed from
29
+ * Port 8170 my be avaiable and reachable by the outside world
30
+
23
31
  ## Usage
24
32
 
25
- TODO: Write usage instructions here
33
+ ### Simple
26
34
 
27
- ## Development
35
+ To generate all required SSL credentials and start a server on port 8170, run the command:
28
36
 
29
- 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.
37
+ ```shell
38
+ mock_code_manager
39
+ ```
30
40
 
31
- 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).
41
+ Note that this service will terminate when your login shell exits. To prevent this, use `screen`, `nohup`, etc.
32
42
 
33
- ## Contributing
43
+ ### Ruby
44
+
45
+ You can use the library as part of your ruby projects if you like. Something like:
46
+
47
+ ```ruby
48
+ Thread.start { MockCodeManager::WEBrick.run! }
49
+ ```
50
+
51
+ In your `spec_helper.rb` along with a delay/test while the credentials are generated should be enough to get you started.
52
+
53
+ ## API completeness
34
54
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mock_code_manager.
55
+ `/code-manager/v1/deploys`
56
+ * Authentication check
57
+ * Deploy all environments (`deploy-all`)
58
+ * Deploy selected environments (`environments`)
59
+
60
+ ## Authentication
61
+ You must pass authentication tokens using the `X-Authentication` HTTP header.
62
+
63
+ The service will return an appropriate response based on the value or absence of this header:
64
+
65
+ ### `PUPPET_DEPLOY_OK`
66
+ Proceed to service request
67
+
68
+ ### Absent
69
+ `puppetlabs.rbac/user-unauthenticated`
70
+
71
+
72
+ ### `PUPPET_DEPLOY_FAIL` (or any other value)
73
+ `puppetlabs.rbac/token-revoked`
74
+
75
+ Note that the above responses are **always** expressed as JSON and always with status `200`.
76
+
77
+ ## Contributing
78
+ PRs accepted :)
@@ -1,3 +1,3 @@
1
1
  module MockCodeManager
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -21,39 +21,120 @@ require 'webrick/log'
21
21
  require "webrick/https"
22
22
  require 'rack'
23
23
  require "json"
24
-
24
+ require "rugged"
25
25
 
26
26
  module MockCodeManager
27
27
  RES_DIR = File.join(File.dirname(File.expand_path(__FILE__)), "..", "res")
28
28
  SSL_DIR = "/tmp/mock_code_manager_ssl"
29
29
 
30
30
  TOKEN_OK = "PUPPET_DEPLOY_OK"
31
- TOKEN_ALWAYS_FAIL = "PUPPET_DEPLOY_FAIL"
32
- JSON_OK=[{"environment"=>"production", "id"=>66, "status"=>"queued"}]
31
+
33
32
  JSON_BAD_TOKEN={"kind"=>"puppetlabs.rbac/token-revoked", "msg"=>"Authentication token has been revoked."}
34
33
  JSON_MISSING_TOKEN={"kind"=>"puppetlabs.rbac/user-unauthenticated", "msg"=>"Route requires authentication"}
35
34
  JSON_NO_ENVIRONMENTS_SPECIFIED=[]
35
+ TEST_REPO="https://github.com/geoffwilliams/puppet-control-test"
36
+ JUNK_ID="deadbeefdeadbeef"
37
+ IDOK=66
38
+ STATUS_QUEUED = "queued"
39
+ STATUS_COMPLETE = "complete"
40
+ STATUS_FAILED = "failed"
36
41
 
37
42
  class MockCodeManager < Sinatra::Base
43
+ def get_all_branches
44
+ branches = {}
45
+ output = %x(git ls-remote #{TEST_REPO})
46
+ output.split("\n").each do |line|
47
+ line_split = line.split(/\t/)
48
+ if line_split.length == 2
49
+ environment = line_split[1].split(/\//)[-1]
50
+ revision = line_split[0]
51
+ branches[environment] = revision
52
+ else
53
+ puts "skipped invalid input: #{line}"
54
+ end
55
+ end
56
+
57
+ return branches
58
+ end
59
+
60
+ def get_deploy_all_json(wait)
61
+ get_deploy_json(get_all_branches.keys.sort, wait)
62
+ end
63
+
64
+ def get_deploy_json(requested_branches, wait)
65
+ json = []
66
+ branches = get_all_branches
67
+ id = IDOK
68
+ requested_branches.each { |requested_branch|
69
+ if branches.has_key? requested_branch
70
+ if wait
71
+ # in wait mode, sleep for a while to simulate a slow-ish server
72
+ sleep(1)
73
+ json << {
74
+ "environment"=>requested_branch,
75
+ "id"=>id,
76
+ "status"=>STATUS_COMPLETE,
77
+ "file-sync"=> {
78
+ "environment-commit"=>JUNK_ID,
79
+ "code-commit"=>JUNK_ID
80
+ },
81
+ "deploy-signature"=>branches[requested_branch]
82
+ }
83
+ else
84
+ json << {
85
+ "environment"=>requested_branch,
86
+ "id"=>id,
87
+ "status"=>STATUS_QUEUED
88
+ }
89
+ end
90
+ else
91
+ json << {
92
+ "environment"=>requested_branch,
93
+ "id"=>id,
94
+ "status"=>STATUS_FAILED,
95
+ "error"=>{
96
+ "kind"=>"puppetlabs.code-manager/deploy-failure",
97
+ "details"=>{
98
+ "corrected-env-name"=>requested_branch},
99
+ "msg"=>"Errors while deploying environment '#{requested_branch}' (exit code: 1):\nERROR\t -> Environment(s) '#{requested_branch}' cannot be found in any source and will not be deployed.\n"
100
+ }
101
+ }
102
+ end
103
+ id += 1
104
+ }
105
+
106
+ json
107
+ end
108
+
109
+ get '/' do
110
+ <<~"END"
111
+ <h1>Mock Code Manager</h1>
112
+ <p>
113
+ Mock control repository in use: <a href="#{TEST_REPO}">#{TEST_REPO}</a>
114
+ </p>
115
+ <pre>
116
+ token: #{TOKEN_OK}
117
+ environments: #{JSON.pretty_generate(get_all_branches)}
118
+ </pre>
119
+ END
120
+ end
121
+
38
122
  post '/code-manager/v1/deploys' do
39
123
  json = JSON.parse(request.body.read)
40
124
  token = request.env['HTTP_X_AUTHENTICATION']
125
+ wait = json.has_key?("wait") && json["wait"]
126
+ environments = json["environments"]
41
127
  if token
42
128
  if token == TOKEN_OK
43
129
 
44
130
  # environments must be a non-empty array for code manager to do anything.
45
131
  # If missing it returns an empty array []
46
132
  if json["deploy-all"] == true
47
- resp = JSON_OK
48
- elsif json["environments"].empty?
133
+ resp = self.get_deploy_all_json(wait)
134
+ elsif environments.empty?
49
135
  resp = JSON_NO_ENVIRONMENTS_SPECIFIED
50
136
  else
51
- resp = []
52
- i=66
53
- json["environments"].each { |environment|
54
- resp.push({"environment"=>environment, "id"=>i, "status"=>"queued"})
55
- i += 1
56
- }
137
+ resp = self.get_deploy_json(environments, wait)
57
138
  end
58
139
  else
59
140
  resp = JSON_BAD_TOKEN
@@ -11,6 +11,8 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{A mock version of Puppet Code Manager}
13
13
  spec.homepage = "https://github.com/GeoffWilliams/puppet-mock_code_manager"
14
+ spec.license = "Apache-2.0"
15
+
14
16
 
15
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
18
  f.match(%r{^(test|spec|features)/})
@@ -19,12 +21,14 @@ Gem::Specification.new do |spec|
19
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
22
  spec.require_paths = ["lib"]
21
23
 
22
- spec.add_development_dependency "bundler", "~> 1.15"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "bundler", "~> 2.0"
25
+ spec.add_development_dependency "rake", "~> 12.3"
26
+ spec.add_development_dependency "rspec", "~> 3.8"
27
+ spec.add_development_dependency "excon", "0.64.0"
25
28
 
26
- spec.add_dependency "sinatra", "1.4.8"
27
- spec.add_dependency "webrick", "1.3.1"
28
- spec.add_dependency "rack", "1.6.8"
29
+ spec.add_dependency "sinatra", "2.0.5"
30
+ spec.add_dependency "webrick", "1.4.2"
31
+ spec.add_dependency "rack", "2.0.7"
32
+ spec.add_dependency "rugged", "~> 0.28.2"
29
33
 
30
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_code_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-04 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,84 +16,112 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.15'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '3.8'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: excon
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.64.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.64.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: sinatra
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - '='
60
74
  - !ruby/object:Gem::Version
61
- version: 1.4.8
75
+ version: 2.0.5
62
76
  type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - '='
67
81
  - !ruby/object:Gem::Version
68
- version: 1.4.8
82
+ version: 2.0.5
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: webrick
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - '='
74
88
  - !ruby/object:Gem::Version
75
- version: 1.3.1
89
+ version: 1.4.2
76
90
  type: :runtime
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - '='
81
95
  - !ruby/object:Gem::Version
82
- version: 1.3.1
96
+ version: 1.4.2
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rack
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - '='
88
102
  - !ruby/object:Gem::Version
89
- version: 1.6.8
103
+ version: 2.0.7
90
104
  type: :runtime
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - '='
95
109
  - !ruby/object:Gem::Version
96
- version: 1.6.8
110
+ version: 2.0.7
111
+ - !ruby/object:Gem::Dependency
112
+ name: rugged
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.28.2
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.28.2
97
125
  description:
98
126
  email:
99
127
  - geoff@geoffwilliams.me.uk
@@ -103,9 +131,6 @@ extensions: []
103
131
  extra_rdoc_files: []
104
132
  files:
105
133
  - ".gitignore"
106
- - ".idea/misc.xml"
107
- - ".idea/modules.xml"
108
- - ".idea/vcs.xml"
109
134
  - ".rspec"
110
135
  - ".travis.yml"
111
136
  - Gemfile
@@ -120,7 +145,8 @@ files:
120
145
  - mock_code_manager.gemspec
121
146
  - res/setup_ssl.sh
122
147
  homepage: https://github.com/GeoffWilliams/puppet-mock_code_manager
123
- licenses: []
148
+ licenses:
149
+ - Apache-2.0
124
150
  metadata: {}
125
151
  post_install_message:
126
152
  rdoc_options: []
@@ -137,8 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
163
  - !ruby/object:Gem::Version
138
164
  version: '0'
139
165
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.5.2
166
+ rubygems_version: 3.0.3
142
167
  signing_key:
143
168
  specification_version: 4
144
169
  summary: A mock version of Puppet Code Manager
data/.idea/misc.xml DELETED
@@ -1,4 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 2.3.3" project-jdk-type="RUBY_SDK" />
4
- </project>
data/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/puppet-mock_code_manager.iml" filepath="$PROJECT_DIR$/.idea/puppet-mock_code_manager.iml" />
6
- </modules>
7
- </component>
8
- </project>
data/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>