Skip to content

Commit

Permalink
Add k8s Wordpress
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhg committed Mar 21, 2018
1 parent 850fb7f commit 153493a
Show file tree
Hide file tree
Showing 12 changed files with 635 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.build/

3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "k8s/vendor/marketplace-tools"]
path = k8s/vendor/marketplace-tools
url = https://github.com/GoogleCloudPlatform/marketplace-k8s-app-tools
1 change: 1 addition & 0 deletions k8s/vendor/marketplace-tools
Submodule marketplace-tools added at ada116
34 changes: 34 additions & 0 deletions k8s/wordpress/Makefile
@@ -0,0 +1,34 @@
APP_NAME = wordpress
AGENT_REPO ?= ../../../ubbagent

include ../vendor/marketplace-tools/gcloud.Makefile

include ../vendor/marketplace-tools/crd.Makefile
include ../vendor/marketplace-tools/app.Makefile

app/build:: .build/deployer .build/init .build/marketplace-ubbagent

.build/deployer: deployer/* manifest/* $(MARKETPLACE_BASE_BUILD)/deployer-kubectl $(MARKETPLACE_BASE_BUILD)/controller $(APP_BUILD)/registry_prefix | app/setup
docker build \
--build-arg MARKETPLACE_REGISTRY="$(MARKETPLACE_REGISTRY)" \
--tag "$(APP_DEPLOYER_IMAGE)" \
-f deployer/Dockerfile \
.
gcloud docker -- push "$(APP_DEPLOYER_IMAGE)"
@touch "$@"

.build/init: init/* $(APP_BUILD)/registry_prefix | app/setup
cd init \
&& docker build \
--tag "$(APP_REGISTRY)/init" \
.
gcloud docker -- push "$(APP_REGISTRY)/init"
@touch "$@"

.build/marketplace-ubbagent: $(AGENT_REPO)/* $(AGENT_REPO)/**/* $(MARKETPLACE_BASE_BUILD)/registry_prefix | base/setup
cd "$(AGENT_REPO)" \
&& docker build \
--tag "$(MARKETPLACE_REGISTRY)/ubbagent" \
.
gcloud docker -- push "$(MARKETPLACE_REGISTRY)/ubbagent"
@touch "$@"
62 changes: 62 additions & 0 deletions k8s/wordpress/README.md
@@ -0,0 +1,62 @@
*This directory contains an example Kubernetes application (app) based on
WordPress for the purpose of demonstrating app integration with
Google Cloud Marketplace. **Not intended for actual use!***

*Content below is intended as a template for end-user documentation. Work in
progress.*

# Overview

WordPress is a free and open-source content management system (CMS) based on PHP
and MySQL...

# Installation

## Quick install with Google Cloud Marketplace

Get up and running with a few clicks! Install this WordPress app to a
Google Kubernetes Engine cluster using Google Cloud Marketplace. Follow the
on-screen instructions:
*TODO: link to solution details page*

## Command line instructions

Follow these instructions to install WordPress from the command line.

### Prerequisites

- Setup cluster
- Permissions
- Setup kubectl
- Install Application Resource
- Acquire License

*TODO: add details above*

### Commands

Set environment variables (modify if necessary):
```
export APP_INSTANCE_NAME=wordpress-1
export NAMESPACE=default
```

Expand manifest template:
```
cat manifest/* | envsubst > expanded.yaml
```

Run kubectl:
```
kubectl apply -f expanded.yaml
```

*TODO: fix instructions*

# Backups

*TODO: instructions for backups*

# Upgrades

*TODO: instructions for upgrades*
4 changes: 4 additions & 0 deletions k8s/wordpress/deployer/Dockerfile
@@ -0,0 +1,4 @@
ARG MARKETPLACE_REGISTRY
FROM ${MARKETPLACE_REGISTRY}/deployer_kubectl_base

ADD manifest/* /data/manifest/
7 changes: 7 additions & 0 deletions k8s/wordpress/init/Dockerfile
@@ -0,0 +1,7 @@
FROM launcher.gcr.io/google/debian9

COPY metering.php.tmpl /
COPY agent-config.yaml /
COPY init.sh /

CMD ["/init.sh"]
25 changes: 25 additions & 0 deletions k8s/wordpress/init/agent-config.yaml
@@ -0,0 +1,25 @@
metrics:
- name: requests
type: int
aggregation:
bufferSeconds: 60
endpoints:
- name: disk
- name: instance_time
type: int
aggregation:
bufferSeconds: 60
endpoints:
- name: disk
endpoints:
- name: disk
disk:
reportDir: $AGENT_REPORT_DIR
expireSeconds: 3600
sources:
- name: instance_time
heartbeat:
metric: instance_time
intervalSeconds: 10
value:
int64Value: 10
39 changes: 39 additions & 0 deletions k8s/wordpress/init/init.sh
@@ -0,0 +1,39 @@
#!/bin/bash
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

if [ -z "$AGENT_LOCAL_PORT" ]; then
echo "AGENT_LOCAL_PORT environment variable must be set"
exit 1
fi

if [ ! -d "/var/www/html" ]; then
echo "/var/www/html directory must be mounted"
exit 1
fi

if [ ! -d "/etc/ubbagent" ]; then
echo "/etc/ubbagent directory must be mounted"
exit 1
fi

# Expand and copy Wordpress metering plugin.
mkdir -p /var/www/html/wp-content/mu-plugins
sed "s/%%AGENT_LOCAL_PORT%%/$AGENT_LOCAL_PORT/g" /metering.php.tmpl > /var/www/html/wp-content/mu-plugins/metering.php

# Copy the metering agent config.
cp /agent-config.yaml /etc/ubbagent/config.yaml
159 changes: 159 additions & 0 deletions k8s/wordpress/init/metering.php.tmpl
@@ -0,0 +1,159 @@
<?php
/*
* Copyright 2018 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @package marketplace_metering
* @version 1.0
*/
/*
Plugin Name: Usage Metering
Plugin URI: https://cloud.google.com/launcher
Description: Wordpress usage metering.
Author: Google LLC
Version: 1.0
Author URI: https://cloud.google.com/
*/

const AGENT_PORT = %%AGENT_LOCAL_PORT%%;

/**
* Returns the current time in ISO8601 format with microsecond resolution.
*/
function now() {
list($usec, $sec) = explode(" ", microtime());
return date("Y-m-d\TH:i:s", $sec) . substr($usec, 1, 8) . date("P", $sec);
}

/**
* Returns the metering agent url with an optional path component.
*/
function agent_url($path) {
$port = AGENT_PORT;
return esc_url_raw( "http://localhost:$port/$path" );
}

/**
* Returns the metering agent report url.
*/
function report_url() {
return agent_url('report');
}

/**
* Returns the metering agent status url.
*/
function status_url() {
return agent_url('status');
}

/*
* Reports a single request to the metering agent.
*/
function report_request() {
$now = now();
$body = array(
'name' => 'requests',
'startTime' => $now,
'endTime' => $now,
'value' => array( 'int64Value' => 1 )
);
$args = array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => json_encode( $body )
);

$response = wp_remote_post( report_url(), $args);
$response_code = wp_remote_retrieve_response_code( $response );

return $response_code == 200;
}

/**
* A 'wp' action that reports requests for singular post/page views.
*/
function handle_view() {
if (is_singular()) {
report_request();
}
}

/**
* Renders the Metering Status widget.
*/
function usage_metering_status_widget_display() {
$response = wp_remote_get(status_url());
$error = '';
$status = NULL;
if (is_wp_error($request)) {
$error = 'Cannot reach agent';
} else {
$body = wp_remote_retrieve_body($response);
$status = json_decode($body, true);
if ($status["currentFailureCount"] > 0) {
$error = 'Agent is failing to report usage';
}
$lastSuccess = DateTime::createFromFormat('Y-m-d\TH:i:s+', $status['lastReportSuccess']);
$now = new DateTime();
$successInterval = $now->diff($lastSuccess)->format('%ad %hh %im %ss');
}

if ($error == '') {
$current_status = '<font color="green">SUCCESS</font>';
} else {
$current_status = "<font color=\"red\">FAILURE: $error</font>";
}

// Widget HTML
?>
<h3>Reporting Health</h3>
<p><?= $current_status ?></p>

<? if ($status != null) : ?>
<h3>Status Data</h3>
<table>
<tr>
<td><b>Last report success</b></td>
<td><?= $successInterval ?> ago</td>
</tr>
<tr>
<td><b>Current failures</b></td>
<td><?= $status['currentFailureCount'] ?></td>
</tr>
<tr>
<td><b>Total failures</b></td>
<td><?= $status['totalFailureCount'] ?></td>
</tr>
</table>
<? endif; ?>
<?
}

/**
* Registers the Metering Status widget.
*/
function register_usage_metering_status_widget() {
wp_add_dashboard_widget(
'usage_metering_status_widget',
'Usage Metering Status',
'usage_metering_status_widget_display'
);
}

// Action registrations.
add_action('wp', 'handle_view');
add_action('wp_dashboard_setup', 'register_usage_metering_status_widget');

?>

0 comments on commit 153493a

Please sign in to comment.