Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shard plugin #877

Merged
merged 11 commits into from
Sep 22, 2016
52 changes: 52 additions & 0 deletions lib/ohai/plugins/shard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# Author:: Phil Dibowitz <[email protected]>
# Copyright:: Copyright (c) 2016 Facebook, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'digest/md5'

Ohai.plugin(:ShardSeed) do
depends "hostname", "dmi", "machine_id"
provides "shard_seed"

def get_dmi_thing(dmi, thing)
%w{system base_board chassis}.each do |section|
unless dmi[:thing][thing].compact.empty?
return dmi[:thing][thing]
end
end
end

collect_data(:default) do
sources = Ohai.config[:plugin][:shard_seed][:source] || [:fqdn]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stil don't like the default on fqdn. machinename would be better since it avoid DNS issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact i'd prefer to not even give the option of using :fqdn here because that depends on DNS and a transient DNS hiccup my change the sharding.

data = ''
sources.each do |src|
data << case base
when :fqdn
fqdn
when :hostname
hostname
when :serial
get_dmi_thing(dmi, :serial_number)
when :uuid
get_dmi_thing(dmi, :uuid)
when :machine_id
machine_id
end
end
shard_seed Digest::MD5.hexdigest(data)[0...7].to_i(16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You aren't requireing either of the digest libraries.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why trim to 8 characters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'll add the requires.

Um, I don't remember why we did the 8 characters. Something something crytpo widths. Let me see if I can track that down.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So best we can tell that 2^15 buckets seemed sufficient and I think we had overflowed a variable or something somewhere without that....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyone know if this will be an issue with FIPS? ISTR that the md5 functions get removed if you have OpenSSL in FIPS mode (groan). Alternatively, just using SHA-2 probably won't hurt anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Digest::MD5 still works in FIPS mode, that's Ruby's internal implementation as opposed to OpenSSL's. It's still in C so probably just as fast. We could also use the Adler CRC implementation in the Zlib module if we didn't want either MD5 or SHA for some reason.

end
end