-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_3_like_servers.php
160 lines (134 loc) · 5.34 KB
/
build_3_like_servers.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
<?php
$cloud_credentials_file = '/path/to/.credentials_file';
$server_build_number = '3';
require_once ('/usr/lib/php5/php-opencloud/lib/rackspace.php');
require_once ('/usr/lib/php5/php-opencloud/lib/compute.php');
/////////////// Change Above //////////////////
define('INIFILE', $cloud_credentials_file);
$ini = parse_ini_file(INIFILE, TRUE);
if (!$ini) {
printf("Unable to load .ini file [%s]\n", INIFILE);
exit;
}
$conn = new OpenCloud\Rackspace(RACKSPACE_US, array('username' => $ini['rackspace_cloud']['NOVA_USERNAME'], 'apiKey' => $ini['rackspace_cloud']['NOVA_API_KEY']));
$isCLI = (php_sapi_name() == 'cli');
if ($isCLI == 'true') {
$shortopts = "R::";
$shortopts .= "S:";
$shortopts .= "F:";
$shortopts .= "I:";
$shortopts .= "V";
$options = getopt($shortopts);
/// Get the Region if its needed, will default to credentials file if it is set in that file.
if (isset($options['R'])) {
$working_region = $options['R'];
}
elseif (isset($ini['rackspace_cloud']['NOVA_REGION_NAME'])) {
$working_region = $ini['rackspace_cloud']['NOVA_REGION_NAME'];
}
else {
echo 'No region found in credentials file or given at the command line'."\n";
usage();
}
/// Get Server Name
if (isset($options['S'])) {
$server_name = $options['S'];
if (!(preg_match("/^([a-z0-9]+-)*[a-z0-9]+$/i", $server_name))) {
print "Server name $server_name is not a valid name.\n";
usage();
}
}
else {
usage();
}
/// Get Flavor (Server Size)
if (isset($options['F'])) {
$server_size = ($options['F'] + 1);
if (!(preg_match("/^[0-9]$/", $server_size))) {
print "Server size should be a single digit for the size/flavor.\n";
usage();
}
}
else {
$server_size = '2';
}
/// Get Image (uuid) to install with.
if (isset($options['I'])) {
$install_image = $options['I'];
if (!(preg_match("/^([a-z0-9]+-)*[a-z0-9]+$/i", $install_image))) {
print "Server image $install_image does not seem to be a valid format.\n";
usage();
}
}
else {
$install_image = 'c195ef3b-9195-4474-b6f7-16e5bd86acd0';
}
/// Show Progress
if (isset($options['V'])) {
$verbose='1';
}
/// Input checks are done, server build work below here:
$compute = $conn->Compute('cloudServersOpenStack', $working_region);
/// Loop for challenge to SDK, production probably would not be a loop like this
print "\n";
for ($c=1; $c<=$server_build_number; $c++) {
$server_name_c = "$server_name"."$c";
$servers[$c] = $compute->Server();
$servers[$c]->Create(array('name' => $server_name_c, 'flavor' => $compute->Flavor($server_size), 'image' => $compute->Image($install_image)));
print "Starting Build on: $server_name_c\n";
}
print "\n-- Displaying info below as it becomes availiable --\n";
$build_complete = '0';
$update_sent=array();
while ($build_complete < $server_build_number) {
sleep(15);
foreach ($servers as $serv_key => $server) {
$server->Refresh($server->id);
if (in_array($server->status, array('ACTIVE', 'ERROR'))) {
$server->ip('4');
printf("Completed: %s uuid: %-38s %3d%% ip: %-24s root pass: %-18s\n", $server->name, $server->id, $server->progress, $server->addresses->public[0]->addr, $server->adminPass);
$build_complete++;
unset($servers[$serv_key]);
}
elseif ((isset($verbose)) && ($verbose == '1')) {
if ( (isset($server->addresses->public[0]->addr)) && (!(empty($server->addresses->public[0]->addr))) ) {
$server->ip('4');
printf(" Building: %s uuid: %-38s %3d%% ip: %-24s root pass: %-18s\n", $server->name, $server->id, $server->progress, $server->addresses->public[0]->addr, $server->adminPass);
}
else {
printf(" Building: %s uuid: %-38s %3d%% Waiting on Networking...\n", $server->name, $server->id, $server->progress);
}
}
else {
if ( (isset($server->addresses->public[0]->addr)) && (!(empty($server->addresses->public[0]->addr))) && (!(array_key_exists($serv_key, $update_sent))) ) {
$server->ip('4');
printf(" Building: %s uuid: %-38s %3d%% ip: %-24s root pass: %-18s\n", $server->name, $server->id, $server->progress, $server->addresses->public[0]->addr, $server->adminPass);
$update_sent[$serv_key] = $serv_key;
}
continue;
}
}
}
// print_r($server);
print "End Program.\n\n";
}
else {
echo '<br><h2>This script is currently CLI (command line) only</h2>';
}
////////////////////////// Functions ////////////////////////////
//// Usage
function usage() {
echo "\n";
echo <<<PRINTUSAGE
Required Options (Must be set in credentials file or specified here):
-R=region # Region (will default to credentials if set with NOVA_REGION_NAME).
-S "Server Name" # Server Name (In this example the server name will be iterated 3 times).
-F Flavor # Size - Mem: 1) 512MB 2) 1GB 3) 2GB 4) 4GB 5) 8GB 6) 15GB 7) 30GB (This example will choose 512MB).
-I Image # What Image UUID to build from. (This example will build from debian6.06 - 8ae428cd-0490-4f3a-818f-28213a7286b0 ).
-V # Verbose output, Show more progress during build processes.
Example: php chal_1_build_3_servers.php -R=ORD -S"TestServ" -F"1" -I"8ae428cd-0490-4f3a-818f-28213a7286b0"
PRINTUSAGE;
echo "\n";
exit();
}
?>