Commit 1b70448e1217987ef48c17f1d905bf877f54ca20

Authored by Charles Oliveira
1 parent f4939c75

Added support for centos distribuiton

Showing 2 changed files with 86 additions and 70 deletions   Show diff stats
fabfile.py
... ... @@ -9,7 +9,6 @@ from fabric.api import env, run, sudo, local
9 9 from fabric.contrib.files import exists
10 10 from fabric.context_managers import prefix, cd, settings, shell_env
11 11  
12   -LINUX_DISTRO = ''
13 12 DEBIAN_FAMILY = ['debian', 'ubuntu']
14 13 REDHAT_FAMILY = ['centos', 'fedora']
15 14  
... ...
puppet/bootstrap.py
1 1 #!/usr/bin/env python2.7
2 2  
3 3 import os
4   -import apt
5   -import apt_pkg
  4 +import re
6 5 import locale
7 6 import platform
8 7 import subprocess
9 8 import urllib
10 9  
11   -from apt import debfile
12 10 from distutils.version import StrictVersion
13 11 from pkg_resources import parse_requirements
14 12 from shutil import copyfile
15   -from subprocess import check_output
16 13  
17   -
18   -PUPPET_TARGET_VERSION="3.4.3-1"
  14 +PUPPET_TARGET_VERSION = "3.6.2"
19 15 PUPPET_DIR = os.path.join(os.path.dirname(__file__))
20 16 MODULES_FILE_PATH = os.path.join(PUPPET_DIR, 'modules.txt')
21 17  
22 18  
23   -def get_package(name):
24   - cache = apt.cache.Cache()
25   - if name in cache:
26   - return cache[name], cache
27   -
28   - return None, None
29   -
30   -
31   -def pkg_available(name):
32   - pkg = get_package(name)[0]
33   - if pkg and pkg.versions.get(PUPPET_TARGET_VERSION):
34   - return True
35   -
36   - return False
37   -
38   -
39   -def config_puppetlabs_repo():
40   - dist = platform.dist()[-1]
41   -
42   - url = 'http://apt.puppetlabs.com/puppetlabs-release-{}.deb'.format(dist)
43   - filename = '/tmp/puppet_apt.deb'
44   - try:
45   - urllib.urlretrieve(url, filename)
46   - except IOError:
47   - print "Could not install puppet"
48   - raise
49   -
50   - deb_pkg = debfile.DebPackage(filename)
51   - if deb_pkg.check():
52   - deb_pkg.install()
53   - cache = apt.cache.Cache()
54   - cache.update()
55   - cache.open()
56   -
57   -
58   -def install_puppet(upgrade=False):
59   - pkg, cache = get_package('puppet')
60   -
61   - pkg.candidate = pkg.versions.get(PUPPET_TARGET_VERSION)
62   - if upgrade:
63   - pkg.mark_upgrade()
  19 +def get_release_name():
  20 + distro = platform.dist()[0].lower()
  21 + if distro == 'centos':
  22 + with open('/etc/centos-release') as release_file:
  23 + regex = r'CentOS release (\d)'
  24 + release = re.search(regex, release_file.read()).group(1)
  25 + return 'centos', release
  26 + elif distro == 'ubuntu':
  27 + with open('/etc/lsb-release') as release_file:
  28 + regex = r'DISTRIB_CODENAME=([a-z]+)'
  29 + release = re.search(regex, release_file.read()).group(1)
  30 + return 'ubuntu', release
64 31 else:
65   - pkg.mark_install()
66   -
67   - cache.commit()
  32 + return '', ''
68 33  
69 34  
70 35 def get_modules_installed():
71   - modules_list = check_output(['puppet', 'module', 'list']).split('\n')
  36 + modules_list = os.popen('puppet module list').read().split('\n')
72 37 modules_list = [line for line in modules_list if 'no modules' not in line]
73 38 modules_list = [item.split()[1:] for item in modules_list]
74 39 modules_list = [item for item in modules_list if item]
... ... @@ -83,9 +48,9 @@ def get_modules_installed():
83 48  
84 49 def run(cmd, module, version=''):
85 50 if version:
86   - version = ' --version {}'.format(version)
  51 + version = ' --version %s' % (version)
87 52  
88   - cmd = 'puppet module {} {}{}'.format(cmd, module, version)
  53 + cmd = 'puppet module %s %s%s' % (cmd, module, version)
89 54 process = subprocess.Popen(cmd, shell=True, executable='/bin/bash')
90 55 process.wait()
91 56  
... ... @@ -97,16 +62,19 @@ def install_puppet_modules():
97 62 modules_requirements = modules_file.read().replace('/', '-')
98 63  
99 64 for module in parse_requirements(modules_requirements):
100   -
101 65 current_cmd, compare, version, version_comparison = '', '', '', None
102 66 if module.project_name in modules_installed:
103   -
104 67 if module.specs:
105 68 compare, version = module.specs[0]
106   - version_comparison = apt_pkg.version_compare(
107   - modules_installed[module.project_name],
108   - version
109   - )
  69 +
  70 + tmp_version = modules_installed[module.project_name]
  71 + installed_version = StrictVersion(tmp_version)
  72 + required_version = StrictVersion(version)
  73 +
  74 + if installed_version >= required_version:
  75 + version_comparison = 0
  76 + else:
  77 + version_comparison = -1
110 78 else:
111 79 continue
112 80  
... ... @@ -125,17 +93,65 @@ def install_puppet_modules():
125 93 if not version_comparison or version_comparison < 0:
126 94 run(current_cmd, module.project_name)
127 95  
128   -def main():
129   - # If the package not found or if the version is outdated, install puppet
130   - if not pkg_available('puppet'):
131   - config_puppetlabs_repo()
132 96  
133   - pkg = get_package('puppet')[0]
134   - if not pkg.is_installed:
135   - install_puppet()
136   - elif apt_pkg.version_compare(pkg.installed.version,
137   - PUPPET_TARGET_VERSION) < 0:
138   - install_puppet(upgrade=True)
  97 +def iscentos(distro):
  98 + return distro == 'centos'
  99 +
  100 +
  101 +def isubuntu(distro):
  102 + return distro == 'ubuntu'
  103 +
  104 +
  105 +def download(url, filename):
  106 + try:
  107 + urllib.urlretrieve(url, filename)
  108 + except IOError:
  109 + print "Could not install puppet"
  110 + raise
  111 +
  112 +
  113 +def main():
  114 + distro, release = get_release_name()
  115 + print('Distro %s, release %s' % (distro, release))
  116 +
  117 + if iscentos(distro):
  118 + cmd = 'rpm'
  119 + flags = '-ivh'
  120 + url = 'http://yum.puppetlabs.com/'
  121 + pkg = 'puppetlabs-release-el-%s.noarch.rpm' % (release)
  122 + update = ['yum', 'update', '-y']
  123 + install = ['yum', 'install', 'puppet', '-y']
  124 + elif isubuntu(distro):
  125 + cmd = 'dpkg'
  126 + flags = '-i'
  127 + url = 'https://apt.puppetlabs.com/'
  128 + pkg = 'puppetlabs-release-%s.deb' % (release)
  129 + update = ['apt-get', 'update', '-y']
  130 + install = ['apt-get', 'install', 'puppet', '-y']
  131 +
  132 + # Needed dependency for import pkg_resources
  133 + pkg_res = ['apt-get', 'install', 'python-pkg-resources', '-y']
  134 + subprocess.call(pkg_res)
  135 + else:
  136 + print('This distribuition is currently not supported!')
  137 + print('exiting...')
  138 + exit(1)
  139 +
  140 + tmp_file = '/tmp/%s' % (pkg)
  141 + download(url + pkg, tmp_file)
  142 + args = [cmd, flags, tmp_file]
  143 +
  144 + # Add repository
  145 + result = subprocess.call(args)
  146 + if result != 0:
  147 + print('Repository %s already set' % pkg)
  148 +
  149 + # Install Puppet
  150 + subprocess.call(update)
  151 + result = subprocess.call(install)
  152 + if result != 0:
  153 + print('Failed installing puppet')
  154 + return result
139 155  
140 156 if os.path.isfile('/vagrant/puppet/hiera.yaml'):
141 157 copyfile('/vagrant/puppet/hiera.yaml', '/etc/puppet/hiera.yaml')
... ... @@ -143,6 +159,7 @@ def main():
143 159 locale.setlocale(locale.LC_ALL, '')
144 160  
145 161 install_puppet_modules()
  162 + return 0
146 163  
147 164  
148 165 if __name__ == '__main__':
... ...