這篇文章主要介紹了openstack中php sdk php-opencloud怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比新興網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式新興網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋新興地區(qū)。費用合理售后完善,10年實體公司更值得信賴。
openstack提供的是rest風格的api接口,其說明文檔見:
http://developer.openstack.org/api-ref-guides/bk-api-ref.pdf,為便于理解和簡化調(diào)用,這里使用開源的php sdk——php-opencoud。
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。
由圖中看到,可以從OpenStack客戶端中根據(jù)需要獲取不同的服務(wù)對象。計算服務(wù)用來管理云主機,通過主機類型、鏡像、密鑰對和網(wǎng)絡(luò)等數(shù)據(jù)就可以創(chuàng)建一臺云主機,而且能夠?qū)υ浦鳈C做出暫停、恢復(fù)、停止、開始和重啟等多種操作,還可以得到基于web vnc的控制臺鏈接。對象存儲服務(wù)是以容器的形式來存放數(shù)據(jù)對象,可以給對象名稱設(shè)置路徑前綴來對對象進行分類,組織成類似windows文件夾的結(jié)構(gòu)。
'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.創(chuàng)建云主機 $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',//設(shè)置密碼 'appid' => '123456789' //設(shè)置自定義元數(shù)據(jù) ))); }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);
'admin', 'password' => 'ADMIN_PASS', 'tenantName' => 'admin' )); //獲取對象存儲服務(wù) $objectStoreService = $client->objectStoreService('swift', 'regionOne'); //1.創(chuàng)建容器(每個云盤使用者都有一個容器跟其對應(yīng)) $containerName = 'testContainer'; printf("Create Container %s------------------------------------------------------\n",$containerName); //檢查名字合法性 $objectStoreService->checkContainerName($containerName); $container = $objectStoreService->createContainer($containerName); if($container == false) { $container = $objectStoreService->getContainer($containerName); } else { //設(shè)置配額 $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結(jié)構(gòu)(目錄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.枚舉目錄下的對象(設(shè)定前綴) 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.刪除容器(刪除云盤使用者時刪除對應(yīng)云盤) $objects = $container->objectList(); //循環(huán)刪除每個對象(不支持批刪) foreach ($objects as $object) { $object->delete(); } $container->delete();
感謝你能夠認真閱讀完這篇文章,希望小編分享的“openstack中php sdk php-opencloud怎么用”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!