• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11
12
13class InfraApi(recipe_api.RecipeApi):
14  @property
15  def goroot(self):
16    return self.m.vars.slave_dir.join('go', 'go')
17
18  @property
19  def go_bin(self):
20    return self.goroot.join('bin')
21
22  @property
23  def go_exe(self):
24    return self.go_bin.join('go')
25
26  @property
27  def go_env(self):
28    return {
29        'GOPATH': self.gopath,
30        'GOROOT': self.goroot,
31        'PATH': '%s:%s:%%(PATH)s' % (self.go_bin, self.gopath),
32    }
33
34  @property
35  def gopath(self):
36    return self.m.vars.slave_dir.join('gopath')
37
38  def go_version(self):
39    """Print the Go version."""
40    env = self.m.context.env
41    env.update(self.go_env)
42    with self.m.context(env=env):
43      self.m.run(
44          self.m.step,
45          'go version',
46          cmd=[self.go_exe, 'version'])
47      self.m.run(
48          self.m.step,
49          'env go version',
50          cmd=['go', 'version'])
51
52  def update_go_deps(self):
53    """Attempt to update go dependencies.
54
55    This fails flakily sometimes, so perform multiple attempts.
56    """
57    self.go_version()
58    env = self.m.context.env
59    env.update(self.go_env)
60    with self.m.context(env=env):
61      self.m.run.with_retry(
62          self.m.step,
63          'update go pkgs',
64          UPDATE_GO_ATTEMPTS,
65          cmd=[self.go_exe, 'get', '-u', '-t', '%s/...' % INFRA_GO_PKG])
66
67  class MetadataFetch():
68    def __init__(self, api, metadata_key, local_file, **kwargs):
69      self.m = api
70      self._key = metadata_key
71      self._local_file = local_file
72
73    def __enter__(self):
74      return self.m.python.inline(
75          'download ' + self._local_file,
76        """
77import os
78import urllib2
79
80TOKEN_FILE = '%s'
81TOKEN_URL = 'http://metadata/computeMetadata/v1/project/attributes/%s'
82
83req = urllib2.Request(TOKEN_URL, headers={'Metadata-Flavor': 'Google'})
84contents = urllib2.urlopen(req).read()
85
86home = os.path.expanduser('~')
87token_file = os.path.join(home, TOKEN_FILE)
88
89with open(token_file, 'w') as f:
90  f.write(contents)
91        """ % (self._local_file, self._key),
92      )
93
94    def __exit__(self, t, v, tb):
95      self.m.python.inline(
96          'cleanup ' + self._local_file,
97        """
98import os
99
100
101TOKEN_FILE = '%s'
102
103
104home = os.path.expanduser('~')
105token_file = os.path.join(home, TOKEN_FILE)
106if os.path.isfile(token_file):
107  os.remove(token_file)
108          """ % (self._local_file),
109      )
110      return v is None
111