seiso-import_chef 0.12.0 → 0.13.0

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
2
  SHA1:
3
- metadata.gz: 74d11fdc37b5d0cd7b62dcae3e6b6d558ab16268
4
- data.tar.gz: 8cc930b7adc6d0f8982550976b6246163b336937
3
+ metadata.gz: 91aca0b492acb4adeb8285b1e17a60553914650b
4
+ data.tar.gz: 973f6a0a8514193fc0648d51d3a28b995c671588
5
5
  SHA512:
6
- metadata.gz: e415076f0438c0a36d53e9519188a88c1074242c06c1f214e6936bf05352723b42282e87da0a9a34bdd8456ebcb63ebfa8bf7a6655580d2c7401534213ac29f9
7
- data.tar.gz: f979b0e2bdd70f046af738526050aef59950fc01d93dc3f67b59668ee49b4f98b97f9b4a4da92176e8cf774a1f71e58d935048d59b60981ebad26e85e70815c3
6
+ metadata.gz: 3d3dfe57a41c2a6dce148b5c6808a662c5c1345590975a48f2f814c2ae074faa8b14259dadac3fea2ddd5565660216802fff2c3cfb4b86c1244eaf932fb08e7b
7
+ data.tar.gz: 84e2a404ab5b99253398abcaf67fe1ef817ef9cfdeaf4f8134d2d9c528c69507c802d470ea67b10b2c95f93a259b4ff291271cc7862f7c8e4bd0784beb5295f8
data/.travis.yml CHANGED
@@ -1,3 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
3
+ - 2.1.4
4
+ before_install:
5
+ - gem uninstall bundler
6
+ - gem install bundler --version '1.10.5'
7
+ #install:
8
+ # - bundle install
data/Rakefile CHANGED
@@ -13,3 +13,8 @@ Rake::TestTask.new do |t|
13
13
  t.test_files = FileList[ 'test/test_*.rb' ]
14
14
  t.verbose = true
15
15
  end
16
+
17
+ task :run do
18
+ Rake::Task['install'].execute
19
+ system('seiso-import-chef --chef ~/.seiso-importers/chef.yml --seiso ~/.seiso-importers/seiso3.yml')
20
+ end
@@ -1,7 +1,8 @@
1
- require "chef/node"
2
- require "chef/rest"
3
- require "seiso/connector"
1
+ require "chef"
2
+ require "json"
3
+
4
4
  require_relative "import_chef/chef_machine_mapper"
5
+ require_relative "import_chef/connector"
5
6
 
6
7
  # Seiso namespace module
7
8
  module Seiso
@@ -12,15 +13,17 @@ module Seiso
12
13
  # Copyright:: Copyright (c) 2014-2015 Expedia, Inc.
13
14
  # License:: Apache 2.0
14
15
  class ImportChef
15
-
16
- # Batch size for putting nodes into Seiso.
16
+
17
17
  PAGE_SIZE = 20
18
-
18
+
19
19
  def initialize(chef_settings, seiso_settings)
20
- @chef = Chef::REST.new(
21
- "#{chef_settings['base_uri']}",
22
- "#{chef_settings['client_name']}",
23
- "#{chef_settings['signing_key']}")
20
+
21
+ chef_options = {
22
+ :client_name => chef_settings['client_name'],
23
+ :signing_key_filename => chef_settings['signing_key'],
24
+ :inflate_json_class => true
25
+ }
26
+ @chef = Chef::ServerAPI.new(chef_settings['base_uri'], chef_options)
24
27
  @seiso = Seiso::Connector.new seiso_settings
25
28
  @mapper = Seiso::ImportChef::ChefMachineMapper.new
26
29
  end
@@ -28,26 +31,23 @@ module Seiso
28
31
  def seiso
29
32
  @seiso
30
33
  end
31
-
34
+
32
35
  # Imports all nodes from the Chef server into Seiso.
33
36
  def import_all
34
37
  nodes = @chef.get_rest "/nodes"
35
38
  node_names = nodes.keys.sort!
36
-
37
- slices = node_names.each_slice(PAGE_SIZE).to_a
38
39
  node_count = 0
39
40
  problem_nodes = []
41
+ machine_batch = []
42
+ slices = node_names.each_slice(PAGE_SIZE).to_a
40
43
  slices.each_with_index do |slice, page_index|
41
- puts "Building page #{page_index + 1} (total_nodes=#{node_names.length}, page_size=#{PAGE_SIZE})"
42
- page = []
44
+ machine_batch = []
43
45
  slice.each do |name|
44
- # name is case-sensitive in Chef, so we can't just downcase it
45
46
  node_count += 1
46
47
  puts "Getting node #{node_count} of #{node_names.length}: #{name}"
47
48
  begin
48
49
  node = @chef.get_rest "/nodes/#{name}"
49
- node_automatic = node.automatic
50
-
50
+ node_automatic = node['automatic']
51
51
  # Have observed this. Need to decide whether to handle default/override/etc.
52
52
  if node_automatic.empty?
53
53
  puts "WARNING: Node #{name} has no automatic attributes"
@@ -55,15 +55,12 @@ module Seiso
55
55
  "name" => name,
56
56
  "reason" => "Node has no automatic attributes."
57
57
  }
58
+ # Since a node without automatic attributes means it doesn't have an IP,
59
+ # we don't want to import. [IDM]
58
60
  next
59
61
  end
60
-
61
62
  machine = @mapper.map_one(node_automatic)
62
-
63
- # FIXME Just setting this here for now since I'm not sure which node field the name corresponds to.
64
- # It's not exactly the FQDN (the case is different).
65
63
  machine['name'] = name
66
- page << machine
67
64
  rescue Net::HTTPServerException
68
65
  puts "ERROR: Couldn't get #{name}"
69
66
  problem_nodes << {
@@ -71,16 +68,11 @@ module Seiso
71
68
  "reason" => "Couldn't get node from Chef server even though Chef server provided the name."
72
69
  }
73
70
  end
71
+ machine_batch << machine
74
72
  end
75
-
76
- # FIXME This is not detecting some HTTP 500s that are happening. When this happens, the whole page fails, the server
77
- # returns an HTTP 500 (I think, anyway--need to confirm), and this call doesn't catch the error. Somehow we need to
78
- # surface these as problem nodes too, maybe indicating that the server didn't like them, as opposed to not being able to
79
- # read them from Chef server.
80
- puts "Flushing nodes to Seiso"
81
- seiso.post_items('machines', page)
73
+ seiso.send_data(machine_batch)
82
74
  end
83
-
75
+
84
76
  if problem_nodes.empty?
85
77
  puts "Successfully imported all machines"
86
78
  else
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'openssl'
4
+
5
+ module Seiso
6
+
7
+ # REST interface to Seiso API.
8
+ #
9
+ # Author:: Willie Wheeler (mailto:wwheeler@expedia.com)
10
+ # Copyright:: Copyright (c) 2014-2016
11
+ # License:: Apache 2.0
12
+ class Connector
13
+
14
+ def initialize(settings)
15
+ @username = settings['username']
16
+ @password = settings['password']
17
+ @use_ssl = settings['use_ssl']
18
+ @request_headers = {
19
+ "accept" => "application/json",
20
+ "Content-Type" => "application/json",
21
+ }
22
+ @base_uri = URI(settings['base_uri'])
23
+ @verify_mode = settings['ssl_verify_none'] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
24
+ end
25
+
26
+ def send_data(data)
27
+ Net::HTTP.start(
28
+ @base_uri.host,
29
+ @base_uri.port,
30
+ :use_ssl => @use_ssl,
31
+ :verify_mode => @verify_mode) do |http|
32
+ http.keep_alive_timeout = 10
33
+ upsert_machines(http, data)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def get(http, path)
40
+ request = Net::HTTP::Get.new(path, initheader = @request_headers)
41
+ request.basic_auth(@username, @password)
42
+ response = http.request(request)
43
+ if response.kind_of?(Net::HTTPSuccess)
44
+ return JSON.parse(response.body)
45
+ elsif response.kind_of?(Net::HTTPNotFound)
46
+ return nil
47
+ else
48
+ # Not sure about what other conditions, if any, need to be checked here. [IDM]
49
+ return nil
50
+ end
51
+ end
52
+
53
+ def do_post(http, path, data)
54
+ puts "POST #{path}"
55
+ request = Net::HTTP::Post.new(path, initheader = @request_headers)
56
+ request.basic_auth(@username, @password)
57
+ request.body = data.to_json
58
+ response = http.request(request)
59
+ display_response(response)
60
+ end
61
+
62
+ def do_put(http, path, data)
63
+ puts "PUT #{path}"
64
+ request = Net::HTTP::Put.new(path, initheader = @request_headers)
65
+ request.basic_auth(@username, @password)
66
+ request.body = data.to_json
67
+ response = http.request(request)
68
+ display_response(response)
69
+ end
70
+
71
+ def upsert_machine(http, payload)
72
+ search_path = "#{@base_uri}/api/machines/search/findByName?name=#{payload['name']}"
73
+ machines_path = "#{@base_uri}/api/machines"
74
+ seiso_machine = get(http, search_path)
75
+ if seiso_machine.nil?
76
+ do_post(http, machines_path, payload)
77
+ else
78
+ self_url = seiso_machine['_links']['self']['href']
79
+ do_put(http, self_url, payload)
80
+ end
81
+ end
82
+
83
+ def upsert_machines(http, machines)
84
+ machines.each do |machine|
85
+ upsert_machine(http, machine)
86
+ end
87
+ puts "Machine page saved to Seiso."
88
+ end
89
+
90
+ # Displays an HTTP response on the command line.
91
+ # - response: response to display
92
+ def display_response(response)
93
+ case response.code
94
+ when "500"
95
+ puts "Server error: #{response.code} #{response.message}"
96
+ else
97
+ puts "#{response.code} #{response.message}"
98
+ end
99
+ end
100
+ end
101
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "seiso-import_chef"
7
- spec.version = "0.12.0"
7
+ spec.version = "0.13.0"
8
8
  spec.authors = ["Willie Wheeler"]
9
9
  spec.email = ["wwheeler@expedia.com"]
10
10
  spec.summary = "Imports Chef data into Seiso."
@@ -23,5 +23,4 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
 
25
25
  spec.add_runtime_dependency "chef", "~> 12.4"
26
- spec.add_runtime_dependency "seiso-connector", "~> 0.1"
27
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seiso-import_chef
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willie Wheeler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2016-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '12.4'
55
- - !ruby/object:Gem::Dependency
56
- name: seiso-connector
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.1'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.1'
69
55
  description: Currently supports importing Chef nodes as Seiso machines.
70
56
  email:
71
57
  - wwheeler@expedia.com
@@ -83,6 +69,7 @@ files:
83
69
  - bin/seiso-import-chef
84
70
  - lib/seiso/import_chef.rb
85
71
  - lib/seiso/import_chef/chef_machine_mapper.rb
72
+ - lib/seiso/import_chef/connector.rb
86
73
  - refresh
87
74
  - sample_conf/chef.yml.sample
88
75
  - sample_conf/seiso.yml.sample
@@ -108,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
95
  version: '0'
109
96
  requirements: []
110
97
  rubyforge_project:
111
- rubygems_version: 2.4.6
98
+ rubygems_version: 2.5.1
112
99
  signing_key:
113
100
  specification_version: 4
114
101
  summary: Imports Chef data into Seiso.