Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OpenStack and PHP

OpenStack and PHP

An introduction to OpenStack and how to access it using PHP.

Be4677c2556e6af27a4c1c82dad3393b?s=128

Glen Campbell

March 15, 2014
Tweet

More Decks by Glen Campbell

Other Decks in Programming

Transcript

  1. March 2014 OpenStack and PHP Glen Campbell • Midwest PHP

    2014
  2. None
  3. http://rack.to/mwphp14

  4. 01

  5. 01

  6. None
  7. OpenStack concepts ✤ virtual infrastructure services ✤ accessed via HTTP

    (RESTful) API ✤ developed by an open-source community ✤ highly configurable
  8. OpenStack governance ✤ Independent foundation ✤ Board of Directors: some

    directors appointed by funding companies, others elected at-large ✤ Technical Committee: elected by active members ✤ Biannual releases and summit/workshops ✤ Project Technical Leaders
  9. OpenStack APIs ✤ Native REST ✤ EC2-compatible ✤ Internal (python)

  10. Compute ✤ Project “Nova” ✤ Supports multiple hypervisors including KVM,

    Xen ✤ Massively-scalable architecture ✤ Working towards interoperable workloads
  11. Storage ✤ Project “Swift” ✤ Elastic “blob” storage ✤ Can

    scale to regional or even global deployments ✤ Eventually consistent
  12. Networking ✤ Project “Neutron” (formerly “Quantum”) ✤ Dynamically create and

    manage L2/L3 networks ✤ Works with plugins for virtual network devices (OpenVSwitch, others)
  13. Dashboard ✤ Project “Horizon” ✤ Clean, simple user interface ✤

    Uses native API to communicate to services
  14. Shared Services ✤ Identity—“Keystone” ✤ Image—“Glance” ✤ Telemetry—“Ceilometer” ✤ Orchestration—“Heat”

  15. Other Projects ✤ OpenStack-SDKs ✤ Savanna—Hadoop provisioning ✤ Trove—Database ✤

    Bare metal—Ironic ✤ Queue—Marconi
  16. How to interact with OpenStack ✤ Control panel ✤ Command

    line client ✤ cURL ✤ SDK
  17. First Q&A

  18. Resources ✤ http://github.com/rackspace/php-opencloud ✤ http://developer.rackspace.com/ ✤ http://docs.openstack.org/

  19. Installing php-opencloud # Install Composer curl -sS https://getcomposer.org/installer | php

    # Require php-opencloud as a dependency php composer.phar require rackspace/php- opencloud:dev-master
  20. Example 1. Create a server

  21. 1.1 Authentication <?php require 'vendor/autoload.php'; use OpenCloud\Rackspace; print "Creating client...\n";

    $client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array( 'username' => getenv('OS_USERNAME'), 'apiKey' => getenv('NOVA_API_KEY'), 'tenantName' => getenv('OS_TENANT_NAME') ));
  22. 1.2 Attach print "Attaching to compute...\n"; try { $compute =

    $client->computeService( 'cloudServersOpenStack', 'IAD'); } catch (\Guzzle\Http\Exception\BadResponseException $e) { echo $e->getResponse(); die("OOPS\n"); }
  23. 1.3 Image print "Getting an image...\n"; $centos = $compute->image('85019bd8-fb5d-4230-b3bc-63b192800f28');

  24. 1.4 Flavor print "Getting a flavor...\n"; $flavor = $compute->flavor('performance1-1');

  25. 1.5 Create print "Creating a server...\n"; use OpenCloud\Compute\Constants\Network; $server =

    $compute->server(); try { $response = $server->create(array( 'name' => 'My lovely server', 'image' => $centos, 'flavor' => $flavor, 'networks' => array( $compute->network(Network::RAX_PUBLIC), $compute->network(Network::RAX_PRIVATE) ) ));
  26. 1.6 Exception Handling } catch (\Guzzle\Http\Exception\BadResponseException $e) { // No!

    Something failed. Let's find out: $responseBody = (string) $e->getResponse()->getBody(); $statusCode = $e->getResponse()->getStatusCode(); $headers = $e->getResponse()->getHeaderLines(); echo sprintf("Status: %s\nBody: %s\nHeaders: %s", $statusCode, $responseBody, implode(', ', $headers)); }
  27. 1.7 Poll use OpenCloud\Compute\Constants\ServerState; $callback = function($server) { if (!empty($server->error))

    { var_dump($server->error); exit; } else { echo sprintf( "\rWaiting on %s/%-12s %4s%%", $server->name(), $server->status(), isset($server->progress) ? $server->progress : 0 ); } }; $server->waitFor(ServerState::ACTIVE, 600, $callback);
  28. 1.8 Let's try it out LIVE DEMO

  29. Example 2. Listing flavors print "Attaching to compute...\n"; try {

    $compute = $client->computeService( 'cloudServersOpenStack', getenv('OS_REGION_NAME')); } catch (\Guzzle\Http\Exception\BadResponseException $e) { echo $e->getResponse(); } $list = $compute->flavorList(true); foreach($list as $flavor) { printf("%20s %-20s\n", $flavor->id(), $flavor->name()); }
  30. Example 3. File upload print "Attaching to object storage...\n"; try

    { $storage = $client->objectStoreService( 'cloudFiles', getenv('OS_REGION_NAME')); } catch (\Guzzle\Http\Exception\BadResponseException $e) { echo $e->getResponse(); die("OOPS\n"); }
  31. 3.1 create a container // create a container $container =

    $storage->createContainer('MidWestPHP'); if ($container === false) $container = $storage-
  32. 3.2 upload a file // create an object (empty) print

    "Creating an object...\n"; use OpenCloud\ObjectStore\Resource\DataObject; $container->uploadObject('myfile', fopen('upload_file.php', 'r+'));
  33. 3.3 list objects // list objects print "Listing objects:\n"; $files

    = $container->objectList(); while ($file = $files->next()) { printf("* %s (%d)\n.", $file->getName(), $file->getContentLength()); }
  34. Final Q&A

  35. https://github.com/gecampbell/mwphp14

  36. Let's keep in touch ✤ glen.campbell@rackspace.com ✤ Twitter: @glenc ✤

    http://developer.rackspace.com