#!/usr/bin/env python
#########
# Copyright (c) 2016 GigaSpaces Technologies Ltd. All rights reserved
#
# 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
from os.path import join, dirname

from cloudify import ctx

ctx.download_resource(
    join('components', 'utils.py'),
    join(dirname(__file__), 'utils.py'))
import utils  # NOQA


MGMT_WORKER_SERVICE_NAME = 'mgmtworker'
CONFIG_PATH = "components/mgmtworker/config"


def configure_mgmtworker():
    # these must all be exported as part of the start operation.
    # they will not persist, so we should use the new agent
    # don't forget to change all localhosts to the relevant ips
    mgmtworker_home = '/opt/mgmtworker'
    mgmtworker_venv = '{0}/env'.format(mgmtworker_home)
    celery_work_dir = '{0}/work'.format(mgmtworker_home)

    ctx.logger.info('Configuring Management worker...')
    # Deploy the broker configuration
    # TODO: This will break interestingly if mgmtworker_venv is empty.
    # Some sort of check for that would be sensible.
    # To sandy: I don't quite understand this check...
    # there is no else here..
    # for python_path in ${mgmtworker_venv}/lib/python*; do
    if os.path.isfile(os.path.join(mgmtworker_venv, 'bin/python')):
        broker_conf_path = os.path.join(celery_work_dir, 'broker_config.json')
        utils.deploy_blueprint_resource(
            '{0}/broker_config.json'.format(CONFIG_PATH), broker_conf_path,
            MGMT_WORKER_SERVICE_NAME)
        # The config contains credentials, do not let the world read it
        utils.sudo(['chmod', '440', broker_conf_path])
    utils.systemd.configure(MGMT_WORKER_SERVICE_NAME)
    utils.logrotate(MGMT_WORKER_SERVICE_NAME)


def configure_logging():
    logging_config_dir = '/etc/cloudify'
    config_name = 'logging.conf'
    config_file_destination = os.path.join(logging_config_dir, config_name)
    config_file_source = os.path.join(CONFIG_PATH, config_name)
    utils.mkdir(logging_config_dir)
    ctx.download_resource(config_file_source, config_file_destination)


configure_mgmtworker()
configure_logging()

@@ -657,18 +657,24 @@
 e.%0A%0A
-%0A
 import 
-os
+tempfile%0A
 %0Afro
@@ -695,16 +695,24 @@
 rt join,
+ isfile,
  dirname
@@ -1694,31 +1694,15 @@
  if 
-os.path.isfile(os.path.
+isfile(
 join
@@ -1762,24 +1762,16 @@
 _path = 
-os.path.
 join(cel
@@ -2211,24 +2211,88 @@
 _logging():%0A
+    ctx.logger.info('Configuring Management worker logging...')%0A
     logging_
@@ -2383,24 +2383,16 @@
 ation = 
-os.path.
 join(log
@@ -2450,16 +2450,8 @@
 e = 
-os.path.
 join
@@ -2522,47 +2522,198 @@
    c
-tx.download_resource(config_file_source
+onfig_file_temp_destination = join(tempfile.gettempdir(), config_name)%0A    ctx.download_resource(config_file_source, config_file_temp_destination)%0A    utils.move(config_file_temp_destination
 , co

handle bootstrap permission