This repository has been archived by the owner on Jul 24, 2019. It is now read-only.
forked from icanhazdevops/DevOps-Challenges-Cycle1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge2.php
181 lines (154 loc) · 6.63 KB
/
challenge2.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
require 'vendor/autoload.php';
use OpenCloud\Rackspace;
use OpenCloud\Compute\Constants\ServerState;
use OpenCloud\Compute\Constants\Network;
$credsFile = $_SERVER['HOME'] . "/.rackspace_cloud_credentials";
// Define the function to loop while the servers are building
function waitLoop($servers, $timeout, $interval) {
while (true) {
$numLines = 0;
$completedServers = 0;
foreach ($servers as &$server) {
// Define finishing states
$states = array('ACTIVE', 'ERROR');
$startTime = time();
$server->refresh();
if ((time() - $startTime) > $timeout) {
echo sprintf("Request timed out: %s", str_pad($server->name(), 50));
$numLines = $numLines + 1;
return;
}
// If no error was returned, check on the progress of the server build
if (empty($server->error)) {
$name = "Building Cloud Server: " . $server->name();
$status = "Status: " . $server->status();
if (!isset($server->progress)) {
$server->progress = 0;
}
$progress = sprintf("Progress: %s%%", $server->progress);
if ($server->status == "BUILD") {
echo sprintf("%s\n", str_pad($name, 50));
echo sprintf("%s\n", str_pad($status, 50));
echo sprintf("%s\n\n", str_pad($progress, 50));
} elseif ($server->status == "ACTIVE") {
echo sprintf("Build complete: %s\n", str_pad($server->name(), 50));
echo sprintf("Server IP: %s\n", str_pad($server->accessIPv4, 50));
echo sprintf("root password: %s\n\n", str_pad($server->adminPass, 50));
}
// Add to the total number of lines to remove next round
$numLines = $numLines + 4;
} else {
// This server build failed
echo sprintf("Build failed: %s\n", str_pad($server->name(), 50));
$numLines = $numLines + 1;
}
if (in_array($server->status(), $states)) {
$completedServers++;
}
}
// Check to see if servers are still being built, and loop if so
if ($completedServers != count($servers)) {
echo chr(27) . "[0G"; // Move the cursor to the first column for overwriting
echo chr(27) . sprintf("[%sA", $numLines); // Move the cursor up $numLines lines for overwriting
// Sleep for $interval
sleep($interval);
} else {
// All servers are in ACTIVE or ERROR state. Exit the wait loop
return;
}
}
}
try {
// Try to open $credsFile and read the credentials from it
if (($cloudCreds = @parse_ini_file($credsFile)) == false) {
throw new Exception("Missing or unreadable INI file: " . $credsFile . "\n");
}
// Auth using credentials from $credsFile
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => $cloudCreds['username'],
'apiKey' => $cloudCreds['api_key']
));
// Set the compute region to DFW
$compute = $client->computeService('cloudServersOpenStack', 'DFW');
// Debian Wheezy image
$image = $compute->image('857d7d36-34f3-409f-8435-693e8797be8b');
// 512MB flavor
$flavor = $compute->flavor('2');
// Get the number of servers to build
$validInput = FALSE;
while ($validInput == FALSE) {
echo "How many servers would you like to build?\nInput a number from 1-3: ";
$handle = fopen ("php://stdin","r");
$numServers = trim(fgets($handle));
$filter_options = array(
'options' => array(
'min_range' => 1,
'max_range' => 3
)
);
if (filter_var($numServers, FILTER_VALIDATE_INT, $filter_options) == FALSE){
echo "Invalid input, please try again.\n\n";
} else {
$validInput = TRUE;
echo "\n";
}
}
// Get the name scheme for the servers (not validating input here)
echo sprintf("Input naming scheme for %s server(s)--will be of the form \$name#\n(blank will name numerically only): ", $numServers);
$handle = fopen ("php://stdin","r");
$nameScheme = trim(fgets($handle));
echo "\n";
// Get the filesystem path of the public SSH key to inject, or generate a new one if no input is given
$validInput = FALSE;
while ($validInput == FALSE) {
echo "Input the filesystem path of the SSH public key to inject for the root user\n(press ENTER with no input to generate a new keypair): ";
$handle = fopen ("php://stdin","r");
$publicKeyFile = trim(fgets($handle));
// Generate a unique ID for the SSH key name
$timestamp = time();
$keyName = sprintf("deployedkey-%s-%s", $nameScheme, $timestamp);
// Instantiate a keypair object
$keypair = $compute->keypair();
if (!is_readable($publicKeyFile) && $publicKeyFile != NULL) {
// TODO: 256 character limit for path+filename
echo "Invalid input. File does not exist or is unreadable. Please try again.\n\n";
} elseif ($publicKeyFile == NULL) {
$validInput = TRUE;
// Generate an SSH keypair for the server
$keypair->create(array(
'name' => $keyName
));
// Provide the SSH keypair to the user
echo sprintf("\nGenerated public key:\n%s\n",$keypair->getPublicKey());
echo sprintf("Generated private key:\n%s\n",$keypair->getPrivateKey());
} else {
$validInput = TRUE;
$keypair->create(array(
'name' => $keyName,
'publicKey' => file_get_contents($publicKeyFile)
));
echo "\n";
}
}
for ($serverCount = 1; $serverCount <= $numServers; $serverCount++) {
// Instantiate a server resource
$servers[$serverCount] = $compute->server();
// Spin it up
$buildResponse[$serverCount] = $servers[$serverCount]->create(array(
'name' => sprintf('%s%s', $nameScheme, $serverCount),
'image' => $image,
'flavor' => $flavor,
'networks' => array(
$compute->network(Network::RAX_PUBLIC),
$compute->network(Network::RAX_PRIVATE)
),
'keypair' => $keyName
));
}
// Loop, waiting for the servers to be built
waitLoop($servers, 600, 15);
} catch (Exception $e) {
die($e->getMessage());
}
?>