大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

openstack中phpsdkphp-opencloud怎么用

这篇文章主要介绍了openstack中php sdk php-opencloud怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联-专业网站定制、快速模板网站建设、高性价比新兴网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式新兴网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖新兴地区。费用合理售后完善,10年实体公司更值得信赖。

   openstack提供的是rest风格的api接口,其说明文档见:

http://developer.openstack.org/api-ref-guides/bk-api-ref.pdf,为便于理解和简化调用,这里使用开源的php sdk——php-opencoud。

1 目录结构

    vendor

 ---------composer

 ---------guzzle

 ---------psr

 ---------rackspace

               --------------php-opencloud

               ---------------------docs

               ---------------------lib

                                               ---OpenCloud

               ---------------------samples

               ---------------------tests

    composer:提供自动加载的功能

    guzzle:处理http请求

    psr:提供日志功能

    rackspace/php-opencloud/lib/OpenCloud:php封装库

    OpenCloud不仅支持openstack的api,还支持Rackspace的api。

2 UML模型图

     openstack中php sdk php-opencloud怎么用

  由图中看到,可以从OpenStack客户端中根据需要获取不同的服务对象。计算服务用来管理云主机,通过主机类型、镜像、密钥对和网络等数据就可以创建一台云主机,而且能够对云主机做出暂停、恢复、停止、开始和重启等多种操作,还可以得到基于web vnc的控制台链接。对象存储服务是以容器的形式来存放数据对象,可以给对象名称设置路径前缀来对对象进行分类,组织成类似windows文件夹的结构。

3 示例代码

3.1 计算服务

 'admin',
    'password'   => 'ADMIN_PASS',
    'tenantName' => 'admin'
));
 
$computeService = $client->computeService('nova','regionOne');
 
//1.枚举镜像
$p_w_picpaths = $computeService->p_w_picpathList();
printf("Images-------------------------------------------------------------------------\n");
foreach($p_w_picpaths as $p_w_picpath)
{
    printf("ID:%s,Name:%s\n",$p_w_picpath->id,$p_w_picpath->name);
}
 
//2.枚举主机类型
$flavors = $computeService->flavorList();
printf("Flavors-------------------------------------------------------------------------\n");
foreach($flavors as $flavor)
{
    printf("ID:%-36s,Name:%-36s,Vcpus:%d,Ram:%d\n",$flavor->id,$flavor->name,$flavor->vcpus,$flavor->ram);
}
 
//3.枚举密钥对(for windows:加密密码传输 for linux:ssh登录)
$keypairs = $computeService->listKeypairs();
printf("Keypairs------------------------------------------------------------------------\n");
foreach($keypairs as $keypair)
{
    printf("Name:%s\n",$keypair->getName());
}
 
//4.创建云主机
$server = $computeService->server();
 
printf("Create Server-------------------------------------------------------------------\n");
$serverName = 'test123';
$p_w_picpathId = 'c36b0707-38ec-45e5-9d62-7e8798f1724f';
$flavorId = '53b1355a-c7c6-41a6-884f-7b1604002897';
$keypairName = 'testKey';
try
{
    $response = $server->create(array(
        'name' => $serverName,
        'p_w_picpathId' => $p_w_picpathId,
        'flavorId' => $flavorId,
        'keypair' => $keypairName,
        'metadata' => array(
            'admin_pass' => '123456',//设置密码
            'appid' => '123456789' //设置自定义元数据
        )));
}catch(BadResponseException $e)
{
    echo $e->getResponse();
}
 
printf("ID:%s,Name:%s,Status:%s\n",$server->id,$server->name,$server->status);
 
function GetServerState($server)
{
    printf("Status:%s\n",$server->status);
}
 
//wait for
$server->waitFor(ServerState::ACTIVE,null,GetServerState,1);
 
if (is_object($server->addresses))
{
    foreach ($server->addresses as $netkey => $netvalue)
    {
        printf("\tNetwork name:%s\n",$netkey);
        if (is_array($netvalue))
        {
            foreach ($netvalue as $infokey => $infovalue)
            {
                printf("\t\tIndex:%s\n",$infokey);
                printf("\t\t\tMac:    %s\n",$infovalue->{'OS-EXT-IPS-MAC:mac_addr'});
                printf("\t\t\tVersion:%s\n",$infovalue->version);
                printf("\t\t\tAddr:   %s\n",$infovalue->addr);
                printf("\t\t\tType:   %s\n",$infovalue->{'OS-EXT-IPS:type'});
            }
        }
    }
}
 
//5.获取vnc访问地址
printf("VNC-----------------------------------------------------------------------------\n");
$console = $server->console();
printf("Type:%s,URL:%s\n",$console->type,$console->url);
 
//6.停止云主机
printf("Stop Server---------------------------------------------------------------------\n");
$server->stop();
 
$server->waitFor('SHUTOFF',null,GetServerState,1);
 
//7.启动云主机
printf("Start Server--------------------------------------------------------------------\n");
$server->start();
$server->waitFor(ServerState::ACTIVE,null,GetServerState,1);
 
//8.重启云主机
printf("ReBoot Server-------------------------------------------------------------------\n");
//$server->reboot(ServerState::REBOOT_STATE_SOFT);//REBOOT_STATE_SOFT:软重启 REBOOT_STATE_HARD:硬重启
//$server->waitFor(ServerState::ACTIVE,null,GetServerState,1);
 
//9.删除云主机
printf("Delete Server-------------------------------------------------------------------\n");
$server->delete();
 
//10.获取镜像信息
printf("Image Info----------------------------------------------------------------------\n");
$p_w_picpath = $computeService->p_w_picpath($server->p_w_picpath->id);
printf("ID:%s,Name:%s\n",$p_w_picpath->id,$p_w_picpath->name);
 
//11.获取主机类型信息
printf("Flavor Info---------------------------------------------------------------------\n");
$flavor = $computeService->flavor($server->flavor->id);
printf("ID:%s,Name:%s,Vcpus:%d,Ram:%d\n",$flavor->id,$flavor->name,$flavor->vcpus,$flavor->ram);

3.2 对象存储服务

 'admin',
    'password'   => 'ADMIN_PASS',
    'tenantName' => 'admin'
));
 
//获取对象存储服务
$objectStoreService = $client->objectStoreService('swift', 'regionOne');
 
//1.创建容器(每个云盘使用者都有一个容器跟其对应)
$containerName = 'testContainer';
printf("Create Container %s------------------------------------------------------\n",$containerName);
//检查名字合法性
$objectStoreService->checkContainerName($containerName);
$container = $objectStoreService->createContainer($containerName);
if($container == false)
{
    $container = $objectStoreService->getContainer($containerName);
}
else
{
    //设置配额
    $container->setBytesQuota(5 * Size::GB); // 5GB
}
 
//2.枚举容器
$containers = $objectStoreService->listContainers();
printf("Containers--------------------------------------------------------------------------\n");
foreach ($containers as $container)
{
    $container = $objectStoreService->getContainer($container->name);
    printf("Name:%-60s,BytesUsed:%15.6fM,ObjectCount:%d",$container->name,$container->getBytesUsed() / 1024 / 1024,$container->getObjectCount());
    $bytesQuota = $container->getBytesQuota();
    if($bytesQuota)
        printf(",BytesQuota:%.6fM",$bytesQuota / 1024 / 1024);
 
    printf("\n");
}
 
//3.容器添加对象
$container = $objectStoreService->getContainer($containerName);
//添加文件
$customHeaders = array('Content-Type' => 'text/plain');
$fileData = fopen('D:/upload_small_object.txt', 'r');
$object = $container->uploadObject('upload_small_object.txt', $fileData,$customHeaders);
//添加目录(注意后面的斜杠)
$dirName = 'dir1/';
$object = $container->uploadObject($dirName,'');
//目录添加文件(文件放到目录中,必须在文件名前加上目录,形成类似a/b/c/d/1.txt结构(目录a/,目录a/b/,目录a/b/c/,目录a/b/c/d/))
$fileData = fopen('D:/upload_small_object.txt', 'r');
$object = $container->uploadObject($dirName . 'upload_small_object.txt', $fileData,$customHeaders);
//目录下再添加目录
$dirName = $dirName . 'dir1-1/';
$object = $container->uploadObject($dirName,'');
//二级目录下再添加文件
$fileData = fopen('D:/upload_small_object.txt', 'r');
$object = $container->uploadObject($dirName . 'upload_small_object.txt', $fileData,$customHeaders);
 
//4.枚举对象
printf("%s Objects-----------------------------------------------------------\n",$containerName);
//是目录的最后一个字符是斜杠
$objects = $container->objectList();
foreach ($objects as $object)
{
    printf("Name:%-60s,Size:%15.6fM,LastModified:%s\n",$object->getName(),$object->getContentLength(),$object->getLastModified());
}
 
//5.枚举目录下的对象(设定前缀)
printf("%s Objects---------------------------------------------------------------\n",$dirName);
$objects = $container->objectList(array('prefix' => $dirName));
foreach ($objects as $object)
{
    printf("Name:%-60s,Size:%15.6fM,LastModified:%s\n",$object->getName(),$object->getContentLength(),$object->getLastModified());
}
 
//6.下载文件
$object = $container->getObject($dirName . 'upload_small_object.txt');
$objectContent = $object->getContent();
$objectContent->rewind();
$stream = $objectContent->getStream();
file_put_contents('D:/123.txt', $stream);
 
//7.删除容器(删除云盘使用者时删除对应云盘)
$objects = $container->objectList();
//循环删除每个对象(不支持批删)
foreach ($objects as $object)
{
   $object->delete();
}
$container->delete();

感谢你能够认真阅读完这篇文章,希望小编分享的“openstack中php sdk php-opencloud怎么用”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


名称栏目:openstack中phpsdkphp-opencloud怎么用
本文路径:http://dzwzjz.com/article/phopic.html
在线咨询
服务热线
服务热线:028-86922220
TOP