You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.5 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2016 Kubespray
  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. import os
  17. import sys
  18. import hashlib
  19. import urllib2
  20. import yaml
  21. import argparse
  22. import shutil
  23. from re import sub
  24. def get_remote_sha256_sum(url, max_file_size=100*1024*1024):
  25. remote = urllib2.urlopen(url)
  26. hash = hashlib.sha256()
  27. total_read = 0
  28. while True:
  29. data = remote.read(4096)
  30. total_read += 4096
  31. if not data or total_read > max_file_size:
  32. break
  33. hash.update(data)
  34. return hash.hexdigest()
  35. def read_vars(var_file):
  36. """
  37. Read the variables file
  38. """
  39. try:
  40. with open(var_file, "r") as f:
  41. kargovars = yaml.load(f)
  42. except:
  43. print(
  44. "Can't read variables file %s" % var_file
  45. )
  46. sys.exit(1)
  47. return kargovars
  48. def get_kube_sha256(version, download_url, binaries):
  49. kube_sha256 = dict()
  50. for k in binaries:
  51. s = get_remote_sha256_sum(download_url + '/' + k)
  52. kube_sha256[k] = s
  53. kube_sha256['kube_apiserver'] = kube_sha256.pop('kube-apiserver')
  54. return(kube_sha256)
  55. def file_sub(file, regex, string):
  56. "Substitute string in a file"
  57. shutil.move(file, file + '~')
  58. f = open(file + '~', 'r')
  59. data = f.read()
  60. o = open(file, 'w')
  61. o.write(sub(regex, string, data))
  62. f.close()
  63. o.close()
  64. os.remove(file + '~')
  65. if __name__ == '__main__':
  66. parser = argparse.ArgumentParser(
  67. prog='change_k8s_version',
  68. description='%(prog)s changes the version to be installed with kargo',
  69. )
  70. parser.add_argument(
  71. '-v', '--version', dest='kube_version', required=True,
  72. help="kubernetes version"
  73. )
  74. parser.add_argument(
  75. '-r', '--repository', dest='docker_repository', required=True,
  76. help="hyperkube docker repository"
  77. )
  78. args = parser.parse_args()
  79. kargo_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  80. file_sub(
  81. os.path.join(kargo_root_path, 'roles/kubernetes/node/defaults/main.yml'),
  82. r'.*hyperkube_image_repo.*', 'hyperkube_image_repo: "%s"' % args.docker_repository
  83. )
  84. file_sub(
  85. os.path.join(kargo_root_path, 'roles/kubernetes/node/defaults/main.yml'),
  86. r'.*hyperkube_image_tag.*', 'hyperkube_image_tag: "%s"' % args.kube_version
  87. )
  88. kube_binaries = ['kubelet', 'kubectl', 'kube-apiserver']
  89. var_files = [
  90. os.path.join(kargo_root_path, 'roles/uploads/vars/kube_versions.yml'),
  91. os.path.join(kargo_root_path, 'roles/download/vars/kube_versions.yml')
  92. ]
  93. kube_download_url = "https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64" % args.kube_version
  94. new = get_kube_sha256(args.kube_version, kube_download_url, kube_binaries)
  95. for f in var_files:
  96. current = read_vars(f)
  97. current['kube_checksum'][args.kube_version] = new
  98. with open(f, 'w') as out:
  99. out.write(yaml.dump(current, indent=4, default_flow_style=False))