装机平台之cobbler api接口的使用

我们可以利用cobbler 提供的api接口,进行快速的完成自动化装机。

对于这个有两种方式,第一种是用cobbler的cli

还有一种是采用cobbler官方提供的api接口。  是python写的,如果python底子够硬,完全可以二次开发了。 当然cobbler二次开发还是有必要的,我们那边已经做了少许的二次开发,让cobbler装机更加得心应手!

下面是官方的一些例子!

Pydoc

Pydoc is a useful tool to read module documentation. You can run the following to view all the public methods available to the Python API:

pydoc cobbler.api

pydoc cobbler.item_distro

pydoc cobbler.item_profile

pydoc cobbler.item_system

pydoc cobbler.item_image

pydoc cobbler.item_repo

The Basics

Every cobbler API script needs an API handle for starters. So always do something like this:

#!/usr/bin/python

import cobbler.api as capi

handle = capi.BootAPI()

The boot API handle is what we call a “singleton” which means you can call it wherever needed in your process, without needing to pass it around. There is a small startup cost, that depends on the size of your cobbler configuration, that you incur the very first time you load it. }}}

Walking Through Objects

Here’s an example that traverses all cobbler distributions. Other object types (we have distros, profiles, systems, repos, images, and soon .. networks) work exactly the same way.

#!/usr/bin/python

import cobbler.api as capi

handle = capi.BootAPI()

for x in handle.distros():

  print x.name

Modifying Objects

The following example takes all machines with “example.org” in the name of the system, assigns them to a new installation profiles, and toggles their PXE netboot flag such that the next time they power cycle they will be reinstalled (or upgraded, depdending on what that kickstart says) automatically.

#!/usr/bin/python

import cobbler.api as capi

handle = capi.BootAPI()

systems = handle.find_system(name=”*”,return_list=True)

for s in systems:

  print “assigning system for reinstallation: %s” % s.name 

  s.netboot_enabled = True

  handle.add_system(s)

Note that if we do not call “add_system” at the bottom, the changes will not be applied.

Modifying Objects Continued

When changing values on objects, make sure you call a method that starts with “set_” to make sure that your input is validated and the right operations are performed.

Unlike more verbose languages (Java), we do not use “get_” methods.

For example, to change kernel options, we would use “obj.set_kernel_options(value)” but to get the options from an object handle it is just “obj.kernel_options”.

Syncing

If cobbler ManageDhcp or ManageDns is used, any changes to the MACs, IPs, and hostnames of the systems should be followed with:

handle.sync()

after making changes to all the objects, to regenerate the DHCP/DNS configuration files and restart the services.

Searching

Suppose we want to find objects that match specific critiera, such as systems that have hostnames that end with a certain suffix.

This is easy to do:

#!/usr/bin/python

import cobbler.api as capi

handle = capi.BootAPI()

for x in handle.find_system(hostname=”*.example.org”,return_list=True):

  print x.name

The wildcards supported are from fnmatch.

pydoc fnmatch

Removing Objects

Here’s an example of how to delete an system object, here named “foo”

#!/usr/bin/python

import cobbler.api as capi

handle = capi.BootAPI()

handle.remove_system(“foo”)

Syncing Repos

It’s possible to sync repositories (i.e. download and mirror new updates) via the API, either for specific repos or all of them.

handle.reposync(name=”F10-updates-i386″, tries=1)

handle.reposync(tries=3)

The tries variable can retry down/busy mirrors.

Importing New Trees

The CLI command cobbler import can be executed from the API as well. It looks like this:

handle.import_tree(“/mnt/dvd/image”,”RHEL-5″,arch=”i386″)

There are more flags documented in the api.py file

Replicating From Another Cobbler Server

This is described at ReplicateMaster and looks like:

handle.replicate(cobbler_master=”centralcobbler.example.org”,sync_all=True)

There are other flags documented in api.py, though that is how a full replication is accomplished.

Power Management

Assuming you have power managment details stored on a cobbler system record, you can manipulate power state as follows:

sys = handle.find_system(name=”foo”)

handle.power_on(sys)

# OR

handle.power_off(sys)

# OR

handle.reboot(sys)

See PowerManagement for details

Kicking Off A New Deployment

The DeployFeature can be triggered via the API to request installation of new virtual machines using koan.

sys1 = handle.find_system(name=”myhost”)

sys2 = handle.find_system(name=”myguest”)

handle.manage_deployment(sys2, virt_host=sys1, method=”func”, operation=”install”)

# OR

handle.manage_deployment(sys2, virt_host=sys1, method=”ssh”, operation=”install”)

Notice that we can also use this same API to control the state of remote guests!

handle.manage_deployment(sys2, method=”ssh”, operation=”start”)

handle.manage_deployment(sys2, method=”ssh”, operation=”restart”)

handle.manage_deployment(sys2, method=”ssh”, operation=”shutdown”)

handle.manage_deployment(sys2, method=”ssh”, operation=”unplug”)  # a less friendly shutdown

handle.manage_deployment(sys2, method=”ssh”, operation=”uninstall”)

Note that once we install the VM we just need to pass the guest handle in, we can forget about which host it was running on since cobbler keeps track of this for us.

Other Actions

Various other features of Cobbler may not be listed above, but are all doable via the Python API.

Consult the pydoc for Cobbler to read more about what is possible, as well as (if you like) read over the Cobbler source to get a feel for what it does. It should be pretty easy to get a hold of, and if not, please ask us.

pydoc cobbler.api


大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

发表评论

邮箱地址不会被公开。 必填项已用*标注