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 |
| 62 step.presentation.step_text = ( |
| 63 'cipd instance_id: %s' % step.json.output['instance_id']) |
| 64 return step |
| 65 |
| 66 def get_executable(self): |
| 67 return self._cipd_executable |
| 68 |
| 69 def build(self, input_dir, output_package, package_name, install_mode=None): |
| 70 assert self._cipd_executable |
| 71 assert not install_mode or install_mode in ['copy', 'symlink'] |
| 72 return self.m.step( |
| 73 'build %s' % self.m.path.basename(package_name), |
| 74 [ |
| 75 self._cipd_executable, |
| 76 'pkg-build', |
| 77 '--in', input_dir, |
| 78 '--name', package_name, |
| 79 '--out', output_package, |
| 80 '--json-output', self.m.json.output(), |
| 81 ] + ( |
| 82 ['--install-mode', install_mode] if install_mode else [] |
| 83 ), |
| 84 step_test_data=lambda: self.test_api.example_build(package_name) |
| 85 ) |
| 86 |
| 87 def register(self, package_name, package_path, refs=None, tags=None): |
| 88 assert self._cipd_executable |
| 89 |
| 90 cmd = [ |
| 91 self._cipd_executable, |
| 92 'pkg-register', package_path, |
| 93 '--json-output', self.m.json.output(), |
| 94 ] |
| 95 if self._cipd_credentials: |
| 96 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 97 if refs: |
| 98 for ref in refs: |
| 99 cmd.extend(['--ref', ref]) |
| 100 if tags: |
| 101 for tag, value in sorted(tags.items()): |
| 102 cmd.extend(['--tag', '%s:%s' % (tag, value)]) |
| 103 return self.m.step( |
| 104 'register %s' % package_name, |
| 105 cmd, |
| 106 step_test_data=lambda: self.test_api.example_register(package_name) |
| 107 ) |
| 108 |
| 109 def create(self, pkg_def, refs=None, tags=None): |
| 110 """Creates a package based on YAML package definition file. |
| 111 |
| 112 This builds and uploads the package in one step. |
| 113 """ |
| 114 assert self._cipd_executable |
| 115 cmd = [ |
| 116 self._cipd_executable, |
| 117 'create', |
| 118 '--pkg-def', pkg_def, |
| 119 '--json-output', self.m.json.output(), |
| 120 ] |
| 121 if self._cipd_credentials: |
| 122 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 123 if refs: |
| 124 for ref in refs: |
| 125 cmd.extend(['--ref', ref]) |
| 126 if tags: |
| 127 for tag, value in sorted(tags.items()): |
| 128 cmd.extend(['--tag', '%s:%s' % (tag, value)]) |
| 129 return self.m.step('create %s' % self.m.path.basename(pkg_def), cmd) |
| 130 |
| 131 def ensure(self, root, packages): |
| 132 """Ensures that packages are installed in a given root dir. |
| 133 |
| 134 packages must be a mapping from package name to its version, where |
| 135 * name must be for right platform (see also ``platform_suffix``), |
| 136 * version could be either instance_id, or ref, or unique tag. |
| 137 |
| 138 If installing a package requires credentials, call |
| 139 ``set_service_account_credentials`` before calling this function. |
| 140 """ |
| 141 assert self._cipd_executable |
| 142 |
| 143 package_list = ['%s %s' % (name, version) |
| 144 for name, version in sorted(packages.items())] |
| 145 list_data = self.m.raw_io.input('\n'.join(package_list)) |
| 146 cmd = [ |
| 147 self._cipd_executable, |
| 148 'ensure', |
| 149 '--root', root, |
| 150 '--list', list_data, |
| 151 '--json-output', self.m.json.output(), |
| 152 ] |
| 153 if self._cipd_credentials: |
| 154 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 155 return self.m.step( |
| 156 'ensure_installed', cmd, |
| 157 step_test_data=lambda: self.test_api.example_ensure(packages) |
| 158 ) |
| 159 |
| 160 def set_tag(self, package_name, version, tags): |
| 161 assert self._cipd_executable |
| 162 |
| 163 cmd = [ |
| 164 self._cipd_executable, |
| 165 'set-tag', package_name, |
| 166 '--version', version, |
| 167 '--json-output', self.m.json.output(), |
| 168 ] |
| 169 if self._cipd_credentials: |
| 170 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 171 for tag, value in sorted(tags.items()): |
| 172 cmd.extend(['--tag', '%s:%s' % (tag, value)]) |
| 173 |
| 174 return self.m.step( |
| 175 'cipd set-tag %s' % package_name, |
| 176 cmd, |
| 177 step_test_data=lambda: self.test_api.example_set_tag( |
| 178 package_name, version |
| 179 ) |
| 180 ) |
| 181 |
| 182 def set_ref(self, package_name, version, refs): |
| 183 assert self._cipd_executable |
| 184 |
| 185 cmd = [ |
| 186 self._cipd_executable, |
| 187 'set-ref', package_name, |
| 188 '--version', version, |
| 189 '--json-output', self.m.json.output(), |
| 190 ] |
| 191 if self._cipd_credentials: |
| 192 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 193 for r in refs: |
| 194 cmd.extend(['--ref', r]) |
| 195 |
| 196 return self.m.step( |
| 197 'cipd set-ref %s' % package_name, |
| 198 cmd, |
| 199 step_test_data=lambda: self.test_api.example_set_ref( |
| 200 package_name, version |
| 201 ) |
| 202 ) |
| 203 |
| 204 def search(self, package_name, tag): |
| 205 assert self._cipd_executable |
| 206 assert ':' in tag, 'tag must be in a form "k:v"' |
| 207 |
| 208 cmd = [ |
| 209 self._cipd_executable, |
| 210 'search', package_name, |
| 211 '--tag', tag, |
| 212 '--json-output', self.m.json.output(), |
| 213 ] |
| 214 if self._cipd_credentials: |
| 215 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 216 |
| 217 return self.m.step( |
| 218 'cipd search %s %s' % (package_name, tag), |
| 219 cmd, |
| 220 step_test_data=lambda: self.test_api.example_search(package_name) |
| 221 ) |
| 222 |
| 223 def describe(self, package_name, version, |
| 224 test_data_refs=None, test_data_tags=None): |
| 225 assert self._cipd_executable |
| 226 |
| 227 cmd = [ |
| 228 self._cipd_executable, |
| 229 'describe', package_name, |
| 230 '--version', version, |
| 231 '--json-output', self.m.json.output(), |
| 232 ] |
| 233 if self._cipd_credentials: |
| 234 cmd.extend(['--service-account-json', self._cipd_credentials]) |
| 235 |
| 236 return self.m.step( |
| 237 'cipd describe %s' % package_name, |
| 238 cmd, |
| 239 step_test_data=lambda: self.test_api.example_describe( |
| 240 package_name, version, |
| 241 test_data_refs=test_data_refs, |
| 242 test_data_tags=test_data_tags |
| 243 ) |
| 244 ) |
OLD | NEW |