Beispiel #1
0
class TestShellCommand(TestCase):
    def setUp(self):
        self.cmd = ShellCommand(database='wordpress',
                                user='******',
                                passwd='pass',
                                mysql_options='--protocol=tcp',
                                mysqldump_options='--single-transaction')

    @staticmethod
    def replace_spaces(string):
        return re.sub(r'\s+', ' ', string)

    def test_redirect(self):
        self.assertEqual(self.cmd.redirect_null, self.cmd.redirect())
        self.assertEqual('1 ' + self.cmd.redirect_null, self.cmd.redirect(1))

    def test_cmd_mysql(self):
        self.assertEqual(
            'mysql --host=localhost --port=3306 --user=user --password=pass '
            '--protocol=tcp --column-type-info wordpress 2 > /dev/null',
            self.replace_spaces(self.cmd.cmd_mysql('--column-type-info')))

    def test_cmd_mysqldump(self):
        self.assertEqual(
            'mysqldump --host=localhost --port=3306 --user=user --password=pass '
            '--single-transaction --skip-opt wordpress 2 > /dev/null',
            self.replace_spaces(self.cmd.cmd_mysqldump('--skip-opt')))

    def test_cmd_snapshot_database(self):
        self.assertEqual(
            'mysqldump --host=localhost --port=3306 --user=user --password=pass '
            '--single-transaction wordpress 2 > /dev/null | gzip > snapshot.sql.gz',
            self.replace_spaces(
                self.cmd.cmd_snapshot_database('snapshot.sql.gz', None)))

    def test_cmd_snapshot_directory(self):
        self.assertEqual(
            'tar czpf snapshot.tar.gz public_html',
            self.replace_spaces(
                self.cmd.cmd_snapshot_directory('snapshot.tar.gz',
                                                'public_html')))

    def test_cmd_replace_local_db(self):
        self.assertEqual(
            'gunzip -c snapshot.sql.gz | '
            'mysql --host=localhost --port=3306 --user=user --password=pass '
            '--protocol=tcp wordpress 2 > /dev/null',
            self.replace_spaces(
                self.cmd.cmd_replace_local_db(snapshot_file='snapshot.sql.gz',
                                              mysql_options='',
                                              pipe='')))

        self.assertEqual(
            'gunzip -c snapshot.sql.gz | sed | pipe-test | '
            'mysql --host=localhost --port=3306 --user=user --password=pass '
            '--protocol=tcp wordpress 2 > /dev/null',
            self.replace_spaces(
                self.cmd.cmd_replace_local_db(snapshot_file='snapshot.sql.gz',
                                              mysql_options='',
                                              pipe=['sed', 'pipe-test'])))

        self.assertEqual(
            'gunzip -c snapshot.sql.gz | sed | '
            'mysql --host=localhost --port=3306 --user=user --password=pass '
            '--protocol=tcp wordpress 2 > /dev/null',
            self.replace_spaces(
                self.cmd.cmd_replace_local_db(snapshot_file='snapshot.sql.gz',
                                              mysql_options='',
                                              pipe='sed')))
def copy2local(*args, **kwargs):
    """
    applypathway.org 를 위한 copy2local 스크립트
    """

    if args or kwargs:
        pass

    check_env()

    remote_shell = ShellCommand(database=env.remote_db_name,
                                user=env.remote_db_user,
                                passwd=env.remote_db_pass)
    local_shell = ShellCommand(database=env.local_db_name,
                               user=env.local_db_user,
                               passwd=env.local_db_pass)

    # dump remote database
    run(
        remote_shell.cmd_snapshot_database(env.remote_sql_snapshot,
                                           '--add-drop-table'))

    # copy dumped sql file to local
    download(env.remote_sql_snapshot, env.local_sql_snapshot)

    # MariaDB to MySQL Conversion pipe
    pipe = "sed -e 's/ENGINE=Aria/ENGINE=InnoDB/' -e 's/PAGE_CHECKSUM=1//' -e 's/TRANSACTIONAL=1//'"

    # replace local table
    local(local_shell.cmd_replace_local_db(env.local_sql_snapshot, '', pipe))

    # tar archive remote wordpress
    with cd(os.path.dirname(env.remote_wp_path)):
        run(
            remote_shell.cmd_snapshot_directory(
                env.remote_wp_snapshot, os.path.basename(env.remote_wp_path)))

    # copy remote archive to local
    download(env.remote_wp_snapshot, env.local_wp_snapshot)

    # replace the local wp
    replace_local_wp(env.local_wp_snapshot, env.local_wp_path)

    # cleanup remote
    run('rm -f {} {}'.format(env.remote_sql_snapshot, env.remote_wp_snapshot))

    # cleanup local
    local('rm -f {} {}'.format(env.local_sql_snapshot, env.local_wp_snapshot))

    # arrange permissions
    change_local_chmod(env.local_wp_path)

    # some developing plugins are symbolic linked.
    local_mu_plugins = [{
        'target':
        os.path.join(env.local_wp_path, 'wp-content', 'mu-plugins', 'ivy-mu'),
        'replace':
        '/home/changwoo/devel/wordpress/mu-plugins/ivy-mu',
    }, {
        'target':
        os.path.join(env.local_wp_path, 'wp-content', 'mu-plugins',
                     'ivy-mu-loader.php'),
        'replace':
        '/home/changwoo/devel/wordpress/mu-plugins/ivy-mu-loader.php',
    }]
    do_symbolic_links(local_mu_plugins)

    local_plugins = [{
        'target':
        os.path.join(env.local_wp_path, 'wp-content', 'plugins',
                     'applypathway'),
        'replace':
        '/home/changwoo/devel/wordpress/plugins/applypathway',
    }]
    do_symbolic_links(local_plugins)

    # remote URL strings will be replaced
    replacement_items = [
        {
            'table': 'wp_options',
            'field': 'option_value',
        },
        {
            'table': 'wp_posts',
            'field': 'post_content',
        },
        {
            'table': 'wp_posts',
            'field': 'guid',
        },
        {
            'table': 'wp_postmeta',
            'field': 'meta_value',
        },
        {
            'table': 'wp_usermeta',
            'field': 'meta_value',
        },
    ]
    update_url(local_shell, replacement_items, env.remote_url,
               'http://applypathway.local')

    # after replacement, the lengths of replaced strings will be changed.
    # Those change will impact on serialized values. Therefore, update serialized option values
    update_serialized_option_value(local_shell, 'wp_options', 'avada_options',
                                   env.remote_url + '.+?')
    update_serialized_option_value(local_shell, 'wp_options', 'fusion_options',
                                   'http://applypathway.local.+?')