Showing with 106 additions and 38 deletions.
  1. +21 −0 CHANGELOG.md
  2. +15 −7 README.md
  3. +2 −2 lib/puppet/face/node_manager.rb
  4. +22 −25 lib/puppet/util/nc_https.rb
  5. +4 −4 metadata.json
  6. +10 −0 tasks/update_classes.json
  7. +32 −0 tasks/update_classes.rb
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## 2018-03-15 - Release 0.7.1

## Summary

- Typo fix in README
- Change of repo name to puppet-node_manager

## 2018-03-15 - Release 0.7.0

### Summary

- Added task for update-classes endpoint
- JRuby method fix for Net::Http

## 2018-01-31 - Release 0.6.1

### Summary

- Typo in face output
- JRuby method fix

## 2017-10-20 - Release 0.6.0

### Summary
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
1. [Authentication](#authentication)
1. [Types](#types)
* [Node_group](#node_group)
1. [Tasks](#tasks)
* [update_classes](#update_classes)
1. [Functions](#functions)
* [node_groups()](#node_groups)
1. [Face](#face)
Expand All @@ -23,13 +25,6 @@ Create and manage PE node groups as resources.
* Puppet >= 3.7.1
* New `https` provider which doesn't need `puppetclassify` gem

## Classes

### Node_manager

The node_manager class facilitates the deployment of the puppetclassify gem
simply include node_manager in your node definition or add it to the pe_master node group

## Authentication

### PE Console server
Expand Down Expand Up @@ -139,6 +134,19 @@ This parameter is supported for PE >=2017.3.x.

Default (empty hash): `{}`

## Tasks

### update_classes

Trigger update-classes job

```shell
puppet task run node_manager::update_classes --nodes 'pe-master' environment=production

```

__NOTE__: Default environment value is `production`.

## Functions

### node_groups()
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/face/node_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@
def check_facts(file, options)
if file && options[:explain]
begin
contents = YAML.load_file(file)
content ||= JSON.parse(File.read file)
contents = YAML.load_file(file)
contents ||= JSON.parse(File.read file)
rescue
fail "Could not file file '#{file}'"
else
Expand Down
47 changes: 22 additions & 25 deletions lib/puppet/util/nc_https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize

begin
nc_settings = YAML.load_file(settings_file)
nc_settings = nc_settings.first if nc_settings.class == Array
nc_settings = nc_settings.first if nc_settings.class == Array
rescue
fail "Could not find file #{settings_file}"
else
Expand All @@ -36,8 +36,7 @@ def initialize
def get_groups
res = do_https('v1/groups', 'GET')
if res.code.to_i != 200
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail('Unable to get node_group list')
else
JSON.parse(res.body)
Expand All @@ -48,8 +47,7 @@ def create_group(data)
endpoint = data.has_key?('id') ? "v1/groups/#{data['id']}" : 'v1/groups'
res = do_https(endpoint, 'POST', data)
if res.code.to_i != 303
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail("Unable to create node_group '#{data['name']}'")
else
new_UID = res['location'].split('/')[-1]
Expand All @@ -61,8 +59,7 @@ def create_group(data)
def delete_group(id)
res = do_https("v1/groups/#{id}", 'DELETE')
if res.code.to_i != 204
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail("Unable to delete node_group '#{data['name']}'")
else
true
Expand All @@ -76,8 +73,7 @@ def update_group(data)

res = do_https("v1/groups/#{data['id']}", 'POST', data)
if res.code.to_i != 200
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail("Unable to update node_group '#{data['name']}'")
else
true
Expand All @@ -87,8 +83,7 @@ def update_group(data)
def import_hierarchy(data)
res = do_https('v1/import-hierarchy', 'POST', data)
if res.code.to_i != 204
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail('Unable to import node_groups')
else
true
Expand All @@ -102,8 +97,7 @@ def get_classes(env = false, name = false)
url_array << name if name
res = do_https(url_array.join('/'), 'GET')
if res.code.to_i != 200
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail JSON.parse(res.body)['msg']
else
JSON.parse(res.body)
Expand All @@ -113,10 +107,9 @@ def get_classes(env = false, name = false)
def update_classes(env = nil)
url_array = ['v1/update-classes']
url_array << "?environment=#{env}" if env
res = do_https(url_array.join('/'), 'POST')
res = do_https(url_array.join(''), 'POST')
if res.code.to_i != 201
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
fail('Unable to update classes')
else
true
Expand All @@ -130,20 +123,18 @@ def get_classified(name, expl = false, facts = {}, trusted = {})
data = facts.merge(trusted)
res = do_https(url_array.join('/'), 'POST', data)
if res.code.to_i != 200
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
else
JSON.parse(res.body)
end
end

def pin_node(node, group_id)
data = { 'nodes' => node, }
data = { 'nodes' => node, }
res = do_https("v1/groups/#{group_id}/pin", 'POST', data)
require 'pry'; binding.pry
if res.code.to_i != 204
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
else
JSON.parse(res.body)
end
Expand All @@ -153,8 +144,7 @@ def unpin_from_all(node)
data = { 'nodes' => [node] }
res = do_https('v1/commands/unpin-from-all', 'POST', data)
if res.code.to_i != 200
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
error_msg(res)
else
JSON.parse(res.body)
end
Expand Down Expand Up @@ -185,7 +175,7 @@ def do_https(endpoint, method = 'post', data = {})
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

req = Object.const_get("Net::HTTP::#{method.capitalize}").new(uri.request_uri)
req = Net::HTTP.const_get(method.capitalize).new(uri.request_uri)
req.body = data.to_json
req.content_type = 'application/json'

Expand All @@ -201,5 +191,12 @@ def do_https(endpoint, method = 'post', data = {})
res
end
end

def error_msg(res)
json = JSON.parse(res.body)
kind = json['kind']
msg = json['msg']
Puppet.err %(node_manager failed with error type '#{kind}': #{msg})
Puppet.debug("Response code: #{res.code}")
Puppet.debug("Response message: #{res.body}")
end
end
8 changes: 4 additions & 4 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "WhatsARanjit-node_manager",
"version": "0.6.0",
"version": "0.7.1",
"author": "WhatsARanjit",
"summary": "Create and manage PE Console node groups as resources.",
"license": "Apache-2.0",
"source": "https://github.com/WhatsARanjit/prosvcs-node_manager",
"project_page": "https://github.com/WhatsARanjit/prosvcs-node_manager",
"issues_url": "https://github.com/WhatsARanjit/prosvcs-node_manager/issues",
"source": "https://github.com/WhatsARanjit/puppet-node_manager",
"project_page": "https://github.com/WhatsARanjit/puppet-node_manager",
"issues_url": "https://github.com/WhatsARanjit/puppet-node_manager/issues",
"tags": [
"PE",
"puppet enterprise",
Expand Down
10 changes: 10 additions & 0 deletions tasks/update_classes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"summary": "Kick off update classes",
"input_method": "stdin",
"parameters": {
"environment": {
"description": "Environment to be update",
"type": "Optional[String[1]]"
}
}
}
32 changes: 32 additions & 0 deletions tasks/update_classes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/opt/puppetlabs/puppet/bin/ruby

require 'puppet'

# Need to append LOAD_PATH
Puppet.initialize_settings
$:.unshift(Puppet[:plugindest])

require 'puppet/util/nc_https'
require 'puppet_x/node_manager/common'


def update_classes(env)
nc = Puppet::Util::Nc_https.new
nc.update_classes(env)
end

params = JSON.parse(STDIN.read)
env = params['env'] || 'production'

begin
res = update_classes(env)
if res.is_a?(Hash)
puts({ responsecode: res.code, responsebody: res.body }.to_json)
else
puts({responsecode: 201, responsebody: %(#{env} update-classes triggered)}.to_json)
end
exit 0
rescue Puppet::Error => e
puts({ status: 'failure', error: e.message }.to_json)
exit 1
end