forked from icanhazdevops/DevOps-Challenges-Cycle1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge1.php
More file actions
94 lines (76 loc) · 2.54 KB
/
challenge1.php
File metadata and controls
94 lines (76 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
// Do the needful
require 'vendor/autoload.php';
use OpenCloud\Rackspace;
use OpenCloud\Compute\Constants\Network;
use OpenCloud\Compute\Constants\ServerState;
// Set the timezone
date_default_timezone_set('America/Chicago');
// Get the Credentials and pull them into a var
$auth_file = getenv('HOME') . "/.rackspace_cloud_credentials";
$auth_config = parse_ini_file($auth_file);
// Instantiate the Rackspace Object with a us endpoint
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => $auth_config['username'],
'apiKey' => $auth_config['api_key']
));
// Get variables to use for server creation
$datacenter = "ORD";
$imagename = "Fedora 19 (Schrodinger's Cat)";
$flavorname = "512MB Standard Instance";
$servername = "challenge1";
// Get the compute object
$compute = $client->computeService('cloudServersOpenStack', $datacenter);
// Get the image
$images = $compute->imageList();
while ($image = $images->next()) {
if($image->name == $imagename) {
printf("Found Image (%s)\n", $image->name);
$foundimage = $image;
}
}
// Get the flavor
$flavors = $compute->flavorList();
while ($flavor = $flavors->next()) {
if($flavor->name == $flavorname) {
printf("Found Flavor (%s)\n", $flavor->name);
$foundflavor = $flavor;
}
}
// Create the serever
$server = $compute->server();
try {
$response = $server->create(array(
'name' => $servername,
'image' => $foundimage,
'flavor' => $foundflavor,
'networks' => array(
$compute->network(Network::RAX_PUBLIC),
$compute->network(Network::RAX_PRIVATE)
)
));
} catch (\Guzzle\Http\Exception\BadResponseException $e) {
$responseBody = (string) $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();
$headers = $e->getResponse()->getHeaderLines();
printf('Status: %s\nBody: %s\nHeaders: %s\n', $statusCode, $responseBody, implode(', ', $headers));
}
// Wait for the server to finish
$callback = function($server) {
if(!empty($server->error)) {
var_dump($server->error);
exit;
} else {
echo sprintf("Waiting on %s/%-12s %4s%%\n",
$server->name(),
$server->status(),
isset($server->progress) ? $server->progress : 0
);
}
};
$newserver = $server->waitFor(ServerState::ACTIVE, 600, $callback);
// Print out the results
printf("Server name: %s\n", $server->name);
printf("Server ip address: %s\n", $server->accessIPv4);
printf("Server admin password: %s\n", $server->adminPass);
?>