2
0

swift.py 40 KB

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