IBM PureApplication System中的脚本

PureApplication System命令行界面(CLI)工具提供了脚本环境,您可以在其中运行命令以远程管理PureApplication System。 您还可以运行Python脚本或单个Python命令。 脚本环境基于Jython(基于Java的Python实现)。 Jython解释器实现了某些Python 2.5.1语言。 CLI使用Jython解释器以及标准Jython库,函数和类来帮助您管理PureApplication System环境。

除了基于Jython的脚本环境外,PureApplication System还提供了REST API,使您可以用任何编程语言编写脚本。

入门

在开始之前,请转到PureApplication System知识中心并为以下两个主题添加书签:

您可以从PureApplication System欢迎页面下载PureCLI。 图1显示了下载屏幕:

图1.下载CLI
下载CLI

该下载文件是一个zip文件,因此请将其解压缩到/purecli.等目录中/purecli. 。 清单1显示了PureCLI安装的示例目录结构:

清单1. PureCLI目录结构
├── bin
│  ├── 2.0.0.0-201409151314631
│  └── 2.0.0.1-201411251434417
├── lib
│  ├── 2.0.0.0-201409151314631
│ │  ├── admin
│ │ │  ├── resources
│ │ │  ├── settings
│ │ │  └── troubleshooting
│ │  ├── deployer
│ │ │  ├── messages
│ │ │ │  └── ext
│ │ │  ├── resources
│ │ │  └── settings
│ │  └── utility
│  └── 2.0.0.1-201411251434417
│      ├── admin
│      │  ├── resources
│      │  ├── settings
│      │  └── troubleshooting
│      ├── deployer
│      │  ├── messages
│      │ │  └── ext
│      │  ├── resources
│      │  └── settings
│      └── utility
├── logs
└── samplesCODE

bin和lib目录具有两个版本的子目录:2.0.0.0和2.0.0.1。 如果PureCLI在IBM PureApplication System中检测到新版本,它将自动更新为新版本。

Hello World示例

使用PureCLI,等效于Hello World示例是从命令行访问PureApplication System。 进入bin目录并执行pure (在Windows®上为pure.bat )。 它应该给您清单2中显示的结果:

清单2.执行纯
pure -h [HOST] -u [USER] -p [PASSWORD]
    pure -h [HOST] -u [USER] -p [PASSWORD] -c [COMMAND]
    pure -h [HOST] -u [USER] -p [PASSWORD] -f [SCRIPT_FILE] [ARG]*

验证主机名或IP地址正确,并且运行CLI命令的计算机具有到端口443的网络连接。使用正确的主机和凭据修改命令行,再次执行它,然后应该获得清单3的输出:

清单3.连接到PureApplication System
Welcome to the IBM PureApplication System CLI.  Enter "help" if you need help getting started.
>>>

输入help命令以开始使用。 清单4显示了帮助的摘录:

清单4.帮助摘录
In addition to the standard Jython libraries, the CLI provides a rich set of functions and classes
in the admin and deployer packages to help you manage your system.  More extensive help is available
for the deployer and admin packages by entering:
   >>> help(deployer)
   >>> help(admin)

现在,您可以开始使用PureCLI进行一些实际工作。

使用PureCLI

如上节所述,您可以交互使用PureCLI。 交互模式的用途包括在将命令放入文件之前测试命令,以及获得有关PureCLI命令的帮助。 例如,要了解您的PureApplication System中有多少个计算节点,请使用以下命令:

  • 输入help(admin)以获取有关可用功能的帮助。 您会看到有一个名为admin.computenodes函数。
  • 执行admin.computenodes以获取有关计算节点的JSON格式对象的列表。
  • 要获取计算节点的总数,请输入len(admin.computenodes) 。 它返回数字6,这意味着您在PureApplication System中有六个计算节点。

作为典型输出的示例,清单5显示了单个计算节点的JSON对象:

清单5.计算节点JSON对象
{
  "acl": (nested object),
  "architecture": "10X",
  "cloud": (nested object),
  "energystats": (nested object),
  "firmwarelevel": "1AOO56G",
  "healthstats": (nested object),
  "id": "176bc2b8-a41c-4468-9236-cb1f62dcac64",
  "leds": (nested object),
  "locations": (nested object),
  "machinetype": "7863",
  "name": "SN#10B4E6B",
  "numberofpowercycles": 3,
  "physicalcpus": (nested object),
  "physicalioadapters": (nested object),
  "physicalmemory": (nested object),
  "powerstate": "powered_on",
  "pvuvalue": 70,
  "state": "available",
  "vios": (nested object),
  "virtualmachines": (nested object)
}

许多属性都有(nested object)作为值。 要获取嵌套对象的JSON对象,只需在函数调用中输入属性名称。 例如,要获取第一个计算节点的所有虚拟机,请输入命令: admin.computenodes[0].virtualmachines

PureCLI提供了许多对象和功能,您可以从命令行控制所有PureApplication System,而无需使用浏览器GUI。

如清单2所示,您可以使用PureCLI执行脚本文件,这是通常使用CLI的方式。 交互式PureCLI是学习和测试各种方法以获取所需内容的好方法,并且当您知道所需内容时,可以将命令放入脚本中。

管理工作量

工作负载是在PureApplication System中运行的模式的一个实例。 模式是PureApplication System的关键技术之一,而模式是应用程序的完整体系结构。 使用PureCLI,您可以从命令行部署和管理模式,还可以脚本化和自动化部署以及其他相关任务。

PureCLI命令help(deployer)提供了用于管理工作负载的可用功能。 以下部分提供了使用PureCLI的示例。

部署模式

使用PureCLI脚本的真实示例是部署模式并查看部署状态。 清单6显示了可用于部署模式的脚本,清单7显示了部署状态。

清单6中的deploy脚本接受两个命令行参数,即pattern nameenvironment profile name 。 部署模式只需要两个参数。 开始部署时,虚拟实例ID将打印到控制台并保存到文件中以备后用。

清单6.部署模式
import sys
import time
import os

if len(sys.argv)!=3:
  print "Usage: %s <pattern name> <environment profile name>"
  exit(1)

#pattern name and env profile name as arguments
patternName=sys.argv[1]
envProfileName=sys.argv[2]
ctime=time.strftime("%H%M%S")
print "Deploying pattern '%s' to profile '%s'..." %  \
  (patternName,envProfileName)
#retrieve pattern and environment profile objects from PureApplication
#based on given names
mypattern=deployer.virtualsystempatterns.list({"app_name": patternName})[0]
envprofile=deployer.environmentprofiles.list({'name':envProfileName})[0]
deploymentParams={
  'environment_profile':envprofile
}
#initiate deployment
virtualInstance=mypattern.deploy("%s %s" %
                                (patternName,ctime),deploymentParams)
#get instance ID
virtualInstanceID=virtualInstance.id
#print out ID and save ID to a file, for later use
print "Pattern %s is deploying. ID: %s" % (patternName,virtualInstanceID)
f=open("virtualInstanceID","w")
f.write(virtualInstanceID)
f.close()

部署开始后,可以使用清单7中的脚本查看部署状态:

清单7.获取部署状态
import sys
import time
import os

if len(sys.argv)==2:
  #read virtual instance id from command line
  virtualInstanceID=sys.argv[1]
else:
  #read id from a file
  f=open("virtualInstanceID","r")
  virtualInstanceID=f.read().strip()
  f.close()
#get instance
instances=deployer.virtualsysteminstances.list({'id':virtualInstanceID})
if len(instances)==0:
  print "Instance %s does not exist." % (virtualInstanceID)
else:
  instance=instances[0]
  #print deployment history
  history=instance.history
  for line in history:
    print "%s: %s" % (line['created_time'],line['current_message'])
  name=instance.deployment_name
  status=instance.status
  print 'Instance "%s"... %s' % (name,status)
  if status=="RUNNING":
    #list virtual machines
    print 'Virtual machines:'
    virtualmachines=instance.virtualmachines
    for vm in virtualmachines:
      name=vm.displayname
      ip=vm.ip
      hostname=ip.userhostname
      ipaddr=ip.ipaddress
      print "  %s (%s): %s" % (hostname,ipaddr,name)

该脚本从清单6中的部署脚本编写的文件中读取虚拟实例ID。您还可以提供虚拟实例ID作为命令行参数。 该ID用于获取实例对象,该脚本会打印出部署历史记录。 如果实例正在运行,则脚本将显示虚拟系统实例中每个虚拟机的名称,主机名和IP地址。 清单8显示了部署状态的示例:

清单8.部署状态
2015-03-23T08:56:27.047Z: Deployment has been queued
2015-03-23T08:56:30.058Z: Reserving cloud resources
2015-03-23T08:56:32.528Z: Generating model for topology and network
2015-03-23T08:56:37.408Z: Pattern deployment starting
2015-03-23T08:56:38.398Z: Registering virtual system sjs Base OS 105502
2015-03-23T08:58:40.275Z: Added new disk for virtual machine OS_Node.11427100964512 of size 20 GB
2015-03-23T08:58:40.876Z: Starting virtual machines in virtual system sjs Base OS 105502
2015-03-23T08:58:40.898Z: Starting virtual machine OS_Node.11427100964512
2015-03-23T08:59:52.763Z: Executing script packages
2015-03-23T09:00:08.451Z: 
     Executing script package Default add disk on virtual machine OS_Node.11427100964512
2015-03-23T09:00:17.143Z: 
     Script package Default add disk on virtual machine OS_Node.11427100964512 completed successfully
2015-03-23T09:00:17.299Z: 
     Executing script package Must Gather Logs on virtual machine OS_Node.11427100964512
2015-03-23T09:00:22.378Z: 
     Script package Must Gather Logs on virtual machine OS_Node.11427100964512 completed successfully
2015-03-23T09:00:23.053Z: 
     Virtual machines are ready
Instance "sjs Base OS 105502"... RUNNING
Virtual machines: ipas3-cg1-vm27.stgt.spc.ihost.com (172.17.63.27): OS_Node.11427100964512

如果需要,可以使用命令deployer.virtualsysteminstances.delete(virtualInstanceID)删除正在运行的实例。

使用REST API

PureCLI非常有用,而REST API甚至更有用-实际上,PureCLI本身使用REST API。 要使用REST API,您无需下载任何内容-您可以从命令行使用curl或使用喜欢的编程语言开始使用它。 清单9显示了如何使用curl命令访问REST API:

清单9.对PureApplication System的curl访问
curl --cacert <IPASHOSTORIP> -k -H "X-IBM-PureSystem-API-Version: 1.0" 
-H "Accept: application/json" --user <UID>:<PWD> https://<IPASHOSTORIP><RESTPATH>

IPASHOSTORIP是PureApplication System的地址,UID和PWD是不言自明的,而RESTPATH是资源的路径。

列出共享服务

REST API的一个明显用例是,当您需要一些在PureCLI中不容易获得的东西时,例如获得共享服务实例的列表以及它们使用了多少RAM和CPU。

REST API的输出是JSON。 如果使用Python编写脚本,则simplejson模块会将JSON字符串转换为易于使用的Python对象。

清单10显示了一个脚本,该脚本使用REST API调用列出了共享服务。 该脚本包括一个用于卷曲的Python包装器,可使用普通的python解释器执行该脚本,而无需PureCLI:

清单10.使用REST API获取共享服务实例
import sys
import time
import os
import simplejson

if len(sys.argv)!=4:
  print "Usage: %s <ip address> <uid> <pwd>" % sys.argv[0]
  exit(1)

pureappIPAddress=sys.argv[1]
uid=sys.argv[2]
password=sys.argv[3]

#call REST API using curl and return JSON
def callREST(path):
  cmd=["curl","--cacert",pureappIPAddress,"https://%s%s" %
     (pureappIPAddress,path),  
     "-k","-H",'"X-IBM-PureSystem-API-Version: 1.0"',
     "-H",'"Accept: application/json"', "--user"," %s:%s" % (uid,password)]
  cmd= " ".join(cmd)
  fname="output.json"
  rc= os.system(cmd+" > "+fname+" 2> err_output.txt")
  outputfile=open(fname,'r')
  jsonStr=outputfile.read()
  outputfile.close()
  json = simplejson.loads(jsonStr)
  if rc != 0:
    print "some error happened: %d" % rc
  return json

path="/resources/sharedServices/"
json=callREST(path)
print "Available services:"
alreadyPrinted=[]
for svc in json:
  name=svc["app_name"]
  if name not in alreadyPrinted:
    print "  %s" % (name)
    alreadyPrinted.append(name)
path="/resources/sharedServiceInstances/"
json=callREST(path)
print "Deployments:"
totalramforsvc=0
totalcpuforsvc=0
for svc in json:
  print "  Name: %s" % svc["deployment_name"]
  totalram=0
  totalcpu=0
  for instance in svc["instances"]:
    ram=instance["memory"]
    totalram=totalram+ram
    vcpu=instance["cpucount"]
    totalcpu=totalcpu+vcpu
    cloudgroup=instance["location"]["cloud_group"]["name"]
    print "    %s (RAM: %d MB, vCPU: %d) %s" %
          (instance["displayname"],ram,vcpu,cloudgroup)
  print "    Total: RAM: %.2f GB, vCPU: %d" % (totalram/1000.0,totalcpu)
  totalramforsvc=totalramforsvc+totalram
  totalcpuforsvc=totalcpuforsvc+totalcpu
print "Totals for shared services:"
totalramforsvc=totalramforsvc/1000.0
print "  RAM : %.2f GB" % totalramforsvc
print "  vCPU: %d" % totalcpuforsvc

上面的脚本通过curl包装函数调用REST API,并返回JSON。 然后打印列出所有共享实例的JSON相关部分,以及有关服务的一些信息。 这是输出的摘录:

清单11. sharedsvc脚本的输出摘录
Name: Caching Service
  Caching-Catalog.21421683118601 (RAM: 16768 MB, vCPU: 2) ipas3-CloudGroup1
  Caching-Catalog.11421683118600 (RAM: 16768 MB, vCPU: 2) ipas3-CloudGroup1
  Caching-Container.11421683118602 (RAM: 15744 MB, vCPU: 2) ipas3-CloudGroup1
  Caching-Master.11421683118599 (RAM: 16768 MB, vCPU: 2) ipas3-CloudGroup1
  Total: RAM: 66.05 GB, vCPU: 8
Name: ELB Proxy Service
  Services-elbInstance.11422448392023 (RAM: 4864 MB, vCPU: 2) ipas3-CloudGroup2
  Services-elbManagement.11422448392022 (RAM: 2432 MB, vCPU: 1) ipas3-CloudGroup2
  Services-elbInstance.21422448392024 (RAM: 4864 MB, vCPU: 2) ipas3-CloudGroup2
  Total: RAM: 12.16 GB, vCPU: 5
Totals for shared services:
  RAM : 146.43 GB
  vCPU: 36

结论

本教程显示了一些使用IBM PureApplication System CLI和REST API的示例。 这两个工具都有很好的文档记录,您可以使用它们来自动执行许多与PureApplication System相关的任务。 PureApplication System中的访问控制机制使这些工具可供PureApplication System的所有用户使用,从系统管理员到部署单个开发环境供个人使用的个体承包商。

致谢

作者要感谢IBM同事MarcusTörnqvist审阅本文。


翻译自: https://www.ibm.com/developerworks/websphere/library/techarticles/1504_salkosuo/1504_salkosuo.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值