swift.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. # swift.py -- Repo implementation atop OpenStack SWIFT
  2. # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
  3. #
  4. # Author: Fabien Boucher <fabien.boucher@enovance.com>
  5. #
  6. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  8. # General Public License as published by the Free Software Foundation; version 2.0
  9. # or (at your option) any later version. You can redistribute it and/or
  10. # modify it under the terms of either of these two licenses.
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # You should have received a copy of the licenses; if not, see
  19. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  20. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  21. # License, Version 2.0.
  22. #
  23. """Repo implementation atop OpenStack SWIFT."""
  24. # TODO: Refactor to share more code with dulwich/repo.py.
  25. # TODO(fbo): Second attempt to _send() must be notified via real log
  26. # TODO(fbo): More logs for operations
  27. import json
  28. import logging
  29. import os
  30. import posixpath
  31. import stat
  32. import sys
  33. import tempfile
  34. import urllib.parse as urlparse
  35. import zlib
  36. from collections.abc import Iterator
  37. from configparser import ConfigParser
  38. from io import BytesIO
  39. from typing import BinaryIO, Callable, Optional, Union, cast
  40. from geventhttpclient import HTTPClient
  41. from ..greenthreads import GreenThreadsMissingObjectFinder
  42. from ..lru_cache import LRUSizeCache
  43. from ..object_store import INFODIR, PACKDIR, ObjectContainer, PackBasedObjectStore
  44. from ..objects import S_ISGITLINK, Blob, Commit, Tag, Tree
  45. from ..pack import (
  46. Pack,
  47. PackData,
  48. PackIndex,
  49. PackIndexer,
  50. PackStreamCopier,
  51. _compute_object_size,
  52. compute_file_sha,
  53. iter_sha1,
  54. load_pack_index_file,
  55. read_pack_header,
  56. unpack_object,
  57. write_pack_header,
  58. write_pack_index_v2,
  59. write_pack_object,
  60. )
  61. from ..protocol import TCP_GIT_PORT
  62. from ..refs import InfoRefsContainer, read_info_refs, split_peeled_refs, write_info_refs
  63. from ..repo import OBJECTDIR, BaseRepo
  64. from ..server import Backend, BackendRepo, TCPGitServer
  65. """
  66. # Configuration file sample
  67. [swift]
  68. # Authentication URL (Keystone or Swift)
  69. auth_url = http://127.0.0.1:5000/v2.0
  70. # Authentication version to use
  71. auth_ver = 2
  72. # The tenant and username separated by a semicolon
  73. username = admin;admin
  74. # The user password
  75. password = pass
  76. # The Object storage region to use (auth v2) (Default RegionOne)
  77. region_name = RegionOne
  78. # The Object storage endpoint URL to use (auth v2) (Default internalURL)
  79. endpoint_type = internalURL
  80. # Concurrency to use for parallel tasks (Default 10)
  81. concurrency = 10
  82. # Size of the HTTP pool (Default 10)
  83. http_pool_length = 10
  84. # Timeout delay for HTTP connections (Default 20)
  85. http_timeout = 20
  86. # Chunk size to read from pack (Bytes) (Default 12228)
  87. chunk_length = 12228
  88. # Cache size (MBytes) (Default 20)
  89. cache_length = 20
  90. """
  91. class PackInfoMissingObjectFinder(GreenThreadsMissingObjectFinder):
  92. """Find missing objects required for pack generation."""
  93. def next(self) -> Optional[tuple[bytes, int, Union[bytes, None]]]:
  94. """Get the next missing object.
  95. Returns:
  96. Tuple of (sha, pack_type_num, name) or None if no more objects
  97. """
  98. while True:
  99. if not self.objects_to_send:
  100. return None
  101. (sha, name, leaf, _) = self.objects_to_send.pop()
  102. if sha not in self.sha_done:
  103. break
  104. if not leaf:
  105. try:
  106. obj = self.object_store[sha]
  107. if isinstance(obj, Commit):
  108. self.add_todo([(obj.tree, b"", None, False)])
  109. elif isinstance(obj, Tree):
  110. tree_items = [
  111. (
  112. item.sha,
  113. item.path
  114. if isinstance(item.path, bytes)
  115. else item.path.encode("utf-8"),
  116. None,
  117. False,
  118. )
  119. for item in obj.items()
  120. ]
  121. self.add_todo(tree_items)
  122. elif isinstance(obj, Tag):
  123. self.add_todo([(obj.object[1], None, None, False)])
  124. if sha in self._tagged:
  125. self.add_todo([(self._tagged[sha], None, None, True)])
  126. except KeyError:
  127. pass
  128. self.sha_done.add(sha)
  129. self.progress(f"counting objects: {len(self.sha_done)}\r")
  130. return (
  131. sha,
  132. 0,
  133. name if isinstance(name, bytes) else name.encode("utf-8") if name else None,
  134. )
  135. def load_conf(path: Optional[str] = None, file: Optional[str] = None) -> ConfigParser:
  136. """Load configuration in global var CONF.
  137. Args:
  138. path: The path to the configuration file
  139. file: If provided read instead the file like object
  140. """
  141. conf = ConfigParser()
  142. if file:
  143. conf.read_file(file, path)
  144. else:
  145. confpath = None
  146. if not path:
  147. try:
  148. confpath = os.environ["DULWICH_SWIFT_CFG"]
  149. except KeyError as exc:
  150. raise Exception("You need to specify a configuration file") from exc
  151. else:
  152. confpath = path
  153. if not os.path.isfile(confpath):
  154. raise Exception(f"Unable to read configuration file {confpath}")
  155. conf.read(confpath)
  156. return conf
  157. def swift_load_pack_index(scon: "SwiftConnector", filename: str) -> "PackIndex":
  158. """Read a pack index file from Swift.
  159. Args:
  160. scon: a `SwiftConnector` instance
  161. filename: Path to the index file objectise
  162. Returns: a `PackIndexer` instance
  163. """
  164. f = scon.get_object(filename)
  165. if f is None:
  166. raise Exception(f"Could not retrieve index file {filename}")
  167. if isinstance(f, bytes):
  168. f = BytesIO(f)
  169. return load_pack_index_file(filename, f)
  170. def pack_info_create(pack_data: "PackData", pack_index: "PackIndex") -> bytes:
  171. """Create pack info file contents.
  172. Args:
  173. pack_data: The pack data object
  174. pack_index: The pack index object
  175. Returns:
  176. Compressed JSON bytes containing pack information
  177. """
  178. pack = Pack.from_objects(pack_data, pack_index)
  179. info: dict = {}
  180. for obj in pack.iterobjects():
  181. # Commit
  182. if obj.type_num == Commit.type_num:
  183. commit_obj = obj
  184. assert isinstance(commit_obj, Commit)
  185. info[obj.id] = (obj.type_num, commit_obj.parents, commit_obj.tree)
  186. # Tree
  187. elif obj.type_num == Tree.type_num:
  188. tree_obj = obj
  189. assert isinstance(tree_obj, Tree)
  190. shas = [
  191. (s, n, not stat.S_ISDIR(m))
  192. for n, m, s in tree_obj.items()
  193. if not S_ISGITLINK(m)
  194. ]
  195. info[obj.id] = (obj.type_num, shas)
  196. # Blob
  197. elif obj.type_num == Blob.type_num:
  198. info[obj.id] = (obj.type_num,)
  199. # Tag
  200. elif obj.type_num == Tag.type_num:
  201. tag_obj = obj
  202. assert isinstance(tag_obj, Tag)
  203. info[obj.id] = (obj.type_num, tag_obj.object[1])
  204. return zlib.compress(json.dumps(info).encode("utf-8"))
  205. def load_pack_info(
  206. filename: str,
  207. scon: Optional["SwiftConnector"] = None,
  208. file: Optional[BinaryIO] = None,
  209. ) -> Optional[dict]:
  210. """Load pack info from Swift or file.
  211. Args:
  212. filename: The pack info filename
  213. scon: Optional Swift connector to use for loading
  214. file: Optional file object to read from instead
  215. Returns:
  216. Dictionary containing pack information or None if not found
  217. """
  218. if not file:
  219. if scon is None:
  220. return None
  221. obj = scon.get_object(filename)
  222. if obj is None:
  223. return None
  224. if isinstance(obj, bytes):
  225. return json.loads(zlib.decompress(obj))
  226. else:
  227. f: BinaryIO = obj
  228. else:
  229. f = file
  230. try:
  231. return json.loads(zlib.decompress(f.read()))
  232. finally:
  233. if hasattr(f, "close"):
  234. f.close()
  235. class SwiftException(Exception):
  236. """Exception raised for Swift-related errors."""
  237. class SwiftConnector:
  238. """A Connector to swift that manage authentication and errors catching."""
  239. def __init__(self, root: str, conf: ConfigParser) -> None:
  240. """Initialize a SwiftConnector.
  241. Args:
  242. root: The swift container that will act as Git bare repository
  243. conf: A ConfigParser Object
  244. """
  245. self.conf = conf
  246. self.auth_ver = self.conf.get("swift", "auth_ver")
  247. if self.auth_ver not in ["1", "2"]:
  248. raise NotImplementedError("Wrong authentication version use either 1 or 2")
  249. self.auth_url = self.conf.get("swift", "auth_url")
  250. self.user = self.conf.get("swift", "username")
  251. self.password = self.conf.get("swift", "password")
  252. self.concurrency = self.conf.getint("swift", "concurrency") or 10
  253. self.http_timeout = self.conf.getint("swift", "http_timeout") or 20
  254. self.http_pool_length = self.conf.getint("swift", "http_pool_length") or 10
  255. self.region_name = self.conf.get("swift", "region_name") or "RegionOne"
  256. self.endpoint_type = self.conf.get("swift", "endpoint_type") or "internalURL"
  257. self.cache_length = self.conf.getint("swift", "cache_length") or 20
  258. self.chunk_length = self.conf.getint("swift", "chunk_length") or 12228
  259. self.root = root
  260. block_size = 1024 * 12 # 12KB
  261. if self.auth_ver == "1":
  262. self.storage_url, self.token = self.swift_auth_v1()
  263. else:
  264. self.storage_url, self.token = self.swift_auth_v2()
  265. token_header = {"X-Auth-Token": str(self.token)}
  266. self.httpclient = HTTPClient.from_url(
  267. str(self.storage_url),
  268. concurrency=self.http_pool_length,
  269. block_size=block_size,
  270. connection_timeout=self.http_timeout,
  271. network_timeout=self.http_timeout,
  272. headers=token_header,
  273. )
  274. self.base_path = str(
  275. posixpath.join(urlparse.urlparse(self.storage_url).path, self.root)
  276. )
  277. def swift_auth_v1(self) -> tuple[str, str]:
  278. """Authenticate with Swift using v1 authentication.
  279. Returns:
  280. Tuple of (storage_url, auth_token)
  281. Raises:
  282. SwiftException: If authentication fails
  283. """
  284. self.user = self.user.replace(";", ":")
  285. auth_httpclient = HTTPClient.from_url(
  286. self.auth_url,
  287. connection_timeout=self.http_timeout,
  288. network_timeout=self.http_timeout,
  289. )
  290. headers = {"X-Auth-User": self.user, "X-Auth-Key": self.password}
  291. path = urlparse.urlparse(self.auth_url).path
  292. ret = auth_httpclient.request("GET", path, headers=headers)
  293. # Should do something with redirections (301 in my case)
  294. if ret.status_code < 200 or ret.status_code >= 300:
  295. raise SwiftException(
  296. "AUTH v1.0 request failed on "
  297. + f"{self.auth_url} with error code {ret.status_code} ({ret.items()!s})"
  298. )
  299. storage_url = ret["X-Storage-Url"]
  300. token = ret["X-Auth-Token"]
  301. return storage_url, token
  302. def swift_auth_v2(self) -> tuple[str, str]:
  303. """Authenticate with Swift using v2 authentication.
  304. Returns:
  305. Tuple of (storage_url, auth_token)
  306. Raises:
  307. SwiftException: If authentication fails
  308. """
  309. self.tenant, self.user = self.user.split(";")
  310. auth_dict = {}
  311. auth_dict["auth"] = {
  312. "passwordCredentials": {
  313. "username": self.user,
  314. "password": self.password,
  315. },
  316. "tenantName": self.tenant,
  317. }
  318. auth_json = json.dumps(auth_dict)
  319. headers = {"Content-Type": "application/json"}
  320. auth_httpclient = HTTPClient.from_url(
  321. self.auth_url,
  322. connection_timeout=self.http_timeout,
  323. network_timeout=self.http_timeout,
  324. )
  325. path = urlparse.urlparse(self.auth_url).path
  326. if not path.endswith("tokens"):
  327. path = posixpath.join(path, "tokens")
  328. ret = auth_httpclient.request("POST", path, body=auth_json, headers=headers)
  329. if ret.status_code < 200 or ret.status_code >= 300:
  330. raise SwiftException(
  331. "AUTH v2.0 request failed on "
  332. + f"{str(auth_httpclient.get_base_url()) + path} with error code {ret.status_code} ({ret.items()!s})"
  333. )
  334. auth_ret_json = json.loads(ret.read())
  335. token = auth_ret_json["access"]["token"]["id"]
  336. catalogs = auth_ret_json["access"]["serviceCatalog"]
  337. object_store = next(
  338. o_store for o_store in catalogs if o_store["type"] == "object-store"
  339. )
  340. endpoints = object_store["endpoints"]
  341. endpoint = next(
  342. endp for endp in endpoints if endp["region"] == self.region_name
  343. )
  344. return endpoint[self.endpoint_type], token
  345. def test_root_exists(self) -> Optional[bool]:
  346. """Check that Swift container exist.
  347. Returns: True if exist or None it not
  348. """
  349. ret = self.httpclient.request("HEAD", self.base_path)
  350. if ret.status_code == 404:
  351. return None
  352. if ret.status_code < 200 or ret.status_code > 300:
  353. raise SwiftException(
  354. f"HEAD request failed with error code {ret.status_code}"
  355. )
  356. return True
  357. def create_root(self) -> None:
  358. """Create the Swift container.
  359. Raises:
  360. SwiftException: if unable to create
  361. """
  362. if not self.test_root_exists():
  363. ret = self.httpclient.request("PUT", self.base_path)
  364. if ret.status_code < 200 or ret.status_code > 300:
  365. raise SwiftException(
  366. f"PUT request failed with error code {ret.status_code}"
  367. )
  368. def get_container_objects(self) -> Optional[list[dict]]:
  369. """Retrieve objects list in a container.
  370. Returns: A list of dict that describe objects
  371. or None if container does not exist
  372. """
  373. qs = "?format=json"
  374. path = self.base_path + qs
  375. ret = self.httpclient.request("GET", path)
  376. if ret.status_code == 404:
  377. return None
  378. if ret.status_code < 200 or ret.status_code > 300:
  379. raise SwiftException(
  380. f"GET request failed with error code {ret.status_code}"
  381. )
  382. content = ret.read()
  383. return json.loads(content)
  384. def get_object_stat(self, name: str) -> Optional[dict]:
  385. """Retrieve object stat.
  386. Args:
  387. name: The object name
  388. Returns:
  389. A dict that describe the object or None if object does not exist
  390. """
  391. path = self.base_path + "/" + name
  392. ret = self.httpclient.request("HEAD", path)
  393. if ret.status_code == 404:
  394. return None
  395. if ret.status_code < 200 or ret.status_code > 300:
  396. raise SwiftException(
  397. f"HEAD request failed with error code {ret.status_code}"
  398. )
  399. resp_headers = {}
  400. for header, value in ret.items():
  401. resp_headers[header.lower()] = value
  402. return resp_headers
  403. def put_object(self, name: str, content: BinaryIO) -> None:
  404. """Put an object.
  405. Args:
  406. name: The object name
  407. content: A file object
  408. Raises:
  409. SwiftException: if unable to create
  410. """
  411. content.seek(0)
  412. data = content.read()
  413. path = self.base_path + "/" + name
  414. headers = {"Content-Length": str(len(data))}
  415. def _send() -> object:
  416. ret = self.httpclient.request("PUT", path, body=data, headers=headers)
  417. return ret
  418. try:
  419. # Sometime got Broken Pipe - Dirty workaround
  420. ret = _send()
  421. except (BrokenPipeError, ConnectionError):
  422. # Second attempt work
  423. ret = _send()
  424. if ret.status_code < 200 or ret.status_code > 300: # type: ignore
  425. raise SwiftException(
  426. f"PUT request failed with error code {ret.status_code}" # type: ignore
  427. )
  428. def get_object(
  429. self, name: str, range: Optional[str] = None
  430. ) -> Optional[Union[bytes, BytesIO]]:
  431. """Retrieve an object.
  432. Args:
  433. name: The object name
  434. range: A string range like "0-10" to
  435. retrieve specified bytes in object content
  436. Returns:
  437. A file like instance or bytestring if range is specified
  438. """
  439. headers = {}
  440. if range:
  441. headers["Range"] = f"bytes={range}"
  442. path = self.base_path + "/" + name
  443. ret = self.httpclient.request("GET", path, headers=headers)
  444. if ret.status_code == 404:
  445. return None
  446. if ret.status_code < 200 or ret.status_code > 300:
  447. raise SwiftException(
  448. f"GET request failed with error code {ret.status_code}"
  449. )
  450. content = ret.read()
  451. if range:
  452. return content
  453. return BytesIO(content)
  454. def del_object(self, name: str) -> None:
  455. """Delete an object.
  456. Args:
  457. name: The object name
  458. Raises:
  459. SwiftException: if unable to delete
  460. """
  461. path = self.base_path + "/" + name
  462. ret = self.httpclient.request("DELETE", path)
  463. if ret.status_code < 200 or ret.status_code > 300:
  464. raise SwiftException(
  465. f"DELETE request failed with error code {ret.status_code}"
  466. )
  467. def del_root(self) -> None:
  468. """Delete the root container by removing container content.
  469. Raises:
  470. SwiftException: if unable to delete
  471. """
  472. objects = self.get_container_objects()
  473. if objects:
  474. for obj in objects:
  475. self.del_object(obj["name"])
  476. ret = self.httpclient.request("DELETE", self.base_path)
  477. if ret.status_code < 200 or ret.status_code > 300:
  478. raise SwiftException(
  479. f"DELETE request failed with error code {ret.status_code}"
  480. )
  481. class SwiftPackReader:
  482. """A SwiftPackReader that mimic read and sync method.
  483. The reader allows to read a specified amount of bytes from
  484. a given offset of a Swift object. A read offset is kept internally.
  485. The reader will read from Swift a specified amount of data to complete
  486. its internal buffer. chunk_length specify the amount of data
  487. to read from Swift.
  488. """
  489. def __init__(self, scon: SwiftConnector, filename: str, pack_length: int) -> None:
  490. """Initialize a SwiftPackReader.
  491. Args:
  492. scon: a `SwiftConnector` instance
  493. filename: the pack filename
  494. pack_length: The size of the pack object
  495. """
  496. self.scon = scon
  497. self.filename = filename
  498. self.pack_length = pack_length
  499. self.offset = 0
  500. self.base_offset = 0
  501. self.buff = b""
  502. self.buff_length = self.scon.chunk_length
  503. def _read(self, more: bool = False) -> None:
  504. if more:
  505. self.buff_length = self.buff_length * 2
  506. offset = self.base_offset
  507. r = min(self.base_offset + self.buff_length, self.pack_length)
  508. ret = self.scon.get_object(self.filename, range=f"{offset}-{r}")
  509. if ret is None:
  510. self.buff = b""
  511. elif isinstance(ret, bytes):
  512. self.buff = ret
  513. else:
  514. self.buff = ret.read()
  515. def read(self, length: int) -> bytes:
  516. """Read a specified amount of Bytes form the pack object.
  517. Args:
  518. length: amount of bytes to read
  519. Returns:
  520. a bytestring
  521. """
  522. end = self.offset + length
  523. if self.base_offset + end > self.pack_length:
  524. data = self.buff[self.offset :]
  525. self.offset = end
  526. return data
  527. if end > len(self.buff):
  528. # Need to read more from swift
  529. self._read(more=True)
  530. return self.read(length)
  531. data = self.buff[self.offset : end]
  532. self.offset = end
  533. return data
  534. def seek(self, offset: int) -> None:
  535. """Seek to a specified offset.
  536. Args:
  537. offset: the offset to seek to
  538. """
  539. self.base_offset = offset
  540. self._read()
  541. self.offset = 0
  542. def read_checksum(self) -> bytes:
  543. """Read the checksum from the pack.
  544. Returns: the checksum bytestring
  545. """
  546. ret = self.scon.get_object(self.filename, range="-20")
  547. if ret is None:
  548. return b""
  549. elif isinstance(ret, bytes):
  550. return ret
  551. else:
  552. return ret.read()
  553. class SwiftPackData(PackData):
  554. """The data contained in a packfile.
  555. We use the SwiftPackReader to read bytes from packs stored in Swift
  556. using the Range header feature of Swift.
  557. """
  558. def __init__(self, scon: SwiftConnector, filename: Union[str, os.PathLike]) -> None:
  559. """Initialize a SwiftPackReader.
  560. Args:
  561. scon: a `SwiftConnector` instance
  562. filename: the pack filename
  563. """
  564. self.scon = scon
  565. self._filename = filename
  566. self._header_size = 12
  567. headers = self.scon.get_object_stat(str(self._filename))
  568. if headers is None:
  569. raise Exception(f"Could not get stats for {self._filename}")
  570. self.pack_length = int(headers["content-length"])
  571. pack_reader = SwiftPackReader(self.scon, str(self._filename), self.pack_length)
  572. (version, self._num_objects) = read_pack_header(pack_reader.read)
  573. self._offset_cache = LRUSizeCache(
  574. 1024 * 1024 * self.scon.cache_length,
  575. compute_size=_compute_object_size,
  576. )
  577. self.pack = None
  578. def get_object_at(
  579. self, offset: int
  580. ) -> tuple[int, Union[tuple[Union[bytes, int], list[bytes]], list[bytes]]]:
  581. """Get the object at a specific offset in the pack.
  582. Args:
  583. offset: The offset in the pack file
  584. Returns:
  585. Tuple of (pack_type_num, object_data)
  586. """
  587. if offset in self._offset_cache:
  588. return self._offset_cache[offset]
  589. assert offset >= self._header_size
  590. pack_reader = SwiftPackReader(self.scon, str(self._filename), self.pack_length)
  591. pack_reader.seek(offset)
  592. unpacked, _ = unpack_object(pack_reader.read)
  593. obj_data = unpacked._obj()
  594. return (unpacked.pack_type_num, obj_data)
  595. def get_stored_checksum(self) -> bytes:
  596. """Get the stored checksum for this pack.
  597. Returns:
  598. The pack checksum as bytes
  599. """
  600. pack_reader = SwiftPackReader(self.scon, str(self._filename), self.pack_length)
  601. return pack_reader.read_checksum()
  602. def close(self) -> None:
  603. """Close the pack data (no-op for Swift)."""
  604. class SwiftPack(Pack):
  605. """A Git pack object.
  606. Same implementation as pack.Pack except that _idx_load and
  607. _data_load are bounded to Swift version of load_pack_index and
  608. PackData.
  609. """
  610. def __init__(self, *args: object, **kwargs: object) -> None:
  611. self.scon = kwargs["scon"]
  612. del kwargs["scon"]
  613. super().__init__(*args, **kwargs) # type: ignore
  614. self._pack_info_path = self._basename + ".info"
  615. self._pack_info: Optional[dict] = None
  616. self._pack_info_load = lambda: load_pack_info(self._pack_info_path, self.scon) # type: ignore
  617. self._idx_load = lambda: swift_load_pack_index(self.scon, self._idx_path) # type: ignore
  618. self._data_load = lambda: SwiftPackData(self.scon, self._data_path) # type: ignore
  619. @property
  620. def pack_info(self) -> Optional[dict]:
  621. """The pack data object being used."""
  622. if self._pack_info is None:
  623. self._pack_info = self._pack_info_load()
  624. return self._pack_info
  625. class SwiftObjectStore(PackBasedObjectStore):
  626. """A Swift Object Store.
  627. Allow to manage a bare Git repository from Openstack Swift.
  628. This object store only supports pack files and not loose objects.
  629. """
  630. def __init__(self, scon: SwiftConnector) -> None:
  631. """Open a Swift object store.
  632. Args:
  633. scon: A `SwiftConnector` instance
  634. """
  635. super().__init__()
  636. self.scon = scon
  637. self.root = self.scon.root
  638. self.pack_dir = posixpath.join(OBJECTDIR, PACKDIR)
  639. self._alternates = None
  640. def _update_pack_cache(self) -> list:
  641. objects = self.scon.get_container_objects()
  642. if objects is None:
  643. return []
  644. pack_files = [
  645. o["name"].replace(".pack", "")
  646. for o in objects
  647. if o["name"].endswith(".pack")
  648. ]
  649. ret = []
  650. for basename in pack_files:
  651. pack = SwiftPack(basename, scon=self.scon)
  652. self._pack_cache[basename] = pack
  653. ret.append(pack)
  654. return ret
  655. def _iter_loose_objects(self) -> Iterator:
  656. """Loose objects are not supported by this repository."""
  657. return iter([])
  658. def pack_info_get(self, sha: bytes) -> Optional[tuple]:
  659. """Get pack info for a specific SHA.
  660. Args:
  661. sha: The SHA to look up
  662. Returns:
  663. Pack info tuple or None if not found
  664. """
  665. for pack in self.packs:
  666. if sha in pack:
  667. if hasattr(pack, "pack_info"):
  668. pack_info = pack.pack_info
  669. if pack_info is not None:
  670. return pack_info.get(sha)
  671. return None
  672. def _collect_ancestors(
  673. self, heads: list, common: Optional[set] = None
  674. ) -> tuple[set, set]:
  675. if common is None:
  676. common = set()
  677. def _find_parents(commit: bytes) -> list:
  678. for pack in self.packs:
  679. if commit in pack:
  680. try:
  681. if hasattr(pack, "pack_info"):
  682. pack_info = pack.pack_info
  683. if pack_info is not None:
  684. return pack_info[commit][1]
  685. except KeyError:
  686. # Seems to have no parents
  687. return []
  688. return []
  689. bases = set()
  690. commits = set()
  691. queue = []
  692. queue.extend(heads)
  693. while queue:
  694. e = queue.pop(0)
  695. if e in common:
  696. bases.add(e)
  697. elif e not in commits:
  698. commits.add(e)
  699. parents = _find_parents(e)
  700. queue.extend(parents)
  701. return (commits, bases)
  702. def add_pack(self) -> tuple[BytesIO, Callable, Callable]:
  703. """Add a new pack to this object store.
  704. Returns: Fileobject to write to and a commit function to
  705. call when the pack is finished.
  706. """
  707. f = BytesIO()
  708. def commit() -> Optional["SwiftPack"]:
  709. """Commit the pack to Swift storage.
  710. Returns:
  711. The created SwiftPack or None if empty
  712. """
  713. f.seek(0)
  714. pack = PackData(file=f, filename="")
  715. entries = pack.sorted_entries()
  716. if entries:
  717. basename = posixpath.join(
  718. self.pack_dir,
  719. f"pack-{iter_sha1(entry[0] for entry in entries).decode('ascii')}",
  720. )
  721. index = BytesIO()
  722. write_pack_index_v2(index, entries, pack.get_stored_checksum())
  723. self.scon.put_object(basename + ".pack", f)
  724. f.close()
  725. self.scon.put_object(basename + ".idx", index)
  726. index.close()
  727. final_pack = SwiftPack(basename, scon=self.scon)
  728. final_pack.check_length_and_checksum()
  729. self._add_cached_pack(basename, final_pack)
  730. return final_pack
  731. else:
  732. return None
  733. def abort() -> None:
  734. """Abort the pack operation (no-op)."""
  735. return f, commit, abort
  736. def add_object(self, obj: object) -> None:
  737. """Add a single object to the store.
  738. Args:
  739. obj: The object to add
  740. """
  741. self.add_objects(
  742. [
  743. (obj, None), # type: ignore
  744. ]
  745. )
  746. def _pack_cache_stale(self) -> bool:
  747. return False
  748. def _get_loose_object(self, sha: bytes) -> None:
  749. return None
  750. def add_thin_pack(self, read_all: Callable, read_some: Callable) -> "SwiftPack":
  751. """Read a thin pack.
  752. Read it from a stream and complete it in a temporary file.
  753. Then the pack and the corresponding index file are uploaded to Swift.
  754. """
  755. fd, path = tempfile.mkstemp(prefix="tmp_pack_")
  756. f = os.fdopen(fd, "w+b")
  757. try:
  758. indexer = PackIndexer(f, resolve_ext_ref=None)
  759. copier = PackStreamCopier(read_all, read_some, f, delta_iter=indexer)
  760. copier.verify()
  761. return self._complete_thin_pack(f, path, copier, indexer)
  762. finally:
  763. f.close()
  764. os.unlink(path)
  765. def _complete_thin_pack(
  766. self, f: BinaryIO, path: str, copier: object, indexer: object
  767. ) -> "SwiftPack":
  768. entries = list(indexer) # type: ignore
  769. # Update the header with the new number of objects.
  770. f.seek(0)
  771. write_pack_header(f, len(entries) + len(indexer.ext_refs())) # type: ignore
  772. # Must flush before reading (http://bugs.python.org/issue3207)
  773. f.flush()
  774. # Rescan the rest of the pack, computing the SHA with the new header.
  775. new_sha = compute_file_sha(f, end_ofs=-20)
  776. # Must reposition before writing (http://bugs.python.org/issue3207)
  777. f.seek(0, os.SEEK_CUR)
  778. # Complete the pack.
  779. for ext_sha in indexer.ext_refs(): # type: ignore
  780. assert len(ext_sha) == 20
  781. type_num, data = self.get_raw(ext_sha)
  782. offset = f.tell()
  783. crc32 = write_pack_object(f, type_num, data, sha=new_sha) # type: ignore
  784. entries.append((ext_sha, offset, crc32))
  785. pack_sha = new_sha.digest()
  786. f.write(pack_sha)
  787. f.flush()
  788. # Move the pack in.
  789. entries.sort()
  790. pack_base_name = posixpath.join(
  791. self.pack_dir,
  792. "pack-" + os.fsdecode(iter_sha1(e[0] for e in entries)),
  793. )
  794. self.scon.put_object(pack_base_name + ".pack", f)
  795. # Write the index.
  796. filename = pack_base_name + ".idx"
  797. index_file = BytesIO()
  798. write_pack_index_v2(index_file, entries, pack_sha)
  799. self.scon.put_object(filename, index_file)
  800. # Write pack info.
  801. f.seek(0)
  802. pack_data = PackData(filename="", file=f)
  803. index_file.seek(0)
  804. pack_index = load_pack_index_file("", index_file)
  805. serialized_pack_info = pack_info_create(pack_data, pack_index)
  806. f.close()
  807. index_file.close()
  808. pack_info_file = BytesIO(serialized_pack_info)
  809. filename = pack_base_name + ".info"
  810. self.scon.put_object(filename, pack_info_file)
  811. pack_info_file.close()
  812. # Add the pack to the store and return it.
  813. final_pack = SwiftPack(pack_base_name, scon=self.scon)
  814. final_pack.check_length_and_checksum()
  815. self._add_cached_pack(pack_base_name, final_pack)
  816. return final_pack
  817. class SwiftInfoRefsContainer(InfoRefsContainer):
  818. """Manage references in info/refs object."""
  819. def __init__(self, scon: SwiftConnector, store: object) -> None:
  820. self.scon = scon
  821. self.filename = "info/refs"
  822. self.store = store
  823. f = self.scon.get_object(self.filename)
  824. if not f:
  825. f = BytesIO(b"")
  826. elif isinstance(f, bytes):
  827. f = BytesIO(f)
  828. super().__init__(f)
  829. def _load_check_ref(
  830. self, name: bytes, old_ref: Optional[bytes]
  831. ) -> Union[dict, bool]:
  832. self._check_refname(name)
  833. obj = self.scon.get_object(self.filename)
  834. if not obj:
  835. return {}
  836. if isinstance(obj, bytes):
  837. f = BytesIO(obj)
  838. else:
  839. f = obj
  840. refs = read_info_refs(f)
  841. (refs, peeled) = split_peeled_refs(refs)
  842. if old_ref is not None:
  843. if refs[name] != old_ref:
  844. return False
  845. return refs
  846. def _write_refs(self, refs: dict) -> None:
  847. f = BytesIO()
  848. f.writelines(write_info_refs(refs, cast("ObjectContainer", self.store)))
  849. self.scon.put_object(self.filename, f)
  850. def set_if_equals(
  851. self,
  852. name: bytes,
  853. old_ref: Optional[bytes],
  854. new_ref: bytes,
  855. committer: Optional[bytes] = None,
  856. timestamp: Optional[float] = None,
  857. timezone: Optional[int] = None,
  858. message: Optional[bytes] = None,
  859. ) -> bool:
  860. """Set a refname to new_ref only if it currently equals old_ref."""
  861. if name == "HEAD":
  862. return True
  863. refs = self._load_check_ref(name, old_ref)
  864. if not isinstance(refs, dict):
  865. return False
  866. refs[name] = new_ref
  867. self._write_refs(refs)
  868. self._refs[name] = new_ref
  869. return True
  870. def remove_if_equals(
  871. self,
  872. name: bytes,
  873. old_ref: Optional[bytes],
  874. committer: object = None,
  875. timestamp: object = None,
  876. timezone: object = None,
  877. message: object = None,
  878. ) -> bool:
  879. """Remove a refname only if it currently equals old_ref."""
  880. if name == "HEAD":
  881. return True
  882. refs = self._load_check_ref(name, old_ref)
  883. if not isinstance(refs, dict):
  884. return False
  885. del refs[name]
  886. self._write_refs(refs)
  887. del self._refs[name]
  888. return True
  889. def allkeys(self) -> Iterator[bytes]:
  890. """Get all reference names.
  891. Returns:
  892. Iterator of reference names as bytes
  893. """
  894. try:
  895. self._refs[b"HEAD"] = self._refs[b"refs/heads/master"]
  896. except KeyError:
  897. pass
  898. return iter(self._refs.keys())
  899. class SwiftRepo(BaseRepo):
  900. """A Git repository backed by Swift object storage."""
  901. def __init__(self, root: str, conf: ConfigParser) -> None:
  902. """Init a Git bare Repository on top of a Swift container.
  903. References are managed in info/refs objects by
  904. `SwiftInfoRefsContainer`. The root attribute is the Swift
  905. container that contain the Git bare repository.
  906. Args:
  907. root: The container which contains the bare repo
  908. conf: A ConfigParser object
  909. """
  910. self.root = root.lstrip("/")
  911. self.conf = conf
  912. self.scon = SwiftConnector(self.root, self.conf)
  913. objects = self.scon.get_container_objects()
  914. if not objects:
  915. raise Exception(f"There is not any GIT repo here : {self.root}")
  916. objects = [o["name"].split("/")[0] for o in objects]
  917. if OBJECTDIR not in objects:
  918. raise Exception(f"This repository ({self.root}) is not bare.")
  919. self.bare = True
  920. self._controldir = self.root
  921. object_store = SwiftObjectStore(self.scon)
  922. refs = SwiftInfoRefsContainer(self.scon, object_store)
  923. BaseRepo.__init__(self, object_store, refs)
  924. def _determine_file_mode(self) -> bool:
  925. """Probe the file-system to determine whether permissions can be trusted.
  926. Returns: True if permissions can be trusted, False otherwise.
  927. """
  928. return False
  929. def _put_named_file(self, filename: str, contents: bytes) -> None:
  930. """Put an object in a Swift container.
  931. Args:
  932. filename: the path to the object to put on Swift
  933. contents: the content as bytestring
  934. """
  935. with BytesIO() as f:
  936. f.write(contents)
  937. self.scon.put_object(filename, f)
  938. @classmethod
  939. def init_bare(cls, scon: SwiftConnector, conf: ConfigParser) -> "SwiftRepo":
  940. """Create a new bare repository.
  941. Args:
  942. scon: a `SwiftConnector` instance
  943. conf: a ConfigParser object
  944. Returns:
  945. a `SwiftRepo` instance
  946. """
  947. scon.create_root()
  948. for obj in [
  949. posixpath.join(OBJECTDIR, PACKDIR),
  950. posixpath.join(INFODIR, "refs"),
  951. ]:
  952. scon.put_object(obj, BytesIO(b""))
  953. ret = cls(scon.root, conf)
  954. ret._init_files(True)
  955. return ret
  956. class SwiftSystemBackend(Backend):
  957. """Backend for serving Git repositories from Swift."""
  958. def __init__(self, logger: "logging.Logger", conf: ConfigParser) -> None:
  959. self.conf = conf
  960. self.logger = logger
  961. def open_repository(self, path: str) -> "BackendRepo":
  962. """Open a repository at the given path.
  963. Args:
  964. path: Path to the repository in Swift
  965. Returns:
  966. SwiftRepo instance
  967. """
  968. self.logger.info("opening repository at %s", path)
  969. return cast("BackendRepo", SwiftRepo(path, self.conf))
  970. def cmd_daemon(args: list) -> None:
  971. """Start a TCP git server for Swift repositories.
  972. Args:
  973. args: Command line arguments
  974. """
  975. import optparse
  976. parser = optparse.OptionParser()
  977. parser.add_option(
  978. "-l",
  979. "--listen_address",
  980. dest="listen_address",
  981. default="127.0.0.1",
  982. help="Binding IP address.",
  983. )
  984. parser.add_option(
  985. "-p",
  986. "--port",
  987. dest="port",
  988. type=int,
  989. default=TCP_GIT_PORT,
  990. help="Binding TCP port.",
  991. )
  992. parser.add_option(
  993. "-c",
  994. "--swift_config",
  995. dest="swift_config",
  996. default="",
  997. help="Path to the configuration file for Swift backend.",
  998. )
  999. options, args = parser.parse_args(args)
  1000. try:
  1001. import gevent
  1002. import geventhttpclient # noqa: F401
  1003. except ImportError:
  1004. print(
  1005. "gevent and geventhttpclient libraries are mandatory "
  1006. " for use the Swift backend."
  1007. )
  1008. sys.exit(1)
  1009. import gevent.monkey
  1010. gevent.monkey.patch_socket()
  1011. from dulwich import log_utils
  1012. logger = log_utils.getLogger(__name__)
  1013. conf = load_conf(options.swift_config)
  1014. backend = SwiftSystemBackend(logger, conf)
  1015. log_utils.default_logging_config()
  1016. server = TCPGitServer(backend, options.listen_address, port=options.port)
  1017. server.serve_forever()
  1018. def cmd_init(args: list) -> None:
  1019. """Initialize a new Git repository in Swift.
  1020. Args:
  1021. args: Command line arguments
  1022. """
  1023. import optparse
  1024. parser = optparse.OptionParser()
  1025. parser.add_option(
  1026. "-c",
  1027. "--swift_config",
  1028. dest="swift_config",
  1029. default="",
  1030. help="Path to the configuration file for Swift backend.",
  1031. )
  1032. options, args = parser.parse_args(args)
  1033. conf = load_conf(options.swift_config)
  1034. if args == []:
  1035. parser.error("missing repository name")
  1036. repo = args[0]
  1037. scon = SwiftConnector(repo, conf)
  1038. SwiftRepo.init_bare(scon, conf)
  1039. def main(argv: list = sys.argv) -> None:
  1040. """Main entry point for Swift Git command line interface.
  1041. Args:
  1042. argv: Command line arguments
  1043. """
  1044. commands = {
  1045. "init": cmd_init,
  1046. "daemon": cmd_daemon,
  1047. }
  1048. if len(sys.argv) < 2:
  1049. print(
  1050. "Usage: {} <{}> [OPTIONS...]".format(sys.argv[0], "|".join(commands.keys()))
  1051. )
  1052. sys.exit(1)
  1053. cmd = sys.argv[1]
  1054. if cmd not in commands:
  1055. print(f"No such subcommand: {cmd}")
  1056. sys.exit(1)
  1057. commands[cmd](sys.argv[2:])
  1058. if __name__ == "__main__":
  1059. main()