def _python(self): """Determine Python version. Args: None Returns: None """ # Initialize key variables valid = True major = 3 minor = 5 major_installed = sys.version_info[0] minor_installed = sys.version_info[1] # Determine whether python version is too low if major_installed < major: valid = False elif major_installed == major and minor_installed < minor: valid = False # Process validity if valid is False: log_message = ( 'Required python version must be >= {}.{}. ' 'Python version {}.{} installed' ''.format(major, minor, major_installed, minor_installed)) log.log2die_safe(1095, log_message) else: log_message = ( 'Python version {}.{}.' ''.format(major_installed, minor_installed)) setup.print_ok(log_message)
def write(self): """Write the config to file. Args: None Returns: None """ # Initialize key variables directory = self.directories[0] # Update configuration file if required for next_directory in self.directories: # Delete all YAML files in the configuration directory general.delete_yaml_files(next_directory) # Write config back to directory filepath = ('%s/config.yaml') % (directory) with open(filepath, 'w') as outfile: yaml.dump(self.config_dict, outfile, default_flow_style=False) # Write status Update setup.print_ok('Created configuration file {}.'.format(filepath))
def validate(self): """Validate . Args: None Returns: None """ # Initialize key variables username = getpass.getuser() added_suggestions = '' line = '*' * 80 prefix = """\ 1) Edit file {}/etc/config.yaml with correct SNMP parameters \ and then restart the daemons. 2) You can restart switchmap-ng daemons with these commands: $ bin/switchmap-ng-cil restart api $ bin/switchmap-ng-cli restart poller """.format(general.root_directory()) ####################################################################### # # Give suggestions as to what to do # # NOTE! # # The root user should only use the systemctl commands as the daemons # could be running as another user and lock and pid files will be owned # by that user. We don't want the daemons to crash at some other time # because these files are owned by root with denied delete privileges ####################################################################### if username != 'root': added_suggestions = """{} 3) Switchmap-NG will not automatically restart after a reboot. \ You need to re-install as the "root" user for this to occur. """.format(prefix) print('{}\n{}\n{}\n'.format(line, added_suggestions, line)) else: print('{}\n{}\n{}\n'.format(line, prefix, line)) # All done setup.print_ok( 'Installation complete, pending changes mentioned above.')
def _systemd(self): """Determine whether systemd is installed. Args: None Returns: None """ # Initialize key variables. directory = '/etc/systemd/system' # Do nothing if this is not the root user. username = getpass.getuser() if username != 'root': return # Test if os.path.isdir(directory) is True: setup.print_ok('Systemd installed.') else: log_message = 'Systemd not installed. Directory {} not found' log.log2die_safe(1234, log_message)
def start(self): """Write the config to file. Args: None Returns: None """ # Get daemon status daemons = ['switchmap-ng-api', 'switchmap-ng-poller'] for daemon in daemons: # Initialize key variables success = False running = self._running(daemon) # Prompt to restart if already running if running is True: restart = input( '\nINPUT - Daemon {} is running. Restart? [Y/n] ' ''.format(daemon)) if bool(restart) is False: success = self._restart(daemon) if success is True: setup.print_ok( 'Successfully restarted daemon {}.'.format(daemon)) elif restart[0].lower() != 'n': success = self._restart(daemon) if success is True: setup.print_ok( 'Successfully restarted daemon {}.'.format(daemon)) else: setup.print_ok( 'Leaving daemon {} unchanged.'.format(daemon)) success = True else: success = self._start(daemon) if success is True: setup.print_ok( 'Successfully started daemon {}.'.format(daemon)) # Message if no success if success is False: log_message = ('Failed to start daemon {}.'.format(daemon)) log.log2see_safe(1001, log_message)
def _pip3_install(module): """Install python module using pip3. Args: module: module to install Returns: None """ # Find pip3 executable cli_string = 'which pip3' response = general.run_script(cli_string, die=False) # Not OK if not fount if bool(response['returncode']) is True: log_message = ('python pip3 not installed.') log.log2die_safe(1041, log_message) else: log_message = 'Python pip3 executable found.' setup.print_ok(log_message) # Determine version of pip3 cli_string = 'pip3 --version' response = os.popen(cli_string).read() version = response.split()[1] # Attempt to install module if version < '9.0.0': cli_string = 'pip3 list | grep {}'.format(module) else: cli_string = 'pip3 list --format columns | grep {}'.format(module) response = bool(os.popen(cli_string).read()) if response is False: # YAML is not installed try to install it cli_string = 'pip3 install --user {}'.format(module) response_install = general.run_script(cli_string, die=False) # Fail if module cannot be installed if bool(response_install['returncode']) is True: log_message = ('python pip3 cannot install "{}".'.format(module)) log.log2die_safe(1100, log_message) else: log_message = ( 'Python module "{}" is installed.'.format(module)) setup.print_ok(log_message) else: log_message = 'Python module "{}" is installed.'.format(module) setup.print_ok(log_message)