Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
[WIP] Add server_puppetdb_data class
Browse files Browse the repository at this point in the history
  Fixes #60
  • Loading branch information
tallenaz committed Jul 26, 2017
1 parent e99bf12 commit 687b4c8
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 26 deletions.
19 changes: 19 additions & 0 deletions app/models/puppetdb_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,23 @@ def ipaddress(fqdn)
response = client.request('facts', ['and', ['=', 'certname', fqdn], ['=', 'name', 'ipaddress']])
response.data.first['value']
end

def all_fqdns
response = client.request('facts', ['=', 'name', 'hostname'])
response.data.collect { |key| key['certname'] }
end

def pupgraded_fqdns
response = client.request('facts', ['and',
['=', 'name', 'fqdn'],
['in', 'certname',
['extract', 'certname',
['select-resources',
['and', ['=', 'type', 'Class'], ['=', 'title', 'Role::Basenode']]]]]])
response.data.collect { |key| key['certname'] }
end

def pupgraded?(fqdn)
pupgraded_fqdns.include?(fqdn) ? true : false
end
end
20 changes: 0 additions & 20 deletions app/models/server_puppet_data.rb

This file was deleted.

24 changes: 24 additions & 0 deletions app/models/server_puppetdb_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class ServerPuppetdbData
attr_reader :client

def initialize
@client = PuppetdbClient.new
end

def self.run
new.load_data
end

def load_data
client.all_fqdns.each do |fqdn|
ip = client.ipaddress(fqdn)
hostname = fqdn.split('.').first
pupgraded = client.pupgraded?(fqdn)
Server.find_or_create_by(fqdn: fqdn).tap do |server|
server.update(ip: ip, hostname: hostname, pupgraded: pupgraded)
end
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20170607163021_create_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :servers do |t|
t.string :hostname
t.string :fqdn
t.inet :ip
t.string :ip
t.string :dev_team
t.boolean :pupgraded

Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
create_table "servers", force: :cascade do |t|
t.string "hostname"
t.string "fqdn"
t.inet "ip"
t.string "ip"
t.string "dev_team"
t.boolean "pupgraded"
t.datetime "created_at", null: false
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/load_data.rake
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

namespace :load_data do
desc 'loads server data from puppet and puppet\'s hiera files'
desc 'loads server data from puppetdb'
task servers: :environment do
ServerPuppetData.run
ServerPuppetdbData.run
end

desc 'loads repo data for the sul-dlss org'
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/files/hostname_facts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"value": "some-server-stage.stanford.edu",
"name": "hostname",
"certname": "some-server-stage.stanford.edu"
},
{
"value": "some-server-prod.stanford.edu",
"name": "hostname",
"certname": "some-server-prod.stanford.edu"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"value": "123.45.67.890",
"name": "ipaddress",
"certname": "some-server-stage.stanford.edu"
},
{
"value": "123.45.67.891",
"name": "ipaddress",
"certname": "some-server-prod.stanford.edu"
}
]
12 changes: 12 additions & 0 deletions spec/fixtures/files/pupgrade_facts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"value": "some-server-stage.stanford.edu",
"name": "fqdn",
"certname": "some-server-stage.stanford.edu"
},
{
"value": "some-server-prod.stanford.edu",
"name": "fqdn",
"certname": "some-server-prod.stanford.edu"
}
]
32 changes: 30 additions & 2 deletions spec/models/puppetdb_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,40 @@

describe '#ipaddress' do
before do
response = file_fixture('puppetdb_fact').read
response = file_fixture('ipaddress_facts').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
end

it 'returns the ipaddress of a server' do
expect(client.ipaddress('some-server-stage.stanford.edu')).to eq '123.45.67.890'
end
end

describe '#all_fqdns' do
before do
response = file_fixture('hostname_facts').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
end
it 'returns all fqdns from the response' do
expect(client.all_fqdns).to eq ['some-server-stage.stanford.edu', 'some-server-prod.stanford.edu']
end
end

describe 'methods about pupgrading' do
before do
response = file_fixture('pupgrade_facts').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
end
describe '#pupgraded_fqdns' do
it 'returns fqdns of pupgraded machines' do
expect(client.pupgraded_fqdns).to eq ['some-server-stage.stanford.edu', 'some-server-prod.stanford.edu']
end
end

describe '#pupgraded?(fqdn)' do
it 'says whether a machine is pupgraded or not' do
expect(client.pupgraded?('some-server-stage.stanford.edu')).to be true
expect(client.pupgraded?('not-pupgraded.stanford.edu')).to be false
end
end
end
end
39 changes: 39 additions & 0 deletions spec/models/server_puppetdb_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'rails_helper'
require 'ipaddr'

RSpec.describe ServerPuppetdbData do
subject(:server_puppetdb_data) { described_class.new }

describe '#initialize' do
it 'creates an PuppetdbClient instance' do
expect(server_puppetdb_data.client).to be_an PuppetdbClient
end
end

describe '#load_data' do
describe 'with valid response' do
before do
hostname_facts = file_fixture('hostname_facts').read
ipaddress_facts = file_fixture('ipaddress_facts').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: hostname_facts, headers: { 'Content-Type' => 'application/json' }).then
.to_return(status: 200, body: ipaddress_facts, headers: { 'Content-Type' => 'application/json' })
end

it 'creates objects from api response' do
server_puppetdb_data.load_data
server = Server.first
expect(server.fqdn).to eq 'some-server-stage.stanford.edu'
expect(server.hostname).to eq 'some-server-stage'
expect(server.ip).to eq '123.45.67.890'
expect(server.pupgraded).to be true
end

it 'only creates unique objects' do
create(:server)
expect { server_puppetdb_data.load_data }.to change { Server.all.count }.by 2
end
end
end
end

0 comments on commit 687b4c8

Please sign in to comment.