使用f2cadm命令行工具


f2cadm是一个FIT2CLOUD部署在虚拟机上的一个命令行工具,通过f2cadm可以:

  • 获取集群虚机组内虚机信息
  • 获取集群虚机组内虚机IP信息
  • 等待虚机组内虚机启动
  • 获取虚机本机信息
  • 获取虚机本机的某些字段信息
  • 获取集群的参数信息

所以f2cadm在实现根据集群内虚拟机信息自动配置组件配置文件时很有用,比如应用自动配置实现中,我们使用f2cadm getLocalServer --field=clusterName获取主机所在集群名,以配置到应用的配置文件中。实现根据集群内虚机信息自动配置相关组件配置文件:

#get mysql role server ip address
waitOutput=`f2cadm waitUntilServerUp --clusterrole-name=wordpress-mysql --timeout=120`
output=`echo $waitOutput * grep "vm is up"`
if [ ${#output} -gt 0 ] ; then
    getIPoutput=`f2cadm getClusterRoleServers --clusterrole-name=wordpress-mysql --field=localIP | head -1`

    checkErrorOutput=`echo $getIPoutput * grep "does not exist"`
    if [ ${#checkErrorOutput} -gt 0 ] ; then
        echo "exceptions happens when get role server ip"
        echo $output
    else
        echo $getIPoutput
        mysqlInternalIP=$getIPoutput
        sed -i "s/localhost/$mysqlInternalIP/g" /var/www/html/wordpress/wp-config.php
    fi
else
   echo exceptions happens when wait until mysql server up, can not get mysql ip, can not configure wp-config.php
   echo $waitOutput
fi

以下是f2cadm的用法和示例:

用法:
    f2cadm <command> [<args>]
命令:
    getLocalServer [--field=<信息项名称>] #信息项包括: clusterName, clusterRoleName, serverRoleName, localIP, remoteIP
    getClusterRoles
    getClusterRoleServers --clusterrole-name=<集群虚机组名称> [--field=<信息项名称>] #信息项包括: localIP, remoteIP, clusterRoleId, serverRoleId
    getClusterParameter --key-name=<参数名称>
    waitUntilServerUp --clusterrole-name=<集群虚机组名称> --timeout=<超时时间(单位秒)>
示例:   
    #获取命令运行所在当前主机信息
        f2cadm getLocalServer 

    #获取命令运行所在当前主机公网IP:
        f2cadm getLocalServer --field=localIP

    #获取命令运行所在当前主机虚机组名称,用于部署脚本根据虚拟机名称识别服务器类型,部署对应的组件:
        f2cadm getLocalServer --field=clusterRoleName

    #列出集群内所有虚机组信息
        f2cadm getClusterRoles

    #列出集群指定的虚机组内所有的虚拟机信息
        f2cadm getClusterRoleServers --clusterrole-name=<集群虚机组名称>

    #列出集群指定的虚机组内所有的虚拟机某项信息
        f2cadm getClusterRoleServers --clusterrole-name=<集群虚机组名称> --field=<信息项名称>

    #获取集群所有参数信息
        f2cadm getClusterParameter 

    #获取集群某参数信息
        f2cadm getClusterParameter --key-name=<参数名称>

    #等待集群中某虚机组内虚拟机启动,设定等待超时时间,单位为秒
        waitUntilServerUp --clusterrole-name=<集群虚机组名称> --timeout=<超时时间(单位秒)>

    #显示帮助信息
        f2cadm -h | --help

    #显示CLI版本号
        f2cadm -v | --version

1.获取集群虚拟组内虚拟机信息

f2cadm getClusterRoleServers --clusterrole-name=<虚机组名称>

例子: 获取wordpress-mysql虚机组虚拟机信息

f2cadm getClusterRoleServers --clusterrole-name=wordpress-mysql

输出: 

> f2cadm getClusterRoleServers --clusterrole-name=wordpress-mysql
[
  {
    "backendCreated": false,
    "serverRoleId": 24,
    "vmId": "i-28mixti7m",
    "imageId": "centos6u5_64_20G_aliaegis_20140926.vhd",
    "vmType": "ecs.t1.xsmall",
    "rabbitmqQueue": "queue_down_14_12154_637fdea6",
    "id": 12154,
    "alertType": null,
    "zone": "cn-qingdao-b",
    "cloudName": "default-dev",
    "backendCreating": false,
    "failedCause": null,
    "clusterRoleName": "wordpress-mysql",
    "clusterType": "default",
    "workId": 24287,
    "localIP": "10.165.63.61",
    "imageName": "centos6u5-20140926",
    "heartbeatStatus": "ONLINE",
    "image": 15,
    "internetBandwidth": 100,
    "status": "initializeResponse",
    "description": null,
    "clusterName": "wordpress-qingdao",
    "clusterId": 14,
    "price": 0.18809999999999999,
    "internetType": "PayByTraffic",
    "excludeVmStatus": null,
    "runningTime": null,
    "remoteIP": "121.42.56.146",
    "password": null,
    "groupId": 34,
    "name": "wordpress-qingdao--wordpress-mysql--12154",
    "agentRetried": 0,
    "vmStatus": "Running",
    "terminateFlag": false,
    "created": 1417244439000,
    "clusterRoleId": 52,
    "region": "cn-qingdao",
    "cloudId": 10,
    "deleted": null
  }
]

2. 获取集群虚拟组内虚拟机IP信息

f2cadm getClusterRoleServers --clusterrole-name=<虚机组名称> --field=localIP | head -1

例子: 获取wordpress-mysql虚机组的虚拟机内网IP

f2cadm getClusterRoleServers --clusterrole-name=wordpress-mysql --field=localIP | head -1 

输出:

> f2cadm getClusterRoleServers --clusterrole-name=wordpress-mysql --field=localIP
10.165.63.61

3. 等待虚机组内虚拟机启动

f2cadm waitUntilServerUp --clusterrole-name=<虚机组名称> --timeout<等待超时时间(单位秒)>

例子: 等待wordpress-mysql虚机组虚拟机启动,等待120秒

f2cadm waitUntilServerUp --clusterrole-name=wordpress-mysql --timeout=120

输出:

> f2cadm waitUntilServerUp --clusterrole-name=wordpress-mysql --timeout=120
to locate clusterrole wordpress-mysql servers..... @ 2014-11-29 15:29:36
clusterrole wordpress-mysql #12154 vm is up, localIP:10.165.63.61 remoteIP:121.42.56.146

4. 获取虚拟机本机的某些字段信息

f2cadm getLocalServer --field=<字段名称>  #字段名: [localIP, remoteIP, clusterRoleId, serverRoleId]

例子: 获取虚拟机本机全部字段信息

f2cadm getLocalServer

输出:
> f2cadm getLocalServer
{
  "clusterName": "wordpress-qingdao",
  "name": "wordpress-qingdao->wordpress-mysql->297",
  "vmId": "",
  "clusterRoleId": 52,
  "clusterRoleName": "wordpress-mysql",
  "serverRoleId": 24,
  "cloudId": 10,
  "clusterId": 14,
  "clusterType": "default",
  "groupId": 34,
  "localIP": "10.163.100.108",
  "remoteIP": "120.27.40.251",
  "region": "cn-qingdao",
  "rabbitmqQueue": "queue_down_14_297_d166bb98",
  "id": 297
}

5. 获取虚拟机本机的某些字段信息

f2cadm getLocalServer --field=<字段名称>  #字段名: [localIP, remoteIP, clusterRoleId, serverRoleId]

例子: 获取虚机的内网IP信息
f2cadm getLocalServer --field=localIP

输出:
> f2cadm getLocalServer --field=localIP
10.163.100.108

6. 获取集群的参数信息

f2cadm getClusterParameter --key-name=<参数名称>

例子: 获取集群的参数信息
f2cadm getClusterParameter --key-name=BuildInfo

输出:
> f2cadm getClusterParameter --key-name=BuildInfo
{"component_name":"web", "version":"0.1", "build_number":"201506121325"}