__init__.py 4.58 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
##############################################################################
#
# Copyright (c) 2011 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
from slapos.recipe.librecipe import GenericBaseRecipe
28
import json
29 30 31 32 33 34 35 36 37 38

class Recipe(GenericBaseRecipe):
  """
  kvm frontend instance configuration.
  """

  def _getRewriteRuleContent(self, slave_instance_list):
    """Generate rewrite rules list from slaves list"""
    rewrite_rule_list = []
    for slave_instance in slave_instance_list:
39 40
      self.logger.info("Processing slave instance %s..." %
          slave_instance['slave_reference'])
41 42 43 44 45 46 47 48 49 50
      # Check for mandatory fields
      if slave_instance.get('host', None) is None:
        self.logger.warn('No "host" parameter is defined for %s slave'\
            'instance. Ignoring it.' % slave_instance['slave_reference'])
        continue
      if slave_instance.get('port', None) is None:
        self.logger.warn('No "host" parameter is defined for %s slave'\
            'instance. Ignoring it.' % slave_instance['slave_reference'])
        continue

51
      current_slave_dict = dict()
52

53
      # Get host, and if IPv6 address, remove "[" and "]"
54
      current_slave_dict['host'] = slave_instance['host'].\
55 56
          replace('[', '').replace(']', '')
      current_slave_dict['port'] = slave_instance['port']
57

58 59
      # Check if target is https or http
      current_slave_dict['https'] = slave_instance.get('https', 'true')
60
      if current_slave_dict['https'] in GenericBaseRecipe.FALSE_VALUES:
61 62 63 64 65 66 67 68 69 70 71 72
        current_slave_dict['https'] = 'false'
      # Set reference and resource url
      # Reference is raw reference from SlapOS Master, resource is
      # URL-compatible name
      reference = slave_instance.get('slave_reference')
      current_slave_dict['reference'] = reference
      current_slave_dict['resource'] = reference.replace('-', '')
      rewrite_rule_list.append(current_slave_dict)
    return rewrite_rule_list

  def _getProxyTableContent(self, rewrite_rule_list):
    """Generate proxy table file content from rewrite rules list"""
73
    proxy_table = dict()
74
    for rewrite_rule in rewrite_rule_list:
75 76 77 78 79
      proxy_table[rewrite_rule['resource']] = {
          'port': rewrite_rule['port'],
          'host': rewrite_rule['host'],
          'https': rewrite_rule['https'],
      }
80

81
    proxy_table_content = json.dumps(proxy_table)
82 83 84 85 86
    return proxy_table_content

  def install(self):
    # Generate rewrite rules
    rewrite_rule_list = self._getRewriteRuleContent(
87
      json.loads(self.options['slave-instance-list']))
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    # Create Map
    map_content = self._getProxyTableContent(rewrite_rule_list)
    map_file = self.createFile(self.options['map-path'], map_content)

    # Create configuration
    conf = open(self.getTemplateFilename('kvm-proxy.js'), 'r')
    conf_file = self.createFile(self.options['conf-path'], conf.read())
    conf.close()

    config = dict(
      ip=self.options['ip'],
      port=self.options['port'],
      key=self.options['ssl-key-path'],
      certificate=self.options['ssl-cert-path'],
      name=self.options['domain'],
      shell_path=self.options['shell-path'],
      node_path=self.options['node-binary'],
      node_env=self.options['node-env'],
      conf_path=conf_file,
      map_path=map_file,
      plain_http='',
    )

    runner_path = self.createExecutable(
      self.options['wrapper-path'],
      self.substituteTemplate(self.getTemplateFilename('nodejs_run.in'),
                              config))

    return [map_file, conf_file, runner_path]