Commit 42d68708 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Reduce wordpress statefulness to just user uploads

All plugins and themes are installed at startup, managed through
configMaps.

Anything related to creating stateful containers is gone.

The custom theme has been added to the image build; it could equally be
a separate project with the theme being installed from a URL.
parent 96fc8d25
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
# syntax = docker/dockerfile:1.0-experimental

FROM php:7.3-fpm as deps

RUN apt-get update \
@@ -22,12 +24,25 @@ RUN apt-get update \
 &&:


FROM debian:buster as theme-build

RUN --mount=type=bind,source=sela-child,target=/src : \
 && apt-get update \
 && apt-get install -y \
    zip \
 && mkdir -p /pkg \
 && cd /src \
 && zip -r /pkg/sela-child.zip . \
 &&:


FROM deps

LABEL uk.org.kodo.maintainer "Dom Sekotill <dom.sekotill@kodo.org.uk>"

COPY --from=compile /usr/local/etc/php /usr/local/etc/php
COPY --from=compile /usr/local/lib/php /usr/local/lib/php
COPY --from=theme-build /pkg /pkg
COPY wp.sh /usr/local/bin/wp
ADD https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar /usr/local/bin/wp.php

+23 −69
Original line number Diff line number Diff line
#!/bin/bash
set -eu
shopt -s nullglob globstar

# constants
WP_ROOT=${WORDPRESS_ROOT:-/var/www/html}
WP_CONTENT=${WP_ROOT}/wp-content
WP_CONFIG=${WP_ROOT}/wp-config.php
MYSQL_CONF=/etc/wordpress/mysql.conf
SECRET_CONF=/etc/wordpress/secret.conf

genkey() { head -c${1:-1M} /dev/urandom | sha1sum | cut -d' ' -f1; }

create_config()
{
	local key=$(genkey)
	source ${MYSQL_CONF}
	cat > $WP_CONFIG <<-END_CONFIG
		<?php
		/**
@@ -21,10 +19,10 @@ create_config()
		 * `date`
		 */

		define('DB_HOST', '${DB_HOST}');
		define('DB_NAME', '${DB_NAME}');
		define('DB_USER', '${DB_USER}');
		define('DB_PASSWORD', '${DB_PASSWORD}');
		define('DB_HOST', '${DB_HOST? Please set DB_HOST in /etc/wordpress/}');
		define('DB_NAME', '${DB_NAME? Please set DB_NAME in /etc/wordpress/}');
		define('DB_USER', '${DB_USER? Please set DB_USER in /etc/wordpress/}');
		define('DB_PASSWORD', '${DB_PASSWORD? Please set DB_PASSWORD in /etc/wordpress/}');

		define('DB_CHARSET', 'utf8');
		define('DB_COLLATE', '');
@@ -40,7 +38,7 @@ create_config()
		define('LOGGED_IN_SALT',   '`genkey 128`');
		define('NONCE_SALT',       '`genkey 128`');

		define('FS_METHOD', 'direct');
		define('DISALLOW_FILE_MODS', true);

		define('WP_DEBUG', false);
		if ( !defined('ABSPATH') )
@@ -50,82 +48,38 @@ create_config()
	END_CONFIG
}

run_setup()
{
	# setup entrypoint
	# ----------------
	# Setup the database, install the scripts & make a config

	if [ -e ${MYSQL_CONF} ]; then
		source ${MYSQL_CONF}
	fi

	# parse command line arguments
	SHIFT=shift
	POP='"$2"; SHIFT=shift; shift'
	while [ $# -gt 0 ]; do
		case "$1" in
			--*=*)
				set ${1%%=*} ${1#*=} "${@:2}"
				continue
				;;
			-*)
				set ${1:0:2} ${1#??} "${@:2}"
				# SHIFT adds the character-option hyphen onto $2 if is not POPed
				# as an argument first.
				SHIFT='set -$2 "${@:3}"; SHIFT=shift'
				continue
				;;

			-c|--clear) unset DB_HOST DB_NAME DB_USER DB_PASSWORD ;;
			-h|--host) eval ARG_HOST=$POP ;;
			-d|--database) eval ARG_NAME=$POP ;;
			-u|--user) eval ARG_USER=$POP ;;
			-p|--password) eval ARG_PASSWORD=$POP ;;
		esac
		$SHIFT
	done

	# command line argument defaults
	: ${ARG_HOST:=${DB_HOST:-mysql}}
	: ${ARG_NAME:=${DB_NAME?A database name is required}}
	: ${ARG_USER:=${DB_USER:-${ARG_NAME}_user}}
	: ${ARG_PASSWORD:=${DB_PASSWORD:-}}

	cat >${MYSQL_CONF} <<-END
		DB_HOST=${ARG_HOST}
		DB_NAME=${ARG_NAME}
		DB_USER=${ARG_USER}
		DB_PASSWORD=${ARG_PASSWORD}
	END
}

update_all() {
setup() {
	wp core update --minor
	wp plugin update --all
	wp theme update --all
	wp plugin install "${PLUGINS[@]}"
	wp theme install "${THEMES[@]}"
	wp language core update
	wp language plugin update --all
	wp language theme update --all

	find -name wp-content -prune \
		-o -name static -prune \
	find -name static -prune \
		-o -type f -not -iname '*.php' \
		-exec install -vD '{}' 'static/{}' \;
}

declare -a THEMES=( sela /pkg/sela-child.zip )
declare -a PLUGINS

for file in /etc/wordpress/*.conf /etc/wordpress/**/*.conf; do
	source ${file}
done

if [[ -e ${PLUGINS_LIST:=/etc/wordpress/plugins.txt} ]]; then
	PLUGINS+=( $(<${PLUGINS_LIST}) )
fi

case "$1" in
	setup)
		shift
		run_setup "$@"
		;;
	php-fpm)
		create_config
		update_all
		setup
		exec "$@"
		;;
	*)
		[ -e ${MYSQL_CONF} ] && create_config || true
		[[ -v DB_HOST ]] && create_config || true
		exec "$@"
		;;
esac
+46 −0
Original line number Diff line number Diff line
<?php
/**
 * The template used for displaying page content.
 *
 * @package Sela
 */


$title = the_title(null, null, false);

if ( function_exists('get_nskw_subtitle') ) {
	$subtitle = get_nskw_subtitle();
} else {
	$subtitle = '';
}

if ( $subtitle == '' ) {
	$title_class = 'entry-title';
} else {
	$title_class = 'entry-title has-subtitle';
}

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

	<header class="entry-header">
		<h1 class="<?php print($title_class); ?>"><?php print($title); ?></h1>
		<?php if ( $subtitle != '' ): ?>
			<h5 class="entry-subtitle"><?php print($subtitle); ?></h5>
		<?php endif; ?>
	</header><!-- .entry-header -->

	<div class="entry-content">
		<?php the_content(); ?>
		<?php
			wp_link_pages( array(
				'before' => '<div class="page-links">' . __( 'Pages:', 'sela' ),
				'after'  => '</div>',
			) );
		?>

	</div><!-- .entry-content -->
	<?php edit_post_link( __( 'Edit', 'sela' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?>

</article><!-- #post-## -->
+23 −0
Original line number Diff line number Diff line
<?php

/* Style loading boilerplate */
add_action( 'wp_enqueue_scripts', 'sela_child_enqueue_styles' );

function sela_child_enqueue_styles()
{
	wp_enqueue_style( 'sela-style',
		get_template_directory_uri() . '/style.css'
	);

	wp_enqueue_style( 'sela-child-style',
		get_stylesheet_directory_uri() . '/style.css',
		array( 'sela-style' ),
		wp_get_theme()->get('Version')
	);

//	wp_enqueue_style( 'sela-child-bootstrap',
//		get_stylesheet_directory_uri() . '/bootstrap3/css/bootstrap.min.css',
//		array( 'sela-style' ),
//		wp_get_theme()->get('Version')
// 	);
}
+60 −0
Original line number Diff line number Diff line
<?php
/**
 * Template Name: Front Page
 *
 * @package Sela-Child
 */

get_header(); ?>

	<div id="primary" class="content-area front-page-content-area">
		<?php while ( have_posts() ) : the_post(); ?>
			<div class="hero">

				<?php if ( has_post_thumbnail() ) : ?>
				<figure class="hero-content">
					<?php the_post_thumbnail( 'sela-hero-thumbnail' ); ?>
					<div class="hero-content-overlayer">
						<div class="hero-container-outer">
							<div class="hero-container-inner">
								<?php get_template_part( 'content', 'page' ); ?>
							</div><!-- .hero-container-inner -->
						</div><!-- .hero-container-outer -->
					</div><!-- .hero-content-overlayer -->
				</figure><!-- .hero-content -->

				<?php else : ?>
					<?php get_template_part( 'content', 'page' ); ?>
				<?php endif; ?>

			</div><!-- .hero -->
		<?php endwhile; ?>
	</div><!-- #primary -->

	<?php get_sidebar( 'front-page' ); ?>

	<?php
		$testimonials = new WP_Query( array(
			'post_type'      => 'jetpack-testimonial',
			'order'          => 'ASC',
			'orderby'        => 'menu_order',
			'posts_per_page' => 2,
			'no_found_rows'  => true,
		) );
	?>

	<?php if ( $testimonials->have_posts() ) : ?>
	<div id="front-page-testimonials" class="front-testimonials testimonials">
		<div class="grid-row">
		<?php
			while ( $testimonials->have_posts() ) : $testimonials->the_post();
				 get_template_part( 'content', 'testimonial' );
			endwhile;
			wp_reset_postdata();
		?>
		</div>
	</div><!-- .testimonials -->
	<?php endif; ?>

<?php get_sidebar( 'footer' ); ?>
<?php get_footer(); ?>
Loading