github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/maintenance/recreate_prow_configmaps.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2018 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  # This script deletes and recreates the prow configmaps
    18  # USE AT YOUR OWN RISK! This is a break-glass tool.
    19  # See September 25th, 2018 in docs/post-mortems.md
    20  
    21  #
    22  # USAGE: have KUBECONFIG pointed at your prow cluster then from test-infra root:
    23  #
    24  # hack/recreate_prow_configmaps.py [--wet]
    25  #
    26  
    27  from __future__ import print_function
    28  
    29  from argparse import ArgumentParser
    30  import os
    31  import sys
    32  import subprocess
    33  
    34  
    35  
    36  def recreate_prow_config(wet, configmap_name, path):
    37      print('recreating prow config:')
    38      cmd = (
    39          'kubectl create configmap %s'
    40          ' --from-file=config.yaml=%s'
    41          ' --dry-run -o yaml | kubectl replace configmap config -f -'
    42      ) % (configmap_name, path)
    43      real_cmd = ['/bin/sh', '-c', cmd]
    44      print(real_cmd)
    45      if wet:
    46          subprocess.check_call(real_cmd)
    47  
    48  
    49  def recreate_plugins_config(wet, configmap_name, path):
    50      print('recreating plugins config:')
    51      cmd = (
    52          'kubectl create configmap %s'
    53          ' --from-file=plugins.yaml=%s'
    54          ' --dry-run -o yaml | kubectl replace configmap config -f -'
    55      ) % (configmap_name, path)
    56      real_cmd = ['/bin/sh', '-c', cmd]
    57      print(real_cmd)
    58      if wet:
    59          subprocess.check_call(real_cmd)
    60  
    61  def recreate_job_config(wet, job_configmap, job_config_dir):
    62      print('recreating jobs config:')
    63       # delete configmap (apply has size limit)
    64      cmd = ["kubectl", "delete", "configmap", job_configmap]
    65      print(cmd)
    66      if wet:
    67          subprocess.check_call(cmd)
    68  
    69      # regenerate
    70      cmd = ["kubectl", "create", "configmap", job_configmap]
    71      for root, _, files in os.walk(job_config_dir):
    72          for name in files:
    73              if name.endswith(".yaml"):
    74                  cmd.append("--from-file=%s=%s" % (name, os.path.join(root, name)))
    75      print(cmd)
    76      if wet:
    77          subprocess.check_call(cmd)
    78  
    79  def main():
    80      parser = ArgumentParser()
    81      # jobs config
    82      parser.add_argument("--job-configmap", default="job-config", help="name of prow jobs configmap")
    83      parser.add_argument(
    84          "--job-config-dir", default="config/jobs",
    85          help="root dir of prow jobs configmap")
    86      # prow config
    87      parser.add_argument("--prow-configmap", default="config",
    88                          help="name of prow primary configmap")
    89      parser.add_argument(
    90          "--prow-config-path", default="prow/config.yaml",
    91          help="path to the primary prow config")
    92      # plugins config
    93      parser.add_argument("--plugins-configmap", default="plugins",
    94                          help="name of prow plugins configmap")
    95      parser.add_argument(
    96          "--plugins-config-path", default="prow/plugins.yaml",
    97          help="path to the prow plugins config")
    98      # wet or dry?
    99      parser.add_argument("--wet", action="store_true")
   100      args = parser.parse_args()
   101  
   102      # debug the current context
   103      out = subprocess.check_output(['kubectl', 'config', 'current-context'])
   104      print('Current KUBECONFIG context: '+out)
   105  
   106      # require additional confirmation in --wet mode
   107      prompt = '!'*65 + (
   108          "\n!!     WARNING THIS WILL RECREATE **ALL** PROW CONFIGMAPS.     !!"
   109          "\n!!    ARE YOU SURE YOU WANT TO DO THIS? IF SO, ENTER 'YES'.    !! "
   110      ) + '\n' + '!'*65 + '\n\n: '
   111      if args.wet:
   112          if raw_input(prompt) != "YES":
   113              print("you did not enter 'YES'")
   114              sys.exit(-1)
   115  
   116      # first prow config
   117      recreate_prow_config(args.wet, args.prow_configmap, args.prow_config_path)
   118      print('')
   119      # then plugins config
   120      recreate_plugins_config(args.wet, args.plugins_configmap, args.plugins_config_path)
   121      print('')
   122      # finally jobs config
   123      recreate_job_config(args.wet, args.job_configmap, args.job_config_dir)
   124  
   125  
   126  
   127  if __name__ == '__main__':
   128      main()