Commit 89f781d03cf9d6b98e9d8b0959340e3489d6341b
1 parent
677567b6
Exists in
master
and in
39 other branches
Replaced script .sh by .py
Showing
2 changed files
with
149 additions
and
15 deletions
Show diff stats
@@ -0,0 +1,149 @@ | @@ -0,0 +1,149 @@ | ||
1 | +#!/usr/bin/env python2.7 | ||
2 | + | ||
3 | +import os | ||
4 | +import apt | ||
5 | +import apt_pkg | ||
6 | +import locale | ||
7 | +import platform | ||
8 | +import subprocess | ||
9 | +import urllib | ||
10 | + | ||
11 | +from apt import debfile | ||
12 | +from distutils.version import StrictVersion | ||
13 | +from pkg_resources import parse_requirements | ||
14 | +from shutil import copyfile | ||
15 | +from subprocess import check_output | ||
16 | + | ||
17 | + | ||
18 | +PUPPET_TARGET_VERSION="3.4.3-1" | ||
19 | +PUPPET_DIR = os.path.join(os.path.dirname(__file__)) | ||
20 | +MODULES_FILE_PATH = os.path.join(PUPPET_DIR, 'modules.txt') | ||
21 | + | ||
22 | + | ||
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() | ||
64 | + else: | ||
65 | + pkg.mark_install() | ||
66 | + | ||
67 | + cache.commit() | ||
68 | + | ||
69 | + | ||
70 | +def get_modules_installed(): | ||
71 | + modules_list = check_output(['puppet', 'module', 'list']).split('\n') | ||
72 | + modules_list = [line for line in modules_list if 'no modules' not in line] | ||
73 | + modules_list = [item.split()[1:] for item in modules_list] | ||
74 | + modules_list = [item for item in modules_list if item] | ||
75 | + modules_dict = {} | ||
76 | + for name, version in modules_list: | ||
77 | + modules_dict[name] = version.split('v')[1].replace( | ||
78 | + '\x1b[0m)', | ||
79 | + '' | ||
80 | + ) | ||
81 | + return modules_dict | ||
82 | + | ||
83 | + | ||
84 | +def run(cmd, module, version=''): | ||
85 | + if version: | ||
86 | + version = ' --version {}'.format(version) | ||
87 | + | ||
88 | + cmd = 'puppet module {} {}{}'.format(cmd, module, version) | ||
89 | + process = subprocess.Popen(cmd, shell=True, executable='/bin/bash') | ||
90 | + process.wait() | ||
91 | + | ||
92 | + | ||
93 | +def install_puppet_modules(): | ||
94 | + modules_installed = get_modules_installed() | ||
95 | + | ||
96 | + with open(MODULES_FILE_PATH) as modules_file: | ||
97 | + modules_requirements = modules_file.read().replace('/', '-') | ||
98 | + | ||
99 | + for module in parse_requirements(modules_requirements): | ||
100 | + | ||
101 | + current_cmd, compare, version, version_comparison = '', '', '', None | ||
102 | + if module.project_name in modules_installed: | ||
103 | + | ||
104 | + if module.specs: | ||
105 | + compare, version = module.specs[0] | ||
106 | + version_comparison = apt_pkg.version_compare( | ||
107 | + modules_installed[module.project_name], | ||
108 | + version | ||
109 | + ) | ||
110 | + else: | ||
111 | + continue | ||
112 | + | ||
113 | + if version_comparison == 0 and compare is not '>': | ||
114 | + # module version installed is equal version | ||
115 | + continue | ||
116 | + else: | ||
117 | + # module version installed is smaller or bigger than version | ||
118 | + current_cmd = 'upgrade' | ||
119 | + else: | ||
120 | + current_cmd = 'install' | ||
121 | + | ||
122 | + if version and compare and '>' not in compare: | ||
123 | + run(current_cmd, module.project_name, version) | ||
124 | + else: | ||
125 | + if not version_comparison or version_comparison < 0: | ||
126 | + run(current_cmd, module.project_name) | ||
127 | + | ||
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 | + | ||
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) | ||
139 | + | ||
140 | + if os.path.isfile('/vagrant/puppet/hiera.yaml'): | ||
141 | + copyfile('/vagrant/puppet/hiera.yaml', '/etc/puppet/hiera.yaml') | ||
142 | + | ||
143 | + locale.setlocale(locale.LC_ALL, '') | ||
144 | + | ||
145 | + install_puppet_modules() | ||
146 | + | ||
147 | + | ||
148 | +if __name__ == '__main__': | ||
149 | + main() |
puppet/bootstrap.sh
@@ -1,15 +0,0 @@ | @@ -1,15 +0,0 @@ | ||
1 | -#!/bin/bash | ||
2 | - | ||
3 | -PUPPET_VERSION=`dpkg -l | grep puppet-common | awk '{ print $3 }' | cut -d- -f1` | ||
4 | -dpkg --compare-versions "$PUPPET_VERSION" "<" "3.3" ; OUTDATED=$? | ||
5 | - | ||
6 | -if [ $OUTDATED -eq 0 ] ; then | ||
7 | - wget -O /tmp/puppet_apt.deb http://apt.puppetlabs.com/puppetlabs-release-precise.deb &> /dev/null | ||
8 | - dpkg -i /tmp/puppet_apt.deb | ||
9 | - DEBIAN_FRONTEND=noninteractive apt-get update -y | ||
10 | - DEBIAN_FRONTEND=noninteractive apt-get install puppet -y | ||
11 | -fi | ||
12 | - | ||
13 | -cp /vagrant/puppet/hiera.yaml /etc/puppet/hiera.yaml -f | ||
14 | - | ||
15 | -update-locale LC_ALL='' |