Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream berskfile support #231

Closed
wants to merge 9 commits into from
11 changes: 9 additions & 2 deletions littlechef/chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
# Path to local patch
basedir = os.path.abspath(os.path.dirname(__file__).replace('\\', '/'))


def save_config(node, force=False):
"""Saves node configuration
if no nodes/hostname.json exists, or force=True, it creates one
Expand Down Expand Up @@ -142,6 +141,10 @@ def _synchronize_node(configfile, node):
for cookbook_path in cookbook_paths:
paths_to_sync.append('./{0}'.format(cookbook_path))

# Add berksfile directory to sync_list
if env.berksfile:
paths_to_sync.append(env.berksfile_cookbooks_directory)

rsync_project(
env.node_work_path,
' '.join(paths_to_sync),
Expand Down Expand Up @@ -330,13 +333,17 @@ def build_node_data_bag():
'data_bags', 'node', node['id'] + '.json'), 'w') as f:
f.write(json.dumps(node))


def remove_local_node_data_bag():
"""Removes generated 'node' data_bag locally"""
node_data_bag_path = os.path.join('data_bags', 'node')
if os.path.exists(node_data_bag_path):
shutil.rmtree(node_data_bag_path)

def cleanup_berksfile_cookbooks():
"""Removes berkshelf vendor directory """

if os.path.isdir(env.berksfile_cookbooks_directory):
shutil.rmtree(env.berksfile_cookbooks_directory)

def ensure_berksfile_cookbooks_are_installed():
"""Run 'berks vendor' to berksfile cookbooks directory"""
Expand Down
6 changes: 4 additions & 2 deletions littlechef/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def get_recipes_in_cookbook(name):
path = None
cookbook_exists = False
metadata_exists = False
for cookbook_path in cookbook_paths:

for cookbook_path in env.cookbook_search_paths:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is cookbook_search_paths for? I didn't find where it is initialized anywere...

OK, sorry, in _readconfig. But why not use cookbook_paths? It can be directly modified in the same way to append berksfile directories

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look at it today.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote this code I thought that it would be better to have one main arrays for all cookbook paths where others will be merged, because cookbook_paths were user customizable.

So I use cookbook_paths as place where to look for non berkshelf cookbooks and berksfile_cookbooks_directory as a place where I will berks vendor cookbooks. Then I will add both directories to one array and use them later.

path = os.path.join(cookbook_path, name)
path_exists = os.path.exists(path)
# cookbook exists if present in any of the cookbook paths
Expand Down Expand Up @@ -368,9 +369,10 @@ def get_recipes_in_node(node):
def get_recipes():
"""Gets all recipes found in the cookbook directories"""
dirnames = set()
for path in cookbook_paths:
for path in env.cookbook_search_paths:
dirnames.update([d for d in os.listdir(path) if os.path.isdir(
os.path.join(path, d)) and not d.startswith('.')])

recipes = []
for dirname in dirnames:
recipes.extend(get_recipes_in_cookbook(dirname))
Expand Down
18 changes: 13 additions & 5 deletions littlechef/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

__testing__ = False

env.berksfile_cookbooks_directory=""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be spaces to both sides of the equality sign. Also a better place to put this statement would be further up, for example right below env.node_work_path = littlechef.node_work_path


@hosts('setup')
def new_kitchen():
Expand Down Expand Up @@ -123,6 +124,9 @@ def nodes_with_tag(tag):
@hosts('setup')
def node(*nodes):
"""Selects and configures a list of nodes. 'all' configures all nodes"""
if env.berksfile:
chef.ensure_berksfile_cookbooks_are_installed()

chef.build_node_data_bag()
if not len(nodes) or nodes[0] == '':
abort('No node was given')
Expand Down Expand Up @@ -156,7 +160,7 @@ def node(*nodes):
with settings():
execute(_node_runner)
chef.remove_local_node_data_bag()

chef.cleanup_berksfile_cookbooks()

def _configure_fabric_for_platform(platform):
"""Configures fabric for a specific platform"""
Expand Down Expand Up @@ -533,16 +537,20 @@ def _readconfig():
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
env.berksfile = None
else:
env.cookbook_search_paths=[]
try:
env.berksfile_cookbooks_directory = config.get('kitchen', 'berksfile_cookbooks_directory')
littlechef.cookbook_paths.append(env.berksfile_cookbooks_directory)
env.cookbook_search_paths.append(env.berksfile_cookbooks_directory)+'/berks_cookbooks'
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
if env.berksfile:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this if before trying to set env.berksfile_cookbooks_directory.
Also, why the hardcoded +'/berks_cookbooks'?

env.berksfile_cookbooks_directory = tempfile.mkdtemp('littlechef-berks')
littlechef.cookbook_paths.append(env.berksfile_cookbooks_directory)
env.berksfile_cookbooks_directory = tempfile.mkdtemp('littlechef-berks')+'/berks_cookbooks'
env.cookbook_search_paths.append(env.berksfile_cookbooks_directory)
else:
env.berksfile_cookbooks_directory = None
chef.ensure_berksfile_cookbooks_are_installed()

#print("Setting env.berksfile_cookbooks_directory = {0}".format(env.berksfile_cookbooks_directory))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot a print statement here

# Add defined cookbooks directories to search path
env.cookbook_search_paths+=cookbook_paths

# Upload Directory
try:
Expand Down
6 changes: 3 additions & 3 deletions littlechef/solo.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ def configure(current_node=None):
if not exists('/etc/chef'):
sudo('mkdir -p /etc/chef')
# Set parameters and upload solo.rb template
reversed_cookbook_paths = cookbook_paths[:]
reversed_cookbook_paths = env.cookbook_search_paths[:]
reversed_cookbook_paths.reverse()
cookbook_paths_list = '[{0}]'.format(', '.join(
['"{0}/{1}"'.format(env.node_work_path, x)
cookbook_paths_list = '{0}'.format(', '.join(
['"{0}/{1}"'.format(env.node_work_path, os.path.basename(x)) \
for x in reversed_cookbook_paths]))
data = {
'node_work_path': env.node_work_path,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import os
import platform
import shutil
import tempfile

from fabric.api import env
from os.path import join, normpath, abspath, split

import sys
Expand All @@ -24,6 +27,8 @@

import littlechef

env.berksfile_cookbooks_directory = tempfile.mkdtemp('littlechef-berks')+'/berks_cookbooks'
env.berksfile = 'Berksfile'

# Set some convenience variables
test_path = split(normpath(abspath(__file__)))[0]
Expand Down Expand Up @@ -133,6 +138,7 @@ def test_valid_environment(self):
class TestRunner(BaseTest):
def test_no_node_given(self):
"""Should abort when no node is given"""
#import ipdb; ipdb.set_trace()
resp, error = self.execute([fix, 'node:'])
self.assertTrue("Fatal error: No node was given" in error)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import tempfile

from ConfigParser import SafeConfigParser

from mock import patch
from nose.tools import raises

from fabric.api import env

from littlechef import runner
from test_base import BaseTest

env.berksfile_cookbooks_directory = tempfile.mkdtemp('littlechef-berks')+'/berks_cookbooks'
env.berksfile = 'Berksfile'

class TestConfig(BaseTest):

Expand Down