1# Copyright 2021 - The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""A client that talks to Oxygen proxy APIs.""" 16 17import logging 18import shlex 19import subprocess 20 21 22logger = logging.getLogger(__name__) 23 24 25class OxygenClient(): 26 """Client that manages Oxygen proxy api.""" 27 28 @staticmethod 29 def LeaseDevice(build_target, build_id, build_branch, system_build_target, 30 system_build_id, kernel_build_target, kernel_build_id, 31 oxygen_client, cmd_args): 32 """Lease one cuttlefish device. 33 34 Args: 35 build_target: Target name, e.g. "aosp_cf_x86_64_phone-userdebug" 36 build_id: Build ID, a string, e.g. "2263051", "P2804227" 37 build_branch: Build branch, e.g. "aosp-master" 38 system_build_target: Target name of system build 39 system_build_id: Build ID of system build 40 kernel_build_target: Target name of kernel build 41 kernel_build_id: Build ID of kernel build 42 oxygen_client: String of oxygen client path. 43 cmd_args: String of lease command args. e.g. "-user user_mail" 44 45 Returns: 46 The response of calling oxygen proxy client. 47 """ 48 cmd = [oxygen_client, "-lease", "-build_id", build_id, "-build_target", 49 build_target, "-build_branch", build_branch] 50 if cmd_args: 51 cmd.extend(shlex.split(cmd_args)) 52 if system_build_id: 53 cmd.extend(["-system_build_id", system_build_id]) 54 cmd.extend(["-system_build_target", system_build_target]) 55 if kernel_build_id: 56 cmd.extend(["-kernel_build_id", kernel_build_id]) 57 cmd.extend(["-kernel_build_target", kernel_build_target]) 58 logger.debug("Command to oxygen client: %s", cmd) 59 response = subprocess.check_output( 60 cmd, stderr=subprocess.STDOUT, encoding='utf-8') 61 logger.debug("The response from oxygen client: %s", response) 62 return response 63 64 @staticmethod 65 def ReleaseDevice(session_id, server_url, oxygen_client): 66 """Release one cuttlefish device. 67 68 Args: 69 session_id: String of session id. 70 server_url: String of server url. 71 oxygen_client: String of oxygen client path. 72 """ 73 response = subprocess.check_output([ 74 oxygen_client, "-release", "-session_id", session_id, 75 "-server_url", server_url 76 ], stderr=subprocess.STDOUT, encoding='utf-8') 77 logger.debug("The response from oxygen client: %s", response) 78