Skip to content

Commit

Permalink
Adding support for setting FQDN
Browse files Browse the repository at this point in the history
  • Loading branch information
Suszyński Krzysztof committed Aug 9, 2017
1 parent 6b55f24 commit 7aa6c19
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 39 deletions.
17 changes: 17 additions & 0 deletions puppeter/domain/gateway/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from abc import ABCMeta, abstractmethod

from six import with_metaclass
from typing import Sequence

from puppeter.domain.model.configurer import Configurer


class ScriptPostProcessor(with_metaclass(ABCMeta)):
@abstractmethod
def postprocess(self, commands):
# type: (Sequence[str]) -> Sequence[str]
pass


class ScriptLibrariesConfigurer(with_metaclass(ABCMeta, Configurer)):
pass
5 changes: 4 additions & 1 deletion puppeter/persistence/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from puppeter import container
from puppeter.domain.gateway.answers import AnswersGateway
from puppeter.domain.gateway.fqdn import FqdnSetterGateway
from puppeter.domain.gateway.script import ScriptPostProcessor, ScriptLibrariesConfigurer
from puppeter.persistence.gateway.answers import YamlAnswersGateway
from puppeter.persistence.gateway.fqdn import FqdnSetterGatewayImpl

from puppeter.persistence.gateway.script import BashScriptPostProcessor, BashScriptLibrariesConfigurer

container.bind(AnswersGateway, YamlAnswersGateway)
container.bind(FqdnSetterGateway, FqdnSetterGatewayImpl)
container.bind(ScriptPostProcessor, BashScriptPostProcessor)
container.bind(ScriptLibrariesConfigurer, BashScriptLibrariesConfigurer)
17 changes: 17 additions & 0 deletions puppeter/persistence/gateway/bash-libraries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set +x
function reload_shell {
set +e
local files='~/.bash_profile ~/.bash_login ~/.profile'
if [ -f /etc/profile ]; then
. /etc/profile
fi
for file in ${files}; do
if [ -f ${file} ]; then
. ${file}
break
fi
done
set -e
}
set -x
4 changes: 3 additions & 1 deletion puppeter/persistence/gateway/fqdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from puppeter.domain.model.configurer import Configurer, CommandsCollector, ScriptFormat
from puppeter.domain.model.fqdn import FqdnConfiguration
from puppeter.domain.model.ordered import Order
from puppeter.domain.model.osfacts import OsFamily
from puppeter.domain.model.osfacts import OsFamily, Docker

FQDN_ORDER = 200

Expand Down Expand Up @@ -63,6 +63,8 @@ def _persist_change(self, collector):
class FqdnSetterGatewayImpl(FqdnSetterGateway):
def process_fully_qualified_domain_name(self, fqdn):
# type: (FqdnConfiguration) -> Sequence[Configurer]
if Facter.get(Docker) == Docker.YES:
raise NotImplementedError('Can\'t set FQDN when running inside Docker!')
osfamily = Facter.get(OsFamily)
try:
return {
Expand Down
3 changes: 1 addition & 2 deletions puppeter/persistence/gateway/installer/debian/puppetagent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ set +e
if ! dpkg -l 'puppet-agent' | grep -q ii; then
set -e
apt-get install -y puppet-agent
su - $(whoami)
set -ex
reload_shell
fi
27 changes: 27 additions & 0 deletions puppeter/persistence/gateway/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re

from puppeter.domain.gateway.script import ScriptPostProcessor, ScriptLibrariesConfigurer
from puppeter.domain.model.ordered import Order


class BashScriptPostProcessor(ScriptPostProcessor):

SHEBANG_REGEX = re.compile('^#!(.*)\n', re.MULTILINE)

def postprocess(self, commands):
ln = "\n"
header = ['#!/usr/bin/env bash', 'set -ex', '']
stripped = self.__strip_shebang(ln.join(commands)).split(ln)
return header + stripped

@staticmethod
def __strip_shebang(script):
return re.sub(BashScriptPostProcessor.SHEBANG_REGEX, '', script)


@Order(-1000)
class BashScriptLibrariesConfigurer(ScriptLibrariesConfigurer):
def produce_commands(self):
return self._collector()\
.collect_from_file('Bash script libraries', 'bash-libraries.sh')\
.lines()
5 changes: 2 additions & 3 deletions puppeter/persistence/gateway/set-fqdn.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash
hostname '@{hostname}'
puppet resource host '@{fqdn}' ensure=present host_aliases='@{hostname}' ip=127.0.0.1 comment='FQDN'
su - $(whoami)
set -ex
hostname '@{hostname}'
reload_shell
19 changes: 9 additions & 10 deletions puppeter/presentation/answersprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from argparse import Namespace
from typing import Sequence, List

from pip._vendor.packaging.markers import Op

import puppeter
from puppeter import container
from puppeter.domain.facter import Facter
from puppeter.domain.gateway.answers import AnswersProcessor
from puppeter.domain.gateway.fqdn import FqdnSetterGateway
from puppeter.domain.gateway.installer import InstallerGateway
from puppeter.domain.gateway.script import ScriptPostProcessor, ScriptLibrariesConfigurer
from puppeter.domain.model import osfacts
from puppeter.domain.model.answers import Answers
from puppeter.domain.model.configurer import Configurer
Expand All @@ -18,14 +17,13 @@

class AnswersProcessorImpl(AnswersProcessor):

SHEBANG_REGEX = re.compile('^#!(.*)\n', re.MULTILINE)

def __init__(self, options):
self.options = options # type: Options
self.__log = puppeter.get_logger(AnswersProcessorImpl)

def process(self, answers):
configurers = []
configurers.extend(self.__attach_libs())
configurers.extend(self.__perform_installation(answers))
configurers.extend(self.__setup_fqdn(answers))
commands = self.__collect_commands(configurers)
Expand Down Expand Up @@ -79,11 +77,12 @@ def __sort_by_order(configurer):
@classmethod
def __postprocess(cls, commands):
# type: (Sequence[str]) -> Sequence[str]
ln = "\n"
header = ['#!/usr/bin/env bash', 'set -ex', '']
stripped = cls.__strip_shebang(ln.join(commands)).split(ln)
return header + stripped
postprocessor = container.get(ScriptPostProcessor)
return postprocessor.postprocess(commands)

@staticmethod
def __strip_shebang(script):
return re.sub(AnswersProcessorImpl.SHEBANG_REGEX, '', script)
def __attach_libs():
# type: () -> Sequence[Configurer]
return [
container.get(ScriptLibrariesConfigurer)
]
4 changes: 2 additions & 2 deletions tests/persistence/gateway/installer/debian/test_pc3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def test_pc3x():

# then
assert 'apt-get update -m' in commands
assert 'sudo apt-get install -y wget' in commands
assert 'apt-get install -y wget' in commands
assert "wget 'https://apt.puppetlabs.com/puppetlabs-release-trusty.deb'" in commands
assert 'sudo apt-get install -y puppet' in commands
assert 'apt-get install -y puppet' in commands
assert 'puppet resource package puppetmaster ensure=installed' in commands


Expand Down
4 changes: 2 additions & 2 deletions tests/persistence/gateway/installer/debian/test_pc4x.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def test_pc4x():

# then
assert 'apt-get update -m' in commands
assert 'sudo apt-get install -y wget' in commands
assert 'apt-get install -y wget' in commands
assert "wget 'https://apt.puppetlabs.com/puppetlabs-release-pc1-precise.deb'" in commands
assert 'sudo apt-get install -y puppet-agent' in commands
assert 'apt-get install -y puppet-agent' in commands
assert 'puppet resource package puppetserver ensure=installed' in commands


Expand Down
4 changes: 2 additions & 2 deletions tests/persistence/gateway/installer/debian/test_pc5x.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_pc5x():
# then
assert 'if [[ "$(getLastAptGetUpdate)" -gt \'86400\' ]]; then' in commands
assert 'apt-get update -m' in commands
assert 'sudo apt-get install -y wget' in commands
assert 'apt-get install -y wget' in commands
assert "wget 'https://apt.puppetlabs.com/puppet5-release-xenial.deb'" in commands
assert 'sudo apt-get install -y puppet-agent' in commands
assert 'apt-get install -y puppet-agent' in commands
assert 'puppet resource package puppetserver ensure=installed' in commands


Expand Down
16 changes: 8 additions & 8 deletions tests/persistence/gateway/installer/redhat/test_pc3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_pc3x_centos_5():
commands = list(map(lambda cmd: cmd.strip(), commands))

# then
assert 'sudo yum install -y wget' in commands
assert 'yum install -y wget' in commands
assert "wget 'https://yum.puppetlabs.com/puppetlabs-release-el-5.noarch.rpm'" in commands
assert 'sudo yum install -y puppet' in commands
assert 'yum install -y puppet' in commands
assert 'puppet resource package puppetmaster ensure=installed' in commands


Expand All @@ -37,9 +37,9 @@ def test_pc3x_scientific_7():
commands = list(map(lambda cmd: cmd.strip(), commands))

# then
assert 'sudo yum install -y wget' not in commands
assert "sudo rpm -Uvh 'https://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm'" in commands
assert 'sudo yum install -y puppet' in commands
assert 'yum install -y wget' not in commands
assert "rpm -Uvh 'https://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm'" in commands
assert 'yum install -y puppet' in commands
assert 'puppet resource package puppetmaster ensure=installed' in commands


Expand All @@ -56,9 +56,9 @@ def test_pc3x_fedora_25():
commands = list(map(lambda cmd: cmd.strip(), commands))

# then
assert 'sudo yum install -y wget' not in commands
assert "sudo rpm -Uvh 'https://yum.puppetlabs.com/puppetlabs-release-fedora-25.noarch.rpm'" in commands
assert 'sudo yum install -y puppet' in commands
assert 'yum install -y wget' not in commands
assert "rpm -Uvh 'https://yum.puppetlabs.com/puppetlabs-release-fedora-25.noarch.rpm'" in commands
assert 'yum install -y puppet' in commands
assert 'puppet resource package puppetmaster ensure=installed' not in commands


Expand Down
10 changes: 5 additions & 5 deletions tests/persistence/gateway/installer/redhat/test_pc4x.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_pc4x_oraclelinux5():
commands = list(map(lambda cmd: cmd.strip(), commands))

# then
assert 'sudo yum install -y wget' in commands
assert 'yum install -y wget' in commands
assert "wget 'https://yum.puppetlabs.com/puppetlabs-release-pc1-el-5.noarch.rpm'" in commands
assert 'sudo yum install -y puppet-agent' in commands
assert 'yum install -y puppet-agent' in commands
assert 'puppet resource package puppetserver ensure=installed' in commands


Expand All @@ -37,9 +37,9 @@ def test_pc4x_rhel_6():
commands = list(map(lambda cmd: cmd.strip(), commands))

# then
assert 'sudo yum install -y wget' not in commands
assert "sudo rpm -Uvh 'https://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm'" in commands
assert 'sudo yum install -y puppet-agent' in commands
assert 'yum install -y wget' not in commands
assert "rpm -Uvh 'https://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm'" in commands
assert 'yum install -y puppet-agent' in commands
assert 'puppet resource package puppetserver ensure=installed' not in commands


Expand Down
6 changes: 3 additions & 3 deletions tests/persistence/gateway/installer/redhat/test_pc5x.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_pc5x():
commands = list(map(lambda cmd: cmd.strip(), commands))

# then
assert 'sudo yum install -y wget' not in commands
assert "sudo rpm -Uvh 'https://yum.puppetlabs.com/puppet5-release-el-7.noarch.rpm'" in commands
assert 'sudo yum install -y puppet-agent' in commands
assert 'yum install -y wget' not in commands
assert "rpm -Uvh 'https://yum.puppetlabs.com/puppet5-release-el-7.noarch.rpm'" in commands
assert 'yum install -y puppet-agent' in commands
assert 'puppet resource package puppetserver ensure=installed' in commands


Expand Down

0 comments on commit 7aa6c19

Please sign in to comment.