#
# Proxmox
#
from vcpsdk.plugins.spec import VcpSpecResource
CCI_VERSION = "1.0"
BUILD_VERSION = "20251001"
[ドキュメント]
class VcpSpecResourceProxmox(VcpSpecResource):
"""
サンプルコード
.. code-block:: python
spec = sdk.get_spec("proxmox", "small")
#
# 変更できること
#
# spec.num_nodes = 1
# spec.params_v = ['/opt:/opt']
# spec.params_e = ['USER_NAME=test']
# spec.ip_addresses = ['起動するnodeの静的なIPアドレス']
# Proxmox 依存
# https://search.opentofu.org/provider/telmate/proxmox/latest/docs/resources/vm_qemu
# spec.num_cpus = 2
# spec.memory = 2048 # MB
# spec.disk_size = 40
# spec.user_name = "testuser"
# spec.mac_addresses = "BC:24:11:B2:A1:01"
# クラウドイメージ(テンプレートとするVMWareの仮想マシンの名前)
# spec.cloud_image = 'vmtemplate01'
# base containerにssh loginするためのssh公開鍵情報を設定
spec.set_ssh_pubkey('tmp/id_rsa.pub')
"""
version = CCI_VERSION + "+" + BUILD_VERSION
vcp_config_key = "proxmox"
def __init__(self, provider_name, flavor, config_dir):
# flavor から spec を設定
super().__init__(provider_name, flavor, config_dir)
# spec attributes名の設定チェックの回避
super().__setattr__("proxmox_cci_", None)
# credentail情報は、vcp_config から設定
self.proxmox_cci_ = {"cci_version": CCI_VERSION}
self.proxmox_cci_["token_id"] = self.vcp_config_[self.vcp_config_key]["token_id"]
self.proxmox_cci_["token_secret"] = self.vcp_config_[self.vcp_config_key]["token_secret"]
self.unit_cci_["private_network"] = self.vcp_config_[self.vcp_config_key][
"private_network"
] # VPNネットワーク名
self.proxmox_cci_["user_name"] = self.vcp_config_[self.vcp_config_key].get('user_name', 'ubuntu')
# flavorから初期化
self.proxmox_cci_["num_cpus"] = self.flavor_["num_cpus"]
self.proxmox_cci_["memory"] = self.flavor_["memory"]
self.proxmox_cci_["disk_size"] = self.flavor_["disk_size"]
self.proxmox_cci_["cloud_image"] = "default"
# 初期化
self.proxmox_cci_["ip_address_list"] = []
self.proxmox_cci_["mac_address_list"] = []
def __str__(self):
text = """
========================
{provider_name}
------------------------
{unit_info}""".format(
provider_name=self.unit_cci_["cloud_provider"], unit_info=super().__str__()
)
text += """
num_cpus: {num_cpus}
memory: {memory}
disk_size: {disk_size}
cloud_image: {cloud_image}""".format_map(
self.proxmox_cci_
)
text += """
tags: {}
========================""".format(
self.tags_cci_
)
return text
[ドキュメント]
def cci(self, name):
"""
CCI生成
:param name: unit名
:return: CCI文字列
"""
# cloud parameter 情報
my_cci = super().cci(name)
# yaml schema check
self.cci_schema_.validate(self.proxmox_cci_, "proxmox")
# cloud_params
my_cci += """
cloud_provider: proxmox
cloud_params:
cci_version: "{cci_version}"
num_cpus: {num_cpus}
disk_size: {disk_size}
memory: {memory}
cloud_image: {cloud_image}
ip_address_list: {ip_address_list}
mac_address_list: {mac_address_list}
token_id: "{token_id}"
token_secret: "{token_secret}"
user_name: {user_name}
""".format_map(
self.proxmox_cci_
)
my_cci += self.cci_tags()
return my_cci
@property
def user_name(self):
"""
onpremisesに依存する ssh login名
"""
return self.proxmox_cci_["user_name"]
@user_name.setter
def user_name(self, v):
self.proxmox_cci_["user_name"] = v
@property
def num_cpus(self) -> int:
"""
Proxmoxに依存するnum_cpus
.. note::
* VCP SDK flavorで設定可能
"""
return self.proxmox_cci_["num_cpus"]
@num_cpus.setter
def num_cpus(self, v: int):
self.proxmox_cci_["num_cpus"] = v
@property
def memory(self) -> int:
"""
Proxmoxに依存するmemory
.. note::
* VCP SDK flavorで設定可能
"""
return self.proxmox_cci_["memory"]
@memory.setter
def memory(self, v: int):
self.proxmox_cci_["memory"] = v
@property
def disk_size(self) -> str:
"""
Proxmoxに依存するdisk_size (単位:GB)
.. note::
* VCP SDK flavorで設定可能
"""
return self.proxmox_cci_["disk_size"]
@disk_size.setter
def disk_size(self, v: str):
self.proxmox_cci_["disk_size"] = v
@property
def ip_addresses(self):
"""
静的ip_address_list
"""
return self.proxmox_cci_["ip_address_list"]
@ip_addresses.setter
def ip_addresses(self, v):
"""
静的ip_address_list
.. note::
`num_nodes`と`ip_addresses`が両方セットされた場合、num_nodesは、指定したアドレスの数になる。
また、mac_address_listと同時に設定された場合、両者の数が同じでなければならない。
"""
if 0 < len(v):
# アドレスの指定がある場合、num_nodesはアドレスの数になる
self.unit_cci_["num_nodes"] = len(v)
self.proxmox_cci_["ip_address_list"] = v
@property
def mac_addresses(self):
"""
mac_address_list
"""
return self.proxmox_cci_["mac_address_list"]
@mac_addresses.setter
def mac_addresses(self, v):
"""
mac_address_list
.. note::
`num_nodes`と`mac_address_list`が両方セットされた場合、num_nodesは、指定したアドレスの数になる。
また、ip_addressesと同時に設定された場合、両者の数が同じでなければならない。
"""
if 0 < len(v):
if 0 < len(self.proxmox_cci_["ip_address_list"]) and \
len(self.proxmox_cci_["ip_address_list"]) != len(v):
raise AttributeError("Count of addressses is not match, ip_addresses={} and mac_address_list={}".format(len(self.proxmox_cci_["ip_address_list"]), len(v)))
# アドレスの指定がある場合、num_nodesはアドレスの数になる
self.unit_cci_["num_nodes"] = len(v)
self.proxmox_cci_["mac_address_list"] = v
# property: cloud_image
@property
def cloud_image(self):
"""
Proxmoxに依存するクラウドイメージ(テンプレートとする仮想マシンの名前)
"""
return self.proxmox_cci_["cloud_image"]
@cloud_image.setter
def cloud_image(self, v):
self.proxmox_cci_["cloud_image"] = v
@VcpSpecResource.num_nodes.setter
def num_nodes(self, v):
"""
起動するnodeの個数
.. note::
`num_nodes` と `ip_addresses` は最後にセットされたほうが有効になる。
"""
self.unit_cci_["num_nodes"] = v
self.proxmox_cci_["ip_address_list"] = []
self.proxmox_cci_["mac_address_list"] = []
def __setattr__(self, name, value):
"""
spec の attributes に不正な値を設定した場合に例外を発生
:param name: attributes名
:param value: 設定値
"""
if not hasattr(self, name):
raise AttributeError("spec resource has no attributes!! [{}]".format(name))
super().__setattr__(name, value)