From ba66a3c9f8f65f8e8b418fbaf186457527c0c995 Mon Sep 17 00:00:00 2001 From: Daniel Baur Date: Tue, 16 Jun 2015 22:55:48 +0200 Subject: [PATCH] Replaced ghost start script with python script --- .../resources/ghost/ghost.default.template | 137 ++++++++++++++++++ ghost-openstack/scripts/ghost/ghost.py | 89 ++++++++++++ ghost-openstack/scripts/ghost/start-ubuntu.sh | 15 -- ghost-openstack/types/ghost.yaml | 21 ++- 4 files changed, 244 insertions(+), 18 deletions(-) create mode 100644 ghost-openstack/resources/ghost/ghost.default.template create mode 100644 ghost-openstack/scripts/ghost/ghost.py delete mode 100755 ghost-openstack/scripts/ghost/start-ubuntu.sh diff --git a/ghost-openstack/resources/ghost/ghost.default.template b/ghost-openstack/resources/ghost/ghost.default.template new file mode 100644 index 0000000..d595a7b --- /dev/null +++ b/ghost-openstack/resources/ghost/ghost.default.template @@ -0,0 +1,137 @@ +// # Ghost Configuration +// Setup your Ghost install for various environments +// Documentation can be found at http://support.ghost.org/config/ + +var path = require('path'), + config; + +config = { + // ### Production + // When running Ghost in the wild, use the production environment + // Configure your URL and mail settings here + production: { + url: 'http://my-ghost-blog.com', + mail: {}, + database: { + client: 'sqlite3', + connection: { + filename: path.join(__dirname, '/content/data/ghost.db') + }, + debug: false + }, + + server: { + // Host to be passed to node's `net.Server#listen()` + host: '0.0.0.0', + // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT` + port: '2368' + } + }, + + // ### Development **(default)** + development: { + // The url to use when providing links to the site, E.g. in RSS and email. + // Change this to your Ghost blogs published URL. + url: 'http://localhost:2368', + + // Example mail config + // Visit http://support.ghost.org/mail for instructions + // ``` + // mail: { + // transport: 'SMTP', + // options: { + // service: 'Mailgun', + // auth: { + // user: '', // mailgun username + // pass: '' // mailgun password + // } + // } + // }, + // ``` + + database: { + client: 'pg', + connection: { + host : '{{ postgre_ip }}', + user : 'postgres', + password : '', + database : 'ghost_testing', + charset : 'utf8' + } + }, + server: { + // Host to be passed to node's `net.Server#listen()` + host: '0.0.0.0', + // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT` + port: '2368' + }, + paths: { + contentPath: path.join(__dirname, '/content/') + } + }, + + // **Developers only need to edit below here** + + // ### Testing + // Used when developing Ghost to run tests and check the health of Ghost + // Uses a different port number + testing: { + url: 'http://127.0.0.1:2369', + database: { + client: 'sqlite3', + connection: { + filename: path.join(__dirname, '/content/data/ghost-test.db') + } + }, + server: { + host: '127.0.0.1', + port: '2369' + }, + logging: false + }, + + // ### Testing MySQL + // Used by Travis - Automated testing run through GitHub + 'testing-mysql': { + url: 'http://127.0.0.1:2369', + database: { + client: 'mysql', + connection: { + host : '127.0.0.1', + user : 'root', + password : '', + database : 'ghost_testing', + charset : 'utf8' + } + }, + server: { + host: '127.0.0.1', + port: '2369' + }, + logging: false + }, + + // ### Testing pg + // Used by Travis - Automated testing run through GitHub + 'testing-pg': { + url: 'http://127.0.0.1:2369', + database: { + client: 'pg', + connection: { + host : '127.0.0.1', + user : 'postgres', + password : '', + database : 'ghost_testing', + charset : 'utf8' + } + }, + server: { + host: '127.0.0.1', + port: '2369' + }, + logging: false + } +}; + +// Export config +module.exports = config; \ No newline at end of file diff --git a/ghost-openstack/scripts/ghost/ghost.py b/ghost-openstack/scripts/ghost/ghost.py new file mode 100644 index 0000000..996f5c6 --- /dev/null +++ b/ghost-openstack/scripts/ghost/ghost.py @@ -0,0 +1,89 @@ +############################################################################### +# 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. +############################################################################### + +import os +import subprocess +import tempfile +from contextlib import contextmanager + +from jinja2 import Template + +from cloudify_rest_client import exceptions as rest_exceptions +from cloudify import ctx +from cloudify.state import ctx_parameters as inputs +from cloudify import exceptions +from cloudify import utils + + +CONFIG_PATH = '~/ghost/config.js' +TEMPLATE_RESOURCE_NAME = 'resources/ghost/ghost.default.template' + + +def configure(subject=None): + subject = subject or ctx + + ctx.logger.info('Configuring ghost.') + + _run('sudo apt-get install unzip','Failed installing unzip') + _run('curl -L https://ghost.org/zip/ghost-latest.zip -o ~/ghost.zip','Failed downloading ghost') + _run('unzip -uo ~/ghost.zip -d ~/ghost','Failed unzipping ghost') + + template = Template(ctx.get_resource(TEMPLATE_RESOURCE_NAME)) + + ctx.logger.debug('Building a dict object that will contain variables ' + 'to write to the Jinja2 template.') + + config = subject.node.properties.copy() + config.update(dict( + postgre_ip=subject.instance.runtime_properties.postgre_ip_address)) + + ctx.logger.debug('Rendering the Jinja2 template to {0}.'.format(CONFIG_PATH)) + ctx.logger.debug('The config dict: {0}.'.format(config)) + + with tempfile.NamedTemporaryFile(delete=False) as temp_config: + temp_config.write(template.render(config)) + + #_run('sudo /usr/sbin/haproxy -f {0} -c'.format(temp_config.name), + # error_message='Failed to Configure') + + _run('sudo mv {0} {1}'.format(temp_config.name, CONFIG_PATH), + error_message='Failed to write to {0}.'.format(CONFIG_PATH)) + + +def start(): + _run('npm install','Failed running npm install') + _run('npm start','Failed running npm start') + + +def stop(): + _run('sudo killall nodejs','Failed killing nodejs') + +def _run(command, error_message): + runner = utils.LocalCommandRunner(logger=ctx.logger) + try: + runner.run(command) + except exceptions.CommandExecutionException as e: + raise NonRecoverableError('{0}: {1}'.format(error_message, e)) + + +def _main(): + invocation = inputs['invocation'] + function = invocation['function'] + args = invocation.get('args', []) + kwargs = invocation.get('kwargs', {}) + globals()[function](*args, **kwargs) + + +if __name__ == '__main__': + _main() \ No newline at end of file diff --git a/ghost-openstack/scripts/ghost/start-ubuntu.sh b/ghost-openstack/scripts/ghost/start-ubuntu.sh deleted file mode 100755 index 0bb2c5e..0000000 --- a/ghost-openstack/scripts/ghost/start-ubuntu.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -e - -ctx logger info "Starting ghost" -ctx logger debug "${COMMAND}" - -sudo apt-get install unzip -cd ~ -curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip -unzip -uo ghost.zip -d ghost -cd ghost -npm install --production -npm start - -ctx logger info "Starting ghost" - diff --git a/ghost-openstack/types/ghost.yaml b/ghost-openstack/types/ghost.yaml index 080abdd..7ab43ee 100644 --- a/ghost-openstack/types/ghost.yaml +++ b/ghost-openstack/types/ghost.yaml @@ -46,9 +46,24 @@ node_types: derived_from: cloudify.nodes.ApplicationModule interfaces: cloudify.interfaces.lifecycle: - # configure: scripts/nodecellar/install-nodecellar-app.sh - start: scripts/ghost/start-ubuntu.sh - # stop: scripts/nodecellar/stop-nodecellar-app.sh + configure: + implementation: scripts/ghost/ghost.py + inputs: + invocation: + default: + function: configure + start: + implementation: scripts/ghost/ghost.py + inputs: + invocation: + default: + function: start + stop: + implementation: scripts/ghost/ghost.py + inputs: + invocation: + default: + function: stop ################################################################ -- GitLab