Ambari中的custom_actions应用实例分析

Ambari中的custom_actions应用实例分析

Ambari中的custom_actions应用实例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1. custom_actions简单介绍

假如我们要在ambari的所有主机执行某个脚本,要在每个主机创建一个用户或者查询所有主机用户,我们可以怎么做呢?

在ambari的安装路径/var/lib/ambari-server/resources/custom_actions/scripts/有各种已有脚本如下:

$ll/var/lib/ambari-server/resources/custom_actions/scripts/*.py-rwxr-xr-x1rootroot25331Jul2721:44check_host.py-rwxr-xr-x1rootroot2221Jul2721:44clear_repocache.py-rwxr-xr-x1rootroot21891Jul2721:44install_packages.py-rwxr-xr-x1rootroot2370Jul2721:44remove_bits.py-rwxr-xr-x1rootroot4891Jul2721:44remove_previous_stacks.py-rwxr-xr-x1rootroot5947Jul2721:44ru_execute_tasks.py-rwxr-xr-x1rootroot4592Jul2721:44stack_select_set_all.py-rwxr-xr-x1rootroot2777Jul2721:44update_repo.py-rwxr-xr-x1rootroot12374Jul2721:44validate_configs.py

我们同样可以添加一个custom_action来为我们执行某些特定操作

custom_action scritpts结构如下:

  • 定义一个类,任意命名

  • 类要继续Script

  • 类中重写方法actionexecute,方法中是我们可以自定义的操作

#!/usr/bin/envpythonclassCheckHost(Script):defactionexecute(self,env):passif__name__=="__main__":CheckHost().execute()

2. 案例: custom_actions实现所有主机用户管理

2.1 custom_action脚本实现

实现脚本并添加至路径/var/lib/ambari-server/resources/custom_actions/scripts/

注意脚本需要执行权限chmod a+x populate_user.py

populate_user.py

#!/usr/bin/pythonfromresource_managementimportScript,formatfromresource_management.coreimportshellfromresource_management.core.loggerimportLoggerclassUserManager(Script):originalUserList=""requestUserList=""defactionexecute(self,env):Logger.info("UserManagerinvokedtoprocessingaccountrequest")config=Script.get_config()structured_output={}request_info=config['roleParams']Logger.debug("requestdetails:"+str(request_info))code,cmd=self.assemble_cmd(request_info)if0!=code:print"invalidrequestinfo",cmdLogger.error(str(code)+""+cmd)structured_output["user_manager"]={"exit_code":code,"message":str(request_info),"output":format(cmd)}self.put_structured_out(structured_output)returnLogger.info("toexecute:"+cmd)code,output=shell.call(cmd,sudo=False)printcode,outputif0==code:structured_output["user_manager"]={"exit_code":0,"message":format("populateuseraccountsuccessfully"),"output":format(output)}else:Logger.error(str(code)+""+output)structured_output["user_manager"]={"exit_code":code,"message":format("populateuseraccountfailed"),"output":format(output)}self.put_structured_out(structured_output)defis_user_existed(self,uname):#retrieveUser="cat/etc/passwd|grep/bin/bash|cut-d:-f1"uexisted='id'+unamecode,resp=shell.call(uexisted,sudo=False)if0!=code:Logger.error(str(code)+""+resp)returnFalseelse:returnTruedefis_group_existed(self,group):check_group_cmd="cat/etc/group|cut-d:-f1"code,resp=shell.call(check_group_cmd,sudo=False)groupls=group.split(",")respls=[]ifcode==0:grps=resp.split("\n")#Logger.info("/etc/group:"+",".join(grps))forgingroupls:ifgnotingrps:respls.append(g)else:Logger.error(str(code)+""+resp)returnFalse,"cmdexecuteerror:"+respiflen(respls)>0:returnFalse,"groups:"+",".join(respls)+"notexisted"else:returnTrue,"groupshasexisted"defassemble_cmd(self,req):"""Jsonexampleforcreateuser/group,deleteuser/group{"action":"create","type":"group","group":"hdp1"}{"action":"delete","type":"group","group":"hdp1"}{"action":"search","type":"group"}{"action":"create","type":"user","group":"hdp1","name":"user1","password":"passwd"}{"action":"delete","type":"user","group":"hdp1","name":"user1"}{"action":"search","type":"user"}{"action":"search","type":"user_groups"}parsejsondatatoassembleinstruction"""type=req['type']action=req['action']#searchifaction=="search"andtype=="user":returnself.search_user()ifaction=="search"andtype=="group":returnself.search_group()ifaction=="search"andtype=="user_groups":returnself.search_user_groups()#checkuserorgroupiftype=='group':gname=req['group']ifgnameisNoneorgname=='':code=1cmd="groupnamemissed"returncode,cmdeliftype=='user':uname=req['name']ifunameisNoneoruname=='':code=1cmd="usernamemissed"returncode,cmdelse:code=1cmd="unsupportedtype"returncode,cmd#create/delete/editifaction=="create"andtype=="user":returnself.create_user(req['name'],req['group'])ifaction=="delete"andtype=="user":returnself.delete_user(req['name'])ifaction=="edit"andtype=="user":returnself.edit_user(req['name'],req['group'])ifaction=="create"andtype=="group":returnself.create_group(req['group'])ifaction=="delete"andtype=="group":returnself.delete_group(req['group'])ifaction=="edit"andtype=="group":self.edit_group(req['new_group'],req['group'])#unknownreturn1,"unknownoperationrequest"defcreate_user(self,uname,gname):code=0#needtodeterminewhetheruserexistedalreadyifself.is_user_existed(uname):code=2cmd="useralreadyexisted"returncode,cmdifgnameisNoneorgname=='':code=1cmd="groupnamemissedwhencreatinguser"returncode,cmdis_grp_existed,grp_resp=self.is_group_existed(gname)ifnotis_grp_existed:code=1cmd=grp_respreturncode,cmdcmd='useradd-m-g'+gname+"-s/bin/bash"+unamereturncode,cmddefdelete_user(self,uname):code=0#checkwhetheruserexistedifnotself.is_user_existed(uname):code=3cmd="usernotexisted"returncode,cmdcmd='userdel-r'+unamereturncode,cmddefedit_user(self,uname,gname):code=0ifnotself.is_user_existed(uname):code=3cmd="usernotexisted"returncode,cmdis_grp_existed,grp_resp=self.is_group_existed(gname)ifnotis_grp_existed:code=1cmd=grp_respreturncode,cmdcmd='usermod-G'+gname+''+unamereturncode,cmddefsearch_user(self):#searchalluserscmd='compgen-u'return0,cmddefcreate_group(self,gname):cmd='groupadd'+gnamereturn0,cmddefdelete_group(self,gname):cmd='groupdel'+gnamereturn0,cmddefedit_group(self,new_gname,old_gname):cmd='groupmod-n'+new_gname+''+old_gnamereturn0,cmddefsearch_group(self):#searchallgroupscmd='compgen-g'return0,cmddefsearch_user_groups(self):"""searchallusersanduser_groups:return:hbase:hbasehadoop"""cmd="foruin`compgen-u`;dogroups$u;done"return0,cmdif__name__=="__main__":UserManager().execute()

2.2 添加定义到system_action_definitions.xml

/var/lib/ambari-server/resources/custom_action_definitions/system_action_definitions.xml中添加custom_action的定义

<actionDefinition><actionName>populate_user</actionName><actionType>SYSTEM</actionType><inputs></inputs><targetService/><targetComponent/><description>Populateuseraccount</description><targetType>ALL</targetType><permissions>HOST.ADD_DELETE_COMPONENTS,HOST.ADD_DELETE_HOSTS,SERVICE.ADD_DELETE_SERVICES</permissions></actionDefinition>

2.3 重启ambari-server并测试

$ ambari-server restart

2.3.1 查看action是否添加成功

<font >可以看到已经有了我们添加的populate_user</font>

$ curl -X GET -u admin:admin 'http://10.1.255.11:8080/api/v1/actions

{"href":"http://10.1.255.11:8080/api/v1/actions","items":[{"href":"http://10.1.255.11:8080/api/v1/actions/check_host","Actions":{"action_name":"check_host"}},{"href":"http://10.1.255.11:8080/api/v1/actions/clear_repocache","Actions":{"action_name":"clear_repocache"}},{"href":"http://10.1.255.11:8080/api/v1/actions/install_packages","Actions":{"action_name":"install_packages"}},{"href":"http://10.1.255.11:8080/api/v1/actions/populate_user","Actions":{"action_name":"populate_user"}},{"href":"http://10.1.255.11:8080/api/v1/actions/remove_previous_stacks","Actions":{"action_name":"remove_previous_stacks"}},{"href":"http://10.1.255.11:8080/api/v1/actions/ru_execute_tasks","Actions":{"action_name":"ru_execute_tasks"}},{"href":"http://10.1.255.11:8080/api/v1/actions/update_repo","Actions":{"action_name":"update_repo"}},{"href":"http://10.1.255.11:8080/api/v1/actions/validate_configs","Actions":{"action_name":"validate_configs"}}]}

2.3.2 通过自定义脚本查询所有主机用户

<font color=red>请求执行populate_user</font>

$curl-XPOST'http://10.1.255.11:8080/api/v1/clusters/dp147/requests/'\-uadmin:admin\-H'x-requested-by:ambari'\--data'{"RequestInfo":{"context":"UserManager2020.12.2313:45:14","action":"populate_user","parameters/type":"user","parameters/action":"search"}}'#响应{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968","Requests":{"id":968,"status":"Accepted"}}

<font color=red>查看populate_user执行结果</font>

#curl-XGET-uadmin:admin'http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968'{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968","Requests":{"aborted_task_count":1,"cluster_host_info":"{}","cluster_name":"dp147","completed_task_count":3,"create_time":1608732220935,"end_time":1608732221608,"exclusive":false,"failed_task_count":0,"id":968,"inputs":"{\"action\":\"search\",\"type\":\"user\"}","operation_level":null,"progress_percent":100.0,"queued_task_count":0,"request_context":"UserManager2020.12.2313:45:14","request_schedule":null,"request_status":"ABORTED","resource_filters":[],"start_time":1608732220999,"task_count":3,"timed_out_task_count":0,"type":"ACTION","user_name":"admin"},"stages":[{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968/stages/0","Stage":{"cluster_name":"dp147","request_id":968,"stage_id":0}}],"tasks":[{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968/tasks/2943","Tasks":{"cluster_name":"dp147","id":2943,"request_id":968,"stage_id":0}},{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968/tasks/2944","Tasks":{"cluster_name":"dp147","id":2944,"request_id":968,"stage_id":0}},{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968/tasks/2945","Tasks":{"cluster_name":"dp147","id":2945,"request_id":968,"stage_id":0}}]}

<font color=red>查看populate_user在某台主机执行结果</font>

$curl-XGET-uadmin:admin'http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968/tasks/2945'{"href":"http://10.1.255.11:8080/api/v1/clusters/dp147/requests/968/tasks/2945","Tasks":{"attempt_cnt":1,"cluster_name":"dp147","command":"ACTIONEXECUTE","command_detail":"populate_userACTIONEXECUTE","end_time":1608732221597,"error_log":"/var/lib/ambari-agent/data/errors-2945.txt","exit_code":0,"host_name":"host-10-1-236-147","id":2945,"ops_display_name":null,"output_log":"/var/lib/ambari-agent/data/output-2945.txt","request_id":968,"role":"populate_user","stage_id":0,"start_time":1608732221032,"status":"COMPLETED","stderr":"None","stdout":"2020-12-2322:03:41,453-UserManagerinvokedtoprocessingaccountrequest\n2020-12-2322:03:41,453-toexecute:compgen-u\n2020-12-2322:03:41,454-call['compgen-u']{'sudo':False}\n2020-12-2322:03:41,532-callreturned(0,'root\\nbin\\ndaemon\\nadm\\nlp\\nsync\\nshutdown\\nhalt\\nmail\\noperator\\ngames\\nftp\\nnobody\\navahi-autoipd\\nsystemd-bus-proxy\\nsystemd-network\\ndbus\\npolkitd\\nabrt\\nlibstoragemgmt\\npostfix\\npcp\\ntss\\nchrony\\nsshd\\nntp\\ntcpdump\\noprofile\\npuaiuc\\npostgres\\nyarn-ats\\nlivy\\nmysql\\nhive\\nzookeeper\\nams\\nambari-qa\\ntez\\nhdfs\\nyarn\\nmapred\\nhbase\\nttt\\ngaokang123\\nlibai\\nhunter\\nsongwukong\\nwwww\\njiazz')\n0root\nbin\ndaemon\nadm\nlp\nsync\nshutdown\nhalt\nmail\noperator\ngames\nftp\nnobody\navahi-autoipd\nsystemd-bus-proxy\nsystemd-network\ndbus\npolkitd\nabrt\nlibstoragemgmt\npostfix\npcp\ntss\nchrony\nsshd\nntp\ntcpdump\noprofile\npuaiuc\npostgres\nyarn-ats\nlivy\nmysql\nhive\nzookeeper\nams\nambari-qa\ntez\nhdfs\nyarn\nmapred\nhbase\nttt\ngaokang123\nlibai\nhunter\nsongwukong\nwwww\njiazz\n\nCommandcompletedsuccessfully!\n","structured_out":{"user_manager":{"exit_code":0,"message":"populateuseraccountsuccessfully","output":"root\nbin\ndaemon\nadm\nlp\nsync\nshutdown\nhalt\nmail\noperator\ngames\nftp\nnobody\navahi-autoipd\nsystemd-bus-proxy\nsystemd-network\ndbus\npolkitd\nabrt\nlibstoragemgmt\npostfix\npcp\ntss\nchrony\nsshd\nntp\ntcpdump\noprofile\npuaiuc\npostgres\nyarn-![](https://oscimg.oschina.net/oscnet/up-b681776f23021da1bbf507e1f383dfab715.png)\ngaokang123\nlibai\nhunter\nsongwukong\nwwww\njiazz"}}}}

在ambari界面查看查看action运行进度如下:

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

发布于 2022-01-06 23:25:59
收藏
分享
海报
0 条评论
46
上一篇:区块链中比特币技术的双重支付概念是什么 下一篇:区块链的比特币技术中系统吞吐率是什么
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码