def deploy_chef(ask="yes", version="11"): """Install chef-solo on a node""" env.host_string = lib.get_env_host_string() if ask == "no" or littlechef.noninteractive: print("Deploying Chef using omnibus installer version: ...".format(version)) else: message = ('\nAre you sure you want to install Chef version:' '{0} on node {1}?'.format(version, env.host_string)) if not confirm(message): abort('Aborted by user') lib.print_header("Configuring Chef Solo on {0}".format(env.host_string)) if not __testing__: solo.install(version) solo.configure() # Build a basic node file if there isn't one already # with some properties from ohai with settings(hide('stdout'), warn_only=True): output = sudo('ohai -l warn') if output.succeeded: try: ohai = json.loads(output) except ValueError: abort("Could not parse ohai's output" ":\n {0}".format(output)) node = {"run_list": []} for attribute in ["ipaddress", "platform", "platform_family", "platform_version"]: if ohai.get(attribute): node[attribute] = ohai[attribute] chef.save_config(node)
def _node_runner(node_data=None): """This is only used by node so that we can execute in parallel""" env.host_string = lib.get_env_host_string() if node_data: node = node_data else: node = lib.get_node(env.host_string) _configure_fabric_for_platform(node.get("platform")) if node.get("gateway"): gateway = node.get("gateway") else: gateway = env.gateway if node.get("http_proxy"): http_proxy = node.get("http_proxy") else: http_proxy = env.http_proxy if node.get("https_proxy"): https_proxy = node.get("https_proxy") else: https_proxy = env.https_proxy with settings(https_proxy=https_proxy, http_proxy=http_proxy, gateway=gateway): if __testing__: print "TEST: would now configure {0}".format(env.host_string) else: lib.print_header("Configuring {0}".format(env.host_string)) if env.autodeploy_chef and not chef.chef_test(): deploy_chef(ask="no") chef.sync_node(node)
def ssh(name): """Executes the given command""" env.host_string = lib.get_env_host_string() print("\nExecuting the command '{0}' on node {1}...".format( name, env.host_string)) # Execute remotely using either the sudo or the run fabric functions with settings(hide("warnings"), warn_only=True): if name.startswith("sudo "): sudo(name[5:]) else: run(name)
def deploy_chef(gems="no", ask="yes", version="11", distro_type=None, distro=None, platform=None, stop_client='yes', method=None): """Install chef-solo on a node""" env.host_string = lib.get_env_host_string() deprecated_parameters = [distro_type, distro, platform] if any(param is not None for param in deprecated_parameters) or gems != 'no': print("DeprecationWarning: the parameters 'gems', distro_type'," " 'distro' and 'platform' will no longer be supported " "in future versions of LittleChef. Use 'method' instead") if distro_type is None and distro is None: distro_type, distro, platform = solo.check_distro() elif distro_type is None or distro is None: abort('Must specify both or neither of distro_type and distro') if method: if method not in ['omnibus', 'gentoo', 'pacman']: abort('Invalid omnibus method {0}. Supported methods are ' 'omnibus, gentoo and pacman'.format(method)) msg = "{0} using the {1} installer".format(version, method) else: if gems == "yes": msg = 'using gems for "{0}"'.format(distro) else: msg = '{0} using "{1}" packages'.format(version, distro) if method == 'omnibus' or ask == "no" or littlechef.noninteractive: print("Deploying Chef {0}...".format(msg)) else: message = ('\nAre you sure you want to install Chef ' '{0} on node {1}?'.format(msg, env.host_string)) if not confirm(message): abort('Aborted by user') _configure_fabric_for_platform(platform) if not __testing__: solo.install(distro_type, distro, gems, version, stop_client, method) solo.configure() # Build a basic node file if there isn't one already # with some properties from ohai with settings(hide('stdout'), warn_only=True): output = sudo('ohai -l warn') if output.succeeded: try: ohai = json.loads(output) except ValueError: abort("Could not parse ohai's output" ":\n {0}".format(output)) node = {"run_list": []} for attribute in ["ipaddress", "platform", "platform_family", "platform_version"]: if ohai.get(attribute): node[attribute] = ohai[attribute] chef.save_config(node)
def _node_runner(): """This is only used by node so that we can execute in parallel""" env.host_string = lib.get_env_host_string() node = lib.get_node(env.host_string) _configure_fabric_for_platform(node.get("platform")) if __testing__: print "TEST: would now configure {0}".format(env.host_string) else: lib.print_header("Configuring {0}".format(env.host_string)) chef.sync_node(node)
def plugin(name): """Executes the selected plugin Plugins are expected to be found in the kitchen's 'plugins' directory """ env.host_string = lib.get_env_host_string() plug = lib.import_plugin(name) lib.print_header("Executing plugin '{0}' on " "{1}".format(name, env.host_string)) node = lib.get_node(env.host_string) if node == {'run_list': []}: node['name'] = env.host_string plug.execute(node) print("Finished executing plugin")
def _node_runner(): """This is only used by node so that we can execute in parallel""" env.host_string = lib.get_env_host_string() node = lib.get_node(env.host_string) _configure_fabric_for_platform(node.get("platform")) if __testing__: print "TEST: would now configure {0}".format(env.host_string) else: lib.print_header("Configuring {0}".format(env.host_string)) if env.autodeploy_chef and not chef.chef_test(): deploy_chef(ask="no") chef.sync_node(node)
def role(role): """Apply the given role to a node Sets the run_list to the given role If no nodes/hostname.json file exists, it creates one """ env.host_string = lib.get_env_host_string() lib.print_header( "Applying role '{0}' to {1}".format(role, env.host_string)) # Now create configuration and sync node data = lib.get_node(env.host_string) data["run_list"] = ["role[{0}]".format(role)] if not __testing__: chef.sync_node(data)
def recipe(recipe): """Apply the given recipe to a node Sets the run_list to the given recipe If no nodes/hostname.json file exists, it creates one """ env.host_string = lib.get_env_host_string() # Create configuration and sync node data = lib.get_node(env.host_string) data["run_list"] = ["recipe[{0}]".format(recipe)] lib.print_header( "Applying recipe '{0}' on node {1}".format(recipe, env.host_string)) _node_runner(node_data=data)
def role(role): """Apply the given role to a node Sets the run_list to the given role If no nodes/hostname.json file exists, it creates one """ env.host_string = lib.get_env_host_string() lib.print_header("Applying role '{0}' to {1}".format( role, env.host_string)) # Now create configuration and sync node data = lib.get_node(env.host_string) data["run_list"] = ["role[{0}]".format(role)] if not __testing__: if env.autodeploy_chef and not chef.chef_test(): deploy_chef(ask="no") chef.sync_node(data)
def recipe(recipe): """Apply the given recipe to a node Sets the run_list to the given recipe If no nodes/hostname.json file exists, it creates one """ env.host_string = lib.get_env_host_string() lib.print_header( "Applying recipe '{0}' on node {1}".format(recipe, env.host_string)) # Create configuration and sync node data = lib.get_node(env.host_string) data["run_list"] = ["recipe[{0}]".format(recipe)] if not __testing__: if env.autodeploy_chef and not chef.chef_test(): deploy_chef(ask="no") chef.sync_node(data)
def roles(*role_list, **kwargs): """Apply the given roles to a node Sets the run_list to the given roles If no nodes/hostname.json file exists, it creates one """ env.host_string = lib.get_env_host_string() lib.print_header( "Applying roles '{0}' to {1}".format(role_list, env.host_string)) override_data = kwargs.get('data', {}) on_sync = kwargs.get('on_sync', None) # Now create configuration and sync node data = lib.get_node(env.host_string) data.update(override_data) data["run_list"] = ["role[{0}]".format(x) for x in role_list] if not __testing__: if env.autodeploy_chef and not chef.chef_test(): deploy_chef(ask="no") chef.sync_node(data, on_sync)
def deploy_chef(ask="yes", version="13.12.14"): """Install chef-solo on a node""" env.host_string = lib.get_env_host_string() if ask == "no" or littlechef.noninteractive: print("Deploying Chef using omnibus installer version: ...".format( version)) else: message = ('\nAre you sure you want to install Chef version:' '{0} on node {1}?'.format(version, env.host_string)) if not confirm(message): abort('Aborted by user') lib.print_header("Configuring Chef Solo on {0}".format(env.host_string)) if not __testing__: solo.install(version) solo.configure() # Build a basic node file if there isn't one already # with some properties from ohai with settings(hide('stdout'), warn_only=True): output = sudo('ohai -l warn') if output.succeeded: try: ohai = json.loads(output) except ValueError: abort("Could not parse ohai's output" ":\n {0}".format(output)) node = {"run_list": []} for attribute in [ "ipaddress", "platform", "platform_family", "platform_version" ]: if ohai.get(attribute): node[attribute] = ohai[attribute] chef.save_config(node)