OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 from recipe_engine import recipe_api | |
6 | |
7 | |
8 class CIPDApi(recipe_api.RecipeApi): | |
9 """CIPDApi provides support for CIPD.""" | |
10 def __init__(self, *args, **kwargs): | |
11 super(CIPDApi, self).__init__(*args, **kwargs) | |
12 self._cipd_executable = None | |
13 self._cipd_version = None | |
14 self._cipd_credentials = None | |
15 | |
16 def set_service_account_credentials(self, path): | |
17 self._cipd_credentials = path | |
18 | |
19 @property | |
20 def default_bot_service_account_credentials(self): | |
21 # Path to a service account credentials to use to talk to CIPD backend. | |
22 # Deployed by Puppet. | |
23 if self.m.platform.is_win: | |
24 return 'C:\\creds\\service_accounts\\service-account-cipd-builder.json' | |
25 else: | |
26 return '/creds/service_accounts/service-account-cipd-builder.json' | |
27 | |
28 def platform_suffix(self): | |
29 """Use to get full package name that is platform indepdent. | |
30 | |
31 Example: | |
32 >>> 'my/package/%s' % api.cipd.platform_suffix() | |
33 'my/package/linux-amd64' | |
34 """ | |
35 return '%s-%s' % ( | |
36 self.m.platform.name.replace('win', 'windows'), | |
37 { | |
38 32: '386', | |
39 64: 'amd64', | |
40 }[self.m.platform.bits], | |
41 ) | |
42 | |
43 def install_client(self, step_name='install cipd', version=None): | |
44 """Ensures the client is installed. | |
45 | |
46 If you specify version as a hash, make sure its correct platform. | |
47 """ | |
48 # TODO(seanmccullough): clean up older CIPD installations. | |
49 step = self.m.python( | |
50 name=step_name, | |
51 script=self.resource('bootstrap.py'), | |
52 args=[ | |
53 '--platform', self.platform_suffix(), | |
54 '--dest-directory', self.m.path['slave_build'].join('cipd'), | |
55 '--json-output', self.m.json.output(), | |
56 ] + | |
57 (['--version', version] if version else []), | |
58 step_test_data=lambda: self.test_api.example_install_client(version) | |
59 ) | |
60 self._cipd_executable = step.json.output['executable'] | |
61 self._cipd_instance_id = step.json.output['instance_id'] | |
62 | |
63 step.presentation.step_text = ( | |
64 'cipd instance_id: %s' % self._cipd_instance_id) | |
65 return step | |
66 | |
67 def get_executable(self): | |
68 return self._cipd_executable | |
69 | |
70 def build(self, input_dir, output_package, package_name, install_mode=None): | |
71 assert self._cipd_executable | |
72 assert not install_mode or install_mode in ['copy', 'symlink'] | |
73 return self.m.step( | |
74 'build %s' % self.m.path.basename(package_name), | |
75 [ | |
76 self._cipd_executable, | |
77 'pkg-build', | |
78 '--in', input_dir, | |
79 '--name', package_name, | |
80 '--out', output_package, | |
81 '--json-output', self.m.json.output(), | |
82 ] + ( | |
83 ['--install-mode', install_mode] if install_mode else [] | |
84 ), | |
85 step_test_data=lambda: self.test_api.example_build(package_name) | |
86 ) | |
87 | |
88 def register(self, package_name, package_path, refs=None, tags=None): | |
89 assert self._cipd_executable | |
90 | |
91 cmd = [ | |
92 self._cipd_executable, | |
93 'pkg-register', package_path, | |
94 '--json-output', self.m.json.output(), | |
95 ] | |
96 if self._cipd_credentials: | |
97 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
98 if refs: | |
99 for ref in refs: | |
100 cmd.extend(['--ref', ref]) | |
101 if tags: | |
102 for tag, value in sorted(tags.items()): | |
103 cmd.extend(['--tag', '%s:%s' % (tag, value)]) | |
104 return self.m.step( | |
105 'register %s' % package_name, | |
106 cmd, | |
107 step_test_data=lambda: self.test_api.example_register(package_name) | |
108 ) | |
109 | |
110 def create(self, pkg_def, refs=None, tags=None): | |
111 """Creates a package based on YAML package definition file. | |
112 | |
113 This builds and uploads the package in one step. | |
114 """ | |
115 assert self._cipd_executable | |
116 cmd = [ | |
117 self._cipd_executable, | |
118 'create', | |
119 '--pkg-def', pkg_def, | |
120 '--json-output', self.m.json.output(), | |
121 ] | |
122 if self._cipd_credentials: | |
123 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
124 if refs: | |
125 for ref in refs: | |
126 cmd.extend(['--ref', ref]) | |
127 if tags: | |
128 for tag, value in sorted(tags.items()): | |
129 cmd.extend(['--tag', '%s:%s' % (tag, value)]) | |
130 return self.m.step('create %s' % self.m.path.basename(pkg_def), cmd) | |
131 | |
132 def ensure(self, root, packages): | |
133 """Ensures that packages are installed in a given root dir. | |
134 | |
135 packages must be a mapping from package name to its version, where | |
136 * name must be for right platform (see also ``platform_suffix``), | |
137 * version could be either instance_id, or ref, or unique tag. | |
138 | |
139 If installing a package requires credentials, call | |
140 ``set_service_account_credentials`` before calling this function. | |
141 """ | |
142 assert self._cipd_executable | |
143 | |
144 package_list = ['%s %s' % (name, version) | |
145 for name, version in sorted(packages.items())] | |
146 list_data = self.m.raw_io.input('\n'.join(package_list)) | |
147 bin_path = self.m.path['slave_build'].join('cipd') | |
148 cmd = [ | |
149 self._cipd_executable, | |
150 'ensure', | |
151 '--root', root, | |
152 '--list', list_data, | |
153 '--json-output', self.m.json.output(), | |
154 ] | |
155 if self._cipd_credentials: | |
156 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
157 return self.m.step( | |
158 'ensure_installed', cmd, | |
159 step_test_data=lambda: self.test_api.example_ensure(packages) | |
160 ) | |
161 | |
162 def set_tag(self, package_name, version, tags): | |
163 assert self._cipd_executable | |
164 | |
165 cmd = [ | |
166 self._cipd_executable, | |
167 'set-tag', package_name, | |
168 '--version', version, | |
169 '--json-output', self.m.json.output(), | |
170 ] | |
171 if self._cipd_credentials: | |
172 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
173 for tag, value in sorted(tags.items()): | |
174 cmd.extend(['--tag', '%s:%s' % (tag, value)]) | |
175 | |
176 return self.m.step( | |
177 'cipd set-tag %s' % package_name, | |
178 cmd, | |
179 step_test_data=lambda: self.test_api.example_set_tag( | |
180 package_name, version | |
181 ) | |
182 ) | |
183 | |
184 def set_ref(self, package_name, version, refs): | |
185 assert self._cipd_executable | |
186 | |
187 cmd = [ | |
188 self._cipd_executable, | |
189 'set-ref', package_name, | |
190 '--version', version, | |
191 '--json-output', self.m.json.output(), | |
192 ] | |
193 if self._cipd_credentials: | |
194 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
195 for r in refs: | |
196 cmd.extend(['--ref', r]) | |
197 | |
198 return self.m.step( | |
199 'cipd set-ref %s' % package_name, | |
200 cmd, | |
201 step_test_data=lambda: self.test_api.example_set_ref( | |
202 package_name, version | |
203 ) | |
204 ) | |
205 | |
206 def search(self, package_name, tag): | |
207 assert self._cipd_executable | |
208 assert ':' in tag, 'tag must be in a form "k:v"' | |
209 | |
210 cmd = [ | |
211 self._cipd_executable, | |
212 'search', package_name, | |
213 '--tag', tag, | |
214 '--json-output', self.m.json.output(), | |
215 ] | |
216 if self._cipd_credentials: | |
217 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
218 | |
219 return self.m.step( | |
220 'cipd search %s %s' % (package_name, tag), | |
221 cmd, | |
222 step_test_data=lambda: self.test_api.example_search(package_name) | |
223 ) | |
224 | |
225 def describe(self, package_name, version, | |
226 test_data_refs=None, test_data_tags=None): | |
227 assert self._cipd_executable | |
228 | |
229 cmd = [ | |
230 self._cipd_executable, | |
231 'describe', package_name, | |
232 '--version', version, | |
233 '--json-output', self.m.json.output(), | |
234 ] | |
235 if self._cipd_credentials: | |
236 cmd.extend(['--service-account-json', self._cipd_credentials]) | |
237 | |
238 return self.m.step( | |
239 'cipd describe %s' % package_name, | |
240 cmd, | |
241 step_test_data=lambda: self.test_api.example_describe( | |
242 package_name, version, | |
243 test_data_refs=test_data_refs, | |
244 test_data_tags=test_data_tags | |
245 ) | |
246 ) | |
OLD | NEW |