def slack(team_id=None, channel=None, params=None): target = Misc.array_pop(params,0) height = Misc.to_int(Misc.array_pop(params, 0)) width = Misc.to_int(Misc.array_pop(params, 0)) if target is None: target = 'general' if width is None: width = 800 if height is None: height = 1000 target_url = '/messages/{0}'.format(target) slack_message(":point_right: taking screenshot of slack channel: `{0}` with height `{1}` and width `{2}`".format(target, height,width), [], channel, team_id) payload = {'target' : target_url, 'channel': channel, 'team_id': team_id, 'width' : width, 'height' : height} aws_lambda = Lambda('osbot_browser.lambdas.slack_web') png_data = aws_lambda.invoke(payload) browser_helper = Browser_Lamdba_Helper() return browser_helper.send_png_data_to_slack(team_id, channel, target, png_data)
def screenshot(team_id=None, channel=None, params=[]): url = params.pop(0).replace('<', '').replace('>', '') # fix extra chars added by Slack delay = Misc.to_int(Misc.array_pop(params,0)) slack_message(":point_right: taking screenshot of url: {0}".format(url),[], channel,team_id) browser_helper = Browser_Lamdba_Helper().setup() png_data = browser_helper.get_screenshot_png(url,full_page=True, delay=delay) return browser_helper.send_png_data_to_slack(team_id,channel,url, png_data)
def get_nodes_and_edges(self, graph_data, nodes=None, edges=None, text_field='Key', append_key_to_text=False): if nodes is None: nodes = [] if edges is None: edges = [] for key, issue in graph_data.get('nodes').items(): if issue and issue.get('Summary'): text = issue.get(text_field) if append_key_to_text: text += " | {0}".format(key) nodes.append({ 'key': key, 'text': text, 'color': Misc.get_random_color() }) for edge in graph_data.get('edges'): edges.append({ 'from': edge[0], 'text': edge[1], 'to': edge[2], 'color': Misc.get_random_color() }) return nodes, edges
def use_command_class(slack_event, params, target_class): channel = Misc.get_value(slack_event, 'channel') user = Misc.get_value(slack_event, 'user') Slack_Commands_Helper(target_class).invoke(team_id=user, channel=channel, params=params) return None, None
def test_message_send(self): with Temp_Queue() as temp_Queue: queue = temp_Queue.queue message_1 = Misc.random_string_and_numbers(prefix='Hello_') message_2 = Misc.random_string_and_numbers(prefix='World_') queue.add(message_1).add(message_2) messages = [queue.get_message(), queue.get_message()] assert message_1 in messages assert message_2 in messages assert queue.get_message() is None
def user_info(team_id=None, channel=None, params=None): if len(params) > 0: user_name = ' '.join(params) users = slack(team_id).users() user = users.get(user_name) if user: return "```{0}```".format(Misc.json_format(user)) for user in users.values(): if user.get('id') == user_name or user.get( 'real_name') == user_name: return "```{0}```".format(Misc.json_format(user)) return ":exclamation: Sorry, could not find user with alias `{0}`".format( user_name)
def test_participant_append_to_field(self): payload = {'action': 'participant_append_to_field', 'name': 'OSS Bot', 'field': 'test_field', 'value': Misc.random_string_and_numbers()} assert self.aws_lambda.invoke(payload) == {'status': 'ok'} payload = {'action': 'participant_append_to_field', 'name': 'OSS Bot', 'field': 'sessions', 'value': Misc.random_string_and_numbers()} assert self.aws_lambda.invoke(payload) == {'status': 'ok'}
def render(team_id, channel, params): if params: target = params.pop(0) delay = Misc.to_int(Misc.array_pop(params,0)) if len(params) == 4: clip = {'x': int(params[0]), 'y': int(params[1]), 'width': int(params[2]), 'height': int(params[3])} else: clip = None else: return None slack_message(":point_right: rendering file `{0}`".format(target), [], channel, team_id) return Browser_Lamdba_Helper().setup().render_file(team_id, channel, target,clip=clip, delay=delay)
def mindmap(team_id=None, channel=None, params=None, root_node_text=None): (go_js, graph_data) = Go_Js_Views._get_graph_data(params, "mindmap",headless=True) go_js.load_page(False) (nodes, edges) = Go_Js_Views._get_nodes_and_edges(graph_data,text_field='Summary') data = { "class": "go.TreeModel", "nodeDataArray": []} width = Misc.to_int(Misc.array_pop(params, 0)) height = Misc.to_int(Misc.array_pop(params, 0)) if width and height: go_js.browser().sync__browser_width(width,height) else: go_js.set_browser_width_based_on_nodes(nodes) nodes_indexed = {} if len(nodes) > 0: for index, node in enumerate(nodes): key = node.get('key') #text = "{1} | {0}".format(key,node.get('text')) # need a better way to trigger this text = node.get('text') nodes_indexed[key] = {'index':index, 'text': text } #root_node_text = "{1} | {0}".format(nodes[0].get('key'), nodes[0].get('text')) # and this if root_node_text is None: root_node_text = nodes[0].get('text') data['nodeDataArray'].append({"key": 0, "text": root_node_text }) # add root node first for edge in edges: from_key = edge['from'] to_key = edge['to'] from_issue = nodes_indexed.get(from_key) to_issue = nodes_indexed.get(to_key) if from_issue: parent = nodes_indexed.get(edge['from']).get('index') else: parent = from_key if to_issue: key = nodes_indexed.get(edge['to' ]).get('index') text = nodes_indexed.get(edge['to' ]).get('text') else: key = to_key text = to_key item = {"key": key, "parent": parent, "text": text, "brush": Misc.get_random_color()} data['nodeDataArray'].append(item) go_js.invoke_js("create_graph_from_json", data) js_code = 'layoutAll()' go_js.api_browser.sync__await_for_element('#animationFinished') Dev.pprint(go_js.exec_js(js_code)) return go_js.send_screenshot_to_slack(team_id=team_id, channel=channel)
def format_node(node): issue = graph_data.get('nodes').get(node.get('id')) if issue: node['label'] = Misc.word_wrap(issue.get('Summary'), 20) #node['label'] = issue.get('Rating') labels = issue.get('Labels') if 'R0' in labels: #node['label'] = issue.get('Summary') node['color'] = '#FF0000' node['font'] = {'color': 'white', 'size': 25} node['mass'] = 2 return node if 'R1' in labels: node['color'] = '#FF6666' node['font'] = {'size': 20} node['mass'] = 3 return node if 'R2' in labels: node['color'] = '#FFAAAA' node['font'] = {'size': 15} #node['mass'] = 1 return node if 'R3' in labels: node['color'] = '#FFDDDD' return node if 'R4' in labels: node['color'] = '#00DDDD' return node
def _create_table_with_headers(team_id, channel, graph_name, headers, columns_defs=[], table_width='1200px'): graph_data = DataTable_Js_Views._get_data(graph_name) if graph_data: nodes = graph_data.get('nodes') rows = [] for index, node in enumerate(nodes.values()): row = [index + 1] for header in headers: value = node.get(header) if type(value).__name__ == 'dict': value = json.dumps(value) else: value = Misc.remove_html_tags(value) row.append(value) rows.append(row) headers.insert(0, '#') return DataTable_Js_Views._create_table( headers, rows, team_id, channel, table_width=table_width, columns_defs=columns_defs, table_title= "<b>{0}</b> <small><small><i>(data from graph)</i></small></small>" .format(graph_name))
def __init__(self, web_root=None): self.src_tmp = '/tmp/temp_web_server' if web_root: self.web_root = web_root else: self.web_root = self.src_tmp + '/html' self.html_file = Files.path_combine(self.web_root, 'index.html') self.port = Misc.random_number(10000, 60000) self.server_proc = None
def create_graph(self, nodes, edges, options=None): self.web_server.start() url = self.web_server.url(self.web_page) self.render_page.get_page_html_via_browser(url) self.load_page(True) layout = { "springLength": 100, "springCoeff": 0.0008, "dragCoeff": 0.02, "gravity": -10.2 } self.invoke_js("set_layout", layout) js_code = "" #for key,issue in nodes.items(): for node in nodes: key = node.get('key') label = node.get('label') img_url = node.get('img_url') img_size = node.get('img_size') params = {"label": label, "img_url": img_url, 'img_size': img_size} js_code += 'graph.addNode("{0}",{1});'.format( key, Misc.json_dumps(params)) for edge in edges: js_code += 'graph.addLink("{0}","{1}");\n'.format(edge[0], edge[2]) js_code += "run_graph()" self.exec_js(js_code) self.web_server.stop() return 42
def run(event, context): team_id = 'T7F3AUXGV' channel = 'DDKUZTK6X' text = ":robot_face: In trigger_server_reload lambda function \n" payload = { "params": ["server", "reload"], "channel": channel, 'team_id': team_id } result = Lambda('pbx_gs_python_utils.lambdas.gs.elastic_jira').invoke( payload) attachments = [{'text': result, 'color': 'good'}] slack_message(text, attachments, channel, team_id) result = Misc.json_format({'text': text}) return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": 209, "body": result }
def test_update_and_invoke(self): file_id = '1yDxu5YxL9FxY5wQ1EEQlAYGt3flIsm2VTyWwPny5RLA' result = self.simple_test.invoke( {'queryStringParameters': { 'file_id': file_id }}) message = Misc.json_load(result.get('body')) Dev.pprint(message)
def _test_lambda_write_cloud_watch__with_asserts(self): group_name = '/unit-tests/test_log_group' stream_name = Misc.random_string_and_numbers(prefix='tmp_stream_') message = 'this is a message sent from an lambda function' lambda_name = 'osbot_aws.lambdas.dev.write_cloud_watch_log' log_group_arn = 'arn:aws:logs:eu-west-2:244560807427:log-group:{0}*'.format( group_name) policy_name = 'temp_policy_for_lambda_write_cloud_watch' role_name = 'temp_role_for_lambda_invocation' policy_actions = ['logs:PutLogEvents'] logs = Logs(group_name=group_name, stream_name=stream_name) logs.group_create() logs.stream_create() iam_role = IAM(role_name=role_name) iam_policy = IAM_Policy(policy_name=policy_name) iam_policy.add_statement_allow(policy_actions, [log_group_arn]) policy_arn = iam_policy.create( delete_before_create=True).get('policy_arn') assert iam_policy.exists() is True assert iam_role.role_exists() is True assert logs.group_exists() is True assert logs.stream_exists() is True assert set(iam_role.role_policies()) == { 'AWSXrayWriteOnlyAccess', 'policy_temp_role_for_lambda_invocation' } iam_role.role_policy_attach(policy_arn) assert set(iam_role.role_policies()) == { 'AWSXrayWriteOnlyAccess', 'policy_temp_role_for_lambda_invocation', 'temp_policy_for_lambda_write_cloud_watch' } sleep(10) # wait for AWS to propagate role update payload = { 'group_name': group_name, 'stream_name': stream_name, 'message': message } lambda_obj = Lambda_Package(lambda_name) #.update_with_root_folder() result = lambda_obj.invoke(payload) sleep(1) # wait for Cloudwatch to update assert result.get('status') == 'ok' assert logs.messages() == [message] assert iam_policy.delete() is True assert logs.group_delete() is True assert logs.group_exists() is False assert set(iam_role.role_policies()) == { 'AWSXrayWriteOnlyAccess', 'policy_temp_role_for_lambda_invocation' }
def __init__(self): self.name = "temp_lambda_{0}".format(Misc.random_string_and_numbers()) self.temp_lambda = Lambda(self.name) self.tmp_folder = Temp_Folder_Code(self.name) self.role_arn = Temp_Aws_Roles().for_lambda_invocation() self.create_log = None self.delete_on_exit = True self.lambda_name = 'tmp_lambda_dev_test' self.s3_bucket = Globals.lambda_s3_bucket self.s3_key = 'unit_tests/lambdas/{0}.zip'.format(self.lambda_name)
def _get_graph_data(params, layout=None, headless=True): load_dependencies(['syncer', 'requests']) from osbot_browser.view_helpers.Am_Charts import Am_Charts am_charts = Am_Charts(headless=headless, layout=layout) graph_name = Misc.array_pop(params, 0) if graph_name: graph_data = am_charts.get_graph_data(graph_name) return am_charts, graph_data return am_charts, None
def test_log_group_create_delete_exists_info(self): tmp_log_group = Misc.random_string_and_numbers( prefix='/unit-tests/test_log_group_') temp_logs = Logs(tmp_log_group, '') assert temp_logs.group_exists() is False assert temp_logs.group_create() is True assert temp_logs.group_exists() is True assert temp_logs.group_info().get('logGroupName') == tmp_log_group assert temp_logs.group_delete() == True assert temp_logs.group_exists() is False
def run(event, context): team_id = 'T7F3AUXGV' channel = 'DDKUZTK6X' text = "in API Gateway test..." attachments = [{ 'text': "{0}".format(Misc.json_format(event)), 'color': 'good' }] #attachments = [{'text': "{0}".format(event), 'color': 'good'}] slack_message(text, attachments, channel, team_id) result = Misc.json_format({'text': text}) return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": 209, "body": result }
def run(event, context): project_name = 'Code_Build_Test' build_id = CodeBuild(project_name=project_name, role_name=project_name).build_start() return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": 200, "body": Misc.json_format({'build_id': build_id}) }
def test_update(self): with Temp_Lambda() as temp_lambda: tmp_text = Misc.random_string_and_numbers(prefix='updated code ') temp_lambda.tmp_folder.create_temp_file('def run(event, context): return "{0}"'.format(tmp_text)) result = temp_lambda.aws_lambda.update() status = result.get('status') data = result.get('data') assert 'TracingConfig' in data #set(data) == { 'CodeSha256', 'CodeSize', 'Description', 'FunctionArn', 'FunctionName', 'Handler', 'LastModified', 'MemorySize', 'ResponseMetadata', 'RevisionId', 'Role', 'Runtime', 'Timeout', 'TracingConfig', 'Version'} assert status == 'ok' assert temp_lambda.aws_lambda.invoke() == tmp_text
def add_node(self, element, title, id=None): title = Misc.word_wrap_escaped(title, self.max_title) if self.on_add_node: # if self.on_add_element is set, then use it for the node render bit puml_to_add = self.on_add_node(element, title, self.fix_id(id), id) if puml_to_add: self.puml += "\t{0} \n".format(puml_to_add) else: if id: self.puml += '\t{0} "{1}" as {2} \n'.format( element, title, self.fix_id(id)) else: self.puml += '\t{0} "{1}"\n'.format(element, title) return self
def create_dashboard_with_test_data(self): self.create_dashboard_with_R1_R2() rows = 6 cells = 6 js_codes = [] for i in range(1, cells + 1): for j in range(1, rows + 1): r2_id = "r{0}_{1}".format(i, j) color = Misc.random_number(1, 5) #color = j self.js_apply_css_color(js_codes, r2_id, color) self.js_eval(js_codes) return self
def test_lambda_write_cloud_watch(self): group_name = '/aws/lambda/unit-tests/test_log_group' stream_name = Misc.random_string_and_numbers(prefix='tmp_stream_') message = 'this is a message sent from an lambda function' lambda_name = 'osbot_aws.lambdas.dev.write_cloud_watch_log' logs = Logs(group_name=group_name, stream_name=stream_name).create() lambda_obj = Lambda_Package(lambda_name) payload = {'group_name': group_name, 'stream_name': stream_name, 'message': message} assert lambda_obj.invoke(payload).get('status') == 'ok' #sleep(0.8) # assert logs.messages() == ['this is a message sent from an lambda function'] assert logs.group_delete() is True
def test_run_container_on_temp_cluster(self): subnet_id = ' subnet-49391932' security_group_id = 'sg-e6ea548e' cluster_name = Misc.random_string_and_numbers(6, 'temp_cluster_') task_name = 'temp_task_on_{0}'.format(cluster_name) task_role = 'task_role_{0}'.format(task_name) execution_role = 'execution_role_{0}'.format(task_name) docker_image_name = 'gs-docker-codebuild' log_group_name = "awslogs-{0}".format(task_name) log_group_region = "eu-west-2" log_group_stream_prefix = "awslogs-example" # Create Cluster self.fargate.cluster_create(cluster_name) # Create Roles self.fargate.policy_create_for_task_role(task_role) self.fargate.policy_create_for_execution_role(execution_role) #Create Task task_arn = self.fargate.task_create( task_name, task_role, execution_role).get('taskDefinitionArn') # Run Task task_run = self.fargate.task_run(cluster_name, task_arn, subnet_id, security_group_id) task_run_arn = task_run.get('taskArn') task_details = self.fargate.task_wait_for_completion( cluster_name, task_run_arn) task_id = 'asd' # Get logs #group_name = 'awslogs-temp_task_on_temp_cluster_X29B3K' #stream_name = 'awslogs-example/gs-docker-codebuild/f8ccf213-b642-466c-8458-86af9933eca9' stream_name = "{0}/{1}{2}".format(log_group_stream_prefix, docker_image_name, task_id) messages = self.cloud_watch.logs_get_messages(log_group_name, stream_name) # Print details Dev.pprint(messages)
def swimlanes(team_id=None, channel=None, params=None): (go_js, graph_data) = Go_Js_Views._get_graph_data(params, "swimlanes") nodes = [{'key': "Pool1", 'text': "By Risk", 'isGroup': True, 'category': "Pool"}] edges = [] groups = [] colors = ["lightblue","lightgreen","lightyellow","orange"] for key,issue in graph_data.get('nodes').items(): if issue: group = Misc.array_add(groups, issue.get('Rating')) nodes.append({'key': key, 'group':group}) #issue.get('Summary') for group in list(set(groups)): nodes.append({'key': group, 'text': group, 'isGroup': True, 'group' : "Pool1", 'color': Misc.array_pop(colors)}) for edge in graph_data.get('edges'): edges.append({ 'from': edge[0],'to': edge[2]}) js_code = 'relayoutLanes()' return go_js.render(nodes, edges, js_code=js_code, width=1400, team_id=team_id, channel=channel)
def run(event, context): team_id = 'T7F3AUXGV' channel = 'DDKUZTK6X' querystring = event.get('queryStringParameters') if querystring and querystring.get('file_id'): file_id = querystring.get('file_id') if querystring.get('action') == 'diff': payload = { "params": ["diff_sheet", file_id], "channel": "DDKUZTK6X", 'team_id': 'T7F3AUXGV' } elif querystring.get('action') == 'sync': payload = { "params": ["sync_sheet", file_id], "channel": "DDKUZTK6X", 'team_id': 'T7F3AUXGV' } else: payload = { "params": ["load_sheet", file_id], "channel": "DDKUZTK6X", 'team_id': 'T7F3AUXGV' } Lambda('pbx_gs_python_utils.lambdas.gs.elastic_jira').invoke(payload) text = ":point_right: [trigger_sync_jira_sheets] completed workflow for file_id: {0} , see channel {1} for more details".format( file_id, channel) status_code = 201 else: text = "Error: file_id value not provided" status_code = 501 slack_message(text, [], channel, team_id) return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": status_code, "body": Misc.json_format({'text': text}) }
def node_label(team_id=None, channel=None, params=None): if len(params) < 2: return "':red_circle: Hi, for the `node_label` view, you need to provide the label field name. Try: `Key`, `Summary`, `Rating`, `Status`" label_key = ' '.join(params[1:]) (graph_name, nodes, edges, graph_data, vis_js) = Vis_Js_Views.default(params=params, no_render=True) graph_name += ' | node_label | ' + label_key issues = graph_data.get('nodes') for node in nodes: issue = issues.get(node['label']) if issue: value = str(issue.get(label_key)) node['label'] = Misc.word_wrap(value, 40) for edge in edges: del edge['label'] options = {'nodes': {'shape': 'box'}, 'edges': {'arrows': 'to'}} options = None return vis_js.create_graph_and_send_screenshot_to_slack( graph_name, nodes, edges, options, team_id, channel)
def hello(slack_event=None, params=None): user = Misc.get_value(slack_event, 'user') return 'Hello <@{0}>, how can I help you?'.format(user), []