This commit is contained in:
James Slagle 2013-08-08 09:25:08 -04:00
parent 10c5ec8dd0
commit e0852a0c0b
10 changed files with 236 additions and 0 deletions

0
dib_elements/__init__.py Normal file
View File

51
dib_elements/element.py Normal file
View File

@ -0,0 +1,51 @@
# Copyright 2013, Red Hat Inc.
# All Rights Reserved.
#
# 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.
import logging
import os
class Element(object):
def __init__(self, directory):
logging.debug('initializing element: %s' % directory)
if not os.access(directory, os.R_OK):
raise Exception
self.directory = directory
self.hooks = {}
self.load_hooks()
def load_hooks(self):
for f in os.listdir(self.directory):
if not os.path.isdir(os.path.join(self.directory, f)):
continue
if not f.endswith('.d'):
continue
hook = f[:-2]
hook_path = os.path.join(self.directory, f)
logging.debug(' found hook: %s' % hook)
for script in os.listdir(hook_path):
logging.debug(' found script: %s' % script)
self.hooks.setdefault(
hook, []).append(os.path.join(hook_path, script))
def get_hook(self, hook):
return self.hooks.get(hook, [])

45
dib_elements/main.py Normal file
View File

@ -0,0 +1,45 @@
# Copyright 2013, Red Hat Inc.
# All Rights Reserved.
#
# 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.
import argparse
import logging
from dib_elements import manager
def load_args():
parser = argparse.ArgumentParser(
description="Execute diskimage-builder elements on the current system.")
parser.add_argument(
'-e', '--element', nargs='*',
help="element(s) to execute")
parser.add_argument(
'-p', '--element-path', nargs='*',
help=("element path(s) to search for elements (ELEMENTS_PATH "
"environment variable will take precedence if defined)"))
return parser.parse_args()
def main():
args = load_args()
logging.basicConfig(level=logging.DEBUG,
format="%(levelname)s:%(asctime)s:%(name)s:%(message)s")
em = manager.ElementManager(args.element, args.element_path)
em.run_hook('install')
if __name__ == '__main__':
main()

74
dib_elements/manager.py Normal file
View File

@ -0,0 +1,74 @@
# Copyright 2013, Red Hat Inc.
# All Rights Reserved.
#
# 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.
import logging
import os
from dib_elements.element import Element
from dib_elements.util import call
from diskimage_builder.elements import expand_dependencies
class ElementManager(object):
def __init__(self, elements, element_paths=None):
self.elements = elements
if os.environ.has_key('ELEMENTS_PATH'):
self.element_paths = os.environ['ELEMENTS_PATH'].split(':')
else:
self.element_paths = element_paths
if self.element_paths is None:
raise Exception
logging.debug('manager initialized with elements path: %s' %
self.element_paths)
self.loaded_elements = {}
self.load_elements()
self.load_dependencies()
def load_elements(self):
for path in self.element_paths:
self.process_path(path)
def process_path(self, path):
if not os.access(path, os.R_OK):
raise Exception
for element in os.listdir(path):
if not os.path.isdir(os.path.join(path, element)):
continue
if self.loaded_elements.has_key(element):
raise Exception
self.loaded_elements[element] = Element(os.path.join(path, element))
def load_dependencies(self):
all_elements = expand_dependencies(
self.loaded_elements.keys(), ':'.join(self.element_paths))
self.elements = all_elements
def run_hook(self, hook):
scripts = []
for element in self.loaded_elements:
if element in self.elements:
scripts += self.loaded_elements[element].get_hook(hook)
scripts = sorted(scripts, key=lambda script: os.path.basename(script))
for script in scripts:
call(['sudo', '-i', '/bin/bash', script])

29
dib_elements/util.py Normal file
View File

@ -0,0 +1,29 @@
# Copyright 2013, Red Hat Inc.
# All Rights Reserved.
#
# 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.
import logging
import subprocess
def call(*args, **kwargs):
logging.debug('executing command: %s' % args)
p = subprocess.Popen(*args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, **kwargs)
rc = p.wait()
logging.debug(' stdout: %s' % p.stdout.read())
logging.debug(' stderr: %s' % p.stderr.read())
logging.debug(' exited with code: %s' % rc)

29
setup.py Normal file
View File

@ -0,0 +1,29 @@
# Copyright 2013, Red Hat Inc.
# All Rights Reserved.
#
# 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.
from setuptools import setup, find_packages
setup(
name = "python-dib-elements",
description = "Execute diskimage-builder elements on the current system.",
version = "0.1",
packages = find_packages(),
entry_points = {
'console_scripts': [
'dib-elements = dib_elements.main:main',
],
},
)

View File

@ -0,0 +1,2 @@
#!/bin/bash
echo DEP1 ELEMENT

View File

@ -0,0 +1 @@
dep1

View File

@ -0,0 +1,2 @@
#!/bin/bash
echo DEP2 ELEMENT

View File

@ -0,0 +1,3 @@
#!/bin/bash
echo TEST ELEMENT