HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/local/bin/dhwp/dhwp/controllers/install.py
import random
import string
import re
import os
import sys
import yaml
from cement import App, Controller, ex
from cement import shell
from dhwp.core.wordpress import Wordpress
from dhwp.core.curl import fetch


class Install(Controller):
    class Meta:
        label = 'wordpress'
        arguments = [
            (['-p', '--path'],
                {'help': 'path to wordpress install',
                 'action': 'store',
                 'dest': 'path', }),
            (['-c', '--config'],
                {'help': 'path to dhwp wordpress config',
                 'action': 'store',
                 'dest': 'config_file', }),
        ]

    def load_config(self):
        data = dict()
        if self.app.pargs.config_file:
            with open(self.app.pargs.config_file) as f:
                data = yaml.safe_load(f)

        for arg in vars(self.app.pargs):
            value = getattr(self.app.pargs, arg)
            if value:
                data[arg] = value

        self.app.log.debug("Configuration %s" % data)
        return data

    @ex(
        help='Download and extract wordpress',

        arguments=[
            (['--source'],
             {'help': 'wordpress source url, zip format only (optional)',
                'action': 'store',
                'dest': 'download_source', }),
            (['--package'],
             {'help': 'wordpress package (created by dhwp)',
                'action': 'store',
                'dest': 'wordpress_package', }),
        ]
    )
    def download(self):
        config = self.load_config()
        wp = Wordpress(self.app.pargs.path)

        if 'download_source' in config:
            wp.download_source(config['download_source'])
        elif 'wordpress_package' in config:
            wp.download_package(config['wordpress_package'])
        else:
            wp.cli('core download')

    @ex(
        help='create a new WP install',
        arguments=[
            (['--url'],
             {'help': 'site url',
                'action': 'store',
                'dest': 'url'}),
            (['--title'],
             {'help': 'site title',
                'action': 'store',
                'dest': 'title'}),
            (['--admin_user'],
             {'help': 'admin username',
                'action': 'store',
                'dest': 'admin_user'}),
            (['--admin_password'],
             {'help': 'admin password',
                'action': 'store',
                'dest': 'admin_password'}),
            (['--admin_email'],
             {'help': 'admin email',
                'action': 'store',
                'dest': 'admin_email'}),
        ]
    )
    def install(self):
        wp = Wordpress(self.app.pargs.path)
        config = self.load_config()
        self.app.log.info('installing wordpress')
        wp.cli('core install',
               url=self.app.pargs.url,
               title=self.app.pargs.title,
               admin_user=self.app.pargs.admin_user,
               admin_password=self.app.pargs.admin_password,
               admin_email=self.app.pargs.admin_email,
               skip__email=True
               )
        self.extras()
        if 'post_install' in config:
            for cli_command in config['post_install']['wpcli']:
                self.app.log.info("exec post install command %s" % cli_command)
                wp.cli(cli_command)

    @ex(
        help='configure a WP install',

        arguments=[
            (['--dbname'],
             {'help': 'database name',
                'action': 'store',
                'dest': 'dbname'}),
            (['--dbuser'],
             {'help': 'database username',
                'action': 'store',
                'dest': 'dbuser'}),
            (['--dbpass'],
             {'help': 'database password',
                'action': 'store',
                'dest': 'dbpass'}),
            (['--dbhost'],
             {'help': 'database host',
                'action': 'store',
                'dest': 'dbhost'}),
            (['--dbprefix'],
             {'help': 'database prefix',
                'action': 'store',
                'dest': 'dbprefix'}),
        ]
    )
    def configure(self):
        wp = Wordpress(self.app.pargs.path)

        self.app.log.info('configuring wordpress install')
        if not self.app.pargs.dbprefix:
            self.app.pargs.dbprefix = 'wp_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=6)) + '_'

        wp.cli('config create',
               dbname=self.app.pargs.dbname,
               dbuser=self.app.pargs.dbuser,
               dbpass=self.app.pargs.dbpass,
               dbhost=self.app.pargs.dbhost,
               dbprefix=self.app.pargs.dbprefix,
               skip__check=True,
               force=True,
               )

    @ex(
        help='Package WP install for deployment',
        arguments=[
            (['-n', '--package-name'],
             {'help': 'name of pacakge',
              'action': 'store',
              'dest': 'package_name'}),
        ]
    )
    def package(self):
        config = self.load_config()
        wp = Wordpress(self.app.pargs.path)
        cwd = os.getcwd()
        pkg_file = "%s/%s.tar.gz" % (cwd, self.app.pargs.package_name)
        sql_file = "%s/inital.sql" % wp.path
        wp.cli('db export ' + sql_file)
        cmd = "cd %s && tar --exclude .git -zcvf %s ." % (wp.path, pkg_file)
        out, err, code = shell.cmd(cmd)
        if code > 0:
            self.app.log.fatal("error creating pacakge %s : %s" % (out, err))
            sys.exit(1)

    @ex(
        help='Install extras (plugins, themes, etc)',
    )
    def extras(self):
        config = self.load_config()
        wp = Wordpress(self.app.pargs.path)

        if not wp.is_installed():
            self.app.log.fatal(
                "WordPress is not fully installed yet, cannot install extras")
            sys.exit(1)

        for item_type in ('plugin', 'theme'):
            if item_type not in config:
                continue

            for item in config[item_type]:
                options = config[item_type][item]
                install_name = item

                command = ""
                if 'location' in options:
                    install_name = options['location']

                if 'no-verify' in options:
                    wp.download_extra(install_name, "/wp-content/{item_type}s".format(item_type=item_type))
                    if 'activate' in options:
                        if options['activate'] is True:
                            command = "{item_type} activate {item_name}".format(item_type=item_type, item_name=item)
                else:
                    command = "%s install %s" % (item_type, install_name)

                    if 'version' in options:
                        command = "%s --version=%s" % (command, options['version'])

                    # TODO can i activate things right now????
                    if 'activate' in options:
                        if options['activate'] is True:
                            command = "%s --activate" % command

                self.app.log.info("installing %s %s" % (item_type, item))
                if command:
                    wp.cli(command)

    @ex(
        help='Update a WP install',
    )
    def update(self):
        print('Updating WordPress')

    @ex(
        arguments=[
            (['--foo'],
             dict(action='store_true')),
            (['--not-bar'],
             dict(action='store_true')),
        ]
    )
    def command1(self):
        data = ""
        if self.app.pargs.foo and self.app.pargs.not_bar:
            data = dict(foo="not-bar")
        else:
            data = dict(foo="bar")
        self.app.render(data, 'test.m')