1# Copyright 2016 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5 6from recipe_engine import recipe_api 7 8 9INFRA_GO_PKG = 'go.skia.org/infra' 10UPDATE_GO_ATTEMPTS = 5 11UPLOAD_ATTEMPTS = 5 12 13 14class InfraApi(recipe_api.RecipeApi): 15 @property 16 def goroot(self): 17 return self.m.vars.slave_dir.join('go', 'go') 18 19 @property 20 def go_bin(self): 21 return self.goroot.join('bin') 22 23 @property 24 def go_exe(self): 25 return self.go_bin.join('go') 26 27 @property 28 def go_env(self): 29 return { 30 'GOPATH': self.gopath, 31 'GOROOT': self.goroot, 32 'PATH': '%s:%s:%%(PATH)s' % (self.go_bin, self.gopath), 33 } 34 35 @property 36 def gopath(self): 37 return self.m.vars.slave_dir.join('gopath') 38 39 def go_version(self): 40 """Print the Go version.""" 41 env = self.m.context.env 42 env.update(self.go_env) 43 with self.m.context(env=env): 44 self.m.run( 45 self.m.step, 46 'go version', 47 cmd=[self.go_exe, 'version']) 48 self.m.run( 49 self.m.step, 50 'env go version', 51 cmd=['go', 'version']) 52 53 def update_go_deps(self): 54 """Attempt to update go dependencies. 55 56 This fails flakily sometimes, so perform multiple attempts. 57 """ 58 self.go_version() 59 env = self.m.context.env 60 env.update(self.go_env) 61 with self.m.context(env=env): 62 self.m.run.with_retry( 63 self.m.step, 64 'update go pkgs', 65 UPDATE_GO_ATTEMPTS, 66 cmd=[self.go_exe, 'get', '-u', '-t', '%s/...' % INFRA_GO_PKG]) 67 68 class DownloadGitCookies(object): 69 """Class to download gitcookies from GS.""" 70 def __init__(self, gs_path, local_path, api): 71 self._gs_path = gs_path 72 self._local_path = local_path 73 self._api = api 74 75 def __enter__(self): 76 cmd = ['gsutil', 'cp', self._gs_path, self._local_path] 77 self._api.step('download gitcookies', cmd=cmd, infra_step=True) 78 79 def __exit__(self, exc_type, _value, _traceback): 80 if self._api.path.exists(self._local_path): 81 self._api.file.remove('remove %s' % self._local_path, self._local_path) 82 83 class MetadataFetch(): 84 def __init__(self, api, metadata_key, local_file, **kwargs): 85 self.m = api 86 self._key = metadata_key 87 self._local_file = local_file 88 89 def __enter__(self): 90 return self.m.python.inline( 91 'download ' + self._local_file, 92 """ 93import os 94import urllib2 95 96TOKEN_FILE = '%s' 97TOKEN_URL = 'http://metadata/computeMetadata/v1/project/attributes/%s' 98 99req = urllib2.Request(TOKEN_URL, headers={'Metadata-Flavor': 'Google'}) 100contents = urllib2.urlopen(req).read() 101 102home = os.path.expanduser('~') 103token_file = os.path.join(home, TOKEN_FILE) 104 105with open(token_file, 'w') as f: 106 f.write(contents) 107 """ % (self._local_file, self._key), 108 ) 109 110 def __exit__(self, t, v, tb): 111 self.m.python.inline( 112 'cleanup ' + self._local_file, 113 """ 114import os 115 116 117TOKEN_FILE = '%s' 118 119 120home = os.path.expanduser('~') 121token_file = os.path.join(home, TOKEN_FILE) 122if os.path.isfile(token_file): 123 os.remove(token_file) 124 """ % (self._local_file), 125 ) 126 return v is None 127