给自己看:
host_names = {}
for hostin api.get_all_hosts():
host_names[host.hostId] = host.hostname
host_names = {} 是一个字典里面,就是 ip:hostname
cluster_name="suibian"
#根据名字 ,host以及cdh版本创建端口
cluster = create_cluster(cluster_name, host_names, cdh_version)
def create_cluster(cluster_name, host_names, cdh_version='CDH5'):
# 部署集群
try:
cluster = api.create_cluster(cluster_name, cdh_version)
except:
cluster = api.get_cluster(cluster_name)
# 获取加入到CM的主机列表
host_list = host_names.keys()
# 将主机列表加入到集群
try:
cluster.add_hosts(host_list)
except:
pass
return cluster
部署一个CDH的Service例如hive,hbase,kafka等,这里以Neo4j为例子
if 'NEO4J' in service_types:
print "Neo4j服务部署中,请耐心等待。。。"
neo4j = setup_neo4j(cluster, neo4j_service_name, neo4j_service_conf, neo4j_host)
这里的service_types是前面定义的
service_types = []
for servicein cluster.get_all_services():
service_types.append(service.type)
这里拿到了所有service的标签,这个标签就是当前service的一个记号,以此作为标记
neo4j_service_name = "Neo4j"
def setup_neo4j(cluster, neo4j_service_name, neo4j_service_conf, neo4j_host):
# 部署neo4j
cluster.delete_service(neo4j_service_name) # 从当前cluster删除服务
neo4j_service_conf={}
#create_service(service_name,service_type)这个方法是创建服务名字以及标签的方法
neo4j_service = cluster.create_service(neo4j_service_name,"NEO4J")
#给这个服务更新配置这个配置可以自己定义,注意是以字典的形式
neo4j_service.update_config(neo4j_service_conf)
#为什么要get_role_config_group 内的字符串是固定的呢?默认是 Neo4j-NEO4J-BASE
neo4j_role = neo4j_service.get_role_config_group("{0}-NEO4J-BASE".format(neo4j_service_name))
list = neo4j_service.get_all_role_config_groups()
print list #print的结果是

但是这个是这个名字根据不同的服务有不同的名字,有些事一样,一下拿kylin举例
kylin_service = cluster.create_service(kylin_service_name,"KYLIN")
list = kylin_service.get_all_role_config_groups()
print list
kylin的结果为
<ApiList>(2): [<ApiRoleConfigGroup>: Kylin-KYLIN_JOBSERVER-BASE (cluster: suibian; service: Kylin), <ApiRoleConfigGroup>: Kylin-KYLIN_QUERYSERVER-BASE (cluster: suibian; service: Kylin)]
为什么会这样?因为我们在手动安装,也就是不用API的时候会看到,添加Kylin角色实例的时候会有两个,而Neo4j只有一个,所以会有不同

如果我们要设置两个角色组,拿我们就要
role_group1 = kylin_service.get_role_config_group("{0}-KYLIN_JOBSERVER-BASE".format(kylin_service_name))
role_group2 = kylin_service.get_role_config_group("{0}-KYLIN_QUERYSERVER-BASE".format(kylin_service_name))
对两个角色组进行设置,其他不同的service如hbase,hive,hdfs也要以此类推
neo4j_role.update_config(neo4j_role_conf)
#neo4j_service_name为名字,这个名字要以字母和下划线为开头,这里是Neo4j ,"NEO4J"是这个角色的标签
neo4j_service.create_role("{0}-neo4j_role".format(neo4j_service_name),"NEO4J", neo4j_host)
return neo4j_service
创建完毕以后显示图是这个

create_role 是创建角色的方法他调用的是services里面的这个方法

实际往上调是调用roles.py的这个方法,可以看到这里的cluster_name是默认的default""

单一角色的对照--kylin
#一下为自己写的Kylin 的简易安装,与上方neo4j进行简单对照,可以更好的看出些东西比如create_role 的时候填写类型的时候
使用的是KYLIN_JOBSERVER
def setup_kylin(cluster, kylin_service_name,kylin_service_conf ,kylin_host):#部署kylin
# cluster.delete_service(kylin_service_name) #这个是根据名字删除Kylin的service的方法
kylin_service = cluster.create_service(kylin_service_name,"KYLIN")
kylin_service.update_config(kylin_service_conf)
kylin_role = kylin_service.get_role_config_group("{0}-KYLIN_JOBSERVER-BASE".format(kylin_service_name))
kylin_role.update_config(kylin_role_conf)
#create_role创建角色参数:名字 类型 地址
kylin_service.create_role("{0}-kylin_role".format(kylin_service_name),"KYLIN_JOBSERVER", kylin_host)
return kylin_service
多个角色对照--hbase
def setup_hbase(cluster,hbase_service_name, hbase_service_conf,regionserver_hosts,hmaster_hosts,hmaster_role_conf,regionserver_role_conf):hbase_service = cluster.create_service(hbase_service_name,"HBASE")
hbase_service.update_config(hbase_service_conf)
hs = hbase_service.get_role_config_group("{0}-MASTER-BASE".format(hbase_service_name))
hs.update_config(hmaster_role_conf)
hmaster_num =0
or hmaster_hostin hmaster_hosts:
hmaster_num +=1
hbase_service.create_role("{0}-hm-".format(hbase_service_name) +str(hmaster_num),"MASTER", hmaster_host)
rs = hbase_service.get_role_config_group("{0}-REGIONSERVER-BASE".format(hbase_service_name))
rs.update_config(regionserver_role_conf)
regionserver_num =0
for regionserver_hostin regionserver_hosts:
regionserver_num +=1
hbase_service.create_role("{0}-rs-".format(hbase_service_name) +str(regionserver_num),"REGIONSERVER", regionserver_host)
return hbase_service
下面是Hbase的初始化设置--实际上是放在安装def前面
# HBASE配置
hmaster_hosts = hbase_master_host.split(',')
regionserver_hosts = regionserver_host.split(',')
if regionserver_hosts == ['']:
regionserver_hosts = []
for host_arrayin disk_host_group.values():
if len(host_array) >0:
regionserver_hosts.extend(host_array)
regionserver_hosts = flat(regionserver_hosts)
else:
regionserver_hosts = regionserver_hosts
hbase_service_name ="Hbase"
hbase_service_conf = { #这个hbase_service的基本配置,配置在这里设置好以后,再安装service的时候加载进去
'hdfs_service': hdfs_service_name, #这个语句 含义是hbase 依赖于 hdfs ,就在这里设置
'zookeeper_service': zk_service_name #这个语句 含义是hbase 依赖于zookeeper ,就在这里设置
}
#hbase的HMaster的配置在这里
hmaster_role_conf = {} #目前这里是空,用默认配置
#hbase的RegionServer的配置
regionserver_role_conf = {} #目前这里也是空,用默认配置
附加-以后用到的附加到这里:
这是Hbase的默认的配置组
[<ApiRoleConfigGroup>: Hbase-REGIONSERVER-BASE (cluster: senwei; service: Hbase), <ApiRoleConfigGroup>: Hbase-MASTER-BASE (cluster: senwei; service: Hbase), <ApiRoleConfigGroup>: Hbase-HBASETHRIFTSERVER-BASE (cluster: senwei; service: Hbase), <ApiRoleConfigGroup>: Hbase-HBASERESTSERVER-BASE (cluster: senwei; service: Hbase), <ApiRoleConfigGroup>: Hbase-GATEWAY-BASE (cluster: senwei; service: Hbase)]
附录-得到CDH的cloudera manager 和CDH的cluster的方法
#得到cloudera manager对象
api = ApiResource("master01", username="admin", password="admin", version=19)
cm = api.get_cloudera_manager().get_service()
#得到所有cluster的对象的list
cluster_group = []
for c in api.get_all_clusters():
if c.version == 'CDH5':
cluster_group.append(c)
网友评论