def run(self):
        if self.run_modal(self.ok, self.cancel):
            if self.password.get_string_value() != self.confirm.get_string_value():
                mforms.Utilities.show_error("Reset Password", "The password and its confirmation do not match. Please try again.", "OK", "", "")
                return self.run()
            
            con = self._conn.shallow_copy()
            old_multi_statements = con.parameterValues.get("CLIENT_MULTI_STATEMENTS")
            old_script = con.parameterValues.get("preInit")
            con.parameterValues["CLIENT_MULTI_STATEMENTS"] = 1
            con.parameterValues["preInit"] = "SET PASSWORD = '******'" % escape_sql_string(self.password.get_string_value())
            #con.parameterValues["preInit"] = "ALTER USER '%s'@'%s' IDENTIFIED BY '%s'" % (con.parameterValues["userName"], con.hostIdentifier.replace("Mysql@", ""), escape_sql_string(self.password.get_string_value()))
            #change_pw = "ALTER USER '%s'@'%s' IDENTIFIED BY '%s'" % (con.parameterValues["userName"], con.hostIdentifier.replace("Mysql@", ""), escape_sql_string(self.password.get_string_value())) 
            retry = False
            result = 1

            c = MySQLConnection(con, password=self.old_password.get_string_value())
            # connect to server so that preInitScript will do the password reset work
            try:
                log_info("About to connecto to MySQL Server to change expired password")
                c.connect()
            except MySQLError, e:
                if mforms.Utilities.show_error("Reset Password", str(e), "Retry", "Cancel", "") == mforms.ResultOk:
                    retry = True
                result = 0

            if retry:
                return self.run()

            return result
 def handle_server_state_changed(self, name, sender, info):
     # called from outside when running state of the server changes
     log_info("received notification that server state changed to %s\n" % info["state"])
     if info["state"]:
         self.ctrl_be.event_from_main("server_started")
     else:
         self.ctrl_be.event_from_main("server_stopped")
    def run(self):
        if self.run_modal(self.ok, self.cancel):
            if self.password.get_string_value() != self.confirm.get_string_value():
                mforms.Utilities.show_error("Reset Password", "The password and its confirmation do not match. Please try again.", "OK", "", "")
                return self.run()
            
            con = self._conn.shallow_copy()
            old_multi_statements = con.parameterValues.get("CLIENT_MULTI_STATEMENTS")
            old_script = con.parameterValues.get("preInit")
            con.parameterValues["CLIENT_MULTI_STATEMENTS"] = 1
            con.parameterValues["preInit"] = "SET PASSWORD = PASSWORD('%s')" % escape_sql_string(self.password.get_string_value())
            #con.parameterValues["preInit"] = "ALTER USER '%s'@'%s' IDENTIFIED BY '%s'" % (con.parameterValues["userName"], con.hostIdentifier.replace("Mysql@", ""), escape_sql_string(self.password.get_string_value()))
            #change_pw = "ALTER USER '%s'@'%s' IDENTIFIED BY '%s'" % (con.parameterValues["userName"], con.hostIdentifier.replace("Mysql@", ""), escape_sql_string(self.password.get_string_value())) 
            retry = False
            result = 1

            c = MySQLConnection(con, password = self.old_password.get_string_value())
            # connect to server so that preInitScript will do the password reset work
            try:
                log_info("About to connecto to MySQL Server to change expired password")
                c.connect()
            except MySQLError, e:
                if mforms.Utilities.show_error("Reset Password", str(e), "Retry", "Cancel", "") == mforms.ResultOk:
                    retry = True
                result = 0

            if retry:
                return self.run()

            return result
    def get_info(self):
        cmd = "%s -al -so %s" % (get_exe_path("ogrinfo"), self.get_path())
        p1 = cmd_executor(cmd)
        sout, serr = p1.communicate(input)
        if serr:
            log_error("There was an error getting file information: %s" % serr)
        import re
        p = re.compile("^(\w+):\s(\w+)\s\(([0-9\.]+)\)", re.IGNORECASE)
        for line in sout.splitlines():
            if line.startswith("Layer name: "):
                self.layer_name_lbl.set_text(line.split(':')[1].strip())
                self.layer_name = line.split(':')[1].strip()
                self.table_name.set_value(line.split(':')[1].strip())
            else:
                m = p.match(line)
                if m is not None:
                    row = self.column_list.add_node()
                    row.set_bool(0, True)
                    row.set_string(1, m.group(1))

        from grt.modules import Utilities
        res = Utilities.fetchAuthorityCodeFromFile(
            "%s.prj" % os.path.splitext(self.get_path())[0])
        if res:
            self.epsg_lbl.set_text(res)
        else:
            self.epsg_lbl.set_text(0)
            log_info("Can't find EPSG fallback to 0")
    def get_info(self):
        cmd = "%s -al -so %s" % (get_exe_path("ogrinfo"), self.get_path())
        p1 = cmd_executor(cmd)
        sout, serr = p1.communicate(input)
        if serr:
            log_error("There was an error getting file information: %s" % serr)
        import re
        p = re.compile("^(\w+):\s(\w+)\s\(([0-9\.]+)\)", re.IGNORECASE)
        for line in sout.splitlines():
            if line.startswith("Layer name: "):
                self.layer_name_lbl.set_text(line.split(':')[1].strip())
                self.layer_name = line.split(':')[1].strip()
                self.table_name.set_value(line.split(':')[1].strip())
            else:
               m = p.match(line)
               if m is not None:
                    row = self.column_list.add_node()
                    row.set_bool(0, True)
                    row.set_string(1, m.group(1))

        from grt.modules import Utilities
        res = Utilities.fetchAuthorityCodeFromFile("%s.prj" % os.path.splitext(self.get_path())[0])
        if res:
            self.epsg_lbl.set_text(res)
        else:
            self.epsg_lbl.set_text(0)
            log_info("Can't find EPSG fallback to 0")
 def handle_server_state_changed(self, name, sender, info):
     # called from outside when running state of the server changes
     log_info("received notification that server state changed to %s\n" %
              info["state"])
     if info["state"]:
         self.ctrl_be.event_from_main("server_started")
     else:
         self.ctrl_be.event_from_main("server_stopped")
Example #7
0
def generateCertificates(parent, conn, conn_id):
    try:
        log_info("Running SSL Wizard\nParent: %s\nUser Folder: %s\nConn Parameters: %s\nConn ID: %s\n" % (str(parent), mforms.App.get().get_user_data_folder(), str(conn.parameterValues), conn_id))
        p = mforms.fromgrt(parent)
        log_info("Running SSL Wizard\n%s\n" % str(p))
        r = SSLWizard(p, conn, conn_id)
        r.run(True)
    except Exception, e:
        log_error("There was an exception running SSL Wizard.\n%s\n\n%s" % (str(e), traceback.format_exc()))
Example #8
0
def generateCertificates(parent, conn, conn_id):
    try:
        log_info("Running SSL Wizard\nParent: %s\nUser Folder: %s\nConn Parameters: %s\nConn ID: %s\n" % (str(parent), mforms.App.get().get_user_data_folder(), str(conn.parameterValues), conn_id))
        p = mforms.fromgrt(parent)
        log_info("Running SSL Wizard\n%s\n" % str(p))
        r = SSLWizard(p, conn, conn_id)
        r.run(True)
    except Exception, e:
        log_error("There was an exception running SSL Wizard.\n%s\n\n%s" % (str(e), traceback.format_exc()))
def get_sys_version_from_script(file_path):
    """Gets the version of the sys schema that's shipped with Workbench."""
    if not os.path.exists(file_path):
        log_info("No sys script found\n")
        return None
    for line in open(file_path):
        if line.startswith("CREATE OR REPLACE"):
            m = re.findall("SELECT '(.*)' AS sys_version", line)
            if m:
                return m[0]
    return None
Example #10
0
    def set_keepalive(self, port, keepalive):
        if keepalive == 0:
            log_info("SSH KeepAlive setting skipped.\n")
            return
        tunnel = self.tunnel_by_port.get(port)
        if not tunnel:
            log_error("Looking up invalid port %s\n" % port)
            return
        transport = tunnel._client.get_transport()
        if transport is None:
            log_error("SSHTransport not ready yet %d\n" % port)
            return

        transport.set_keepalive(keepalive)
    def set_keepalive(self, port, keepalive):
        if keepalive == 0:
            log_info("SSH KeepAlive setting skipped.\n")
            return
        tunnel = self.tunnel_by_port.get(port)
        if not tunnel:
            log_error("Looking up invalid port %s\n" % port)
            return
        transport = tunnel._client.get_transport()
        if transport is None:
            log_error("SSHTransport not ready yet %d\n" % port)
            return

        transport.set_keepalive(keepalive)
Example #12
0
    def run(self):
        if self.run_modal(self.ok, self.cancel):
            if self.password.get_string_value(
            ) != self.confirm.get_string_value():
                mforms.Utilities.show_error(
                    "Reset Password",
                    "The password and its confirmation do not match. Please try again.",
                    "OK", "", "")
                return self.run()

            con = self._conn.shallow_copy()

            con.parameterValues["CLIENT_MULTI_STATEMENTS"] = 1
            if self.legacy.get_active():
                con.parameterValues[
                    "preInit"] = "SET PASSWORD = PASSWORD('%s')" % escape_sql_string(
                        self.password.get_string_value())
            else:
                con.parameterValues[
                    "preInit"] = "SET PASSWORD = '******'" % escape_sql_string(
                        self.password.get_string_value())
            retry = False
            result = 1

            c = MySQLConnection(con,
                                password=self.old_password.get_string_value())
            # connect to server so that preInitScript will do the password reset work
            try:
                log_info(
                    "About to connecto to MySQL Server to change expired password"
                )
                c.connect()
                mforms.Utilities_store_password(
                    c.connect_info.hostIdentifier,
                    c.connect_info.parameterValues.userName,
                    self.password.get_string_value())
            except MySQLError as e:
                if mforms.Utilities.show_error("Reset Password", str(e),
                                               "Retry", "Cancel",
                                               "") == mforms.ResultOk:
                    retry = True
                result = 0
            except Exception as e:
                print("Error handling expired password: %s" % str(e))

            if retry:
                return self.run()

            return result
        return 0
        def create_select_type(row):
            def sel_changed(sel, output):
                selection = sel.get_string_value()
                for v in type_items:
                    if selection in type_items[v]:
                        if output['type'] == 'double' and type_items[
                                v] != 'double':
                            self.show_ds_box(False)

                        if output['type'] == 'datetime' and type_items[
                                v] != 'datetime':
                            self.show_df_box(False)

                        if type_items[v] == 'double':
                            self.show_ds_box(True)
                        if type_items[v] == 'datetime':
                            self.show_df_box(True)

                        output['type'] = type_items[v]

                        break

            sel = mforms.newSelector()
            sel.set_size(120, -1)

            items = [
                type for type in type_items.values()
                if not ((type == "geometry" or type == "json") and self.
                        input_file_type == "json" and not self.is_server_5_7)
            ]
            sel.add_items(items)
            if not self.is_server_5_7 and (
                    row['type'] == "geometry" or row["type"]
                    == "json") and self.input_file_type == "json":
                row['type'] = "text"  # If it's server older than 5.7 we don't have support for geojson so we can't properly import this file, instead we fallback to text
                log_info(
                    "Column %s is of type GeoJso but server doesn't support this, importing as text instead."
                    % row['name'])

            for i, v in enumerate(items):
                if row['type'] == v:
                    sel.set_selected(i)
                    break

            sel.add_changed_callback(lambda: sel_changed(sel, row))
            return sel
    def execute_cmd(self, cmd, progress_notify):
        p1 = cmd_executor(cmd)

        self.process_handle = p1

        pct = 0.0
        progress_notify(pct, "0 %")
        while p1 and p1.returncode is None:
            p1.poll()
            char = p1.stdout.read(1)

            if char == "0":
                continue
            else:
                if char == ".":
                    pct = pct + 0.03
                else:
                    try:
                        num = float(char)
                    except ValueError:
                        #it means that child process pass away, we can do nothing with it
                        break

                    if num == 1.0 and pct > 3.0:
                        progress_notify(pct, "Finished")
                        break
                    pct = num / 10.0
            progress_notify(pct, "%d %%" % int(pct * 100))

        sout, serr = p1.communicate()
        self.returncode = p1.returncode
        if self.returncode != 0:
            if self.user_cancel:
                log_info(
                    "Execute command failed with error: %s, the exit code was: %d.\n"
                    % (serr, self.returncode))
                raise grt.UserInterrupt()
            else:
                log_error(
                    "Execute command failed with error: %s, the exit code was: %d.\n"
                    % (serr, self.returncode))
                raise Exception(serr)

        log_info("Execute command succeeed.\n")
    def install_scripts(self, files, message):
        try:
            for f, db in files:
                self._progress_status = "%s %s..." % (message, f)
                self._progress_value = 0
                self.importer.import_script(f, db)

            log_info("sys schema installation finished\n")
        except grt.DBLoginError as e:
            log_error("MySQL login error installing sys schema: %s\n" % e)
            self._worker_queue.put(e)
        except grt.DBError as e:
            log_error("MySQL error installing sys schema: %s\n" % e)
            self._worker_queue.put(e)
        except Exception as e:
            import traceback
            log_error("Unexpected exception installing sys schema: %s\n%s\n" % (e, traceback.format_exc()))
            self._worker_queue.put(e)
        self._worker_queue.put(None)
    def __init__(self, interval, server_profile, ctrl_be, widgets, cpu_widget,
                 sql):
        self.ctrl_be = ctrl_be
        self.sources = []  # List of sources
        self.running = [True]
        self.poll_thread = None
        self.interval = interval

        self.ctrl_be.add_me_for_event("server_started", self)
        self.ctrl_be.add_me_for_event("server_offline", self)
        self.ctrl_be.add_me_for_event("server_stopped", self)

        if server_profile.target_os != wbaOS.windows:
            if server_profile.is_local or server_profile.remote_admin_enabled:
                cmd = ShellDataSource("host_stats",
                                      server_profile.detected_os_name, self,
                                      cpu_widget)
                self.sources.append(cmd)
            else:
                log_info(
                    "WBAMonBE: Data sources were not added. Profile set to non-local or remote admin is disabled."
                )
        else:
            if server_profile.uses_wmi:  # Force WMI for local boxes - pointless to force WMI, if it was not setup earlier (throws exc)
                self.wmimon = WMIStats(ctrl_be, server_profile, cpu_widget)
                self.sources.append(self.wmimon)
            else:
                if server_profile.remote_admin_enabled and server_profile.connect_method == 'ssh':
                    stats = WinRemoteStats(ctrl_be, server_profile,
                                           self.running, cpu_widget)
                    self.sources.append(stats)

        sql_sources = DBStatusDataSource(self)
        for name, query in sql.iteritems():
            widget = None
            if name in widgets:
                widget = widgets[name]
            sql_sources.add_source(name, query, widget)

        self.sources.append(sql_sources)

        self.poll_thread = None
    def run(self, progress_notify):
        cmd_args = {}
        cmd_args['host'] = self.my_host
        cmd_args['schema'] = self.my_schema
        cmd_args['user'] = self.my_user
        cmd_args['pwd'] = self.my_pwd
        cmd_args['port'] = self.my_port
        cmd_args['features_per_transcation'] = self.features_per_transcation
        if self.import_table:
            cmd_args['table_name'] = " -nln %s" % self.import_table
        else:
            cmd_args['table_name'] = ""

        cmd_args['opts'] = "-lco GEOMETRY_NAME=%s" % self.my_geo_column
        if self.skipfailures:
            cmd_args['opts'] = cmd_args['opts'] + " -skipfailures"

        if self.import_append:
            cmd_args['opts'] = cmd_args['opts'] + " -append"

        if self.import_overwrite:
            cmd_args['opts'] = cmd_args['opts'] + " -overwrite"

        if self.selected_fields:
            cmd_args[
                'opts'] = cmd_args['opts'] + " -select " + self.selected_fields

        cmd_args['filepath'] = self.filepath

        cmd = """ogr2ogr -f "MySQL" MySQL:"%(schema)s,host=%(host)s,user=%(user)s,password=%(pwd)s,port=%(port)d" %(filepath)s %(table_name)s %(opts)s -progress -gt %(features_per_transcation)d -lco ENGINE=InnoDb -lco SPATIAL_INDEX=NO""" % cmd_args
        self.is_running = True
        try:
            self.execute_cmd(cmd, progress_notify)
        except grt.UserInterrupt:
            log_info("User cancelled")
            raise
        except Exception, exc:
            import traceback
            log_error(
                "An error occured during execution of ogr2ogr file import: %s, stack: %s\n"
                % (exc, traceback.format_exc()))
            raise
Example #18
0
    def run(self, file):
        self.set_title("Run SQL Script - %s" % file)

        known_schemas = [""]
        result = self.editor.executeManagementQuery("SHOW SCHEMAS", 0)
        if result:
            while result.nextRow():
                s = result.stringFieldValue(0)
                if s not in [
                        "performance_schema", "mysql", "information_schema"
                ]:
                    known_schemas.append(s)
            self.schema.add_items(sorted(known_schemas))

        try:
            self.preview_data = open(file).read(4098)
            self.detected_charset = None
            r = re.compile("/\*[^ ]* SET NAMES ([a-zA-Z0-9_]*) \*/")
            # try to detect the charset name from the SET NAMES line
            for line in self.preview_data.split("\n"):
                if line.startswith("/*"):
                    g = r.match(line)
                    if g:
                        c = g.groups()[0]
                        log_info(
                            "Character set of file %s detected to be %s\n" %
                            (file, c))
                        self.detected_charset = c
                        break

            self.file_info.set_text(
                "%i total bytes in file, displaying first %i bytes" %
                (os.stat(file).st_size, len(self.preview_data)))
            self.update_preview()
        except Exception as e:
            mforms.Utilities.show_error("Run SQL Script", str(e), "OK", "", "")
            return

        self.set_size(800, 600)
        self.center()
        return self.run_modal(self.ok, self.cancel)
Example #19
0
def newConnectionFromClipboard():
    text = mforms.Utilities.get_clipboard_text()
    if not text:
        return 0

    existing = set()
    # make a normalized set of the connections that already exist
    for con in grt.root.wb.rdbmsMgmt.storedConns:
        existing.add(connectionStringFromConnection(con))

    parse_errors = False
    found_instances = 0
    for line in text.encode("utf8").split("\n"):
        conn = connectionFromString(line)
        if not conn and not parse_errors:
            mforms.Utilities.show_error("Add Connection(s) from Clipboard", "Could not parse connection parameters from string '%s'" % line, "OK", "", "")
            parse_errors = True
            continue
        
        if connectionStringFromConnection(conn) in existing:
            mforms.Utilities.show_error("Add Connection(s) from Clipboard", "The connection %s already exists and was not added." % line,
                                        "OK", "", "")
            continue
        i = 1
        name = conn.parameterValues.get("hostName", "local")
        prefix = name
        while any(conn.name == name for conn in grt.root.wb.rdbmsMgmt.storedConns):
            name = "%s (%i)" % (prefix, i)
            i += 1
        conn.name = name
        log_info("Added connection %s from clipboard\n" % conn.name)
        found_instances = found_instances + 1
        grt.root.wb.rdbmsMgmt.storedConns.append(conn)

    grt.modules.Workbench.refreshHomeConnections()

    if found_instances > 0:
        mforms.Utilities.show_message('Add Connection(s) from Clipboard', 'Found %s servers.' % found_instances, 'OK', '', '')

    return 1
Example #20
0
def newConnectionFromClipboard():
    text = mforms.Utilities.get_clipboard_text()
    if not text:
        return 0

    existing = set()
    # make a normalized set of the connections that already exist
    for con in grt.root.wb.rdbmsMgmt.storedConns:
        existing.add(connectionStringFromConnection(con))

    parse_errors = False
    found_instances = 0
    for line in text.encode("utf8").split("\n"):
        conn = connectionFromString(line)
        if not conn and not parse_errors:
            mforms.Utilities.show_error("Add Connection(s) from Clipboard", "Could not parse connection parameters from string '%s'" % line, "OK", "", "")
            parse_errors = True
            continue
        
        if connectionStringFromConnection(conn) in existing:
            mforms.Utilities.show_error("Add Connection(s) from Clipboard", "The connection %s already exists and was not added." % line,
                                        "OK", "", "")
            continue
        i = 1
        name = conn.parameterValues.get("hostName", "local")
        prefix = name
        while any(conn.name == name for conn in grt.root.wb.rdbmsMgmt.storedConns):
            name = "%s (%i)" % (prefix, i)
            i += 1
        conn.name = name
        log_info("Added connection %s from clipboard\n" % conn.name)
        found_instances = found_instances + 1
        grt.root.wb.rdbmsMgmt.storedConns.append(conn)

    grt.modules.Workbench.refreshHomeConnections()

    if found_instances > 0:
        mforms.Utilities.show_message('Add Connection(s) from Clipboard', 'Found %s servers.' % found_instances, 'OK', '', '')

    return 1
    def execute_cmd(self, cmd, progress_notify):
        p1 = cmd_executor(cmd)

        self.process_handle = p1

        pct = 0.0
        progress_notify(pct, "0 %")
        while p1 and p1.returncode is None:
            p1.poll()
            char = p1.stdout.read(1)
             
            if char == "0":
                continue
            else:
                if char == ".":
                    pct = pct + 0.03
                else:
                    try:
                        num = float(char)
                    except ValueError:
                        #it means that child process pass away, we can do nothing with it
                        break

                    if num == 1.0 and pct > 3.0:
                        progress_notify(pct, "Finished")
                        break
                    pct = num/10.0
            progress_notify(pct, "%d %%" % int(pct * 100))

        sout, serr = p1.communicate()
        self.returncode = p1.returncode
        if self.returncode !=0:
            if self.user_cancel:
                log_info("Execute command failed with error: %s, the exit code was: %d.\n" % (serr, self.returncode))
                raise grt.UserInterrupt()
            else:
                log_error("Execute command failed with error: %s, the exit code was: %d.\n" % (serr, self.returncode))
                raise Exception(serr)
            
        log_info("Execute command succeeed.\n")
    def work(self, files):
        try:
            if self.ctrl_be.target_version >= Version(5, 7, 10):
                self.importer.reset_schemas()
            else:
                location = download_server_install_script(self.ctrl_be)
              
                if location:
                    workbench_version_string = get_current_sys_version(None)
                    server_version_string = get_sys_version_from_script(location)
                    
                    maj, min, rel = [int(i) for i in workbench_version_string.split(".")]
                    workbench_version = Version(maj, min, rel)
                    maj, min, rel = [int(i) for i in server_version_string.split(".")]
                    server_version = Version(maj, min, rel)

                    if server_version >= workbench_version:
                        log_info("Installing sys schema supplied by the server: %s\n" % str(location))
                        self.install_scripts([(location, None)], "Installing server script")
                        return
                    else:
                        log_info("Server sys schema install script exists but it's outdated compared to the one supplied by Workbench...\n")
                        
                        
                log_info("Installing sys schema supplied by workbench\n")
                self.install_scripts(files, "Installing Workbench script")
        except Exception as e:
              log_error("Runtime error when installing the sys schema: %s\n" % str(e))
              self._worker_queue.put(e)
        
        # This makes the screen refresh
        self._worker_queue.put(None)      
Example #23
0
 def verify_preconditions(self):
     try:
         if not os.path.exists(self.main.certificates_root) or not os.path.isdir(self.main.certificates_root):
             log_info("Creating certificates toor directory[%s]" % self.main.certificates_root)
             os.mkdir(self.main.certificates_root, 0700)
         
         if os.path.exists(self.path) and not os.path.isdir(self.path):
             self.display_error("Checking requirements", "The selected path is a file. You should select a directory.")
             return False
         if not os.path.exists(self.path):
             if mforms.Utilities.show_message("Create directory", "The directory you selected does not exists. Do you want to create it?", "Create", "Cancel", "") == mforms.ResultCancel:
                 self.display_error("Create directory", "The operation was canceled.")
                 return False
             os.mkdir(self.path, 0700)
             
         return True
     except OSError, e:
         self.display_error("Create directory", "There was an error (%d) - %s\n%s" % (e.errno, str(e), str(traceback.format_exc())))
         if e.errno == 17:
             return True
             #raise
         return False
Example #24
0
def testInstanceSettingByName(what, connection, server_instance):
    global test_ssh_connection
    log_debug("Test %s in %s\n" % (what, connection.name))

    profile = ServerProfile(connection, server_instance)
    if what == "connect_to_host":
        if test_ssh_connection:
            test_ssh_connection = None

        log_info("Instance test: Connecting to %s\n" % profile.ssh_hostname)

        try:
            test_ssh_connection = wb_admin_control.WbAdminControl(profile, None, connect_sql=False, test_only = True)
            test_ssh_connection.init()
                
            grt.send_info("connected.")
        except Exception, exc:
            log_error("Exception: %s" % exc.message)
            import traceback
            log_debug2("Backtrace was: " % traceback.format_stack())
            return "ERROR "+str(exc)
        except:
Example #25
0
    def work(self, file, schema, charset):
        try:
            log_info("Executing %s...\n" % file)
            self._progress_status = "Executing %s..." % file
            self._progress_value = 0
            self.importer.import_script(file,
                                        default_schema=schema,
                                        default_charset=charset)

            log_info("Run script finished\n")
        except grt.DBLoginError as e:
            log_error("MySQL login error running script: %s\n" % e)
            self._worker_queue.put(e)
        except grt.DBError as e:
            log_error("MySQL error running script: %s\n" % e)
            self._worker_queue.put(e)
        except Exception as e:
            import traceback
            log_error("Unexpected exception running script: %s\n%s\n" %
                      (e, traceback.format_exc()))
            self._worker_queue.put(e)
        self._worker_queue.put(None)
    def run(self, progress_notify):
        cmd_args = {}
        cmd_args['host'] = self.my_host
        cmd_args['schema'] = self.my_schema
        cmd_args['user'] = self.my_user
        cmd_args['pwd'] = self.my_pwd
        cmd_args['port'] = self.my_port
        cmd_args['features_per_transcation'] = self.features_per_transcation
        if self.import_table:
            cmd_args['table_name'] = " -nln %s" % self.import_table
        else:
            cmd_args['table_name'] = ""

        cmd_args['opts'] = "-lco GEOMETRY_NAME=%s" % self.my_geo_column
        if self.skipfailures:
            cmd_args['opts'] = cmd_args['opts'] + " -skipfailures"
            
        if self.import_append:
            cmd_args['opts'] = cmd_args['opts'] + " -append"

        if self.import_overwrite:
            cmd_args['opts'] = cmd_args['opts'] + " -overwrite"
        
        if self.selected_fields:
            cmd_args['opts'] = cmd_args['opts'] + " -select " + self.selected_fields
        
        cmd_args['filepath'] = self.filepath

        cmd = """ogr2ogr -f "MySQL" MySQL:"%(schema)s,host=%(host)s,user=%(user)s,password=%(pwd)s,port=%(port)d" %(filepath)s %(table_name)s %(opts)s -progress -gt %(features_per_transcation)d -lco ENGINE=InnoDb -lco SPATIAL_INDEX=NO""" % cmd_args
        self.is_running = True
        try:
            self.execute_cmd(cmd, progress_notify)
        except grt.UserInterrupt:
            log_info("User cancelled")
            raise
        except Exception, exc:
            import traceback
            log_error("An error occurred during execution of ogr2ogr file import: %s, stack: %s\n" % (exc, traceback.format_exc()))
            raise
def testInstanceSettingByName(what, connection, server_instance):
    global test_ssh_connection
    log_debug("Test %s in %s\n" % (what, connection.name))

    profile = ServerProfile(connection, server_instance)
    if what == "connect_to_host":
        if test_ssh_connection:
            test_ssh_connection = None

        log_info("Instance test: Connecting to %s\n" % profile.ssh_hostname)

        try:
            test_ssh_connection = wb_admin_control.WbAdminControl(profile, None, connect_sql=False, test_only=True)
            test_ssh_connection.init()

            grt.send_info("connected.")
        except Exception, exc:
            log_error("Exception: %s\n" % exc.message)
            import traceback
            log_debug2("Backtrace was: ", traceback.format_stack())
            return "ERROR "+str(exc)
        except:
Example #28
0
 def verify_preconditions(self):
     try:
         if not os.path.exists(self.main.certificates_root) or not os.path.isdir(self.main.certificates_root):
             log_info("Creating certificates toor directory[%s]" % self.main.certificates_root)
             os.mkdir(self.main.certificates_root, 0700)
         
         if os.path.exists(self.path) and not os.path.isdir(self.path):
             self.display_error("Checking requirements", "The selected path is a file. You should select a directory.")
             return False
         if not os.path.exists(self.path):
             if mforms.Utilities.show_message("Create directory", "The directory you selected does not exists. Do you want to create it?", "Create", "Cancel", "") == mforms.ResultCancel:
                 self.display_error("Create directory", "The operation was canceled.")
                 return False
             os.mkdir(self.path, 0700)
             
         return True
     except OSError, e:
         self.display_error("Create directory", "There was an error (%d) - %s\n%s" % (e.errno, str(e), str(traceback.format_exc())))
         if e.errno == 17:
             return True
             #raise
         return False
def testInstanceSettingByName(what, connection, server_instance):
    global test_ssh_connection

    log_debug("Test %s in %s\n" % (what, connection.name))

    profile = ServerProfile(connection, server_instance)

    if what == "connect_to_host":
        if test_ssh_connection:
            test_ssh_connection = None

        log_info("Instance test: Connecting to %s\n" % profile.ssh_hostname)

        try:
            test_ssh_connection = wb_admin_control.WbAdminControl(
                profile, None, connect_sql=False)
            test_ssh_connection.init()
            grt.send_info("connected.")
        except Exception, exc:
            import traceback
            traceback.print_exc()
            return "ERROR " + str(exc)
        except:
Example #30
0
    def __init__(self, interval, server_profile, ctrl_be, widgets, cpu_widget, sql):
        self.ctrl_be   = ctrl_be
        self.sources     = [] # List of sources
        self.running     = [True]
        self.poll_thread = None
        self.interval  = interval

        self.ctrl_be.add_me_for_event("server_started", self)
        self.ctrl_be.add_me_for_event("server_offline", self)
        self.ctrl_be.add_me_for_event("server_stopped", self)

        if server_profile.target_os != wbaOS.windows:
            if server_profile.is_local or server_profile.remote_admin_enabled:
                cmd = ShellDataSource("host_stats", server_profile.detected_os_name, self, cpu_widget)
                self.sources.append(cmd)
            else:
                log_info("WBAMonBE: Data sources were not added. Profile set to non-local or remote admin is disabled.")
        else:
            if server_profile.uses_wmi: # Force WMI for local boxes - pointless to force WMI, if it was not setup earlier (throws exc)
                self.wmimon = WMIStats(ctrl_be, server_profile, cpu_widget)
                self.sources.append(self.wmimon)
            else:
                if server_profile.remote_admin_enabled and server_profile.connect_method == 'ssh':
                    stats = WinRemoteStats(ctrl_be, server_profile, self.running, cpu_widget)
                    self.sources.append(stats)

        sql_sources = DBStatusDataSource(self)
        for name, query in sql.iteritems():
            widget = None
            if name in widgets:
                widget = widgets[name]
            sql_sources.add_source(name, query, widget)

        self.sources.append(sql_sources)

        self.poll_thread = None
        def create_select_type(row):
            def sel_changed(sel, output):
                selection = sel.get_string_value()
                for v in type_items:
                    if selection in type_items[v]:
                        if output['type'] == 'double' and type_items[v] != 'double':
                            self.show_ds_box(False)
                        
                        if output['type'] == 'datetime' and type_items[v] != 'datetime':
                            self.show_df_box(False)
                            
                        if type_items[v] == 'double':
                            self.show_ds_box(True)
                        if type_items[v] == 'datetime':
                            self.show_df_box(True)
                            
                        output['type'] = type_items[v]
                        
                        break  
                
            sel = mforms.newSelector()
            sel.set_size(120, -1)

            items = [type for type in type_items.values() if not ((type == "geometry" or type == "json") and self.input_file_type == "json" and not self.is_server_5_7) ]
            sel.add_items(items)
            if not self.is_server_5_7 and (row['type'] == "geometry" or row["type"] == "json") and self.input_file_type == "json":
                row['type'] = "text" # If it's server older than 5.7 we don't have support for geojson so we can't properly import this file, instead we fallback to text
                log_info("Column %s is of type GeoJso but server doesn't support this, importing as text instead." % row['name'])
                
            for i, v in enumerate(items):
                if row['type'] == v:
                    sel.set_selected(i)
                    break
            
            sel.add_changed_callback(lambda: sel_changed(sel, row))
            return sel
Example #32
0
    def __init__(self,
                 ctrl_be,
                 log_file_param,
                 pat,
                 chunk_size,
                 truncate_long_lines,
                 append_gaps=True):
        """Constructor

            :param ctrl_be:  Control backend instance to retrieve root password if needed
            :param log_file_param: The path to the log file to read from or a file like instance to
            read log entries from
            :type log_file_param: str/file
            :param pat: A regular expression pattern that matches a log entry
            :type pat: regular expression object
            :param chunk_size: The size in bytes of the chunks that are read from the log file
            :type chunk_size: int
            :param truncate_long_lines: Whether the log entries that are long should be abbreviated
            :type truncate_long_lines: bool
            :param append_gaps: Whether the data between the end of the record regex and the start
            of the next record regex should be added to the previous entry
            :type append_gaps: (not used)
            """

        self.pat = pat  # the regular expression that identifies a record
        self.pat2 = pat  # subclasses may override this if they want a different pattern for parsing chunks
        self.append_gaps = append_gaps
        # regex are considered to belong to the last field of the record
        self.truncate_long_lines = truncate_long_lines
        self.ctrl_be = ctrl_be

        self.partial_support = False

        # If there isn't a directory component in the path, use @@datadir as the directory for the log file:
        log_file_param = log_file_param.strip(' "')
        ospath = server_os_path(self.ctrl_be.server_profile)
        datadir = self.ctrl_be.server_profile.datadir
        if not datadir and ctrl_be.is_sql_connected():
            datadir = self.ctrl_be.get_server_variable('datadir')
        self.log_file_name = log_file_param if ospath.isabs(
            log_file_param) else ospath.join(datadir, log_file_param)

        use_sftp = False
        use_event_viewer = False
        if self.ctrl_be.server_profile.target_is_windows:
            use_sudo = False
            # In Windows we can either access the file locally as a plain file or remotely with sftp
            # Ther is no upport for reading log files that are only readable by the admin
            if log_file_param.lower() == "stderr":
                use_event_viewer = True
            if not self.ctrl_be.server_profile.is_local:
                if not self.ctrl_be.server_profile.remote_admin_enabled:
                    raise LogFileAccessError(
                        '''You have not enabled remote administration for this server. Without it this log file cannot be shown.
                        Please enable remote administration in this server instance and try again.'''
                    )
                if not self.ctrl_be.server_profile.uses_ssh:
                    raise LogFileAccessError(
                        '''Remote log files are only supported for SSH connection.
                        Please configure an SSH connection and try again.''')
                use_sftp = True
        else:
            use_sudo = self.ctrl_be.server_profile.use_sudo
            # In Mac/Linux, we can access local files with sudo and remote files with sudo too
            if not self.ctrl_be.server_profile.is_local:
                if not self.ctrl_be.server_profile.remote_admin_enabled:
                    raise LogFileAccessError(
                        '''You have not enabled remote administration for this server. Without it this log file cannot be shown.
                        Please enable remote administration in this server instance and try again.'''
                    )

                if not self.ctrl_be.server_profile.uses_ssh:
                    raise LogFileAccessError(
                        '''Remote log files are only supported for SSH connection.
                        Please configure an SSH connection and try again.''')
                use_sftp = True

        if use_sudo:
            log_info("Will use sudo and dd to get contents of log file %s\n" %
                     self.log_file_name)
            password = ctrl_be.password_handler.get_password_for(
                'file', cached_only=True)

            retry = False
            try:
                self.log_file = SudoTailInputFile(self.ctrl_be.server_helper,
                                                  self.log_file_name, password)
                self.file_size = self.log_file.size
            except InvalidPasswordError as error:
                if password is None:
                    retry = True
                else:
                    log_error("Invalid password to sudo %s\n" % error)
                    ctrl_be.password_handler.reset_password_for('file')
                    raise
            if retry:  # either there was no password cached or the cached password was wrong, so retry interactively
                password = ctrl_be.password_handler.get_password_for(
                    'file', cached_only=False)
                try:
                    self.log_file = SudoTailInputFile(
                        self.ctrl_be.server_helper, self.log_file_name,
                        password)
                    self.file_size = self.log_file.size
                except InvalidPasswordError as error:
                    log_error("Invalid password to sudo %s\n" % error)
                    ctrl_be.password_handler.reset_password_for('file')
                    raise
        elif use_event_viewer:
            if not self.ctrl_be.server_profile.is_local:
                raise LogFileAccessError(
                    '''An attempt to read events from the remote server failed. Events can only be read from the local machine, 
                    hence installed MySQL Workbench on the remote machine to allow showing the server's event log.'''
                )
            self.log_file = EventLogInput(self.ctrl_be, self.log_file_name)
            self.file_size = self.log_file.size
        elif use_sftp:
            log_info("Will use sftp to get contents of log file %s\n" %
                     self.log_file_name)
            self.log_file = SFTPInputFile(self.ctrl_be, self.log_file_name)
            self.file_size = self.log_file.size
        else:
            log_info(
                "Will use plain file access to get contents of log file %s\n" %
                self.log_file_name)
            self.log_file = LocalInputFile(self.log_file_name)
            self.file_size = self.log_file.size

        self.chunk_size = chunk_size
        # chunk_start is the start of the chunk to be read (ie, we start reading from the end of the file, so we get the last page starting from there)
        self.chunk_start = max(0, self.file_size - chunk_size)
        self.chunk_end = self.file_size

        self.record_count = 0

        data = self.log_file.get_range(0, self.chunk_size)
        self.first_record_position = self._get_offset_to_first_record(data)
Example #33
0
    def import_script(self, path, default_schema=None, default_charset="utf8"):
        if not self._tool_path:
            raise RuntimeError(
                "mysql command line client not found. Please fix its path in Preferences -> Administration"
            )

        is_windows = platform.system() == 'Windows'

        if is_windows:
            params = ['"%s"' % self._tool_path]
            pwdfile = tempfile.NamedTemporaryFile(delete=False, suffix=".cnf")
            pwdfilename = pwdfile.name
            tmpdir = None
        else:
            params = [to_unicode(self._tool_path)]
            # use a pipe to feed the password to the client
            tmpdir = tempfile.mkdtemp()
            pwdfilename = os.path.join(tmpdir, 'extraparams.cnf')
            os.mkfifo(pwdfilename)
        params.append('--defaults-extra-file=' + pwdfilename)

        if default_charset:
            params.append("--default-character-set=%s" % default_charset)
        params += self._connection_params
        params += self._extra_params

        if default_schema:
            params.append(default_schema)

        cmdstr = " ".join(params)

        workdir = os.path.dirname(path)

        log_info("Feeding data from %s to %s (cwd=%s)\n" %
                 (path, cmdstr, workdir))
        p1 = None
        try:
            self.report_progress("Preparing...", None, None)
            if not is_windows:
                try:
                    p1 = subprocess.Popen(params,
                                          cwd=workdir,
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          encoding='utf8')
                except OSError as exc:
                    log_error("Error executing command %s\n%s\n" %
                              (" ".join(params), exc))
                    raise RuntimeError("Error executing %s:\n%s" %
                                       (" ".join(params), str(exc)))

            # in !Windows feed password to client after it's started (otherwise the fifo would block on open for writing)
            with open(pwdfilename, 'w') as pwdfile:
                pwdfile.write('[client]\npassword='******''
                pwdfile.write(self._password.replace("\\", "\\\\"))
                pwdfile.write('\n')

            if is_windows:
                try:
                    info = subprocess.STARTUPINFO()
                    info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                    info.wShowWindow = subprocess.SW_HIDE
                    # Command line can contain object names in case of export and filename in case of import
                    # Object names must be in utf-8 but filename must be encoded in the filesystem encoding,
                    # which probably isn't utf-8 in windows.
                    log_debug("Executing command: %s\n" % cmdstr)
                    p1 = subprocess.Popen(cmdstr,
                                          cwd=workdir,
                                          stdout=subprocess.PIPE,
                                          stdin=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          startupinfo=info,
                                          shell=cmdstr[0] != '"',
                                          encoding='utf8')
                except OSError as exc:
                    log_error("Error executing command %s\n%s\n" %
                              (cmdstr, str(exc)))
                    import traceback
                    traceback.print_exc()
                    raise RuntimeError("Error executing %s:\n%s" %
                                       (cmdstr, str(exc)))

            # do the import
            total_size = os.stat(path).st_size
            processed = 0
            self.report_progress("Importing %s..." % os.path.basename(path), 0,
                                 total_size)
            stdout_q, thr = start_reading_from(p1.stdout)

            with open(path, "r") as input_file:
                while p1 and p1.poll() == None:
                    try:
                        if stdout_q:
                            text = stdout_q.get_nowait()
                            if text:
                                log_info("Task stdout: %s\n" % text)
                                if 'Access denied for user' in text:
                                    raise grt.DBLoginError(text)
                                elif "Can't open named pipe to host" in text and sys.platform.lower(
                                ) == "win32":
                                    text = "%s\n%s" % (
                                        text,
                                        "Please check if the server started with the --enabled-named-pipe parameter. The parameter can also be set in the config file."
                                    )
                                self.report_output(text.strip())
                            elif text is None:
                                stdout_q = None
                    except Empty:
                        pass

                    line = input_file.readline()
                    if not line:
                        break
                    processed += len(line)
                    try:
                        p1.stdin.write(line)
                    except IOError as e:
                        log_error(
                            "Exception writing to stdin from cmdline client: %s\n"
                            % e)
                        if e.errno == 32:  # broken pipe
                            log_error("Broken pipe from child process\n")
                            break
                        elif e.errno == 22:  # invalid argument (happens in Windows, when child process just dies)
                            log_error("Broken pipe from child process\n")
                            break
                        raise e
                    self.report_progress(None, processed, total_size)

            # close the writer end of the client's pipe, so it can exit
            p1.stdin.close()

            self.report_progress("Finished executing script", processed,
                                 total_size)

            # flush queue from reader
            if stdout_q:
                while True:
                    text = stdout_q.get()
                    if text is None:
                        break
                    log_info("Task stdout: %s\n" % text)
                    if 'Access denied for user' in text:
                        raise grt.DBLoginError(text)
                    elif "Can't open named pipe to host" in text and sys.platform.lower(
                    ) == "win32":
                        text = "%s\n%s" % (
                            text,
                            "Please check if the server started with the --enabled-named-pipe parameter. The parameter can also be set in the config file."
                        )
                    self.report_output(text.strip())

            # let reader thread die
            thr.join()

            p1.wait()

            exitcode = p1.returncode

            log_info("mysql tool exited with code %s\n" % exitcode)

            if exitcode != 0:
                self.report_progress(
                    "Operation failed with exitcode " + str(exitcode), None,
                    None)
            else:
                self.report_progress("Operation completed successfully", None,
                                     None)

            return exitcode
        finally:
            if pwdfilename:
                os.remove(pwdfilename)
            if tmpdir:
                os.rmdir(tmpdir)
Example #34
0
class MySQLScriptImporter(object):
    """Import a SQL script using the MySQL command line tool"""
    def __init__(self, connection_params):
        self._extra_params = []
        self._password = ""

        self._tool_path = get_path_to_mysql()

        self._tunnel = ConnectionTunnel(connection_params)

        conn = connection_params.parameterValues
        params = []
        if connection_params.driver.name == "MysqlNativeSocket":
            params.append("--protocol=" +
                          ("pipe" if sys.platform == "win32" else "socket"))
            if conn["socket"]:
                params.append("--socket=" + conn["socket"])
        else:
            params.append("--protocol=tcp")
            if self._tunnel.port or conn["port"]:
                params.append("--port=" +
                              str(self._tunnel.port or conn["port"]))
            if (self._tunnel.port and ["localhost"] or [conn["hostName"]])[0]:
                params.append("--host=" + (self._tunnel.port and ["localhost"]
                                           or [conn["hostName"]])[0])

        if conn.get("useSSL", 0):
            if conn.get("sslCert", ""):
                params.append("--ssl-cert=%s" % conn["sslCert"])
            if conn.get("sslCA", ""):
                params.append("--ssl-ca=%s" % conn["sslCA"])
            if conn.get("sslKey", ""):
                params.append("--ssl-key=%s" % conn["sslKey"])
            if conn.get("sslCipher", ""):
                params.append("--ssl-cipher=%s" % conn["sslCipher"])

        if conn.get("OPT_ENABLE_CLEARTEXT_PLUGIN", ""):
            params.append("--enable-cleartext-plugin")

        params += ["--user="******"userName"]]
        self._connection_params = params

    def set_extra_params(self, param_list):
        self._extra_params = param_list

    def set_password(self, password):
        self._password = password

    def report_progress(self, message, current, total):
        pass

    def report_output(self, text):
        print text

    def import_script(self, path, default_schema=None, default_charset="utf8"):
        if not self._tool_path:
            raise RuntimeError(
                "mysql command line client not found. Please fix its path in Preferences -> Administration"
            )

        is_windows = platform.system() == 'Windows'

        if is_windows:
            params = ['"%s"' % self._tool_path]
            pwdfile = tempfile.NamedTemporaryFile(delete=False, suffix=".cnf")
            pwdfilename = pwdfile.name
            tmpdir = None
        else:
            params = [self._tool_path]
            # use a pipe to feed the password to the client
            tmpdir = tempfile.mkdtemp()
            pwdfilename = os.path.join(tmpdir, 'extraparams.cnf')
            os.mkfifo(pwdfilename)
        params.append('--defaults-extra-file=' + pwdfilename)

        if default_charset:
            params.append("--default-character-set=%s" % default_charset)
        params += self._connection_params
        params += self._extra_params

        if default_schema:
            params.append(default_schema)

        cmdstr = " ".join(params)

        workdir = os.path.dirname(path)

        log_info("Feeding data from %s to %s (cwd=%s)\n" %
                 (path, cmdstr, workdir))
        p1 = None
        try:
            self.report_progress("Preparing...", None, None)
            if not is_windows:
                try:
                    p1 = subprocess.Popen(params,
                                          cwd=workdir,
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT)
                except OSError, exc:
                    log_error("Error executing command %s\n%s\n" %
                              (" ".join(params), exc))
                    raise RuntimeError("Error executing %s:\n%s" %
                                       (" ".join(params), str(exc)))

            # in !Windows feed password to client after it's started (otherwise the fifo would block on open for writing)
            pwdfile = open(pwdfilename, 'w')
            pwdfile.write('[client]\npassword='******'\n')
            pwdfile.close()

            if is_windows:
                try:
                    info = subprocess.STARTUPINFO()
                    info.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
                    info.wShowWindow = _subprocess.SW_HIDE
                    # Command line can contain object names in case of export and filename in case of import
                    # Object names must be in utf-8 but filename must be encoded in the filesystem encoding,
                    # which probably isn't utf-8 in windows.
                    fse = sys.getfilesystemencoding()
                    cmd = cmdstr.encode(fse) if isinstance(cmdstr,
                                                           unicode) else cmdstr
                    log_debug("Executing command: %s\n" % cmdstr)
                    p1 = subprocess.Popen(cmd,
                                          cwd=workdir,
                                          stdout=subprocess.PIPE,
                                          stdin=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          startupinfo=info,
                                          shell=cmdstr[0] != '"')
                except OSError, exc:
                    log_error("Error executing command %s\n%s\n" %
                              (cmdstr, exc))
                    import traceback
                    traceback.print_exc()
                    raise RuntimeError("Error executing %s:\n%s" %
                                       (cmdstr, str(exc)))

            # do the import
            total_size = os.stat(path).st_size
            processed = 0

            self.report_progress("Importing %s..." % os.path.basename(path), 0,
                                 total_size)
            stdout_q, thr = start_reading_from(p1.stdout)

            input_file = open(path, "r")
            while p1 and p1.poll() == None:
                try:
                    if stdout_q:
                        text = stdout_q.get_nowait()
                        if text:
                            log_info("Task stdout: %s\n" % text)
                            if 'Access denied for user' in text:
                                raise grt.DBLoginError(text)
                            elif "Can't open named pipe to host" in text and sys.platform.lower(
                            ) == "win32":
                                text = "%s\n%s" % (
                                    text,
                                    "Please check if the server started with the --enabled-named-pipe parameter. The parameter can also be set in the config file."
                                )
                            self.report_output(text.strip())
                        elif text is None:
                            stdout_q = None
                except Empty:
                    pass

                line = input_file.readline()
                if not line:
                    break
                processed += len(line)
                try:
                    p1.stdin.write(line)
                except IOError, e:
                    log_error(
                        "Exception writing to stdin from cmdline client: %s\n"
                        % e)
                    if e.errno == 32:  # broken pipe
                        log_error("Broken pipe from child process\n")
                        break
                    elif e.errno == 22:  # invalid argument (happens in Windows, when child process just dies)
                        log_error("Broken pipe from child process\n")
                        break
                    raise e
                self.report_progress(None, processed, total_size)
    def run(self, progress_notify):
        cmd_args = {}
        cmd_args['ogr2ogr'] = get_exe_path("ogr2ogr")
        cmd_args['host'] = self.my_host
        cmd_args['schema'] = self.my_schema
        cmd_args['user'] = self.my_user
        cmd_args['pwd'] = self.my_pwd
        cmd_args['port'] = self.my_port

        cmd = []
        cmd.append("exec")
        cmd.append(get_exe_path("ogr2ogr"))
        cmd.append("-f")
        cmd.append('"MySQL"')
        cmd.append(
            'MySQL:"%(schema)s,host=%(host)s,user=%(user)s,password=%(pwd)s,port=%(port)d"'
            % cmd_args)
        cmd.append(self.filepath)

        if self.import_table:
            cmd.append('-nln')
            cmd.append('"%s"' % self.import_table)

        if self.destination_EPSG:
            cmd.append('-t_srs')
            cmd.append('EPSG:' % self.destination_EPSG)

        if self.skipfailures:
            cmd.append('-skipfailures')

        if self.import_append:
            cmd.append('-append')

        if self.import_overwrite:
            cmd.append('-overwrite')

        if self.selected_fields:
            cmd.append('-select')
            cmd.append(self.selected_fields)

        cmd.append('-progress')
        cmd.append('-gt')
        cmd.append(str(self.features_per_transcation))
        cmd.append('-lco')
        cmd.append('GEOMETRY_NAME="%s"' % self.my_geo_column)
        cmd.append('-lco')
        cmd.append('ENGINE=InnoDb')
        cmd.append('-lco')
        if self.spatial_index:
            cmd.append('SPATIAL_INDEX=YES')
        else:
            cmd.append('SPATIAL_INDEX=NO')

        self.is_running = True
        try:
            self.execute_cmd(" ".join(cmd), progress_notify)
        except grt.UserInterrupt:
            log_info("User cancelled")
            raise
        except Exception, exc:
            import traceback
            log_error(
                "An error occured during execution of ogr2ogr file import: %s, stack: %s\n The command was: %s\n"
                % (exc, traceback.format_exc(), cmd))
            raise
Example #36
0
            return "ERROR "+str(exc)
        except:
            return "ERROR"

        try:
            test_ssh_connection.acquire_admin_access()
        except Exception, exc:
            log_error("Exception: %s" % exc.message)
            import traceback
            log_debug2("Backtrace was: " % traceback.format_stack())
            return "ERROR "+str(exc)

        os_info = test_ssh_connection.detect_operating_system_version()
        if os_info:
            os_type, os_name, os_variant, os_version = os_info
            log_info("Instance test: detected remote OS: %s (%s), %s, %s\n" % (os_info))

            # check if the admin access error was because of wrong OS set
            if os_type != profile.target_os:
                return "ERROR Wrong Remote OS configured for connection. Set to %s, but was detected as %s" % (profile.target_os, os_type)
        else:
            log_warning("Instance test: could not determine OS version information\n")

            return "ERROR Could not determine remote OS details"

        return "OK"

    elif what == "disconnect":
        if test_ssh_connection:
            test_ssh_connection = None
        return "OK"
Example #37
0
            return "ERROR " + str(exc)
        except:
            return "ERROR"

        try:
            test_ssh_connection.acquire_admin_access()
        except Exception, exc:
            log_error("Exception: %s" % exc.message)
            import traceback
            log_debug2("Backtrace was: " % traceback.format_stack())
            return "ERROR " + str(exc)

        os_info = test_ssh_connection.detect_operating_system_version()
        if os_info:
            os_type, os_name, os_variant, os_version = os_info
            log_info("Instance test: detected remote OS: %s (%s), %s, %s\n" %
                     (os_info))

            # check if the admin access error was because of wrong OS set
            if os_type != profile.target_os:
                return "ERROR Wrong Remote OS configured for connection. Set to %s, but was detected as %s" % (
                    profile.target_os, os_type)
        else:
            log_warning(
                "Instance test: could not determine OS version information\n")

            return "ERROR Could not determine remote OS details"

        return "OK"

    elif what == "disconnect":
        if test_ssh_connection:
    def import_script(self, path, default_schema=None, default_charset="utf8"):
        if not self._tool_path:
            raise RuntimeError("mysql command line client not found. Please fix its path in Preferences -> Administration")

        is_windows = platform.system() == 'Windows'
        
        if is_windows:
            params = ['"%s"' % self._tool_path]
            pwdfile = tempfile.NamedTemporaryFile(delete=False, suffix=".cnf")
            pwdfilename = pwdfile.name
            tmpdir = None
        else:
            params = [self._tool_path]
            # use a pipe to feed the password to the client
            tmpdir = tempfile.mkdtemp()
            pwdfilename = os.path.join(tmpdir, 'extraparams.cnf')
            os.mkfifo(pwdfilename)
        params.append('--defaults-extra-file=' + pwdfilename)

        if default_charset:
            params.append("--default-character-set=%s" % default_charset)
        params += self._connection_params
        params += self._extra_params

        if default_schema:
            params.append(default_schema)

        cmdstr = " ".join(params)

        workdir = os.path.dirname(path)

        log_info("Feeding data from %s to %s (cwd=%s)\n" % (path, cmdstr, workdir))
        p1 = None
        try:
            self.report_progress("Preparing...", None, None)
            if not is_windows:
                try:
                    p1 = subprocess.Popen(params, cwd=workdir, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                except OSError, exc:
                    log_error("Error executing command %s\n%s\n" % (" ".join(params), exc))
                    raise RuntimeError("Error executing %s:\n%s" % (" ".join(params), str(exc)))

            # in !Windows feed password to client after it's started (otherwise the fifo would block on open for writing)
            pwdfile = open(pwdfilename, 'w')
            pwdfile.write('[client]\npassword='******''
            pwdfile.write(self._password.replace("\\", "\\\\"))
            pwdfile.write('\n')
            pwdfile.close()

            if is_windows:
                try:
                    info = subprocess.STARTUPINFO()
                    info.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
                    info.wShowWindow = _subprocess.SW_HIDE
                    # Command line can contain object names in case of export and filename in case of import
                    # Object names must be in utf-8 but filename must be encoded in the filesystem encoding,
                    # which probably isn't utf-8 in windows.
                    fse = sys.getfilesystemencoding()
                    cmd = cmdstr.encode(fse) if isinstance(cmdstr, unicode) else cmdstr
                    log_debug("Executing command: %s\n" % cmdstr)
                    p1 = subprocess.Popen(cmd, cwd=workdir, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT,startupinfo=info, shell=cmdstr[0] != '"')
                except OSError, exc:
                    log_error("Error executing command %s\n%s\n" % (cmdstr, exc))
                    import traceback
                    traceback.print_exc()
                    raise RuntimeError("Error executing %s:\n%s" % (cmdstr, str(exc)))
                self.report_progress(None, processed, total_size)
                
            input_file.close()

            # close the writer end of the client's pipe, so it can exit
            p1.stdin.close()

            self.report_progress("Finished executing script", processed, total_size)
            
            # flush queue from reader
            if stdout_q:
                while True:
                    text = stdout_q.get()
                    if text is None:
                        break
                    log_info("Task stdout: %s\n" % text)
                    if 'Access denied for user' in text:
                        raise grt.DBLoginError(text)
                    elif "Can't open named pipe to host" in text and sys.platform.lower() == "win32":
                        text = "%s\n%s" % (text, "Please check if the server started with the --enabled-named-pipe parameter. The parameter can also be set in the config file.")
                    self.report_output(text.strip())

            # let reader thread die
            thr.join()

            p1.wait()

            exitcode = p1.returncode
            
            log_info("mysql tool exited with code %s\n" % exitcode)
Example #40
0
def testInstanceSettingByName(what, connection, server_instance):
    global test_ssh_connection
    log_debug("Test %s in %s\n" % (what, connection.name))

    profile = ServerProfile(connection, server_instance)
    if what == "connect_to_host":
        if test_ssh_connection:
            test_ssh_connection = None

        log_info("Instance test: Connecting to %s\n" % profile.ssh_hostname)

        try:
            test_ssh_connection = wb_admin_control.WbAdminControl(
                profile, None, connect_sql=False, test_only=True)
            test_ssh_connection.init()

            grt.send_info("connected.")
        except Exception as exc:
            log_error("Exception: %s\n" % str(exc))
            import traceback
            log_debug2("Backtrace was: ", traceback.format_stack())
            return "ERROR " + str(exc)
        except:
            return "ERROR"

        try:
            test_ssh_connection.acquire_admin_access()
        except Exception as exc:

            log_error("Exception: %s\n" % str(exc))
            import traceback
            log_debug2("Backtrace was: " % traceback.format_stack())
            return "ERROR " + str(exc)

        os_info = test_ssh_connection.detect_operating_system_version()
        if os_info:
            os_type, os_name, os_variant, os_version = os_info
            log_info("Instance test: detected remote OS: %s (%s), %s, %s\n" %
                     (os_info))

            # check if the admin access error was because of wrong OS set
            if os_type != profile.target_os:
                return "ERROR Wrong Remote OS configured for connection. Set to %s, but was detected as %s" % (
                    profile.target_os, os_type)
        else:
            log_warning(
                "Instance test: could not determine OS version information\n")

            return "ERROR Could not determine remote OS details"

        return "OK"

    elif what == "disconnect":
        if test_ssh_connection:
            test_ssh_connection = None
        return "OK"

    elif what == "check_privileges":
        return "ERROR"

    elif what in ("find_config_file", "check_config_path",
                  "check_config_section"):
        config_file = profile.config_file_path
        print("Check if %s exists in remote host" % config_file)
        try:
            if not test_ssh_connection.ssh.fileExists(config_file):
                return "ERROR File %s doesn't exist" % config_file
            else:
                print("File was found in expected location")
        except IOError:
            return 'ERROR Could not verify the existence of the file %s' % config_file

        if what == "check_config_path":
            return "OK"

        section = profile.config_file_section
        cfg_file_content = ""
        print("Check if %s section exists in %s" % (section, config_file))
        try:
            #local_file = test_ssh_connection.fetch_file(config_file)
            cfg_file_content = test_ssh_connection.server_helper.get_file_content(
                path=config_file)
        except Exception as exc:
            import traceback
            traceback.print_exc()
            return "ERROR " + str(exc)

        if ("[" + section + "]") in cfg_file_content:
            return "OK"
        return "ERROR Couldn't find section %s in the remote config file %s" % (
            section, config_file)

    elif what in ("find_config_file/local", "check_config_path/local",
                  "check_config_section/local"):
        config_file = profile.config_file_path
        config_file = wb_admin_control.WbAdminControl(
            profile, None,
            connect_sql=False).expand_path_variables(config_file)
        print("Check if %s can be accessed" % config_file)
        if os.path.exists(config_file):
            print("File was found at the expected location")
        else:
            return "ERROR File %s doesn't exist" % config_file

        if what == "check_config_path/local":
            return "OK"

        section = profile.config_file_section
        print("Check if section for instance %s exists in %s" %
              (section, config_file))
        if check_if_config_file_has_section(open(config_file, "r"), section):
            print("[%s] section found in configuration file" % section)
            return "OK"
        return "ERROR Couldn't find section [%s] in the config file %s" % (
            section, config_file)

    elif what == "find_error_files":
        return "ERROR"

    elif what == "check_admin_commands":
        path = profile.start_server_cmd
        cmd_start = None
        if path.startswith("/"):
            cmd_start = path.split()[0]
            if not test_ssh_connection.ssh.fileExists(cmd_start):
                return "ERROR %s is invalid" % path

        path = profile.stop_server_cmd
        if path.startswith("/"):
            cmd = path.split()[0]
            if cmd != cmd_start and not test_ssh_connection.ssh.fileExists(
                    cmd):
                return "ERROR %s is invalid" % path

        return "OK"

    elif what == "check_admin_commands/local":
        path = profile.start_server_cmd
        cmd_start = None
        if path.startswith("/"):
            cmd_start = path.split()[0]
            if not os.path.exists(cmd_start):
                return "ERROR %s is invalid" % path

        path = profile.stop_server_cmd
        if path.startswith("/"):
            cmd = path.split()[0]
            if cmd != cmd_start and not os.path.exists(cmd):
                return "ERROR %s is invalid" % path

        return "OK"

    return "ERROR bad command"
Example #41
0
 def report_output(self, message):
     log_info("%s\n" % message)
     self._worker_queue.put(message)
Example #42
0
            # do the import
            total_size = os.stat(path).st_size
            processed = 0

            self.report_progress("Importing %s..." % os.path.basename(path), 0,
                                 total_size)
            stdout_q, thr = start_reading_from(p1.stdout)

            input_file = open(path, "r")
            while p1 and p1.poll() == None:
                try:
                    if stdout_q:
                        text = stdout_q.get_nowait()
                        if text:
                            log_info("Task stdout: %s\n" % text)
                            if 'Access denied for user' in text:
                                raise grt.DBLoginError(text)
                            elif "Can't open named pipe to host" in text and sys.platform.lower(
                            ) == "win32":
                                text = "%s\n%s" % (
                                    text,
                                    "Please check if the server started with the --enabled-named-pipe parameter. The parameter can also be set in the config file."
                                )
                            self.report_output(text.strip())
                        elif text is None:
                            stdout_q = None
                except Empty:
                    pass

                line = input_file.readline()
Example #43
0
    def import_script(self, path, default_schema=None, default_charset="utf8"):
        if not self._tool_path:
            raise RuntimeError(
                "mysql command line client not found. Please fix its path in Preferences -> Administration"
            )

        is_windows = platform.system() == 'Windows'

        if is_windows:
            params = ['"%s"' % self._tool_path]
            pwdfile = tempfile.NamedTemporaryFile(delete=False, suffix=".cnf")
            pwdfilename = pwdfile.name
            tmpdir = None
        else:
            params = [self._tool_path]
            # use a pipe to feed the password to the client
            tmpdir = tempfile.mkdtemp()
            pwdfilename = os.path.join(tmpdir, 'extraparams.cnf')
            os.mkfifo(pwdfilename)
        params.append('--defaults-extra-file=' + pwdfilename)

        if default_charset:
            params.append("--default-character-set=%s" % default_charset)
        params += self._connection_params
        params += self._extra_params

        if default_schema:
            params.append(default_schema)

        cmdstr = " ".join(params)

        workdir = os.path.dirname(path)

        log_info("Feeding data from %s to %s (cwd=%s)\n" %
                 (path, cmdstr, workdir))
        p1 = None
        try:
            self.report_progress("Preparing...", None, None)
            if not is_windows:
                try:
                    p1 = subprocess.Popen(params,
                                          cwd=workdir,
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT)
                except OSError, exc:
                    log_error("Error executing command %s\n%s\n" %
                              (" ".join(params), exc))
                    raise RuntimeError("Error executing %s:\n%s" %
                                       (" ".join(params), str(exc)))

            # in !Windows feed password to client after it's started (otherwise the fifo would block on open for writing)
            pwdfile = open(pwdfilename, 'w')
            pwdfile.write('[client]\npassword='******''
            pwdfile.write(self._password.replace("\\", "\\\\"))
            pwdfile.write('\n')
            pwdfile.close()

            if is_windows:
                try:
                    info = subprocess.STARTUPINFO()
                    info.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
                    info.wShowWindow = _subprocess.SW_HIDE
                    # Command line can contain object names in case of export and filename in case of import
                    # Object names must be in utf-8 but filename must be encoded in the filesystem encoding,
                    # which probably isn't utf-8 in windows.
                    fse = sys.getfilesystemencoding()
                    cmd = cmdstr.encode(fse) if isinstance(cmdstr,
                                                           unicode) else cmdstr
                    log_debug("Executing command: %s\n" % cmdstr)
                    p1 = subprocess.Popen(cmd,
                                          cwd=workdir,
                                          stdout=subprocess.PIPE,
                                          stdin=subprocess.PIPE,
                                          stderr=subprocess.STDOUT,
                                          startupinfo=info,
                                          shell=cmdstr[0] != '"')
                except OSError, exc:
                    log_error("Error executing command %s\n%s\n" %
                              (cmdstr, exc))
                    import traceback
                    traceback.print_exc()
                    raise RuntimeError("Error executing %s:\n%s" %
                                       (cmdstr, str(exc)))
Example #44
0
    def query_server_installation_info(self):
        normpath = self.server_profile.path_module.normpath

        def get_config_options():
            if self.server_profile.config_file_path and self.server_helper:
                try:
                    cfg_file = io.StringIO(
                        self.server_helper.get_file_content(
                            self.server_profile.config_file_path))
                except PermissionDeniedError:
                    log_debug(
                        'Could not open the file "%s" as the current user. Trying as admin\n'
                        % self.server_profile.config_file_path)
                    while True:
                        try:
                            password = self.password_handler.get_password_for(
                                'file')
                            cfg_file = io.StringIO(
                                self.server_helper.get_file_content(
                                    self.server_profile.config_file_path,
                                    as_user=Users.ADMIN,
                                    user_password=password))
                            break
                        except InvalidPasswordError:
                            self.password_handler.reset_password_for('file')
                        except Exception as err:
                            log_error('Could not open the file "%s": %s\n' %
                                      (self.server_profile.config_file_path,
                                       str(err)))
                            return {}
                except Exception as err:
                    log_error('Could not open the file "%s": %s\n' %
                              (self.server_profile.config_file_path, str(err)))
                    return {}

                opts = {}
                section = 'root'
                for line in cfg_file:
                    line = line.strip()
                    if line == '' or line.startswith('#'):
                        continue
                    elif line.startswith('[') and line.endswith(']'):
                        section = line[1:-1].strip()
                    else:
                        k, d, v = line.partition('=')
                        val = v.strip() or 'ON'
                        opts.setdefault(section, {})[k.strip()] = val
                return opts
            return {}

        opts = get_config_options()
        config_section = self.server_profile.config_file_section or 'mysqld'

        request_save_profile = False

        if self.server_variables:
            hostname = self.server_variables.get('hostname', '')
            if not hostname and self.server_profile.is_local:
                hostname = socket.gethostname()

            datadir = self.server_variables.get('datadir', '')
            if datadir and self.server_profile.datadir != datadir:
                self.server_profile.datadir = datadir
                request_save_profile = True

            basedir = self.server_variables.get('basedir', '')
            if basedir and self.server_profile.basedir != basedir:
                self.server_profile.basedir = basedir
                request_save_profile = True

            try:
                general_log_enabled = self.server_variables.get(
                    'general_log') in ('ON', '1')
                if self.server_profile.general_log_enabled != general_log_enabled:
                    self.server_profile.general_log_enabled = general_log_enabled
                    request_save_profile = True
            except ValueError:
                pass

            try:
                if self.target_version and self.target_version.is_supported_mysql_version_at_least(
                        5, 1, 29):
                    slow_query_var = 'slow_query_log'
                else:
                    slow_query_var = 'log_slow_queries'

                slow_log_enabled = self.server_variables.get(
                    slow_query_var) in ('ON', '1')
                if self.server_profile.slow_log_enabled != slow_log_enabled:
                    self.server_profile.slow_log_enabled = slow_log_enabled
                    request_save_profile = True
            except ValueError:
                pass

        if not self.target_version or not self.target_version.is_supported_mysql_version_at_least(
                5, 1, 29):
            general_log_file_path = opts[config_section].get(
                'log', '').strip(' "') if config_section in opts else ''
            general_log_file_path = normpath(
                general_log_file_path) if general_log_file_path else ''
            if general_log_file_path and self.server_profile.general_log_file_path != general_log_file_path:
                self.server_profile.general_log_file_path = general_log_file_path or os.path.join(
                    self.server_profile.datadir, hostname + '.log')
                request_save_profile = True

            slow_query_log_file = opts[config_section].get(
                'log-slow-queries',
                '').strip(' "') if config_section in opts else ''
            slow_query_log_file = normpath(
                slow_query_log_file) if slow_query_log_file else ''
            if slow_query_log_file and self.server_profile.slow_log_file_path != slow_query_log_file:
                self.server_profile.slow_log_file_path = slow_query_log_file or os.path.join(
                    self.server_profile.datadir, hostname + '.slow')
                request_save_profile = True

            error_log_file_path = opts[config_section].get(
                'log-error', '').strip(' "') if config_section in opts else ''
            error_log_file_path = normpath(
                error_log_file_path) if error_log_file_path else ''
            if error_log_file_path and self.server_profile.error_log_file_path != error_log_file_path:
                self.server_profile.error_log_file_path = error_log_file_path or os.path.join(
                    self.server_profile.datadir, hostname + '.err')
                request_save_profile = True

        else:
            if self.server_variables:
                path = self.server_variables.get('general_log_file')
                general_log_file_path = normpath(
                    path) if path and path != '0' else ''
                if self.server_profile.general_log_file_path != general_log_file_path:
                    self.server_profile.general_log_file_path = general_log_file_path
                    request_save_profile = True

                path = self.server_variables.get('slow_query_log_file')
                slow_query_log_file_path = normpath(
                    path) if path and path != '0' else ''
                if self.server_profile.slow_log_file_path != slow_query_log_file_path:
                    self.server_profile.slow_log_file_path = slow_query_log_file_path
                    request_save_profile = True

                path = self.server_variables.get('log_error')
                log_error_path = normpath(path) if path and path != '0' else ''
                if self.server_profile.error_log_file_path != log_error_path:
                    self.server_profile.error_log_file_path = log_error_path
                    request_save_profile = True

        log_info("Currently connected to MySQL server version " +
                 repr(self.raw_version) + ", conn status = " +
                 repr(self.is_sql_connected()) + ", active plugins = " +
                 str([x[0] for x in self.server_active_plugins]) + '\n')

        # Save the server profile if at least one of its values has changed:
        if request_save_profile:
            grt.modules.Workbench.saveInstances()
Example #45
0
 def raw_log(self, data):
     log_info(data + "\n")
    def run(self, progress_notify):
        cmd_args = {}
        cmd_args['ogr2ogr'] = get_exe_path("ogr2ogr")
        cmd_args['host'] = self.my_host
        cmd_args['schema'] = self.my_schema
        cmd_args['user'] = self.my_user
        cmd_args['pwd'] = self.my_pwd
        cmd_args['port'] = self.my_port
        
        cmd = []
        cmd.append("exec")
        cmd.append(get_exe_path("ogr2ogr"))
        cmd.append("-f")
        cmd.append('"MySQL"')
        cmd.append('MySQL:"%(schema)s,host=%(host)s,user=%(user)s,password=%(pwd)s,port=%(port)d"' % cmd_args)
        cmd.append(self.filepath)

        if self.import_table:
            cmd.append('-nln')
            cmd.append('"%s"' % self.import_table)
            
        if self.destination_EPSG:
            cmd.append('-t_srs')
            cmd.append('EPSG:' % self.destination_EPSG)

        if self.skipfailures:
            cmd.append('-skipfailures')

        if self.import_append:
            cmd.append('-append')

        if self.import_overwrite:
            cmd.append('-overwrite')

        if self.selected_fields:
            cmd.append('-select')
            cmd.append(self.selected_fields)
        
        cmd.append('-progress')
        cmd.append('-gt')
        cmd.append(str(self.features_per_transcation))
        cmd.append('-lco')
        cmd.append('GEOMETRY_NAME="%s"' % self.my_geo_column)
        cmd.append('-lco')
        cmd.append('ENGINE=InnoDb')
        cmd.append('-lco')
        if self.spatial_index:
            cmd.append('SPATIAL_INDEX=YES')
        else:
            cmd.append('SPATIAL_INDEX=NO')

        self.is_running = True
        try:
            self.execute_cmd(" ".join(cmd), progress_notify)
        except grt.UserInterrupt:
            log_info("User cancelled")
            raise
        except Exception, exc:
            import traceback
            log_error("An error occured during execution of ogr2ogr file import: %s, stack: %s\n The command was: %s\n" % (exc, traceback.format_exc(), cmd))
            raise