github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/velodrome/config.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2017 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  import os
    18  import string
    19  import sys
    20  
    21  import yaml
    22  
    23  CONFIG = "config.yaml"
    24  
    25  DEPLOYMENTS = {
    26      "fetcher": "fetcher/deployment.yaml",
    27      "transform": "transform/deployment.yaml",
    28      "influxdb": "grafana-stack/influxdb.yaml",
    29      "prometheus": "grafana-stack/prometheus.yaml",
    30      "prometheus-config": "grafana-stack/prometheus-config.yaml",
    31      "grafana": "grafana-stack/grafana.yaml",
    32      "grafana-config": "grafana-stack/grafana-config.yaml",
    33      "nginx": "grafana-stack/nginx.yaml",
    34      "sqlproxy": "mysql/sqlproxy.yaml",
    35  }
    36  
    37  
    38  def main():
    39      if len(sys.argv) != 1:
    40          print >> sys.stderr, "Too many arguments."
    41          sys.exit(128)
    42  
    43      with open(get_absolute_path(CONFIG)) as config_file:
    44          config = yaml.load(config_file)
    45          print_deployments(["sqlproxy"], {})
    46          for project_name, project in config['projects'].items():
    47              public_ip = project.get('nginx', {}).get('public-ip', '') or ''
    48              print_deployments(["influxdb", "grafana", "nginx"], {
    49                  "PROJECT": project_name,
    50                  "NGINX_PUBLIC_IP": public_ip,
    51              })
    52              if 'prometheus' in project:
    53                  print_deployments(["prometheus"], {
    54                      "PROJECT": project_name,
    55                  })
    56                  patch_configuration("prometheus-config",
    57                                      project['prometheus'],
    58                                      {"PROJECT": project_name})
    59              if 'grafana' in project:
    60                  patch_configuration("grafana-config",
    61                                      project['grafana'],
    62                                      {"PROJECT": project_name})
    63              for repository, transforms in project['repositories'].items():
    64                  print_deployments(["fetcher"], {
    65                      "GH_ORGANIZATION": repository.split("/")[0],
    66                      "GH_REPOSITORY": repository.split("/")[1],
    67                      "PROJECT": project_name,
    68                  })
    69                  for metric, transformer in (transforms or {}).items():
    70                      plugin = metric
    71                      if "plugin" in transformer:
    72                          plugin = transformer["plugin"]
    73                      args = []
    74                      if "args" in transformer:
    75                          args = transformer["args"]
    76                      apply_transform(args, {
    77                          "GH_ORGANIZATION": repository.split("/")[0],
    78                          "GH_REPOSITORY": repository.split("/")[1],
    79                          "PROJECT": project_name,
    80                          "TRANSFORM_PLUGIN": plugin,
    81                          "TRANSFORM_METRIC": metric,
    82                      })
    83  
    84  
    85  def apply_transform(new_args, env):
    86      with open(get_absolute_path(DEPLOYMENTS["transform"])) as fp:
    87          config = yaml.load(fp)
    88          config['spec']['template']['spec']['containers'][0]['args'] += new_args
    89      print_deployment(yaml.dump(config, default_flow_style=False), env)
    90  
    91  
    92  def patch_configuration(component, values, env):
    93      with open(get_absolute_path(DEPLOYMENTS[component])) as fp:
    94          config = yaml.load(fp)
    95          # We want to fail if we have unknown keys in values
    96          unknown_keys = set(values) - set(config['data'])
    97          if unknown_keys:
    98              raise ValueError("Unknown keys in config:", unknown_keys)
    99          config['data'] = values
   100      print_deployment(yaml.dump(config, default_flow_style=False), env)
   101  
   102  
   103  def get_absolute_path(path):
   104      return os.path.join(os.path.dirname(__file__), path)
   105  
   106  
   107  def print_deployments(components, env):
   108      for component in components:
   109          with open(get_absolute_path(DEPLOYMENTS[component])) as fp:
   110              print_deployment(fp.read(), env)
   111  
   112  
   113  def print_deployment(deployment, env):
   114      print string.Template(deployment).safe_substitute(**env),
   115      print '---'
   116  
   117  if __name__ == '__main__':
   118      main()