<commit_before>#!/usr/bin/env python
#
# Copyright 2012, Rackspace US, Inc.
#
# 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 netifaces
import json
import urllib2

from bashscriptrunner import BashScriptRunner
import os

name = 'chef'


def setup(config={}):
    LOG.debug('Doing setup in plugin_chef.py using bash_path %s' %
              global_config['main']['bash_path'])
    if not 'bash_path' in global_config['main']:
        LOG.error('bash_path is not set')
        raise ValueError('bash_path not set')

    if 'cookbook_channels_manifest_url' not in global_config['chef']:
        LOG.error('cookbook_channels_manifest_url is not set')
        raise ValueError('cookbook_channels_manifest_url not set')

    script_path = [os.path.join(global_config['main']['bash_path'], name)]
    env = {'OPENCENTER_BASH_DIR': global_config['main']['bash_path']}
    script = BashScriptRunner(script_path=script_path, log=LOG,
                              environment=env)
    config['cookbook_channels_manifest_url'] \
        = global_config['chef']['cookbook_channels_manifest_url']
    chef = ChefThing(script, config)
    register_action(
        'install_chef', chef.dispatch, [],
        ['facts.backends := union(facts.backends, "chef-client")',
         'facts.chef_server_consumed := {chef_server}'],
        {'chef_server': {'type': 'interface',
                         'name': 'chef-server',
                         'required': True},
         'CHEF_SERVER_URL': {'type': 'evaluated',
                             'expression': 'nodes.{chef_server}.'
                             'facts.chef_server_uri'},
         'CHEF_SERVER_PEM': {'type': 'evaluated',
                             'expression': 'nodes.{chef_server}.'
                             'facts.chef_server_pem'},
         'CHEF_SERVER_HOSTNAME': {'type': 'evaluated',
                                  'expression': 'nodes.{chef_server}.name'}},
        timeout=300)
    register_action('run_chef', chef.dispatch, timeout=600)
    register_action('install_chef_server', chef.dispatch, timeout=600)
    register_action('uninstall_chef_server', chef.dispatch)
    register_action('rollback_install_chef_server', chef.dispatch)
    register_action('get_chef_info', chef.dispatch)
    register_action('get_cookbook_channels', chef.dispatch)
    register_action(
        'get_latest_channel_version', chef.dispatch, [], [],
        {'channel_name': {'type': 'string',
                          'required': True}})
    register_action(
        'download_cookbooks', chef.dispatch, [], [],
        {'CHEF_SERVER_COOKBOOK_CHANNELS': {
            'type': 'evaluated',
            'expression': 'self.facts.chef_server_cookbook_channels'}},
        timeout=120)
    register_action('uninstall_chef', chef.dispatch)
    register_action('rollback_install_chef', chef.dispatch)
    register_action('update_cookbooks', chef.dispatch,
                    [],
                    ['facts.chef_server_ready := true'])
    register_action('subscribe_cookbook_channel',
                    chef.dispatch,
                    [],
                    ['facts.chef_server_cookbook_channels := '
                     '"{channel_name}"'],
                    {'channel_name': {'type': 'string',
                                      'required': True}})


def get_environment(required, optional, payload):
    env = dict([(k, v) for k, v in payload.iteritems()
                if k in required + optional])
    for r in required:
        if not r in env:
            return False, {'result_code': 22,
                           'result_str': 'Bad Request (missing %s)' % r,
                           'result_data': None}
    return True, env


def retval(result_code, result_str, result_data):
    return {'result_code': result_code,
            'result_str': result_str,
            'result_data': result_data}


def success(result_str='success', result_data=None):
    return retval(0, result_str, result_data)


class ChefThing(object):
    def __init__(self, script, config):
        self.script = script
        self.config = config

    def install_chef(self, input_data):
        payload = input_data['payload']
        action = input_data['action']<commit_msg>Add friendly name/default/description for channel_name arg<commit_after>#!/usr/bin/env python
#
# Copyright 2012, Rackspace US, Inc.
#
# 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 netifaces
import json
import urllib2

from bashscriptrunner import BashScriptRunner
import os

name = 'chef'


def setup(config={}):
    LOG.debug('Doing setup in plugin_chef.py using bash_path %s' %
              global_config['main']['bash_path'])
    if not 'bash_path' in global_config['main']:
        LOG.error('bash_path is not set')
        raise ValueError('bash_path not set')

    if 'cookbook_channels_manifest_url' not in global_config['chef']:
        LOG.error('cookbook_channels_manifest_url is not set')
        raise ValueError('cookbook_channels_manifest_url not set')

    script_path = [os.path.join(global_config['main']['bash_path'], name)]
    env = {'OPENCENTER_BASH_DIR': global_config['main']['bash_path']}
    script = BashScriptRunner(script_path=script_path, log=LOG,
                              environment=env)
    config['cookbook_channels_manifest_url'] \
        = global_config['chef']['cookbook_channels_manifest_url']
    chef = ChefThing(script, config)
    register_action(
        'install_chef', chef.dispatch, [],
        ['facts.backends := union(facts.backends, "chef-client")',
         'facts.chef_server_consumed := {chef_server}'],
        {'chef_server': {'type': 'interface',
                         'name': 'chef-server',
                         'required': True},
         'CHEF_SERVER_URL': {'type': 'evaluated',
                             'expression': 'nodes.{chef_server}.'
                             'facts.chef_server_uri'},
         'CHEF_SERVER_PEM': {'type': 'evaluated',
                             'expression': 'nodes.{chef_server}.'
                             'facts.chef_server_pem'},
         'CHEF_SERVER_HOSTNAME': {'type': 'evaluated',
                                  'expression': 'nodes.{chef_server}.name'}},
        timeout=300)
    register_action('run_chef', chef.dispatch, timeout=600)
    register_action('install_chef_server', chef.dispatch, timeout=600)
    register_action('uninstall_chef_server', chef.dispatch)
    register_action('rollback_install_chef_server', chef.dispatch)
    register_action('get_chef_info', chef.dispatch)
    register_action('get_cookbook_channels', chef.dispatch)
    register_action(
        'get_latest_channel_version', chef.dispatch, [], [],
        {'channel_name': {'type': 'string',
                          'required': True}})
    register_action(
        'download_cookbooks', chef.dispatch, [], [],
        {'CHEF_SERVER_COOKBOOK_CHANNELS': {
            'type': 'evaluated',
            'expression': 'self.facts.chef_server_cookbook_channels'}},
        timeout=120)
    register_action('uninstall_chef', chef.dispatch)
    register_action('rollback_install_chef', chef.dispatch)
    register_action('update_cookbooks', chef.dispatch,
                    [],
                    ['facts.chef_server_ready := true'])
    register_action(
        'subscribe_cookbook_channel',
        chef.dispatch,
        [],
        ['facts.chef_server_cookbook_channels := "{channel_name}"'],
        {'channel_name': {
            'type': 'string',
            'friendly': 'Channel Name',
            'description': 'The name of the cookbook channel to subscribe to.',
            'default': 'current',
            'required': True}})


def get_environment(required, optional, payload):
    env = dict([(k, v) for k, v in payload.iteritems()
                if k in required + optional])
    for r in required:
        if not r in env:
            return False, {'result_code': 22,
                           'result_str': 'Bad Request (missing %s)' % r,
                           'result_data': None}
    return True, env


def retval(result_code, result_str, result_data):
    return {'result_code': result_code,
            'result_str': result_str,
            'result_data': result_data}


def success(result_str='success', result_data=None):
    return retval(0, result_str, result_data)


class ChefThing(object):
    def __init__(self, script, config):
        self.script = script
        self.config = config

    def install_chef(self, input_data):
        payload = input_data['payload']
        action = input_data['action']
{'fraction_non_alphanumeric': 0.11877154497022877, 'fraction_numerical': 0.005014102162331557, 'mean_word_length': 2.693672839506173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2358328', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2697, 'n_words': 634}