def screenshot(): path = PATH("%s/screenshot" % os.getcwd()) utils.shell("screencap -p /data/local/tmp/tmp.png").wait() if not os.path.isdir(path): os.makedirs(path) utils.adb("pull /data/local/tmp/tmp.png %s" % PATH("%s/%s.png" % (path, utils.timestamp()))).wait() utils.shell("rm /data/local/tmp/tmp.png")
def terminate_instances(self, parameters): """ Stop one of more EC2 instances using the ec2-terminate-instance command. The input instance IDs are fetched from the 'instance_ids' parameters in the input map. (Also see documentation for the BaseAgent class) Args: parameters A dictionary of parameters """ instance_ids = parameters[self.PARAM_INSTANCE_IDS] arg = ' '.join(instance_ids) utils.shell('{0}-terminate-instances {1} 2>&1'.format(self.prefix, arg))
def record(): utils.shell("screenrecord /data/local/tmp/video.mp4") input_key = raw_input("Please press the Enter key to stop recording:\n") if input_key == "": utils.adb("kill-server") print "Get Video file..." utils.adb("start-server") time.sleep(1.5) path = PATH("%s/video" % os.getcwd()) if not os.path.isdir(path): os.makedirs(path) utils.adb("pull /data/local/tmp/video.mp4 %s" % PATH("%s/%s.mp4" % (path, utils.timestamp()))).wait()
def start_dump_latency_data(self, ignore_pending_fence_time=False): results = [] if self.__clear_buffer(): time.sleep(self.dump_time) results = utils.shell("{0} {1}".format(self.FRAME_LATENCY_CMD, self.activity_name))\ .stdout.readlines() self.refresh_period = int(results[0].strip()) if self.refresh_period < 0: return False data_invalid_flag = False for line in results: if not line.strip(): break if len(line.split()) == 1 or line.split()[0] == "0": continue elif line.split()[1] == str(self.PENDING_FENCE_TIME): if ignore_pending_fence_time: data_invalid_flag = True else: return False self.frame_buffer_data.append(line.split()) if not data_invalid_flag: self.frame_latency_data_size += 1 return True
def get_match_apk(package_name, path): list = [] for packages in utils.shell("pm list packages -f %s" %package_name).stdout.readlines(): if packages.split(":")[0] == "package": list.append(packages.split(":")[-1].split("=")[0]) utils.adb("pull %s %s" % (list[0], path)).wait()
def get_crash_log(time_list): log_file = PATH("%s/crash_log/%s.txt" % (os.getcwd(), utils.timestamp())) f = open(log_file, "w") for time in time_list: cash_log = utils.shell("dumpsys dropbox --print %s" % time).stdout.read() f.write(cash_log) f.close()
def get_match_apk(package_name): list = [] for packages in utils.shell("pm list packages -f %s" %package_name).stdout.readlines(): list.append(packages.split(":")[-1].split("=")[0]) apk_name = list[0].split("/")[-1] utils.adb("pull %s %s" % (list[0], tempFile)).wait() return PATH("%s/%s" %(tempFile, apk_name))
def get_permission_list(package_name): permission_list = [] result_list = utils.shell( "dumpsys package %s | %s android.permission" % (package_name, utils.find_util)).stdout.readlines() for permission in result_list: permission_list.append(permission.strip()) return permission_list
def get_crash_time_list(): time_list = [] result_list = utils.shell("dumpsys dropbox | %s data_app_crash" % utils.find_util).stdout.readlines() for time in result_list: temp_list = time.split(" ") temp_time = [] temp_time.append(temp_list[0]) temp_time.append(temp_list[1]) time_list.append(" ".join(temp_time)) return time_list
def top(): cpu = [] mem = [] top_info = utils.shell("top -n %s | %s %s$" % (str(times), utils.find_util, pkg_name)).stdout.readlines() # PID PR CPU% S #THR VSS RSS PCY UID Name for info in top_info: #temp_list = del_space(info) temp_list = info.split() cpu.append(temp_list[2]) mem.append(temp_list[6]) return (cpu, mem)
def describe_instances(self, parameters): """ Execute the ec2-describe-instances command and returns a summary of the already running EC2 instances. (Also see documentation for the BaseAgent class) Args: parameters A dictionary containing the 'keyname' parameter Returns: A tuple of the form (public_ips, private_ips, instances) where each member is a list. """ keyname = parameters[self.PARAM_KEYNAME] describe_instances = utils.shell(self.prefix + '-describe-instances 2>&1') utils.log('describe-instances says {0}'.format(describe_instances)) fqdn_regex = re.compile('\s+({0})\s+({0})\s+running\s+{1}\s'.format(self.FQDN_REGEX, keyname)) instance_regex = re.compile('INSTANCE\s+(i-\w+)') all_ip_addresses = utils.flatten(fqdn_regex.findall(describe_instances)) instances = utils.flatten(instance_regex.findall(describe_instances)) public_ips, private_ips = self.get_ip_addresses(all_ip_addresses) return public_ips, private_ips, instances
def get_optimal_spot_price(self, instance_type): """ Returns the spot price for an EC2 instance of the specified instance type. ec2-describe-spot-price-history command is used to obtain a set of spot prices from EC2 and the returned value is computed by averaging all the returned values and incrementing it by extra 20%. Args: instance_type An EC2 instance type Returns: The estimated spot price for the specified instance type """ command = 'ec2-describe-spot-price-history -t {0} | grep \'Linux/UNIX\' | '\ 'awk \'{{print $2}}\''.format(instance_type) prices = utils.shell(command).split('\n') sum = 0.0 for price in prices: sum += float(price) average = sum / len(prices) plus_twenty = average * 1.20 utils.log('The average spot instance price for a {0} machine is {1}, '\ 'and 20% more is {2}'.format(instance_type, average, plus_twenty)) return plus_twenty
PATH = lambda p: os.path.abspath(p) def record(): utils.shell("screenrecord /data/local/tmp/video.mp4") input_key = raw_input("Please press the Enter key to stop recording:\n") if input_key == "": utils.adb("kill-server") print "Get Video file..." utils.adb("start-server") time.sleep(1.5) path = PATH("%s/video" % os.getcwd()) if not os.path.isdir(path): os.makedirs(path) utils.adb("pull /data/local/tmp/video.mp4 %s" % PATH("%s/%s.mp4" % (path, utils.timestamp()))).wait() if __name__ == "__main__": sdk = string.atoi( utils.shell("getprop ro.build.version.sdk").stdout.read()) if sdk < 19: print ">>> sdk version is %s, less than 19!" sys.exit(0) else: record() print "Completed"
def run_instances(self, count, parameters, security_configured): """ Spawn the specified number of EC2 instances using the parameters provided. This method relies on the ec2-run-instances command to spawn the actual VMs in the cloud. This method is blocking in that it waits until the requested VMs are properly booted up. However if the requested VMs cannot be procured within 1800 seconds, this method will treat it as an error and return. (Also see documentation for the BaseAgent class) Args: count No. of VMs to spawned parameters A dictionary of parameters. This must contain 'keyname', 'group', 'image_id' and 'instance_type' parameters. security_configured Uses this boolean value as an heuristic to detect brand new AppScale deployments. Returns: A tuple of the form (instances, public_ips, private_ips) """ image_id = parameters[self.PARAM_IMAGE_ID] instance_type = parameters[self.PARAM_INSTANCE_TYPE] keyname = parameters[self.PARAM_KEYNAME] group = parameters[self.PARAM_GROUP] spot = False utils.log('[{0}] [{1}] [{2}] [{3}] [ec2] [{4}] [{5}]'.format(count, image_id, instance_type, keyname, group, spot)) start_time = datetime.datetime.now() active_public_ips = [] active_private_ips = [] active_instances = [] if os.environ.has_key('EC2_URL'): utils.log('EC2_URL = [{0}]'.format(os.environ['EC2_URL'])) else: utils.log('Warning: EC2_URL environment not found in the process runtime!') while True: active_public_ips, active_private_ips, active_instances =\ self.describe_instances(parameters) # If security has been configured on this agent just now, # that's an indication that this is a fresh cloud deployment. # As such it's not expected to have any running VMs. if len(active_instances) > 0 or security_configured: break args = '-k {0} -n {1} --instance-type {2} --group {3} {4}'.format(keyname, count, instance_type, group, image_id) if spot: price = self.get_optimal_spot_price(instance_type) command_to_run = '{0}-request-spot-instances -p {1} {2}'.format(self.prefix, price, args) else: command_to_run = '{0}-run-instances {1}'.format(self.prefix, args) while True: run_instances = utils.shell(command_to_run) utils.log('Run instances says {0}'.format(run_instances)) status, command_to_run = self.run_instances_response(command_to_run, run_instances) if status: break utils.log('sleepy time') utils.sleep(5) instances = [] public_ips = [] private_ips = [] utils.sleep(10) end_time = datetime.datetime.now() + datetime.timedelta(0, self.MAX_VM_CREATION_TIME) now = datetime.datetime.now() while now < end_time: describe_instances = utils.shell(self.prefix + '-describe-instances 2>&1') utils.log('[{0}] {1} seconds left...'.format(now, (end_time - now).seconds)) utils.log(describe_instances) fqdn_regex = re.compile('\s+({0})\s+({0})\s+running\s+{1}\s'.format(self.FQDN_REGEX, keyname)) instance_regex = re.compile('INSTANCE\s+(i-\w+)') all_ip_addresses = utils.flatten(fqdn_regex.findall(describe_instances)) instances = utils.flatten(instance_regex.findall(describe_instances)) public_ips, private_ips = self.get_ip_addresses(all_ip_addresses) public_ips = utils.diff(public_ips, active_public_ips) private_ips = utils.diff(private_ips, active_private_ips) instances = utils.diff(instances, active_instances) if count == len(public_ips): break time.sleep(self.SLEEP_TIME) now = datetime.datetime.now() if not public_ips: sys.exit('No public IPs were able to be procured within the time limit') if len(public_ips) != count: for index in range(0, len(public_ips)): if public_ips[index] == '0.0.0.0': instance_to_term = instances[index] utils.log('Instance {0} failed to get a public IP address and is being terminated'.\ format(instance_to_term)) utils.shell(self.prefix + '-terminate-instances ' + instance_to_term) pass end_time = datetime.datetime.now() total_time = end_time - start_time if spot: utils.log('TIMING: It took {0} seconds to spawn {1} spot instances'.format( total_time.seconds, count)) else: utils.log('TIMING: It took {0} seconds to spawn {1} regular instances'.format( total_time.seconds, count)) return instances, public_ips, private_ips
def __clear_buffer(self): results = utils.shell("{0} {1}".format(self.CLEAR_BUFFER_CMD, self.activity_name))\ .stdout.readlines() return not len(results)
def get_apk_list(): apps = [] for apk in utils.shell("pm list packages -f -3").stdout.readlines(): apps.append(apk.split(":")[-1].split("=")[0]) return apps
def configure_instance_security(self, parameters): """ Setup EC2 security keys and groups. Required input values are read from the parameters dictionary. More specifically, this method expects to find a 'keyname' parameter and a 'group' parameter in the parameters dictionary. Using these provided values, this method will create a new EC2 key-pair and a security group. Security group will be granted permissions to access any port on the instantiated VMs. (Also see documentation for the BaseAgent class) Args: parameters A dictionary of parameters """ keyname = parameters[self.PARAM_KEYNAME] group = parameters[self.PARAM_GROUP] ssh_key = os.path.abspath('/etc/appscale/keys/cloud1/{0}.key'.format(keyname)) utils.log('About to spawn EC2 instances - Expecting to find a key at {0}'.format(ssh_key)) utils.log(utils.get_obscured_env(['EC2_ACCESS_KEY', 'EC2_SECRET_KEY'])) if not os.path.exists(ssh_key): utils.log('Creating keys/security group') ec2_output = '' while True: ec2_output = utils.shell('{0}-add-keypair {1} 2>&1'.format(self.prefix, keyname)) if ec2_output.find('BEGIN RSA PRIVATE KEY') != -1: break utils.log('Trying again. Saw this from {0}-add-keypair: {1}'.format( self.prefix, ec2_output)) utils.shell('{0}-delete-keypair {1} 2>&1'.format(self.prefix, keyname)) utils.write_key_file(ssh_key, ec2_output) utils.shell('{0}-add-group {1} -d appscale 2>&1'.format(self.prefix, group)) utils.shell('{0}-authorize {1} -p 1-65535 -P udp 2>&1'.format(self.prefix, group)) utils.shell('{0}-authorize {1} -p 1-65535 -P tcp 2>&1'.format(self.prefix, group)) utils.shell('{0}-authorize {1} -s 0.0.0.0/0 -P icmp -t -1:-1 2>&1'.format(self.prefix, group)) return True else: utils.log('Not creating keys/security group') return False