Giter VIP home page Giter VIP logo

mcclient_python's Introduction

Cloudpods Python 3 SDK

本仓库提供了访问Cloudpods API的Python 3 SDK以及基于该SDK的climc客户端代码。

命令行使用方法

首先准备rc文件,内容如下:

export OS_USERNAME=sysadmin
export OS_PASSWORD=MXX2VKe067jtD
export OS_PROJECT_NAME=system
export OS_DOMAIN_NAME=Default
export OS_AUTH_URL=http://10.68.22.1:5000/v3
export OS_REGION_NAME=LocalTest

首先source该rc文件,然后执行climc,例如:

$ source ~/rc_admin
$ climc server-list # 列出主机
$ climc image-list # 列出模板
$ climc server-create --disk <image_id> --disk 40g --mem 2g --ncpu 2 --allow-delete test --hypervisor esxi # 以模板 image_id 创建主机,数据盘40g,内存2g,虚拟CPU 2核,允许删除,机器名称test,虚拟化平台为esxi (VMWare)

SDK调用方法

首先安装yunionclient,执行

sudo python3 setup.py install

依赖安装包:

  • argparse
  • prettytable
  • httplib2
  • pycrypto>=2.6

示例代码:

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

import yunionclient.api.client

desc = {
    'project_name': 'system',
    'project_id': None,
    'args': (
        'https://nn.nnn.nnn.nnn:30357/v3',  # auth_url
        'sysadmin',                         # username
        'pppppppppppppppp',                 # password
        None,                               # domain
    ),
    'kwargs': {
        'region': 'YunionHQ',
        'zone': None,
        'insecure': True,
        'endpoint_type': 'publicURL',
    },
}

args = desc['args']
kwargs = desc['kwargs']
client = yunionclient.api.client.Client(*args, **kwargs)
project_name = desc.get('project_name')
project_id = desc.get('project_id')
if project_name is not None or project_id is not None:
    client.set_project(project_name=project_name, project_id=project_id)

# List all public images
imgs, total, limit, offset = client.images.list(is_public='false')
if len(imgs) == 0:
    raise Exception('No image found')

def waitStatus(guest, xstatus):
	import time
	while True:
		time.sleep(1)
		guest = client.guests.get(guest['id'])
		status = guest['status']
		print('guest status: {}'.format(status))
		if status in xstatus:
			return

# Create a guest server with the 1st image in the list
img_id = imgs[0]['id']
params = {}
params['name'] = 'test'
params['vcpu_count'] = 1
params['vmem_size'] = 64 # memory size 64MB
params['disable_delete'] = False
params['disks'] = [
    {
        'index': 0,
        'image_id': img_id,	# rootfs for operating system
    },
    {
        'index': 1,
        'size': '1024',		# data disk 1024MB
    },
]

# To do batch create, call client.guests.batch_create(3, **params).  When count
# is greater than 1, the returned value will be a list of created guests
guest = client.guests.create(**params)
if isinstance(guest, list) and len(guest) > 0:
    guest = guest[0]['body']
print('guest created:', guest)

print('start guest when it\'s ready')
waitStatus(guest, ['ready'])
client.guests.perform_action(guest['id'], 'start')
waitStatus(guest, ['running'])

print('put it into recycle bin (pending_deleted=True)')
client.guests.delete(guest['id'])
waitStatus(guest, ['ready'])

print('real delete it to actually reclaim resources')
client.guests.delete(guest['id'], override_pending_delete=True)
from yunionclient.common import exceptions
try:
	waitStatus(guest, [])
except exceptions.NotFound:
	print('guest deleted')

mcclient_python's People

Contributors

yousong avatar zexi avatar swordqiu avatar dependabot[bot] avatar ioito avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.