foreman_envsync 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
+ SHA256:
3
+ metadata.gz: 43ba5621c980daec52589e93abf35284dae0b74b6702f17f119150d11eeb8213
4
+ data.tar.gz: a7c2fbe23fda6490e1c672bd25a4c29ff65cfc73025a0316178c710b153fda71
5
+ SHA512:
6
+ metadata.gz: 5eb9986dd85245db0d9887494455e2092f41581659d8db6ea0d78db3a4f1057aa8c0fb2971882e7450a7ee16ca2099ca58002038ad9a0d3f15479996364cc52d
7
+ data.tar.gz: 595fbeb07c6df54ef4153a406c2279b3775582ca0f6ad4646503e99f622fb0be5256f45378644ad5af14452d4e461199e0cd3778494bb047d843b5a6f2b1f72c
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.2
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-06-04
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in foreman_envsync.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Joshua Hoblitt
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,39 @@
1
+ # ForemanEnvsync
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/foreman_envsync`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'foreman_envsync'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install foreman_envsync
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/foreman_envsync.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "foreman_envsync"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,170 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require "open3"
6
+ require "optparse"
7
+ require "rest-client"
8
+ require "socket"
9
+ require "yaml"
10
+
11
+ @options = {}
12
+ option_parser = OptionParser.new do |opts|
13
+ opts.banner = "Usage: foreman_envsync [options]"
14
+ opts.separator ""
15
+ opts.separator "Specifc options:"
16
+
17
+ opts.on("-v", "--verbose", "Enable verbose output") do |o|
18
+ @options[:verbose] = [o]
19
+ end
20
+
21
+ opts.on_tail("-h", "--help", "Show this message") do
22
+ puts opts
23
+ exit
24
+ end
25
+ end
26
+ option_parser.parse!
27
+
28
+ def verbose_list(msg, items)
29
+ return unless @options[:verbose] && !items.nil?
30
+
31
+ printf(msg, items.count)
32
+ puts
33
+ puts "#{YAML.dump(items)}\n" unless items.empty?
34
+ end
35
+
36
+ def cert_file(file)
37
+ OpenSSL::X509::Certificate.new(File.read(file))
38
+ end
39
+
40
+ def key_file(file)
41
+ OpenSSL::PKey::RSA.new(File.read(file))
42
+ end
43
+
44
+ def hammer_cmd(cmd)
45
+ stdout, stderr, s = Open3.capture3(cmd)
46
+ unless s.success?
47
+ puts "command #{cmd} failed"
48
+ puts stderr
49
+ exit s.exitstatus
50
+ end
51
+
52
+ stdout
53
+ end
54
+
55
+ def hammer_cmd_parse(cmd)
56
+ stdout = hammer_cmd(cmd)
57
+
58
+ JSON.parse(stdout)
59
+ end
60
+
61
+ def collect_one_field(data, field)
62
+ # hammer output format is an array of hashes -- one hash per item
63
+ # convert it to a flat array
64
+ data.collect { |x| x[field] }
65
+ end
66
+
67
+ def hammer_cmd_parse_one(cmd, field)
68
+ collect_one_field(hammer_cmd_parse(cmd), field)
69
+ end
70
+
71
+ def foreman_env_list
72
+ field = "Name"
73
+ cmd = "hammer --output=json puppet-environment list --fields #{field}"
74
+
75
+ hammer_cmd_parse_one(cmd, field)
76
+ end
77
+
78
+ def foreman_env_delete(name)
79
+ cmd = "hammer --output=json puppet-environment delete --name #{name}"
80
+
81
+ hammer_cmd_parse(cmd)
82
+ end
83
+
84
+ def foreman_env_create(name, location_ids, org_ids)
85
+ cmd = "hammer --output=json puppet-environment create --name #{name}"
86
+ cmd += " --location-ids #{location_ids.join(",")}"
87
+ cmd += " --organization-ids #{org_ids.join(",")}"
88
+
89
+ hammer_cmd_parse(cmd)
90
+ end
91
+
92
+ def foreman_location_ids
93
+ field = "Id"
94
+ cmd = "hammer --output=json location list --fields #{field}"
95
+
96
+ hammer_cmd_parse_one(cmd, field)
97
+ end
98
+
99
+ def foreman_org_ids
100
+ field = "Id"
101
+ cmd = "hammer --output=json organization list --fields #{field}"
102
+
103
+ hammer_cmd_parse_one(cmd, field)
104
+ end
105
+
106
+ def puppetserver_env_list
107
+ hostname = Socket.gethostname
108
+
109
+ res = RestClient::Request.execute(
110
+ method: :get,
111
+ url: "https://#{hostname}:8140/puppet/v3/environments",
112
+ ssl_client_cert: cert_file("/etc/puppetlabs/puppet/ssl/certs/#{hostname}.pem"),
113
+ ssl_client_key: key_file("/etc/puppetlabs/puppet/ssl/private_keys/#{hostname}.pem"),
114
+ verify_ssl: true,
115
+ ssl_ca_file: "/etc/puppetlabs/puppet/ssl/ca/ca_crt.pem"
116
+ )
117
+
118
+ JSON.parse(res)["environments"].keys
119
+ end
120
+
121
+ #
122
+ # Fetch list of puppet environments from puppetserver API.
123
+ #
124
+ ps_envs = puppetserver_env_list
125
+ verbose_list "found %d puppetserver environment(s).", ps_envs
126
+
127
+ #
128
+ # Fetch list of puppet environments from foreman. The hammer cli is used to
129
+ # avoid having to manage credentials. In theory, foreman supports auth using
130
+ # x509 similar to puppetserver but this failed when tested using both `curl` and
131
+ # configuring hammer to use x509.
132
+ #
133
+ f_envs = foreman_env_list
134
+ verbose_list "found %d foreman environment(s).", f_envs
135
+
136
+ #
137
+ # Does foreman have any puppet envs puppetserver is unaware of?
138
+ #
139
+ extra_envs = f_envs - ps_envs
140
+ verbose_list "found %d foreman environment(s) unknown to puppetserver.", extra_envs
141
+
142
+ #
143
+ # Remove any foreman envs unknown to puppetserver
144
+ #
145
+ report = extra_envs.collect { |x| foreman_env_delete(x) } unless extra_envs.empty?
146
+ verbose_list "deleted %d foreman environment(s).", report
147
+
148
+ # update foreman envs if anything was deleted
149
+ f_envs = foreman_env_list unless report.nil?
150
+
151
+ #
152
+ # Does puppetserver have any envs foreman is unaware of?
153
+ #
154
+ new_envs = ps_envs - f_envs
155
+ verbose_list "found %d puppetserver environment(s) unknown to foreman.", new_envs
156
+
157
+ # if not, exit
158
+ exit 0 if new_envs.empty?
159
+
160
+ #
161
+ # Create new foreman env(s) with all existing locations and organizations
162
+ #
163
+ location_ids = foreman_location_ids
164
+ verbose_list "found %d foreman location(s).", location_ids
165
+
166
+ org_ids = foreman_org_ids
167
+ verbose_list "found %d foreman organization(s).", org_ids
168
+
169
+ report = new_envs.collect { |x| foreman_env_create(x, location_ids, org_ids) }
170
+ verbose_list "created %d foreman environment(s).", report
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
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/foreman_envsync/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "foreman_envsync"
7
+ spec.version = ForemanEnvsync::VERSION
8
+ spec.authors = ["Joshua Hoblitt"]
9
+ spec.email = ["josh@hoblitt.com"]
10
+
11
+ spec.summary = "Sync pupperserver envs with foreman"
12
+ spec.homepage = "https://github.com/lsst-it/foreman_envsync"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.5.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/lsst-it/foreman_envsync"
18
+ spec.metadata["changelog_uri"] = "https://github.com/lsst-it/foreman_envsync/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "hammer_cli"
30
+ spec.add_dependency "hammer_cli_foreman"
31
+ spec.add_dependency "rest-client", "~> 2.1.0"
32
+ spec.add_dependency "rubocop-rake"
33
+ spec.add_dependency "rubocop-rspec"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "foreman_envsync/version"
4
+
5
+ module ForemanEnvsync
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanEnvsync
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreman_envsync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Hoblitt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hammer_cli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hammer_cli_foreman
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - josh@hoblitt.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/main.yml"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - CHANGELOG.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/foreman_envsync
101
+ - bin/setup
102
+ - foreman_envsync.gemspec
103
+ - lib/foreman_envsync.rb
104
+ - lib/foreman_envsync/version.rb
105
+ homepage: https://github.com/lsst-it/foreman_envsync
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ homepage_uri: https://github.com/lsst-it/foreman_envsync
110
+ source_code_uri: https://github.com/lsst-it/foreman_envsync
111
+ changelog_uri: https://github.com/lsst-it/foreman_envsync/blob/master/CHANGELOG.md
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.5.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.1.4
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Sync pupperserver envs with foreman
131
+ test_files: []