ghost.py 3.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
###############################################################################
#   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.')
    
Daniel Baur's avatar
Daniel Baur committed
38 39 40
    _run('sudo apt-get install unzip',error_message='Failed installing unzip')
    _run('curl -L https://ghost.org/zip/ghost-latest.zip -o ~/ghost.zip',error_message='Failed downloading ghost')
    _run('unzip -uo ~/ghost.zip -d ~/ghost',error_message='Failed unzipping ghost')  
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    
    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():
Daniel Baur's avatar
Daniel Baur committed
65 66
    _run('npm install',error_message='Failed running npm install')
    _run('npm start',error_message='Failed running npm start')
67 68 69


def stop():
Daniel Baur's avatar
Daniel Baur committed
70
    _run('sudo killall nodejs',error_message='Failed killing nodejs')
71 72 73 74 75 76

def _run(command, error_message):
    runner = utils.LocalCommandRunner(logger=ctx.logger)
    try:
        runner.run(command)
    except exceptions.CommandExecutionException as e:
Daniel Baur's avatar
Daniel Baur committed
77
        raise exceptions.NonRecoverableError('{0}: {1}'.format(error_message, e))
78 79 80 81 82 83 84 85 86 87 88 89


def _main():
    invocation = inputs['invocation']
    function = invocation['function']
    args = invocation.get('args', [])
    kwargs = invocation.get('kwargs', {})
    globals()[function](*args, **kwargs)


if __name__ == '__main__':
    _main()