| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Package cipd implements client side of Chrome Infra Package Deployer. | 5 // Package cipd implements client side of Chrome Infra Package Deployer. |
| 6 // | 6 // |
| 7 // Binary package file format (in free form representation): | 7 // Binary package file format (in free form representation): |
| 8 // <binary package> := <zipped data> | 8 // <binary package> := <zipped data> |
| 9 // <zipped data> := DeterministicZip(<all input files> + <manifest json>) | 9 // <zipped data> := DeterministicZip(<all input files> + <manifest json>) |
| 10 // <manifest json> := File{ | 10 // <manifest json> := File{ |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 | 312 |
| 313 // ClientOptions is passed to NewClient factory function. | 313 // ClientOptions is passed to NewClient factory function. |
| 314 type ClientOptions struct { | 314 type ClientOptions struct { |
| 315 // ServiceURL is root URL of the backend service. | 315 // ServiceURL is root URL of the backend service. |
| 316 ServiceURL string | 316 ServiceURL string |
| 317 | 317 |
| 318 // Root is a site root directory (a directory where packages will be | 318 // Root is a site root directory (a directory where packages will be |
| 319 // installed to). It also hosts .cipd/* directory that tracks internal s
tate | 319 // installed to). It also hosts .cipd/* directory that tracks internal s
tate |
| 320 // of installed packages and keeps various cache files. 'Root' can be an
empty | 320 // of installed packages and keeps various cache files. 'Root' can be an
empty |
| 321 // string if the client is not going to be used to deploy or remove loca
l | 321 // string if the client is not going to be used to deploy or remove loca
l |
| 322 » // packages. In that case caches are also disabled. | 322 » // packages. If both Root and CacheDir are empty, tag cache is disabled. |
| 323 Root string | 323 Root string |
| 324 | 324 |
| 325 // Logger is a logger to use for logs (null-logger by default). | 325 // Logger is a logger to use for logs (null-logger by default). |
| 326 Logger logging.Logger | 326 Logger logging.Logger |
| 327 | 327 |
| 328 // AuthenticatedClientFactory lazily creates http.Client to use for maki
ng | 328 // AuthenticatedClientFactory lazily creates http.Client to use for maki
ng |
| 329 // RPC requests. | 329 // RPC requests. |
| 330 AuthenticatedClientFactory HTTPClientFactory | 330 AuthenticatedClientFactory HTTPClientFactory |
| 331 | 331 |
| 332 // AnonymousClientFactory lazily creates http.Client to use for making | 332 // AnonymousClientFactory lazily creates http.Client to use for making |
| 333 // requests to storage. | 333 // requests to storage. |
| 334 AnonymousClientFactory HTTPClientFactory | 334 AnonymousClientFactory HTTPClientFactory |
| 335 | 335 |
| 336 // UserAgent is put into User-Agent HTTP header with each request. | 336 // UserAgent is put into User-Agent HTTP header with each request. |
| 337 UserAgent string | 337 UserAgent string |
| 338 |
| 339 // CacheDir is a directory for shared cache. If empty, tags are cached |
| 340 // inside the site root. If both Root and CacheDir are empty, tag cache |
| 341 // is disabled. |
| 342 CacheDir string |
| 338 } | 343 } |
| 339 | 344 |
| 340 // NewClient initializes CIPD client object. | 345 // NewClient initializes CIPD client object. |
| 341 func NewClient(opts ClientOptions) Client { | 346 func NewClient(opts ClientOptions) Client { |
| 342 if opts.ServiceURL == "" { | 347 if opts.ServiceURL == "" { |
| 343 opts.ServiceURL = ServiceURL | 348 opts.ServiceURL = ServiceURL |
| 344 } | 349 } |
| 345 if opts.Logger == nil { | 350 if opts.Logger == nil { |
| 346 opts.Logger = logging.Null() | 351 opts.Logger = logging.Null() |
| 347 } | 352 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 378 | 383 |
| 379 // storage knows how to upload and download raw binaries using signed UR
Ls. | 384 // storage knows how to upload and download raw binaries using signed UR
Ls. |
| 380 // Thread safe. | 385 // Thread safe. |
| 381 storage storage | 386 storage storage |
| 382 | 387 |
| 383 // deployer knows how to install packages to local file system. Thread s
afe. | 388 // deployer knows how to install packages to local file system. Thread s
afe. |
| 384 deployer local.Deployer | 389 deployer local.Deployer |
| 385 | 390 |
| 386 // tagCache is used to cache (pkgname, tag) -> instanceID mapping. | 391 // tagCache is used to cache (pkgname, tag) -> instanceID mapping. |
| 387 // Thread safe, but lazily initialized under lock. | 392 // Thread safe, but lazily initialized under lock. |
| 388 » tagCache *internal.TagCache | 393 » tagCache *internal.TagCache |
| 394 » tagCacheInit sync.Once |
| 389 | 395 |
| 390 // authClient is a lazily created http.Client to use for authenticated | 396 // authClient is a lazily created http.Client to use for authenticated |
| 391 // requests. Thread safe, but lazily initialized under lock. | 397 // requests. Thread safe, but lazily initialized under lock. |
| 392 authClient *http.Client | 398 authClient *http.Client |
| 393 | 399 |
| 394 // anonClient is a lazily created http.Client to use for anonymous reque
sts. | 400 // anonClient is a lazily created http.Client to use for anonymous reque
sts. |
| 395 // Thread safe, but lazily initialized under lock. | 401 // Thread safe, but lazily initialized under lock. |
| 396 anonClient *http.Client | 402 anonClient *http.Client |
| 397 } | 403 } |
| 398 | 404 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 417 *c, err = fac() | 423 *c, err = fac() |
| 418 } | 424 } |
| 419 return *c, err | 425 return *c, err |
| 420 }() | 426 }() |
| 421 if err != nil { | 427 if err != nil { |
| 422 return nil, err | 428 return nil, err |
| 423 } | 429 } |
| 424 return httpClient.Do(req) | 430 return httpClient.Do(req) |
| 425 } | 431 } |
| 426 | 432 |
| 427 // tagCachePath returns path to a tag cache file or "" if no root dir. | 433 // tagCachePath returns path to a tag cache file or "" if tag cache is disabled. |
| 428 func (client *clientImpl) tagCachePath() string { | 434 func (client *clientImpl) tagCachePath() string { |
| 429 » if client.Root == "" { | 435 » var dir string |
| 436 » switch { |
| 437 » case client.CacheDir != "": |
| 438 » » dir = client.CacheDir |
| 439 |
| 440 » case client.Root != "": |
| 441 » » dir = filepath.Join(client.Root, local.SiteServiceDir) |
| 442 |
| 443 » default: |
| 430 return "" | 444 return "" |
| 431 } | 445 } |
| 432 » return filepath.Join(client.Root, local.SiteServiceDir, "tagcache.db") | 446 |
| 447 » return filepath.Join(dir, "tagcache.db") |
| 433 } | 448 } |
| 434 | 449 |
| 435 // getTagCache lazy-initializes tagCache instance and returns it. | 450 // getTagCache lazy-initializes tagCache instance and returns it. |
| 436 func (client *clientImpl) getTagCache() *internal.TagCache { | 451 func (client *clientImpl) getTagCache() *internal.TagCache { |
| 437 » client.lock.Lock() | 452 » client.tagCacheInit.Do(func() { |
| 438 » defer client.lock.Unlock() | |
| 439 » if client.tagCache == nil { | |
| 440 if path := client.tagCachePath(); path != "" { | 453 if path := client.tagCachePath(); path != "" { |
| 441 var err error | 454 var err error |
| 442 client.tagCache, err = internal.LoadTagCacheFromFile(pat
h) | 455 client.tagCache, err = internal.LoadTagCacheFromFile(pat
h) |
| 443 if err != nil { | 456 if err != nil { |
| 444 client.Logger.Warningf("cipd: failed to load tag
cache - %s", err) | 457 client.Logger.Warningf("cipd: failed to load tag
cache - %s", err) |
| 445 } | 458 } |
| 446 } | 459 } |
| 447 if client.tagCache == nil { | 460 if client.tagCache == nil { |
| 448 client.tagCache = &internal.TagCache{} | 461 client.tagCache = &internal.TagCache{} |
| 449 } | 462 } |
| 450 » } | 463 » }) |
| 451 return client.tagCache | 464 return client.tagCache |
| 452 } | 465 } |
| 453 | 466 |
| 454 // closeTagCache dumps any changes made to tag cache to disk, if necessary. | 467 // closeTagCache dumps any changes made to tag cache to disk, if necessary. |
| 455 // Must be called under lock. | 468 // Must be called under lock. |
| 456 func (client *clientImpl) closeTagCache() { | 469 func (client *clientImpl) closeTagCache() { |
| 457 path := client.tagCachePath() | 470 path := client.tagCachePath() |
| 458 if client.tagCache == nil || path == "" || !client.tagCache.Dirty() { | 471 if client.tagCache == nil || path == "" || !client.tagCache.Dirty() { |
| 459 client.tagCache = nil | 472 client.tagCache = nil |
| 460 return | 473 return |
| (...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 } | 1032 } |
| 1020 | 1033 |
| 1021 // buildInstanceIDMap builds mapping {package name -> instance ID}. | 1034 // buildInstanceIDMap builds mapping {package name -> instance ID}. |
| 1022 func buildInstanceIDMap(pins []common.Pin) map[string]string { | 1035 func buildInstanceIDMap(pins []common.Pin) map[string]string { |
| 1023 out := map[string]string{} | 1036 out := map[string]string{} |
| 1024 for _, p := range pins { | 1037 for _, p := range pins { |
| 1025 out[p.PackageName] = p.InstanceID | 1038 out[p.PackageName] = p.InstanceID |
| 1026 } | 1039 } |
| 1027 return out | 1040 return out |
| 1028 } | 1041 } |
| OLD | NEW |