def temp_dir(apply_chown=None, apply_chmod=None, remove_using_sudo=None, remove_force=False): """ Creates a temporary directory on the remote machine. The directory is removed when no longer needed. Failure to do so will be ignored. :param apply_chown: Optional; change the owner of the directory. :type apply_chown: unicode :param apply_chmod: Optional; change the permissions of the directory. :type apply_chmod: unicode :param remove_using_sudo: Use sudo for removing the directory. ``None`` (default) means it is used depending on whether ``apply_chown`` has been set. :type remove_using_sudo: bool | NoneType :param remove_force: Force the removal. :type remove_force: bool :return: Path to the temporary directory. :rtype: unicode """ path = get_remote_temp() try: if apply_chmod: run(chmod(apply_chmod, path)) if apply_chown: if remove_using_sudo is None: remove_using_sudo = True sudo(chown(apply_chown, path)) yield path finally: remove_ignore(path, use_sudo=remove_using_sudo, force=remove_force)
def _make_worpress(nocache): APT_DEPS = [ 'curl', 'zip', 'apache2', 'libapache2-mod-php5', 'php5-mysql', 'php5-gd', 'php5-curl', 'mysql-client', 'gettext', ] WORDPRESS_PLUGINS_DL_URL = 'https://downloads.wordpress.org/plugin/{slug}.{version}.zip' wpurl = WORDPRESS_DL_URL.format(WORDPRESS_VERSION) wpprefix = '/var/www/wordpress' with DockerFile('debian:jessie') as df: df.volumes = ['{}/wp-content/uploads'.format(wpprefix)] df.expose = '80' df.command = '/run.sh' # Download and install df.run(' && '.join([ 'apt-get update', 'DEBIAN_FRONTEND=noninteractive apt-get -yq install %s' % ' '.join(APT_DEPS), 'rm -rf /var/lib/apt/lists/*', ])) # We don't do checksums because they're redundant. We already download through HTTPS. # If we trust the source, we can trust that the content is not malicious. assert wpurl.startswith('https') df.run(' && '.join([ 'curl -o wordpress.zip -sSL {}'.format(wpurl), 'unzip wordpress.zip -d /var/www', 'rm wordpress.zip', ])) # remove useless stuff from WP df.run('rm {}/wp-content/plugins/hello.php'.format(wpprefix)) # install plugins. we don't use wp-cli because installing a plugin with it requires the DB for (pluginname, version) in WORDPRESS_PLUGINS: url = WORDPRESS_PLUGINS_DL_URL.format(slug=pluginname, version=version) df.run(' && '.join([ 'curl -o {slug}.zip -sSL {url}'.format(slug=pluginname, url=url), 'unzip {slug}.zip -d {wpprefix}/wp-content/plugins'.format(slug=pluginname, wpprefix=wpprefix), 'rm {slug}.zip'.format(slug=pluginname), ])) # wpcli setup df.run('cd / && curl -s -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar') # setup DB df.run(' && '.join([ 'cd {}'.format(wpprefix), 'php /wp-cli.phar --allow-root core config --dbhost=mariadb --dbname={} --dbuser={} --dbpass={} --skip-check'.format(DBNAME, DBUSER, DBPASS), ])) # add theme df.add_file('theme', '{}/wp-content/themes/local_theme'.format(wpprefix)) # fix permissions df.run('chown -R www-data:www-data {}'.format(wpprefix)) # WP/apache config df.run('a2enmod rewrite') df.add_file('conf/wordpress_apache.conf', '/etc/apache2/sites-enabled/000-default.conf') df.add_file('conf/wordpress_run.sh', '/run.sh') df.run(chmod('+x', '/run.sh')) _get_client().build_from_file(df, WORDPRESS_IMGNAME, rm=True, nocache=nocache)
def copy_resources(src_container, src_resources, storage_dir, dst_directories=None, apply_chown=None, apply_chmod=None): """ Copies files and directories from a Docker container. Multiple resources can be copied and additional options are available than in :func:`copy_resource`. Unlike in :func:`copy_resource`, Resources are copied as they are and not compressed to a tarball, and they are left on the remote machine. :param src_container: Container name or id. :type src_container: unicode :param src_resources: Resources, as (file or directory) names to copy. :type src_resources: iterable :param storage_dir: Remote directory to store the copied objects in. :type storage_dir: unicode :param dst_directories: Optional dictionary of destination directories, in the format ``resource: destination``. If not set, resources will be in the same relative structure to one another as inside the container. For setting a common default, use ``*`` as the resource key. :type dst_directories: dict :param apply_chown: Owner to set for the copied resources. Can be a user name or id, group name or id, both in the notation ``user:group``, or as a tuple ``(user, group)``. :type apply_chown: unicode or tuple :param apply_chmod: File system permissions to set for the copied resources. Can be any notation as accepted by `chmod`. :type apply_chmod: unicode """ def _copy_resource(resource): default_dest_path = generic_path if generic_path is not None else resource dest_path = directories.get(resource, default_dest_path).strip(posixpath.sep) head, tail = posixpath.split(dest_path) rel_path = posixpath.join(storage_dir, head) run(mkdir(rel_path, check_if_exists=True)) run('docker cp {0}:{1} {2}'.format(src_container, resource, rel_path), shell=False) directories = dst_directories or {} generic_path = directories.get('*') for res in src_resources: _copy_resource(res) if apply_chmod: run(chmod(apply_chmod, storage_dir)) if apply_chown: sudo(chown(apply_chown, storage_dir))
def temp_dir(apply_chown=None, apply_chmod=None): """ Creates a temporary directory on the remote machine. The directory is removed when no longer needed. Failure to do so will be ignored. :param apply_chown: Optional; change the owner of the directory. :type apply_chown: bool :param apply_chmod: Optional; change the permissions of the directory. :type apply_chmod: bool :return: Path to the temporary directory. :rtype: unicode """ path = get_remote_temp() if apply_chmod: run(chmod(apply_chmod, path)) if apply_chown: sudo(chown(apply_chown, path)) yield path remove_ignore(path, True)