swift.py 41 KB

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