All of the hosts added to /etc/hosts on each vm

Information about the hosts is placed into hiera database. And then
records about hosts created in hosts file by calling
puppet "create_resources". To make the code more concise the method
Puppet.executeInline was added.

Change-Id: Ib350e4833a992bc112731df94bba217bf84f0d0a
This commit is contained in:
Alexey Khivin 2016-05-17 12:35:31 +03:00 committed by Alexey Khivin
parent 41c4b991e6
commit 534126e732
8 changed files with 171 additions and 2 deletions

View File

@ -5,6 +5,7 @@ Namespaces:
sys: io.murano.system
ci_cd_pipeline_murano_app: org.openstack.ci_cd_pipeline_murano_app
puppet: org.openstack.ci_cd_pipeline_murano_app.puppet
net: org.openstack.ci_cd_pipeline_murano_app.utils.net
Name: CiCdEnvironment
@ -128,3 +129,13 @@ Methods:
- $this.ldap.deploy()
- $this.gerrit.deploy()
- $this.jenkins.deploy()
- $._environment.reporter.report($this, 'Adding hosts...')
- $hosts: new(net:Hosts)
- $apps: [$.jenkins, $.gerrit, $.ldap]
- $apps.select($hosts.addHostByInstance($.instance))
- $apps.select($hosts.applyTo($.instance))
- $._environment.reporter.report($this, 'Hosts added.')

View File

@ -15,3 +15,4 @@ Require:
org.openstack.ci_cd_pipeline_murano_app.Jenkins:
org.openstack.ci_cd_pipeline_murano_app.Gerrit:
org.openstack.ci_cd_pipeline_murano_app.puppet.Puppet:
org.openstack.ci_cd_pipeline_murano_app.utils.CiCdUtils:

View File

@ -0,0 +1,91 @@
Namespaces:
=: org.openstack.ci_cd_pipeline_murano_app.utils.net
std: io.murano
res: io.murano.resources
sys: io.murano.system
puppet: org.openstack.ci_cd_pipeline_murano_app.puppet
conf: io.murano.configuration
Name: Hosts
Properties:
# The list of entries to be added to /etc/hosts
hosts:
Contract: {}
Usage: InOut
Methods:
.init:
Body:
- $this.hosts: dict()
#
# Add new entry to be put into /etc/hosts
#
addHost:
Arguments:
hostname:
Contract: $.string().notNull()
ip:
Contract: $.string().notNull()
aliases:
Contract: [$.string()]
Default: []
Body:
- $this.hosts[$hostname]:
name: $hostname
ip: $ip
host_aliases: $aliases
#
# Ask instance it's hostname and add it by calling addHost
#
addHostByInstance:
Arguments:
instance:
Contract: $.class(res:LinuxMuranoInstance).notNull()
aliases:
Contract: [$.string()]
Default: []
Body:
- $hostname: $this.getHostName($instance, true)
- $shortname: $this.getHostName($instance, false)
- $aliases: $aliases.append($shortname)
- $ip: $instance.ipAddresses[0]
- $this.addHost(hostname=>$hostname, ip=>$ip, aliases=>$aliases)
#
# Get the hostname which gives us OS
#
getHostName:
Arguments:
instance:
Contract: $.class(res:LinuxMuranoInstance).notNull()
fqdn:
Contract: $.bool().notNull()
Default: false
Body:
- $linux: new(conf:Linux)
- If: $fqdn
Then:
- Return: $linux.runCommand($instance.agent, 'hostname -f').stdout
- Return: $linux.runCommand($instance.agent, 'hostname -s').stdout
# Put all of the hosts to the particular instance
# All of the data will be added to the hiera
# and then puppet scenario will generate hosts entries
#
applyTo:
Arguments:
instance:
Contract: $.class(puppet:PuppetInstance)
Body:
- $data:
hosts: $this.hosts
- $instance.putHieraData($data)
- $instance.syncHieraData()
- $resources: new(sys:Resources)
- $instance.executeInline("create_resources('host', hiera('hosts'))")

View File

@ -8,4 +8,7 @@ Description: |
Author: 'Mirantis, Inc'
Tags: [CI, Utils]
Classes:
# TBD.
org.openstack.ci_cd_pipeline_murano_app.utils.net.Hosts: net/Hosts.yaml
Require:
org.openstack.ci_cd_pipeline_murano_app.puppet.Puppet:

View File

@ -124,3 +124,18 @@ Methods:
Body:
- $.hiera.putData($data)
#
# Apply puppet manifest to the particular instance
# Known issues:
# * do not use " inside the code parameter
#
executeInline:
Arguments:
- code:
Contract: $.string().notNull()
Body:
- $resources: new(sys:Resources)
- $template: $resources.yaml('PuppetExecuteInline.template').bind(dict(
code => $code
))
- Return: $.agent.call($template, $resources)

View File

@ -0,0 +1,34 @@
# 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.
FormatVersion: 2.1.0
Version: 1.0.0
Name: RunPuppetInline
Parameters:
code: $code
Body: |
import base64
param = base64.b64encode(args.code)
return runPuppet(param).stdout
Scripts:
runPuppet:
Type: Application
Version: 1.0.0
EntryPoint: exec_puppet_inline.sh
Files: []
Options:
captureStdout: true
captureStderr: true
verifyExitcode: true

View File

@ -1,4 +1,4 @@
FormatVersion: 2.0.0
FormatVersion: 2.1.0
Version: 1.0.0
Name: Hiera Data

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
set +e
PUPPET_CODE=`echo -n $1 | base64 -d`
puppet apply --detailed-exitcodes --color=false --execute "${PUPPET_CODE}"
PUPPET_RETURN=$?
if [ "${PUPPET_RETURN}" -eq 4 ] || [ "${PUPPET_RETURN}" -eq 6 ] ; then
exit ${PUPPET_RETURN}
fi
set -e