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') elif nodes[0] == 'all': # Fetch all nodes and add them to env.hosts for node in lib.get_nodes(env.chef_environment): env.hosts.append(node['name']) if not len(env.hosts): abort('No nodes found in /nodes/') message = "Are you sure you want to configure all nodes ({0})".format( len(env.hosts)) if env.chef_environment: message += " in the {0} environment".format(env.chef_environment) message += "?" if not __testing__: if not lib.global_confirm(message): abort('Aborted by user') else: # A list of nodes was given env.hosts = list(nodes) env.all_hosts = list(env.hosts) # Shouldn't be needed # Check whether another command was given in addition to "node:" if not(littlechef.__cooking__ and 'node:' not in sys.argv[-1] and 'nodes_with_role:' not in sys.argv[-1] and 'nodes_with_recipe:' not in sys.argv[-1] and 'nodes_with_tag:' not in sys.argv[-1]): # If user didn't type recipe:X, role:Y or deploy_chef, # configure the nodes with settings(): execute(_node_runner) chef.remove_local_node_data_bag() chef.cleanup_berksfile_cookbooks()
def _readconfig(): """Configures environment variables""" config = ConfigParser.SafeConfigParser() try: found = config.read(littlechef.CONFIGFILE) except ConfigParser.ParsingError as e: abort(str(e)) if not len(found): try: found = config.read(['config.cfg', 'auth.cfg']) except ConfigParser.ParsingError as e: abort(str(e)) if len(found): print('\nDeprecationWarning: deprecated config file name \'{0}\'.' ' Use {1}'.format(found[0], littlechef.CONFIGFILE)) else: abort('No {0} file found in the current ' 'directory'.format(littlechef.CONFIGFILE)) in_a_kitchen, missing = _check_appliances() missing_str = lambda m: ' and '.join(', '.join(m).rsplit(', ', 1)) if not in_a_kitchen: abort("Couldn't find {0}. " "Are you executing 'fix' outside of a kitchen?\n" "To create a new kitchen in the current directory " " type 'fix new_kitchen'".format(missing_str(missing))) # We expect an ssh_config file here, # and/or a user, (password/keyfile) pair try: env.ssh_config_path = config.get('userinfo', 'ssh-config') except ConfigParser.NoSectionError: abort('You need to define a "userinfo" section' ' in the config file. Refer to the README for help ' '(http://github.com/tobami/littlechef)') except ConfigParser.NoOptionError: env.ssh_config_path = None if env.ssh_config_path: env.ssh_config = _SSHConfig() env.ssh_config_path = os.path.expanduser(env.ssh_config_path) env.use_ssh_config = True try: env.ssh_config.parse(open(env.ssh_config_path)) except IOError: abort("Couldn't open the ssh-config file " "'{0}'".format(env.ssh_config_path)) except Exception: abort("Couldn't parse the ssh-config file " "'{0}'".format(env.ssh_config_path)) else: env.ssh_config = None # check for a gateway try: env.gateway = config.get('connection', 'gateway') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.gateway = None # check for http_proxy which will be put into solo.rb try: env.http_proxy = config.get('connection', 'http_proxy') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.http_proxy = None try: env.https_proxy = config.get('connection', 'https_proxy') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.https_proxy = None # Check for an encrypted_data_bag_secret file and set the env option try: env.encrypted_data_bag_secret = config.get('userinfo', 'encrypted_data_bag_secret') except ConfigParser.NoOptionError: env.encrypted_data_bag_secret = None if env.encrypted_data_bag_secret: env.encrypted_data_bag_secret = os.path.expanduser( env.encrypted_data_bag_secret) try: open(env.encrypted_data_bag_secret) except IOError as e: abort("Failed to open encrypted_data_bag_secret file at " "'{0}'".format(env.encrypted_data_bag_secret)) try: sudo_prefix = config.get('ssh', 'sudo_prefix', raw=True) except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): pass else: env.sudo_prefix = sudo_prefix try: env.user = config.get('userinfo', 'user') except ConfigParser.NoOptionError: if not env.ssh_config_path: msg = 'You need to define a user in the "userinfo" section' msg += ' of {0}. Refer to the README for help' msg += ' (http://github.com/tobami/littlechef)' abort(msg.format(littlechef.CONFIGFILE)) user_specified = False else: user_specified = True try: env.password = config.get('userinfo', 'password') or None except ConfigParser.NoOptionError: pass try: #If keypair-file is empty, assign None or fabric will try to read key " env.key_filename = config.get('userinfo', 'keypair-file') or None except ConfigParser.NoOptionError: pass if (user_specified and not env.password and not env.key_filename and not env.ssh_config): abort('You need to define a password, keypair file, or ssh-config ' 'file in {0}'.format(littlechef.CONFIGFILE)) # Node's Chef Solo working directory for storing cookbooks, roles, etc. try: env.node_work_path = os.path.expanduser(config.get('kitchen', 'node_work_path')) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): env.node_work_path = littlechef.node_work_path else: if not env.node_work_path: abort('The "node_work_path" option cannot be empty') # Follow symlinks try: env.follow_symlinks = config.getboolean('kitchen', 'follow_symlinks') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): env.follow_symlinks = False try: env.berksfile = config.get('kitchen', 'berksfile') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: env.berksfile = None else: try: env.berksfile_cookbooks_directory = config.get('kitchen', 'berksfile_cookbooks_directory') littlechef.cookbook_paths.append(env.berksfile_cookbooks_directory) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: if env.berksfile: env.berksfile_cookbooks_directory = tempfile.mkdtemp('littlechef-berks') littlechef.cookbook_paths.append(env.berksfile_cookbooks_directory) else: env.berksfile_cookbooks_directory = None chef.ensure_berksfile_cookbooks_are_installed() # Upload Directory try: env.sync_packages_dest_dir = config.get('sync-packages', 'dest-dir') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.sync_packages_dest_dir = None # Local Directory try: env.sync_packages_local_dir = config.get('sync-packages', 'local-dir') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.sync_packages_local_dir = None try: env.autodeploy_chef = config.get('userinfo', 'autodeploy_chef') or None except ConfigParser.NoOptionError: env.autodeploy_chef = None
def _readconfig(): """Configures environment variables""" config = ConfigParser.SafeConfigParser() try: found = config.read(littlechef.CONFIGFILE) except ConfigParser.ParsingError as e: abort(str(e)) if not len(found): try: found = config.read(['config.cfg', 'auth.cfg']) except ConfigParser.ParsingError as e: abort(str(e)) if len(found): print( '\nDeprecationWarning: deprecated config file name \'{0}\'.' ' Use {1}'.format(found[0], littlechef.CONFIGFILE)) else: abort('No {0} file found in the current ' 'directory'.format(littlechef.CONFIGFILE)) in_a_kitchen, missing = _check_appliances() missing_str = lambda m: ' and '.join(', '.join(m).rsplit(', ', 1)) if not in_a_kitchen: abort("Couldn't find {0}. " "Are you executing 'fix' outside of a kitchen?\n" "To create a new kitchen in the current directory " " type 'fix new_kitchen'".format(missing_str(missing))) # We expect an ssh_config file here, # and/or a user, (password/keyfile) pair try: env.ssh_config_path = config.get('userinfo', 'ssh-config') except ConfigParser.NoSectionError: abort('You need to define a "userinfo" section' ' in the config file. Refer to the README for help ' '(http://github.com/tobami/littlechef)') except ConfigParser.NoOptionError: env.ssh_config_path = None if env.ssh_config_path: env.ssh_config = _SSHConfig() env.ssh_config_path = os.path.expanduser(env.ssh_config_path) env.use_ssh_config = True try: env.ssh_config.parse(open(env.ssh_config_path)) except IOError: abort("Couldn't open the ssh-config file " "'{0}'".format(env.ssh_config_path)) except Exception: abort("Couldn't parse the ssh-config file " "'{0}'".format(env.ssh_config_path)) else: env.ssh_config = None # check for a gateway try: env.gateway = config.get('connection', 'gateway') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.gateway = None # check for http_proxy which will be put into solo.rb try: env.http_proxy = config.get('connection', 'http_proxy') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.http_proxy = None try: env.https_proxy = config.get('connection', 'https_proxy') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.https_proxy = None # check for no_proxy try: env.no_proxy = config.get('connection', 'no_proxy') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.no_proxy = None # Check for an encrypted_data_bag_secret file and set the env option try: env.encrypted_data_bag_secret = config.get( 'userinfo', 'encrypted_data_bag_secret') except ConfigParser.NoOptionError: env.encrypted_data_bag_secret = None if env.encrypted_data_bag_secret: env.encrypted_data_bag_secret = os.path.expanduser( env.encrypted_data_bag_secret) try: open(env.encrypted_data_bag_secret) except IOError as e: abort("Failed to open encrypted_data_bag_secret file at " "'{0}'".format(env.encrypted_data_bag_secret)) try: sudo_prefix = config.get('ssh', 'sudo_prefix', raw=True) except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): pass else: env.sudo_prefix = sudo_prefix try: env.user = config.get('userinfo', 'user') except ConfigParser.NoOptionError: if not env.ssh_config_path: msg = 'You need to define a user in the "userinfo" section' msg += ' of {0}. Refer to the README for help' msg += ' (http://github.com/tobami/littlechef)' abort(msg.format(littlechef.CONFIGFILE)) user_specified = False else: user_specified = True try: env.password = config.get('userinfo', 'password') or None except ConfigParser.NoOptionError: pass try: # If keypair-file is empty, assign None or fabric will try to read key env.key_filename = config.get('userinfo', 'keypair-file') or None except ConfigParser.NoOptionError: pass if (user_specified and not env.password and not env.key_filename and not env.ssh_config): abort('You need to define a password, keypair file, or ssh-config ' 'file in {0}'.format(littlechef.CONFIGFILE)) # Node's Chef Solo working directory for storing cookbooks, roles, etc. try: env.node_work_path = os.path.expanduser( config.get('kitchen', 'node_work_path')) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): env.node_work_path = littlechef.node_work_path else: if not env.node_work_path: abort('The "node_work_path" option cannot be empty') # Follow symlinks try: env.follow_symlinks = config.getboolean('kitchen', 'follow_symlinks') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): env.follow_symlinks = False try: env.berksfile = config.get('kitchen', 'berksfile') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: env.berksfile = None else: try: env.berksfile_cookbooks_directory = config.get( 'kitchen', 'berksfile_cookbooks_directory') littlechef.cookbook_paths.append(env.berksfile_cookbooks_directory) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: if env.berksfile: env.berksfile_cookbooks_directory = tempfile.mkdtemp( 'littlechef-berks') littlechef.cookbook_paths.append( env.berksfile_cookbooks_directory) else: env.berksfile_cookbooks_directory = None chef.ensure_berksfile_cookbooks_are_installed() # Upload Directory try: env.sync_packages_dest_dir = config.get('sync-packages', 'dest-dir') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.sync_packages_dest_dir = None # Local Directory try: env.sync_packages_local_dir = config.get('sync-packages', 'local-dir') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): env.sync_packages_local_dir = None try: env.autodeploy_chef = config.get('userinfo', 'autodeploy_chef') or None except ConfigParser.NoOptionError: env.autodeploy_chef = None