From 3e1b84a3566a1b10f78bdb94aa7fb34ad31d27ef Mon Sep 17 00:00:00 2001 From: Alex Ruiz Estradera Date: Tue, 23 Aug 2016 18:47:58 +0200 Subject: [PATCH] Fix the midonet role Change-Id: Ib17b95f5a6a7137c91eb92a2472513d45ea15f86 --- lib/puppet/parser/functions/host_id.rb | 26 ++++++++++ .../midonet_api_caller.rb | 29 +++++------ lib/puppet/type/midonet_host_registry.rb | 13 ++--- manifests/init.pp | 25 ++++++---- manifests/resources/network_creation.pp | 9 ++-- spec/functions/host_id_spec.rb | 18 +++++++ .../midonet_api_caller_spec.rb | 48 ++++--------------- .../puppet/type/midonet_host_registry_spec.rb | 30 ++++-------- 8 files changed, 98 insertions(+), 100 deletions(-) create mode 100644 lib/puppet/parser/functions/host_id.rb create mode 100644 spec/functions/host_id_spec.rb diff --git a/lib/puppet/parser/functions/host_id.rb b/lib/puppet/parser/functions/host_id.rb new file mode 100644 index 0000000..d5b318a --- /dev/null +++ b/lib/puppet/parser/functions/host_id.rb @@ -0,0 +1,26 @@ +# Copyright 2015 Midokura SARL, Inc. +# +# 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 'logger' + +module Puppet::Parser::Functions + newfunction(:host_id, :type => :rvalue + ) do |args| + begin + return File.read('/etc/midonet_host_id.properties').split('=')[1].sub("\n","") + rescue + raise 'Midonet agent is not running on the host you are trying to register' + end + end + +end diff --git a/lib/puppet/provider/midonet_host_registry/midonet_api_caller.rb b/lib/puppet/provider/midonet_host_registry/midonet_api_caller.rb index 703d416..a353f07 100644 --- a/lib/puppet/provider/midonet_host_registry/midonet_api_caller.rb +++ b/lib/puppet/provider/midonet_host_registry/midonet_api_caller.rb @@ -32,15 +32,11 @@ Puppet::Type.type(:midonet_host_registry).provide(:midonet_api_caller) do end end - host = call_get_host() - if host.empty? - raise 'Midonet agent does not run on the host you are trying to register' - else - host_id = host[0]['id'] + host_id = call_get_host() + if host_id.empty? + raise 'Midonet agent is not running on the host you are trying to register' end - host_id = host[0]['id'] - message = Hash.new message['hostId'] = "#{host_id}" message['ipAddress'] = "#{resource[:underlay_ip_address]}" @@ -57,11 +53,10 @@ Puppet::Type.type(:midonet_host_registry).provide(:midonet_api_caller) do end tz_id = tz[0]['id'] - host = call_get_host() - if host.empty? + host_id = call_get_host() + if host_id.empty? return end - host_id = host[0]['id'] reg_host = call_get_tunnelzone_host(tz_id, host_id) if reg_host.empty? @@ -87,11 +82,10 @@ Puppet::Type.type(:midonet_host_registry).provide(:midonet_api_caller) do end tz_id = tz[0]['id'] - host = call_get_host() - if host.empty? + host_id = call_get_host() + if host_id.empty? return false end - host_id = host[0]['id'] reg_host = call_get_tunnelzone_host(tz_id, host_id) if reg_host.empty? @@ -140,12 +134,11 @@ Puppet::Type.type(:midonet_host_registry).provide(:midonet_api_caller) do end def call_get_host() - res = @connection.get do |req| - req.url "/midonet-api/hosts" + begin + return File.read('/etc/midonet_host_id.properties').split('=')[1].sub("\n","") + rescue + raise 'Midonet agent is not running on the host you are trying to register' end - - output = JSON.parse(res.body) - return output.select{ |host| host['name'] == resource[:hostname].to_s } end def call_create_tunnelzone(message) diff --git a/lib/puppet/type/midonet_host_registry.rb b/lib/puppet/type/midonet_host_registry.rb index d0cbbbc..e5bd8f2 100644 --- a/lib/puppet/type/midonet_host_registry.rb +++ b/lib/puppet/type/midonet_host_registry.rb @@ -7,7 +7,7 @@ Puppet::Type.newtype(:midonet_host_registry) do Example: - midonet_host_registry {'hostname': + midonet_host_registry {'myhost': $midonet_api_url => 'http://controller:8080', $username => 'admin', $password => 'admin', @@ -16,15 +16,8 @@ Puppet::Type.newtype(:midonet_host_registry) do } ensurable - newparam(:hostname, :namevar => true) do - desc 'Hostname of the host that wants to register in MidoNet cloud' - # Regex obtained from StackOverflow question: - # http://stackoverflow.com/questions/1418423/the-hostname-regex - validate do |value| - unless value =~ /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/ - raise ArgumentError, "'%s' is not a valid hostname" % value - end - end + newparam(:name) do + desc "The name of the host." end newparam(:tunnelzone_name) do diff --git a/manifests/init.pp b/manifests/init.pp index fc79f13..d5c0f0c 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -42,15 +42,6 @@ class midonet { include ::midonet::params - # Add midonet-agent - class { 'midonet::agent': - controller_host => '127.0.0.1', - metadata_port => '8775', - shared_secret => 'testmido', - zookeeper_hosts => [{ - 'ip' => $::ipaddress} - ], - } # Add midonet-cluster class {'midonet::cluster': @@ -63,8 +54,22 @@ class midonet { keystone_host => '127.0.0.1' } + # Add midonet-agent + class { 'midonet::agent': + controller_host => '127.0.0.1', + metadata_port => '8775', + shared_secret => 'testmido', + zookeeper_hosts => [{ + 'ip' => $::ipaddress} + ], + require => ['::midonet::cluster::install','::midonet::cluster::run'] + } + # Add midonet-cli - class {'midonet::cli':} + class {'midonet::cli': + username => 'midogod', + password => 'midogod', + tenant_name => 'midokura',} midonet_host_registry { $::hostname: ensure => present, diff --git a/manifests/resources/network_creation.pp b/manifests/resources/network_creation.pp index 8084cf2..0c92361 100644 --- a/manifests/resources/network_creation.pp +++ b/manifests/resources/network_creation.pp @@ -104,10 +104,12 @@ define midonet::resources::network_creation( $edge_cidr = '172.17.0.0/24', $port_name = 'testport', $port_fixed_ip = '172.17.0.102', - $port_interface_name = 'eth1' + $port_interface_name = 'eth1', + $binding_host_id = undef ) { +include stdlib if($::osfamily == 'Debian' and $::operatingsystemmajrelease == '16.04') { @@ -127,7 +129,7 @@ define midonet::resources::network_creation( neutron_network { $network_external: ensure => present, router_external => true, - shared => true, + shared => false, } -> neutron_subnet { $subnet_name: @@ -162,9 +164,10 @@ define midonet::resources::network_creation( } -> neutron_port { $port_name: + ensure => present, network_name => $edge_network_name, - binding_host_id => $::fqdn, + binding_host_id => pick($binding_host_id,$::hostname), binding_profile => { 'interface_name' => c7_int_name($port_interface_name) }, diff --git a/spec/functions/host_id_spec.rb b/spec/functions/host_id_spec.rb new file mode 100644 index 0000000..4243276 --- /dev/null +++ b/spec/functions/host_id_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + + +output = <<-EOF +#Mon Aug 22 16:42:19 UTC 2016 +host_uuid=41b4d082-daf8-40bb-8812-504a0e9e5dac +EOF + +describe 'host_id' do + it 'should work without errors' do + allow(File).to receive(:read).and_return('test') + allow(File).to receive(:read).with('/etc/midonet_host_id.properties').and_return(output) + + is_expected.to run.with_params().and_return('41b4d082-daf8-40bb-8812-504a0e9e5dac') + + end + +end diff --git a/spec/unit/puppet/provider/midonet_host_registry/midonet_api_caller_spec.rb b/spec/unit/puppet/provider/midonet_host_registry/midonet_api_caller_spec.rb index 38950f1..b621a42 100644 --- a/spec/unit/puppet/provider/midonet_host_registry/midonet_api_caller_spec.rb +++ b/spec/unit/puppet/provider/midonet_host_registry/midonet_api_caller_spec.rb @@ -6,8 +6,8 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) let(:resource) { Puppet::Type.type(:midonet_host_registry).new( { + :title => 'test', :ensure => :present, - :hostname => 'compute.midonet', :midonet_api_url => 'http://controller:8080', :username => 'username', :password => 'password', @@ -32,20 +32,13 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) ] } - let(:hosts) { - [ - { - "id" => "04f7361c-4cb8-4cda-a50f-1744fd8b7851", - "name" => "compute.midonet" - } - ] - } + let(:host) { '04f7361c-4cb8-4cda-a50f-1744fd8b7851' } before :each do allow(provider).to receive(:call_get_tunnelzone).and_return(tzones) - allow(provider).to receive(:call_get_host).and_return(hosts) + allow(provider).to receive(:call_get_host).and_return(host) allow(provider).to receive(:call_create_tunnelzone_host) - allow(provider).to receive(:call_get_tunnelzone_host).and_return(hosts) + allow(provider).to receive(:call_get_tunnelzone_host).and_return(host) allow(provider).to receive(:call_delete_tunnelzone_host) allow(provider).to receive(:call_get_tunnelzone_hosts).and_return([]) allow(provider).to receive(:call_delete_tunnelzone) @@ -73,14 +66,7 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) end describe 'when no tunnelzones' do - let(:hosts) { - [ - { - "id" => "04f7361c-4cb8-4cda-a50f-1744fd8b7851", - "name" => "compute.midonet" - } - ] - } + let(:host) { '04f7361c-4cb8-4cda-a50f-1744fd8b7851' } let(:tzones) { [ @@ -95,7 +81,7 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) it 'creates the tunnelzone and the host' do allow(provider).to receive(:call_get_tunnelzone).and_return([]) allow(provider).to receive(:call_create_tunnelzone).and_return(tzones) - allow(provider).to receive(:call_get_host).and_return(hosts) + allow(provider).to receive(:call_get_host).and_return(host) allow(provider).to receive(:call_create_tunnelzone_host) allow(provider).to receive(:call_get_token).and_return('thisisafaketoken') @@ -117,14 +103,7 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) ] } - let(:host_to_unregister) { - [ - { - "id" => "04f7361c-4cb8-4cda-a50f-1744fd8b7851", - "name" => "compute.midonet" - } - ] - } + let(:host_to_unregister) { "04f7361c-4cb8-4cda-a50f-1744fd8b7851"} let(:host_left_in_tunnelzone) { [ @@ -145,7 +124,7 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) allow(provider).to receive(:call_get_token).and_return('thisisafaketoken') # Set the behaviour expectations - expect(provider).to receive(:call_delete_tunnelzone_host).with(tzones[0]['id'], host_to_unregister[0]['id']) + expect(provider).to receive(:call_delete_tunnelzone_host).with(tzones[0]['id'], host_to_unregister) expect(provider).not_to receive(:call_delete_tunnelzone) provider.destroy @@ -236,14 +215,7 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) ] } - let(:hosts) { - [ - { - "id" => "04f7361c-4cb8-4cda-a50f-1744fd8b7851", - "name" => "compute.midonet" - } - ] - } + let(:hosts) {"04f7361c-4cb8-4cda-a50f-1744fd8b7851" } it 'should not fail' do allow(provider).to receive(:call_get_tunnelzone).and_return(tzones) @@ -253,7 +225,7 @@ describe Puppet::Type.type(:midonet_host_registry).provider(:midonet_api_caller) expect(provider).to receive(:call_get_tunnelzone).once expect(provider).to receive(:call_get_host).once - expect(provider).to receive(:call_get_tunnelzone_host).once.with(tzones[0]['id'], hosts[0]['id']) + expect(provider).to receive(:call_get_tunnelzone_host).once.with(tzones[0]['id'], hosts) provider.destroy end diff --git a/spec/unit/puppet/type/midonet_host_registry_spec.rb b/spec/unit/puppet/type/midonet_host_registry_spec.rb index c912c6f..89ae834 100644 --- a/spec/unit/puppet/type/midonet_host_registry_spec.rb +++ b/spec/unit/puppet/type/midonet_host_registry_spec.rb @@ -8,7 +8,7 @@ describe Puppet::Type::type(:midonet_host_registry) do context 'on default values' do let(:resource) do Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin') @@ -22,23 +22,11 @@ describe Puppet::Type::type(:midonet_host_registry) do end end - context 'on invalid hostname' do - it do - expect { - Puppet::Type.type(:midonet_host_registry).new( - :hostname => '_invalid_hostname.local', - :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', - :username => 'admin', - :password => 'admin') - }.to raise_error(Puppet::ResourceError) - end - end - context 'on invalid api url' do it do expect { Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => '87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin') @@ -49,7 +37,7 @@ describe Puppet::Type::type(:midonet_host_registry) do context 'on tenant_name valid value' do let(:resource) do Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin', @@ -64,7 +52,7 @@ describe Puppet::Type::type(:midonet_host_registry) do context 'on tunnelzone valid name' do let(:resource) do Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin', @@ -79,7 +67,7 @@ describe Puppet::Type::type(:midonet_host_registry) do context 'on tunnelzone valid type gre' do let(:resource) do Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin', @@ -94,7 +82,7 @@ describe Puppet::Type::type(:midonet_host_registry) do context 'on tunnelzone valid type vxlan' do let(:resource) do Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin', @@ -110,7 +98,7 @@ describe Puppet::Type::type(:midonet_host_registry) do it do expect { Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin', @@ -122,7 +110,7 @@ describe Puppet::Type::type(:midonet_host_registry) do context 'on underlay_ip_address valid IP' do let(:resource) do Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin', @@ -139,7 +127,7 @@ describe Puppet::Type::type(:midonet_host_registry) do it do expect { Puppet::Type.type(:midonet_host_registry).new( - :hostname => Facter['hostname'].value, + :title => 'test', :midonet_api_url => 'http://87.23.43.2:8080/midonet-api', :username => 'admin', :password => 'admin',