kierranm / ec2-tags-ohai-plugin Goto Github PK
View Code? Open in Web Editor NEWAn Ohai plugin for getting EC2 instance tags
An Ohai plugin for getting EC2 instance tags
This is a really odd specific case, but I'll do my best to explain it. I run the ec2-tags-ohai-plugin
on my hosts. I run Chef Automate which has "runner" processes that run on hosts and do the work for Workflow to build and deploy applications. The runners are installed by the main cli utility automate-ctl
. Among other things, it installs ChefDK on to the node with a desired version and a delivery-cmd
script that handles getting instructions from the main Chef Automate server.
delivery-cmd
contains the following code near the top of the main block:
Chef::Config.from_file(Chef::Config.platform_specific_path("/etc/chef/client.rb"))
client = Chef::Client.new
client.run_ohai
client.node_name
When running with ChefDK 1.4.3, everything works as expected. But with ChefDK 1.5.0 and higher, running it yields this:
/usr/local/bin/delivery-cmd:103:in `<main>': undefined method `node_name' for #<Aws::EC2::Client> (NoMethodError)
Which instantly led me to look at this plugin because the prior line is client.run_ohai
. I put a pry breakpoint in /etc/chef/ohai/plugins/ec2-tags.rb and after some debugging, it seems that with 1.4.3 the ec2-tags-ohai-plugin
isn't loaded, but with 1.5.0 it is and that's where things get weird. When the ohai plugin is loaded, apparently a local variable client
already exists in the plugin's scope and it's an instance of Chef::Client
. When the ec2-tags-ohai-plugin
is executed, the client
variable is overridden with an instance of Aws::EC2::Client
which persists after the plugin execution is complete in the main scope of the script and causes it to fail.
Here's some debugger output:
# /usr/local/bin/delivery-cmd
Frame number: 0/11
From: /etc/chef/ohai/plugins/ec2-tags.rb @ line 14 :
9: collect_data do
10: begin
11: require 'aws-sdk-core'
12: binding.pry
13:
=> 14: client = Aws::EC2::Client.new region: ec2['placement_availability_zone'][0...-1]
15: resp = client.describe_tags(
16: filters: [
17: {
18: name: 'resource-id',
19: values: [
[1] pry(#<Ohai::NamedPlugin::EC2Tags>)> ls
Ohai::Mixin::Command#methods: run_command_backend run_command_unix run_command_windows
Ohai::Mixin::SecondsToHuman#methods: seconds_to_human
Ohai::Util::FileHelper#methods: which
Ohai::DSL::Plugin#methods: [] []= attribute? data each from from_with_regex get_attribute has_key? has_run? hint? method_missing reset! run safe_run set set_attribute
Ohai::DSL::Plugin::VersionVII#methods: configuration dependencies name provides require_plugin run_plugin source version
instance variables: @data @has_run @source @version
locals:
_ _dir_ _file_ _out_ builder_auth_headers chef_stacktrace_file client command_line custom_cert_filepath elapsed_time http_proxy job_data path raw_json rest start_datetime workspace_bin
__ _ex_ _in_ _pry_ change_file cli_env cmd compliance_json e end_datetime https_proxy no_proxy path_compliance resp server streamy workspace_root
[2] pry(#<Ohai::NamedPlugin::EC2Tags>)> client
=> #<Chef::Client:0x0000000324d620 ...>
[4] pry(#<Ohai::NamedPlugin::EC2Tags>)> next
From: /etc/chef/ohai/plugins/ec2-tags.rb @ line 15 :
10: begin
11: require 'aws-sdk-core'
12: binding.pry
13:
14: client = Aws::EC2::Client.new region: ec2['placement_availability_zone'][0...-1]
=> 15: resp = client.describe_tags(
16: filters: [
17: {
18: name: 'resource-id',
19: values: [
20: ec2['instance_id']
[4] pry(#<Ohai::NamedPlugin::EC2Tags>):1> client
=> #<Aws::EC2::Client>
The quick workaround is to just rename the client
variable in the plugin to something that doesn't collide like ec2_client
and I'll submit a pull request for that. I'm not really sure where the problem is exactly upstream to report that. Any thoughts are appreciated.
Hey, currently using the latest version of this recipe. Currently using it against a base AWS Windows Server 2016 instance. It's currently unable to find any tags even though the name tag exists. Any help with this? When inspecting node['ec2'] it contains the rest of the info besides the tags which appears non-existent.
Perhaps because of some API changes on the AWS side, this plugin does not want to function. The gem complains that no credentials were provided:
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/aws-sdk-core-3.21.2/lib/aws-sdk-core/plugins/signature_v4.rb:72:in
sign_request': unable to sign request without credentials set (Aws::Errors::MissingCredentialsError)`
If you try to use the gem locally and have your ~/.aws/credentials
set up, it works. Not sure if it makes sense to deploy AWS credentials before or with this plugin to the node, or if it's better to look at using the EC2 http endpoint as described at the link below.
Edit: The last line of the http endpoint script that uses aws ec2...
to actually fetch the tags, despite having read it, escaped my brain.
Hey,
I am trying to get it work on windows images both 2012 and 2016, while getting the following error:
[2018-08-05T11:18:53+00:00] DEBUG: Plugin EC2Tags threw #<NameError: uninitialized constant Aws>
[2018-08-05T11:18:53+00:00] DEBUG: C:/chef/ohai/plugins/ec2-tags.rb:25:in rescue in block (2 levels) in <main>' [2018-08-05T11:18:53+00:00] DEBUG: C:/chef/ohai/plugins/ec2-tags.rb:9:in
block (2 levels) in
instance_eval' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/dsl/plugin/versionvii.rb:90:in
run_plugin'run' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/dsl/plugin.rb:185:in
safe_run'run_v7_plugin' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/runner.rb:47:in
block in run_plugin'measure' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/runner.rb:39:in
run_plugin'block in run_plugins' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/system.rb:104:in
each'run_plugins' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/system.rb:82:in
all_plugins'run_application' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/application.rb:80:in
block in run'measure' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.8.0/lib/ohai/application.rb:78:in
run'<top (required)>' [2018-08-05T11:18:53+00:00] DEBUG: C:/opscode/chef/bin/ohai:29:in
load'Some details:
Base AMI: ami-f7ece81c , also tried: ami-8017156b
Gem version: C:\opscode\chef\embedded\bin\gem list
aws-sdk (2.0.48)
aws-sdk-core (2.0.48)
aws-sdk-resources (2.0.48)
Please assist.
Thanks,
Mor.
I am trying to install ec2-tags-ohai-plugin but getting following error on Windows 2016
ohai_plugin[ec2-tags] (infra-windows-cookbook::join-computer2AD line 5) had an error: Chef::Exceptions::FileNotFound: template[C:/chef/ohai/plugins/ec2-tags.rb] (C:/chef/cache/cookbooks/ohai
/resources/plugin.rb line 25) had an error: Chef::Exceptions::FileNotFound: Cookbook 'infra-windows-cookbook' (2.0.9) does not contain a file at any of these locations:
templates/windows-10.0.14393/ec2-tags.rb
templates/windows/ec2-tags.rb
templates/default/ec2-tags.rb
templates/ec2-tags.rb
But I could able to see the file under templates/default/ec2-tags.rb in ec2-tags-ohai-plugin cookbook.
Please let me know how to resolve it?
If I run the current version of ec2-tags-ohai-plugin (0.2.4) and try to use the current version of the aws cookbook, I get the following error:
ERROR: Unable to activate aws-sdk-resources-2.10.61, because aws-sdk-core-3.6.0 conflicts with aws-sdk-core (= 2.10.61)
Because the aws cookbook isn't run as a recipe (it just exposes resource providers), there is no way to control when it executes as far as I can tell.
The aws cookbook includes:
gem 'aws-sdk', '~> 2'
But the ec2-tags-ohai-plugin cookbook has gem
entry in metadata; it just uses the chef_gem
resource to install it in the recipe, with no version pin.
Adding gem 'aws-sdk', '~> 2'
to my the metadata of my cookbook that calls ec2-tags-ohai-plugin seems to fix the issue, as does pinning ec2-tags-ohai-plugin to v0.2.3. If I was calling this cookbook directly in the runlist and then trying to use the aws resources in another cookbook, I don't know how this could be worked around.
Maybe adding an attribute to allow pinning the version of the gem installed using the chef_gem resource would fix the issue?
Today we had an issue where the plugin wasn't populating the node['ec2']['tags'] hash. We tracked it down to an update to the ruby aws-sdk that happened today version 3.0.0. It seems that something is wrong either with their update or they have changed some core functionality with describing tags.
They may have realized their error because they seem to have taken the release off of their github releases page, but the version still exists in the gem repo for ubuntu.
We ended up having to pin the version as of now at 2.10.35. As of right now, I'm unsure what the issue is because the plugin installation doesn't throw any errors when it compiles and installs the sdk for the first time.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.