def _before_install_new_packages(self): """ Specific actions before new packages installation """ print "The installation of MySQL for db_governor has started" check_file("/usr/local/directadmin/custombuild/build") check_file("/usr/local/directadmin/custombuild/options.conf") # MYSQL_DA_TYPE=`cat /usr/local/directadmin/custombuild/options.conf | grep mysql_inst= | cut -d= -f2` try: MYSQL_DA_TYPE = grep("/usr/local/directadmin/custombuild/options.conf", "mysql_inst=")[0].split("=")[1] except IndexError: MYSQL_DA_TYPE = "" if os.path.exists("/usr/share/lve/dbgovernor/da.tp.old"): if MYSQL_DA_TYPE == "no": MYSQL_DA_TYPE = read_file("/usr/share/lve/dbgovernor/da.tp.old") else: write_file("/usr/share/lve/dbgovernor/da.tp.old", MYSQL_DA_TYPE) else: write_file("/usr/share/lve/dbgovernor/da.tp.old", MYSQL_DA_TYPE) exec_command_out("/usr/local/directadmin/custombuild/build set mysql_inst no") self._mysqlservice("stop")
def write_csv(ts_fields): try: csv_file = scriptsFolder + '/offline.csv' # Allowed ThingSpeak fields: csv_columns = [ 'datetime', 'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8', 'latitude', 'longitude', 'elevation', 'status' ] check_file(csv_file, 5, 10, 1) # Create row with data row = {} row['datetime'] = datetime.now() for key, value in ts_fields.items(): row[key] = str(value) # Write to CSV File write_header = (not os.path.isfile(csv_file) or os.stat(csv_file).st_size == 0 ) # exists or is empty with io.open(csv_file, 'a', newline='', encoding='utf8') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=csv_columns, extrasaction='ignore', delimiter=',', lineterminator='\n') if write_header: writer.writeheader() # file doesn't exist yet, write a header writer.writerow(row) except IOError as ex1: error_log(ex1, "Write-CSV IOError") except Exception as ex: error_log(ex, "Write-CSV Exception")
def _detect_version_if_auto(self): """ Detect vesrion of MySQL if mysql.type is auto """ print "Detect MySQL version for AUTO" check_file("/usr/local/directadmin/custombuild/build") check_file("/usr/local/directadmin/custombuild/options.conf") MYSQL_DA_VER = "" # MYSQL_DA_TYPE=`cat /usr/local/directadmin/custombuild/options.conf | grep mysql_inst= | cut -d= -f2` try: MYSQL_DA_VER = grep("/usr/local/directadmin/custombuild/options.conf", "mysql=")[0].split("=")[1].strip() MYSQL_DA_TYPE = grep("/usr/local/directadmin/custombuild/options.conf", "mysql_inst=")[0].split("=")[1].strip() except IndexError: MYSQL_DA_VER = "" MYSQL_DA_TYPE = "" if MYSQL_DA_TYPE == "no": if os.path.exists("/usr/share/lve/dbgovernor/da.tp.old"): MYSQL_DA_TYPE = read_file("/usr/share/lve/dbgovernor/da.tp.old") elif os.path.exists("/usr/bin/mysql"): result = exec_command("/usr/bin/mysql -V | grep -c 'MariaDB' -i || true", True) if result == "0": MYSQL_DA_TYPE = "mysql" else: MYSQL_DA_TYPE = "mariadb" print "I got %s and %s" % (MYSQL_DA_VER, MYSQL_DA_TYPE) mysql_version_map = { "5.0": "mysql50", "5.1": "mysql51", "5.5": "mysql55", "5.6": "mysql56", "5.7": "mysql57", "10.0.0": "mariadb100", "10.1.1": "mariadb101" } mariadb_version_map = { "10.1": "mariadb101", "10.0": "mariadb100", "5.6": "mariadb100", "5.5": "mariadb100", "10.0.0": "mariadb100", "10.1.1": "mariadb100" } if MYSQL_DA_TYPE == "mysql": MYSQL_DA_VER = mysql_version_map[MYSQL_DA_VER] elif MYSQL_DA_TYPE == "mariadb": MYSQL_DA_VER = mariadb_version_map[MYSQL_DA_VER] return MYSQL_DA_VER
def _set_mysql_access(self): """ Set mysql admin login and password and save it to governor config """ self.get_mysql_user() if self.MYSQLUSER and self.MYSQLPASSWORD: print "Patch governor configuration file" check_file("/etc/container/mysql-governor.xml") patch_governor_config(self.MYSQLUSER, self.MYSQLPASSWORD) if exec_command("rpm -qa governor-mysql", True): service("restart", "db_governor") print "DB-Governor restarted..."
def write_singlechannel_csv(ts_fields_cleaned, channelId, ts_datetime=None): try: csv_file = scriptsFolder + '/offline-' + str(channelId) + '.csv' # Allowed ThingSpeak fields: csv_columns = [ 'datetime', 'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8', 'latitude', 'longitude', 'elevation', 'status' ] check_file(csv_file, 5, 10, 1) # Create row with data row = {} if ts_datetime is not None: row['datetime'] = ts_datetime else: row['datetime'] = thingspeak_datetime() for key, value in ts_fields_cleaned.items(): row[key] = str(value) # Write to CSV File write_header = (not os.path.isfile(csv_file) or os.stat(csv_file).st_size == 0 ) # exists or is empty with io.open(csv_file, 'a', newline='', encoding='utf8') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=csv_columns, extrasaction='ignore', delimiter=',', lineterminator='\n') if write_header: writer.writeheader() # file doesn't exist yet, write a header writer.writerow(row) return True except IOError as ex1: logger.error("IOError in write_singlechannel_csv: " + repr(ex1)) except Exception as ex: logger.exception("Unhandled exception in write_singlechannel_csv") return False
def _delete(self, installed_packages): """ Remove installed packages """ check_file("/usr/local/directadmin/custombuild/build") print "Removing mysql for db_governor start" self._mysqlservice("stop") # remove governor package exec_command_out("rpm -e governor-mysql") # delete installed packages remove_packages(installed_packages) param = "mysql" if os.path.exists("/usr/share/lve/dbgovernor/da.tp.old"): param = read_file("/usr/share/lve/dbgovernor/da.tp.old") exec_command_out("/usr/local/directadmin/custombuild/build set mysql_inst %s" % param) exec_command_out("/usr/local/directadmin/custombuild/build mysql update") print "Removing mysql for db_governor completed"
def delete(self): """ Delete governor packages """ # first check config file check_file("/etc/my.cnf") # save current installed mysql version self._save_previous_version() # get list of installed packages installed_packages = self._load_current_packages() # remove repo file if os.path.exists("/etc/yum.repos.d/cl-mysql.repo"): shutil.move("/etc/yum.repos.d/cl-mysql.repo", "/etc/yum.repos.d/cl-mysql.repo.bak") # remove cron file if os.path.exists("/etc/cron.d/dbgovernor-usermap-cron"): shutil.move("/etc/cron.d/dbgovernor-usermap-cron", "/etc/cron.d/dbgovernor-usermap-cron.bak") # backup my.cnf file for restore if uninstall will be failed # if os.path.exists("/etc/my.cnf"): # shutil.copy2("/etc/my.cnf", "/etc/my.cnf.prev") self.my_cnf_manager('backup') # run trigger before governor uninstal self._before_delete() # run uninstall action self._delete(installed_packages) # run trigger after governor uninstall self._after_delete()
def parse_include_directive(line, line_elements=None): """.include <filename> [*comments] """ if line_elements is None: line_elements = line.split() if not len(line_elements) > 1 or \ (len(line_elements) > 2 and not line_elements[2][0] == '*'): raise NetlistParseError, "" path = line_elements[1] if not utilities.check_file(path): raise RuntimeError, "" fnew = open(path, "r") return [None, path, True]
options.transient_max_time_iter = int( float(cli_options.transient_max_time_iter)) if cli_options.shooting_max_nr_iter is not None: options.shooting_max_nr_iter = int( float(cli_options.shooting_max_nr_iter)) if cli_options.gmin is not None: options.gmin = float(cli_options.gmin) if cli_options.cmin is not None: options.cmin = float(cli_options.cmin) if cli_options.eps: utilities.EPS = utilities.calc_eps() print "Detected machine precision: " + str(utilities.EPS) if not len(remaning_args) == 1: print "Usage: ./ahkab.py [options] <filename>\n./ahkab.py -h for help" sys.exit(1) if remaning_args[0] == '-': read_netlist_from_stdin = True else: read_netlist_from_stdin = False if not read_netlist_from_stdin and not utilities.check_file( remaning_args[0]): sys.exit(23) # Program execution ahkab.main(filename=remaning_args[0], outfile=cli_options.outfile, tran_method=cli_options.method, \ no_step_control=cli_options.no_step_control, dc_guess=cli_options.dc_guess, \ print_circuit=cli_options.print_circuit, remote=False, verbose=verbose) sys.exit(0)
if cli_options.transient_max_nr_iter is not None: options.transient_max_nr_iter = int(float(cli_options.transient_max_nr_iter)) if cli_options.transient_max_time_iter is not None: options.transient_max_time_iter = int(float(cli_options.transient_max_time_iter)) if cli_options.shooting_max_nr_iter is not None: options.shooting_max_nr_iter = int(float(cli_options.shooting_max_nr_iter)) if cli_options.gmin is not None: options.gmin = float(cli_options.gmin) if cli_options.cmin is not None: options.cmin = float(cli_options.cmin) if cli_options.eps: utilities.EPS = utilities.calc_eps() print "Detected machine precision: " + str(utilities.EPS) if not len(remaning_args) == 1: print "Usage: ./ahkab.py [options] <filename>\n./ahkab.py -h for help" sys.exit(1) if remaning_args[0] == '-': read_netlist_from_stdin = True else: read_netlist_from_stdin = False if not read_netlist_from_stdin and not utilities.check_file(remaning_args[0]): sys.exit(23) # Program execution ahkab.main(filename=remaning_args[0], outfile=cli_options.outfile, tran_method=cli_options.method, \ no_step_control=cli_options.no_step_control, dc_guess=cli_options.dc_guess, \ print_circuit=cli_options.print_circuit, remote=False, verbose=verbose) sys.exit(0)