Packages definition #18

Merged
merged 2 commits into from Jan 12, 2016
Jump to file or symbol
Failed to load files and symbols.
+57 −1
Split
View
@@ -1,9 +1,13 @@
#!/usr/bin/env python3
# Load modules from $CHARM_DIR/lib
+import os
import sys
sys.path.append('lib')
+# This is an upgrade-charm context, make sure we install latest deps
+os.unlink('wheelhouse/.bootstrapped')
+
from charms.bootstrap import bootstrap_charm_deps
bootstrap_charm_deps()
View
@@ -0,0 +1,5 @@
+defines:
+ packages:
+ type: array
+ description: Additional packages to be installed at time of bootstrap
+
View
@@ -9,7 +9,9 @@ def bootstrap_charm_deps():
return
# bootstrap wheelhouse
if os.path.exists('wheelhouse'):
- check_call(['apt-get', 'install', '-yq', 'python3-pip'])
+ apt_install(['python3-pip', 'python3-yaml'])
+ # install packages defined in layer.yaml
+ install_charm_deps()
# need newer pip, to fix spurious Double Requirement error https://github.com/pypa/pip/issues/56
check_call(['pip3', 'install', '-U', '--no-index', '-f', 'wheelhouse', 'pip'])
# install the rest of the wheelhouse deps
@@ -21,3 +23,26 @@ def bootstrap_charm_deps():
# Non-namespace-package libs (e.g., charmhelpers) are available
# without having to reload the interpreter. :/
os.execl(sys.argv[0], sys.argv[0])
+
+
+def install_charm_deps():
+ from charms import layer
+
+ cfg = layer.options('basic')
+ apt_install(cfg.get('packages', []))
+
+
+def apt_install(packages):
+ if isinstance(packages, basestring):
+ packages = [packages]
+
+ env = os.environ.copy()
+
+ if 'DEBIAN_FRONTEND' not in env:
+ env['DEBIAN_FRONTEND'] = 'noninteractive'
+
+ cmd = ['apt-get',
+ '--option=Dpkg::Options::=--force-confold',
+ '--assume-yes',
+ 'install']
+ check_call(cmd + packages, env=env)
View
@@ -0,0 +1,22 @@
+
+import os
+import yaml
+
+
+class LayerOptions(dict):
+ def __init__(self, layer_file, section=None):
+ with open(layer_file) as f:
+ layer = yaml.safe_load(f.read())
+ opts = layer.get('options', {})
+ if section and section in opts:
+ super(LayerOptions, self).__init__(opts.get(section))
+ else:
+ super(LayerOptions, self).__init__(opts)
+
+
+def options(section=None, layer_file=None):
+ if not layer_file:
+ base_dir = os.environ.get('CHARM_DIR', os.getcwd())
+ layer_file = os.path.join(base_dir, 'layer.yaml')
+
+ return LayerOptions(layer_file, section)