utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2013 dotCloud inc.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import io
  12. import tarfile
  13. import tempfile
  14. from distutils.version import StrictVersion
  15. import requests
  16. from fig.packages import six
  17. def mkbuildcontext(dockerfile):
  18. f = tempfile.NamedTemporaryFile()
  19. t = tarfile.open(mode='w', fileobj=f)
  20. if isinstance(dockerfile, io.StringIO):
  21. dfinfo = tarfile.TarInfo('Dockerfile')
  22. if six.PY3:
  23. raise TypeError('Please use io.BytesIO to create in-memory '
  24. 'Dockerfiles with Python 3')
  25. else:
  26. dfinfo.size = len(dockerfile.getvalue())
  27. elif isinstance(dockerfile, io.BytesIO):
  28. dfinfo = tarfile.TarInfo('Dockerfile')
  29. dfinfo.size = len(dockerfile.getvalue())
  30. else:
  31. dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile')
  32. t.addfile(dfinfo, dockerfile)
  33. t.close()
  34. f.seek(0)
  35. return f
  36. def tar(path):
  37. f = tempfile.NamedTemporaryFile()
  38. t = tarfile.open(mode='w', fileobj=f)
  39. t.add(path, arcname='.')
  40. t.close()
  41. f.seek(0)
  42. return f
  43. def compare_version(v1, v2):
  44. """Compare docker versions
  45. >>> v1 = '1.9'
  46. >>> v2 = '1.10'
  47. >>> compare_version(v1, v2)
  48. 1
  49. >>> compare_version(v2, v1)
  50. -1
  51. >>> compare_version(v2, v2)
  52. 0
  53. """
  54. s1 = StrictVersion(v1)
  55. s2 = StrictVersion(v2)
  56. if s1 == s2:
  57. return 0
  58. elif s1 > s2:
  59. return -1
  60. else:
  61. return 1
  62. def ping(url):
  63. try:
  64. res = requests.get(url)
  65. except Exception:
  66. return False
  67. else:
  68. return res.status_code < 400
  69. def _convert_port_binding(binding):
  70. result = {'HostIp': '', 'HostPort': ''}
  71. if isinstance(binding, tuple):
  72. if len(binding) == 2:
  73. result['HostPort'] = binding[1]
  74. result['HostIp'] = binding[0]
  75. elif isinstance(binding[0], six.string_types):
  76. result['HostIp'] = binding[0]
  77. else:
  78. result['HostPort'] = binding[0]
  79. else:
  80. result['HostPort'] = binding
  81. if result['HostPort'] is None:
  82. result['HostPort'] = ''
  83. else:
  84. result['HostPort'] = str(result['HostPort'])
  85. return result
  86. def convert_port_bindings(port_bindings):
  87. result = {}
  88. for k, v in six.iteritems(port_bindings):
  89. key = str(k)
  90. if '/' not in key:
  91. key = key + '/tcp'
  92. if isinstance(v, list):
  93. result[key] = [_convert_port_binding(binding) for binding in v]
  94. else:
  95. result[key] = [_convert_port_binding(v)]
  96. return result
  97. def parse_repository_tag(repo):
  98. column_index = repo.rfind(':')
  99. if column_index < 0:
  100. return repo, ""
  101. tag = repo[column_index+1:]
  102. slash_index = tag.find('/')
  103. if slash_index < 0:
  104. return repo[:column_index], tag
  105. return repo, ""