def sync_node(node): """Builds, synchronizes and configures a node. It also injects the ipaddress to the node's config file if not already existent. """ if node.get("dummy") or "dummy" in node.get("tags", []): lib.print_header("Skipping dummy: {0}".format(env.host)) return False # Get merged attributes current_node = _build_node_data_bag() with lib.credentials(): # Always configure Chef Solo solo.configure(current_node) ipaddress = _get_ipaddress(node) # Everything was configured alright, so save the node configuration # This is done without credentials, so that we keep the node name used # by the user and not the hostname or IP translated by .ssh/config filepath = save_config(node, ipaddress) with lib.credentials(): try: # Synchronize the kitchen directory _synchronize_node(filepath, node) # Execute Chef Solo _configure_node() finally: _remove_local_node_data_bag() _node_cleanup() return True
def sync_node(node): """Builds, synchronizes and configures a node. It also injects the ipaddress to the node's config file if not already existent. """ if node.get('dummy') or 'dummy' in node.get('tags', []): lib.print_header("Skipping dummy: {0}".format(env.host)) return False # Get merged attributes current_node = _build_node_data_bag() with lib.credentials(): # Always configure Chef Solo solo.configure(current_node) ipaddress = _get_ipaddress(node) # Everything was configured alright, so save the node configuration # This is done without credentials, so that we keep the node name used # by the user and not the hostname or IP translated by .ssh/config filepath = save_config(node, ipaddress) with lib.credentials(): try: # Synchronize the kitchen directory _synchronize_node(filepath, node) # Execute Chef Solo _configure_node() finally: _remove_local_node_data_bag() _node_cleanup() return True
def ssh(name): """Executes the given command""" if not env.host_string: abort('no node specified\nUsage: fix node:MYNODES ssh:COMMAND') print("\nExecuting the command '{0}' on the 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 "): with lib.credentials(): sudo(name[5:]) else: with lib.credentials(): run(name)
def configure(): """Deploy chef-solo specific files""" with credentials(): # Ensure that config directories exist cache_dir = "{0}/cache".format(node_work_path) if not exists(cache_dir): sudo('mkdir -p {0}'.format(cache_dir)) 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.reverse() cookbook_paths_list = '[{0}]'.format(', '.join( ['"{0}/{1}"'.format(node_work_path, x) \ for x in reversed_cookbook_paths])) data = { 'node_work_path': node_work_path, 'cookbook_paths_list': cookbook_paths_list } upload_template(os.path.join(basedir, 'solo.rb'), '/etc/chef/', context=data, use_sudo=True, mode=0400) sudo('chown root:root {0}'.format('/etc/chef/solo.rb'))
def sync_node(node): """Buils, synchronizes and configures a node""" cookbooks = _build_node(node) with lib.credentials(): _synchronize_node(cookbooks) # Everything was configured alright, so save the node configuration filepath = _save_config(node) _configure_node(filepath)
def configure(current_node=None): """Deploy chef-solo specific files""" current_node = current_node or {} with credentials(): # Ensure that the /tmp/chef-solo/cache directory exist cache_dir = "{0}/cache".format(env.node_work_path) if not exists(cache_dir): with settings(hide('running', 'stdout'), warn_only=True): output = sudo('mkdir -p {0}'.format(cache_dir)) if output.failed: error = "Could not create {0} dir. ".format(env.node_work_path) error += "Do you have sudo rights?" abort(error) # Change ownership of /tmp/chef-solo/ so that we can rsync with hide('running', 'stdout'): with settings(warn_only=True): output = sudo('chown -R {0} {1}'.format( env.user, env.node_work_path)) if output.failed: error = "Could not modify {0} dir. ".format(env.node_work_path) error += "Do you have sudo rights?" abort(error) # Set up chef solo configuration if not exists(logging_path): sudo('mkdir -p {0}'.format(logging_path)) 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.reverse() cookbook_paths_list = '[{0}]'.format(', '.join( ['"{0}/{1}"'.format(env.node_work_path, x) \ for x in reversed_cookbook_paths])) data = { 'node_work_path': env.node_work_path, 'cookbook_paths_list': cookbook_paths_list, 'environment': current_node.get('chef_environment', '_default'), 'verbose': "true" if env.verbose else "false" } with settings(hide('everything')): try: upload_template(os.path.join(BASEDIR, 'solo.rb'), '/etc/chef/', context=data, use_sudo=True, backup=False, mode=0400) except SystemExit: error = ( "Failed to upload '/etc/chef/solo.rb'\n" "This can happen when the deployment user does not have a " "home directory, which is needed as a temporary location") abort(error) with hide('stdout'): sudo('chown root:root {0}'.format('/etc/chef/solo.rb'))
def configure(current_node=None): """Deploy chef-solo specific files""" current_node = current_node or {} with credentials(): # Ensure that the /tmp/chef-solo/cache directory exist cache_dir = "{0}/cache".format(env.node_work_path) if not exists(cache_dir): with settings(hide("running", "stdout"), warn_only=True): output = sudo("mkdir -p {0}".format(cache_dir)) if output.failed: error = "Could not create {0} dir. ".format(env.node_work_path) error += "Do you have sudo rights?" abort(error) # Change ownership of /tmp/chef-solo/ so that we can rsync with hide("running", "stdout"): with settings(warn_only=True): output = sudo("chown -R {0} {1}".format(env.user, env.node_work_path)) if output.failed: error = "Could not modify {0} dir. ".format(env.node_work_path) error += "Do you have sudo rights?" abort(error) # Set up chef solo configuration if not exists(logging_path): sudo("mkdir -p {0}".format(logging_path)) 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.reverse() cookbook_paths_list = "[{0}]".format( ", ".join(['"{0}/{1}"'.format(env.node_work_path, x) for x in reversed_cookbook_paths]) ) data = { "node_work_path": env.node_work_path, "cookbook_paths_list": cookbook_paths_list, "environment": current_node.get("chef_environment", "_default"), "verbose": "true" if env.verbose else "false", } with settings(hide("everything")): try: upload_template( os.path.join(BASEDIR, "solo.rb"), "/etc/chef/", context=data, use_sudo=True, backup=False, mode=0400 ) except SystemExit: error = ( "Failed to upload '/etc/chef/solo.rb'\n" "This can happen when the deployment user does not have a " "home directory, which is needed as a temporary location" ) abort(error) with hide("stdout"): sudo("chown root:root {0}".format("/etc/chef/solo.rb"))
def sync_node(node): """Builds, synchronizes and configures a node. It also injects the ipaddress to the node's config file if not already existent. """ with lib.credentials(): # Get merged attributes current_node = _build_node_data_bag() # Always configure Chef Solo solo.configure(current_node) # Everything was configured alright, so save the node configuration filepath = save_config(node, _get_ipaddress(node)) _synchronize_node(filepath) _remove_node_data_bag() _configure_node()
def check_distro(): """Check that the given distro is supported and return the distro type""" debian_distros = ['wheezy', 'squeeze', 'lenny'] ubuntu_distros = ['natty', 'maverick', 'lucid', 'karmic'] rpm_distros = ['centos', 'rhel', 'sl', 'amazon'] with credentials( hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True): output = sudo('cat /etc/issue') if 'Debian GNU/Linux 5.0' in output: distro = "lenny" distro_type = "debian" elif 'Debian GNU/Linux 6.0' in output: distro = "squeeze" distro_type = "debian" elif 'Debian GNU/Linux wheezy' in output: distro = "wheezy" distro_type = "debian" elif 'Ubuntu' in output: distro = sudo('lsb_release -cs') distro_type = "debian" elif 'CentOS' in output: distro = "CentOS" distro_type = "rpm" elif 'Red Hat Enterprise Linux' in output: distro = "Red Hat" distro_type = "rpm" elif 'Scientific Linux SL' in output: distro = "Scientific Linux" distro_type = "rpm" elif 'This is \\n.\\O (\\s \\m \\r) \\t' in output: distro = "Gentoo" distro_type = "gentoo" elif 'Arch Linux \\r (\\n) (\\l)' in output: distro = "Arch Linux" distro_type = "pacman" elif 'Amazon Linux' in output: distro = "Amazon Linux" distro_type = "rpm" else: print "Currently supported distros are:" print " Debian: " + ", ".join(debian_distros) print " Ubuntu: " + ", ".join(ubuntu_distros) print " RHEL: " + ", ".join(rpm_distros) print " Gentoo" print " Arch Linux" abort("Unsupported distro '{0}'".format(output)) return distro_type, distro
def install(distro_type, distro, gems, version): with credentials(): if distro_type == "debian": if gems == "yes": _gem_apt_install() else: _apt_install(distro, version) elif distro_type == "rpm": if gems == "yes": _gem_rpm_install() else: _rpm_install() elif distro_type == "gentoo": _emerge_install() else: abort('wrong distro type: {0}'.format(distro_type))
def install(distro_type, distro, gems, version): with credentials(): if distro_type == "debian": if gems == "yes": _gem_apt_install() else: _apt_install(distro, version) elif distro_type == "rpm": if gems == "yes": _gem_rpm_install() else: _rpm_install() elif distro_type == "gentoo": _emerge_install() else: abort("wrong distro type: {0}".format(distro_type))
def check_distro(): """Check that the given distro is supported and return the distro type""" debian_distros = ['wheezy', 'squeeze', 'lenny'] ubuntu_distros = ['natty', 'maverick', 'lucid', 'karmic'] rpm_distros = ['centos', 'rhel', 'sl'] with credentials( hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True): output = sudo('cat /etc/issue') if 'Debian GNU/Linux 5.0' in output: distro = "lenny" distro_type = "debian" elif 'Debian GNU/Linux 6.0' in output: distro = "squeeze" distro_type = "debian" elif 'Debian GNU/Linux wheezy' in output: distro = "wheezy" distro_type = "debian" elif 'Ubuntu' in output: distro = sudo('lsb_release -cs') distro_type = "debian" elif 'CentOS' in output: distro = "CentOS" distro_type = "rpm" elif 'Red Hat Enterprise Linux' in output: distro = "Red Hat" distro_type = "rpm" elif 'Scientific Linux SL' in output: distro = "Scientific Linux" distro_type = "rpm" elif 'This is \\n.\\O (\\s \\m \\r) \\t' in output: distro = "Gentoo" distro_type = "gentoo" elif 'Arch Linux \\r (\\n) (\\l)' in output: distro = "Arch Linux" distro_type = "pacman" else: print "Currently supported distros are:" print " Debian: " + ", ".join(debian_distros) print " Ubuntu: " + ", ".join(ubuntu_distros) print " RHEL: " + ", ".join(rpm_distros) print " Gentoo" print " Arch Linux" abort("Unsupported distro '{0}'".format(output)) return distro_type, distro
def sync_node(node): """Builds, synchronizes and configures a node. It also injects the ipaddress to the node's config file if not already existent. """ with lib.credentials(): # Get merged attributes current_node = _build_node_data_bag() # Always configure Chef Solo solo.configure(current_node) # Everything was configured alright, so save the node configuration filepath = save_config(node, _get_ipaddress(node)) try: _synchronize_node(filepath) _configure_node() finally: _remove_local_node_data_bag() _node_cleanup()
def configure(current_node=None): """Deploy chef-solo specific files""" current_node = current_node or {} with credentials(): # Ensure that the /tmp/chef-solo/cache directory exist cache_dir = "{0}/cache".format(node_work_path) if not exists(cache_dir): with settings(hide('running', 'stdout'), warn_only=True): output = sudo('mkdir -p {0}'.format(cache_dir)) if output.failed: abort("Could not create {0} dir. Do you have sudo rights?". format(node_work_path)) # Change ownership of /tmp/chef-solo/ so that we can rsync with hide('running', 'stdout'): with settings(warn_only=True): output = sudo('chown -R {0} {1}'.format( env.user, node_work_path)) if output.failed: abort("Could not modify {0} dir. Do you have sudo rights?". format(node_work_path)) # Set up chef solo configuration 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.reverse() cookbook_paths_list = '[{0}]'.format(', '.join( ['"{0}/{1}"'.format(node_work_path, x) \ for x in reversed_cookbook_paths])) data = { 'node_work_path': node_work_path, 'cookbook_paths_list': cookbook_paths_list, 'environment': current_node.get('chef_environment', '_default'), 'verbose': "true" if env.verbose else "false" } with hide('running', 'stdout'): upload_template(os.path.join(BASEDIR, 'solo.rb'), '/etc/chef/', context=data, use_sudo=True, backup=False, mode=0400) with hide('stdout'): sudo('chown root:root {0}'.format('/etc/chef/solo.rb'))
def check_distro(): """Check that the given distro is supported and return the distro type""" debian_distros = ["wheezy", "squeeze", "lenny"] ubuntu_distros = ["natty", "maverick", "lucid", "karmic"] rpm_distros = ["centos", "rhel", "sl"] with credentials(hide("warnings", "running", "stdout", "stderr"), warn_only=True): output = sudo("cat /etc/issue") if "Debian GNU/Linux 5.0" in output: distro = "lenny" distro_type = "debian" elif "Debian GNU/Linux 6.0" in output: distro = "squeeze" distro_type = "debian" elif "Debian GNU/Linux wheezy" in output: distro = "wheezy" distro_type = "debian" elif "Ubuntu" in output: distro = sudo("lsb_release -cs") distro_type = "debian" elif "CentOS" in output: distro = "CentOS" distro_type = "rpm" elif "Red Hat Enterprise Linux" in output: distro = "Red Hat" distro_type = "rpm" elif "Scientific Linux SL" in output: distro = "Scientific Linux" distro_type = "rpm" elif "This is \\n.\\O (\\s \\m \\r) \\t" in output: distro = "Gentoo" distro_type = "gentoo" elif "Arch Linux \\r (\\n) (\\l)" in output: distro = "Arch Linux" distro_type = "pacman" else: print "Currently supported distros are:" print " Debian: " + ", ".join(debian_distros) print " Ubuntu: " + ", ".join(ubuntu_distros) print " RHEL: " + ", ".join(rpm_distros) print " Gentoo" print " Arch Linux" abort("Unsupported distro '{0}'".format(output)) return distro_type, distro
def configure(current_node=None): """Deploy chef-solo specific files""" current_node = current_node or {} with credentials(): # Ensure that the /tmp/chef-solo/cache directory exist cache_dir = "{0}/cache".format(node_work_path) if not exists(cache_dir): with settings(hide('running', 'stdout'), warn_only=True): output = sudo('mkdir -p {0}'.format(cache_dir)) if output.failed: abort( "Could not create {0} dir. Do you have sudo rights?".format( node_work_path)) # Change ownership of /tmp/chef-solo/ so that we can rsync with hide('running', 'stdout'): with settings(warn_only=True): output = sudo( 'chown -R {0} {1}'.format(env.user, node_work_path)) if output.failed: abort( "Could not modify {0} dir. Do you have sudo rights?".format( node_work_path)) # Set up chef solo configuration if not exists(logging_path): sudo('mkdir -p {0}'.format(logging_path)) 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.reverse() cookbook_paths_list = '[{0}]'.format(', '.join( ['"{0}/{1}"'.format(node_work_path, x) \ for x in reversed_cookbook_paths])) data = { 'node_work_path': node_work_path, 'cookbook_paths_list': cookbook_paths_list, 'environment': current_node.get('chef_environment', '_default'), 'verbose': "true" if env.verbose else "false" } with hide('running', 'stdout'): upload_template(os.path.join(BASEDIR, 'solo.rb'), '/etc/chef/', context=data, use_sudo=True, backup=False, mode=0400) with hide('stdout'): sudo('chown root:root {0}'.format('/etc/chef/solo.rb'))
def install(distro_type, distro, gems, version, stop_client): """Calls the appropriate installation function for the given distro""" with credentials(): if distro_type == "debian": if gems == "yes": _gem_apt_install() else: _apt_install(distro, version, stop_client) elif distro_type == "rpm": if gems == "yes": _gem_rpm_install() else: _rpm_install() elif distro_type == "gentoo": _emerge_install() elif distro_type == "pacman": _gem_pacman_install() else: abort('wrong distro type: {0}'.format(distro_type))
def configure(): """Deploy chef-solo specific files""" with credentials(): # Ensure that config directories exist cache_dir = "{0}/cache".format(node_work_path) if not exists(cache_dir): sudo('mkdir -p {0}'.format(cache_dir)) 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.reverse() cookbook_paths_list = '[{0}]'.format(', '.join( ['"{0}/{1}"'.format(node_work_path, x) \ for x in reversed_cookbook_paths])) data = {'node_work_path': node_work_path, 'cookbook_paths_list': cookbook_paths_list} upload_template(os.path.join(basedir, 'solo.rb'), '/etc/chef/', context=data, use_sudo=True, mode=0400) sudo('chown root:root {0}'.format('/etc/chef/solo.rb'))
def configure(): """Deploy chef-solo specific files""" with credentials(): sudo("mkdir -p {0}/cache".format(node_work_path)) sudo("umask 0377; touch solo.rb") text = "" text += 'file_cache_path "{0}/cache"'.format(node_work_path) text += "\n" reversed_cookbook_paths = cookbook_paths[:] reversed_cookbook_paths.reverse() cookbook_paths_line = "cookbook_path [{0}]".format( ", ".join(['"{0}/{1}"'.format(node_work_path, x) for x in reversed_cookbook_paths]) ) text += cookbook_paths_line + "\n" text += cookbook_paths_line + "\n" text += 'role_path "{0}/roles"'.format(node_work_path) + "\n" text += 'data_bag_path "{0}/data_bags"'.format(node_work_path) + "\n" append("solo.rb", text, use_sudo=True) sudo("mkdir -p /etc/chef") sudo("mv solo.rb /etc/chef/")
def configure(): """Deploy chef-solo specific files""" with credentials(): sudo('mkdir -p {0}/cache'.format(node_work_path)) sudo('umask 0377; touch solo.rb') text = "" text += 'file_cache_path "{0}/cache"'.format(node_work_path) text += "\n" reversed_cookbook_paths = cookbook_paths[:] reversed_cookbook_paths.reverse() cookbook_paths_line = 'cookbook_path [{0}]'.format(', '.join( ['"{0}/{1}"'.format(node_work_path, x) \ for x in reversed_cookbook_paths])) text += cookbook_paths_line + "\n" text += cookbook_paths_line + "\n" text += 'role_path "{0}/roles"'.format(node_work_path) + "\n" text += 'data_bag_path "{0}/data_bags"'.format(node_work_path) + "\n" append('solo.rb', text, use_sudo=True) sudo('mkdir -p /etc/chef') sudo('mv solo.rb /etc/chef/')
def test_credentials_ignores_ssh_config_loglevel(self): """Ignores LogLevel in ssh config""" with lib.credentials(): runner.env.ssh_config.lookup.assert_called_once_with('nodename') self.assertEqual(runner.env.loglevel, 'original_loglevel')