swift.py 32 KB

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