utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. elif isinstance(binding, dict):
  80. if 'HostPort' in binding:
  81. result['HostPort'] = binding['HostPort']
  82. if 'HostIp' in binding:
  83. result['HostIp'] = binding['HostIp']
  84. else:
  85. raise ValueError(binding)
  86. else:
  87. result['HostPort'] = binding
  88. if result['HostPort'] is None:
  89. result['HostPort'] = ''
  90. else:
  91. result['HostPort'] = str(result['HostPort'])
  92. return result
  93. def convert_port_bindings(port_bindings):
  94. result = {}
  95. for k, v in six.iteritems(port_bindings):
  96. key = str(k)
  97. if '/' not in key:
  98. key = key + '/tcp'
  99. if isinstance(v, list):
  100. result[key] = [_convert_port_binding(binding) for binding in v]
  101. else:
  102. result[key] = [_convert_port_binding(v)]
  103. return result
  104. def convert_volume_binds(binds):
  105. result = []
  106. for k, v in binds.items():
  107. if isinstance(v, dict):
  108. result.append('%s:%s:%s' % (
  109. k, v['bind'], 'ro' if v.get('ro', False) else 'rw'
  110. ))
  111. else:
  112. result.append('%s:%s:rw' % (k, v))
  113. return result
  114. def parse_repository_tag(repo):
  115. column_index = repo.rfind(':')
  116. if column_index < 0:
  117. return repo, None
  118. tag = repo[column_index+1:]
  119. slash_index = tag.find('/')
  120. if slash_index < 0:
  121. return repo[:column_index], tag
  122. return repo, None