cloudstack 开发

所需积分/C币:10 2015-07-18 13:24:26 489KB PDF
8
收藏 收藏
举报

cloudstack 开发.一个小例子提示了如何添加一个简单的插件集成到cloud stack的环境中去
user GetMgmt Capacity Cmd. java L response GetMgmt Capacity CmdResponse java mgmtcap ApiMgmt CapacityService java ApiMgmt Capacity ServiceImpl java target test 7.对于以上的目录及文件结构,我们来分析一下各文件的作用 1) pom. Xm是我们新加的AP模块的 Mayen项目配置文件,用于管理这个AP的整个生命周期,其 内容已经在前面展现 2) resources/META-INF/ cloudstack/ mgmtcap目录下的两个文件,用于标识新的的模块属于哪个 父模块,以及 Spring实例化Bean; module. properties内容 name=mgmtcap parent=api spring-mgmtcap-context xm内容 beansxmlns="htTp://www.springframework,org/schema/beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance xmlns:context="http://www.springframeworkorg/schema/context xmlns:aop="http://www.springframeworkorgschema/aop xsi:schemalocation=http://www.springframeworkorg/schema/beans http://www.springframeworkorg/schema/beans/spring beans-3.0.Xsd http://www.springframework.org/schema/aophttp:// www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframeworkorg/schema/context http://www.springframeworkorg/schema/context spring-context-3.0.xsd" <bean id="apiMgmt CapacityServiceImpL class="org. apache cloudstack. mgmtcap ApiMgmt CapacityServiceImpl"/> beans> 3)在sc目录的AP的命令及响应源码 API Cmd: org/apache/cloudstack/api/command/user/GetMgmtCapacity Cmd java API Response: org/apache/cloudstack/api/response/GetMgmtCapacityCmdResponse java 由于我们的AP命令允许普通用户调用,所有约定其所在的包是 org. apachecloudstack api. commanduser;以下是 GetMgmt Capacity Cmd. java的源码,忽略ACL20 的许可声明: package org. apache cloudstack api. command user; import javax. injectInject; 1 import org. apache cLoudstack, api. APICommand; I import org. apache cloudstackapi. BaseCmd import org. apache cloudstack api. ServerApiException import org. apache cloudstack api response. GetMgmt Capacity CmdResponse; mport org. apache CLoudstack. mgmtcap ApiMgmt Capacity Service; import org. apache. 1og4jLogger; APICommand(name ="getMgmt Capacity", responseObject GetMgmt CapacityCmdResponse. class, description ="get management server capacity information by ApiMgmt CapacityService plugin", since =4.2.0") public class GetMgmt Capacity Cmd extends BaseCmd public static final Logger s_logger ogger. getLogger (GetMgmt Capacity Cmd. cLass getName O)) private static final string s_name getmgmtcapacityresponse iNject ApiMgmt Capacityservice _apiMgmt Capacityservice, @Override public void execute throws ServerApiexception i GetMgmt CapacityCmdResponse response=new GetMgmt CapacityCmdResponseO) response. setobjectName ("mgmtcapacity") response. setResponseName(get CommandName o); apiMgmt CapacityService formatMgmt Server Capacitycresponse); this setResponseObject(response) @Override public String get CommandName(i return s name @Override public long getEntityOwnerIdOi / no owner is needed for list command eturn 0: 以下是响应文件 GetMgmtCapacityCmdResponse. java的源码内容 package org. apache cloudstack api response; import org. apache cloudstack. api. Apiconstants import com. cloud serializer Param mport com. googLe. gson. annotations. Serializedname; import org. apache cloudstack api. BaseResponse; import org. apache cloudstackapi. BaseCmd import org. apache cloudstack api. APICommand import org. apache cloudstack api. ServerApiException import java. util. HashSet import java. util. Set; aSuppressWarnings("unused") public class GetMgmt Capacity CmdResponse extends BaseResponse i aSerializedName (api constants. NAME) @Param(description="the name of the api command") private string name; @Serial izedName (api constants. DE SCRIPTIOI param(description="description of the api private string description; @serializedName(Api Constants. SINcE) @Param(description="version of CloudStack the api was introduced in") private string since ap, oSerializedName (Api Constants IS_ASYNC) @Param(description=true if is asynchronous") private Boolean isAsync OSerializedName(Api Constants TYPE)pAram(description="response field type") private string type; sErialize dName("cpucore")@Param(description="cpu core number") private Integer cpucore; @SerializedName ("memtotal")@Param(description ="total memory in MB") private Integer memtotaL OSerializedName("memfree")@Param( description="free memory in MB") private Integer memfree; @serializedName ("osname") @Param(description="the os name") private string csname @Serial izedName( " osversion")@Param(description="the os version") private string osversion public getMgmt Capacity CmdResponseo isAsync false sLnce="4.2.0"; public void setName (String name)i this name name public string getName()t return name public void setDescription(String description)t 个 this description description public string getDescription o i return description; public void setsince(string since)t this since= since. public string getsinceo i return since; public void setAsync(Boolean isasync) t this isAsync isAsync, public boolean getAsyncO t return iSAsync: public void setCpuCore(Integer num)t this. cpucore num public Integer getCpuCoreot return this. cpucore; public void setOsName(String name)t this csname name, public String getosNameOi return this csname public void setosVersion(String version)t this osversion version public string getS Versionot return this, osversion public void setMemTotal(Integer totaL)t this memtotal total public integer getMemTotalOt return this. memtotal public void setMemFree (Integer free)t this memfree free public Integer getMemFreeOt return this memfree 接下来我们看一下A門的接口与实现的源码,接口文件 org/apache/cloudstack/mgmtcap/ApiMgmt capacity Service. java package org. apache cloudstack. mgmtcap import org. apache cLoudstack api response. GetMgmtCapacityCmdResponse import com. cLoud utils. component. PluggableService; public interface ApiMgmt Capacity Service extends PluggableService i public void formatMgmt Server Capacity (GetMgmt Capacity CmdResponse response); 实现文件 org/apache/cloudstack/mgmtcap/ApiMgmt Capacity ServiceImpl java package org. apache cloudstack. mgmtcap; import java. util. ArrayList import java. util.List import jav∨x:ejb,LocL; mport org. apache CLoudstack api. command user. GetMgmt Capacity Cmd; import org. apache cloudstack api response. GetMgmt Capacity CmdResponse; import org. apache. 1og4j Logger import org. springframework stereotype Component; component OLocal(value =ApiMgmtCapacityService. class) public class ApiMgmt CapacityServiceImpl implements ApiMgmt CapacityServicei private static final Logger s_logger Logger. getLogger (ApiMgmt Capacity ServiceImpL class protected ApiMgmt CapacityServiceImplo t supero @Override public list<Class<?>> get)t List<Class<?>> cmdList new ArrayList<Class<?>>0; cmdList. add cgetMgmt Capacity Cmd. class); return cmdlist; @Override public void formatMgmtServer Capacity (GetMgmt Capacity CmdResponse response) response, set Cpu Core (runtime. getRuntime(.availableprocessorso) response. setosName (System. getProperty ("os name" )) response. setosVersion (System getProperty ("os version"D); response. setMemTotaL(Integer valueof ((int Runtime. getRuntimeO. totaLMemory o/1024))) esponse. setMemFree(Integer valueOf((int) (Runtime. getRuntimeO) freeMemory ()/1024)); 8.如果你是在 Eclipse里导入整个 Cloud stack的 Maven项目,那新加的 mgmtcap的工程也会在其中 cloud-plugin-api-limit-account-based scloud-plugin-api-mgmtcap 卢 Spring Ele Bsrc V org. apache cloudstack. api. commanduser J GetMgmtCapacityCmd java )G GetMgmt CapacityCmd V E org.apache cloudstack api resp >D GetMgmtCapacityCmdResponse java V E org. apache cloudstack. mgmtcap D ApiMgmtCapacityServicejava D ApiMgmtCapacityServicelmpl java > te E JRE System Library [avaSE-16 >E Maven Dependencies >targ JM pom iycloud-plugin-console-proxy 9.进入/ opt/cloudstack/ client目录,配置 pom.xm,这样在编译cen时也会编译到我们新加的AP 在/ opt/cloudstack/ client/pom.XIm的依赖里添加 <dependency> <groupId>org. apache cLoudstack</groupId kartifactId>cloud-plugin-api-mgmtcaplartifactId <version>sproject version]</version> </dependency? 在/ opt/cloudstack/ client/tomcatconf′ commands. properties.in里最后添加 件## get mgt server cap getMgmt Capacity=15 10.使用如下的方式编译并测试新加的AP 1)进入目录 opt/cloudstack 2)执行: mvn clean install 根据机器及网络环境,执行些命令需要几分钟到十几分钟,成功执行后,可以看到我们新加 的AP也构建了 [INFO] Apache CloudStack Plugin -Management Capacity... SUCCESS [10.824s 3)初始化数据库:mvn- P developer- Ddeploydb- pl developer 4)运行 web container: mvn- pl client jetty : run 以上命令执行完以后, Cloudstacki管理服务器就在运行状态了,接下来测试一下我们新加的AP 是否起作用,因为是开发环境,API服务器的8096端口默认就已经打开,我们可以直接测试如下 1)执行 getMgmtcapacity命令,注意大小写,可以通过浏览器或cu命令进行 curl"http:/ocalhost:8096/client/api?command=getmgmtcapacity 默认响应的类型是XML,上述命令返回 ?xml version="1.0"encoding="UTF-8"7><getmgmtcapacityresponse cloud stack-version="4. 3. 0-SNAPSHOT"><mgmtcapacity><since>4.2.0<, sincesxisasync>false</isasync><cpucore>8</cpucore><memtotaL261480< memtotaL><memfree>97227</memfree> sname>Mac OS X< csname><osversion>10. 9</osversion></mgmtcapacity>< getmgmtcapacityresponse> 也可以返回json格式,命令curl"http:localhost8096/client/api? command=getMgmtcapacity &response=json 上述命令返回 getmgmtcapacityresponse":imgmtcapacity I"since":4.2.0", "isasync": false, "cpucore":8, " memtotal": 261480, "memfree 95800,"osname": "Mac OS X","osversion": 10.9"33 3 2)调用AP|命令 istApis看我们新加的AP是否能找到 Apa:cUrl"http://ocalhost:8096/client/api?command=listapis&name=getmgmtcapacity' 上述命令返回: xmL version="1.0" encoding="UTF-8"><Listapisresponse cloud-stack ersion="4.3.0-SNAPSHOT"><count>1</ countxxapi><name>getMgmt Capacity< name><description>get management server capacity information by ApiMgmt CapacityService plugin</description><since>4. 2.0< since<i sasync>false</isasyncx<related><, related> <response> <name>osversion</name> <description> the os version<, description><type>string</type></response><response><namestypes names<description>response field type</description><typesstring</type response>cresponse><name>name</name><description>the name of the api command</description><type>string</type>< response><response><name>memfree</name><description>free memory in MB< description><type>integer</type></response><response> <name>since<, name><description>version of CloudStack the api was introduced in<, description><type>string</type></response><response><name>memtotal hame><description>total memory in MB</description><type>integer</type></ response<response><name>description</name><description> description of the api</description><type>string</type><. responsexresponsex<name>osname</name><description>the os name<. description><type>string</type></response><response><name>cpucore< name><description>cpu core number</description><type>integer</type>< response><response><name>isasync</name><description>true if api is asynchronous</description><type>boolean</typex</response></api>s, listapisresponse> 大功告成!以上只是按步骤写了如何添加,我们接下来看几个关键点,弄清楚这些,才是真正掌握。 CloudStack4.3里模块的层级结构 在 Cloud stack管理服务器启动时,会搜索固定目录下的一些文件,比如 resources∧META-|NF cloudstack/ module properties和*- context. xml,这些大家可以参照 cloud-framework- spring- module 工程。在初始化时,模块的上下文就如上图所示,我们新开发的AP的父级模块是api,再往上是 core,模块的实例化丛 bootstrap开始,整个树型结构就会遍历并完成对象的创建。

...展开详情
试读 12P cloudstack 开发
立即下载
限时抽奖 低至0.43元/次
身份认证后 购VIP低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
上传资源赚钱or赚积分
最新推荐
cloudstack 开发 10积分/C币 立即下载
1/12
cloudstack 开发第1页
cloudstack 开发第2页
cloudstack 开发第3页

试读结束, 可继续读1页

10积分/C币 立即下载