Fix the midonet role

Change-Id: Ib17b95f5a6a7137c91eb92a2472513d45ea15f86
This commit is contained in:
Alex Ruiz Estradera 2016-08-23 18:47:58 +02:00
parent 288d1dc7c1
commit 3e1b84a356
8 changed files with 98 additions and 100 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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,

View File

@ -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)
},

View File

@ -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

View File

@ -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

View File

@ -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',