chef-boneyard / chef-provisioning-docker

Docker provisioner for chef-provisioning
Apache License 2.0
91 stars 40 forks source link

search(:node) does not contain the ipaddress #65

Open yazgoo opened 9 years ago

yazgoo commented 9 years ago

Here are two scripts. One creates a machine:

require 'chef/provisioning/docker_driver'
machine "dummy" do
    tag "dummy"
    converge true
    machine_options({ docker_options: {
        base_image: { name: 'baseimage', repository: 'phusion/baseimage', tag: '0.9.17' },
        command: '/sbin/my_init'} })
end

The other searches for it:

require 'chef/provisioning/docker_driver'
search(:node, "tags:*dummy*").map do |node|
    p node.ipaddress
end

When I run these scripts with bundle and this Gemfile:

gem 'chef-provisioning-docker'

The second script works fine.

When I try and run against the master git HEAD version :

gem 'chef-provisioning-docker', :path => '../chef-provisioning-docker' 

I get the following error:

ERROR: Undefined method or attribute `ipaddress' on `node'
yazgoo commented 9 years ago

Here is a bypass which uses docker inspect to get the ipaddress if it is not set:

class Chef::Resource::Machine
    def running_on? kind
        kind == ENV['CHEF_DRIVER'].sub(/::.*$/, "").to_sym
    end
    def get_docker_ip_address name
        JSON.parse(`docker inspect #{name}`).first["NetworkSettings"]["IPAddress"]
    end
    alias :old_search :search
    def search what, query
        result = old_search what, query
        if what == :node and running_on? :docker
            result.map! do |node|
                node.default["ipaddress"] = get_docker_ip_address(node.name) if node["ipaddress"] == nil
                node
            end
        end
        return result
    end
end
marc- commented 9 years ago

Hm, there are no much changes between 0.7 and master. I personally have ipaddress among node attributes when running CPD. Though, I use forked version of my own without changes introduced by 07753c4520d3364124dbdca434cd102e471b8bb4 .