-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage_class.php
125 lines (119 loc) · 2.64 KB
/
manage_class.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
<?php
$root = dirname( __FILE__ );
include_once $root . '/includes.php';
class manage
{
/**
* Runs regex on arguments
*
* @param array $argv
*
* @return $arguments
*/
public function validateArguments($argv)
{
$arguments = array();
if (count($argv) == 0 || !isset($argv[1]))
return $arguments; //empty or !set
foreach ($argv as $idx => $arg)
$arguments[$idx] = $arg;
$size = false;
$region = false;
$valid = false;
if ($arguments[1] == '--create')
{
//size
foreach (SIZES as $s)
{
if($arguments[3] == $s)
$size = true;
}
//region
foreach (REGIONS as $r)
{
if($arguments[4] == $r)
$region = true;
}
if(!$region)
{
manage::printMessage(3, "Syntax Error, please provide a proper droplet region. See https://github.com/danetuso/DigitalOceanAPI for a list.");
exit;
}
if(!$size)
{
manage::printMessage(3, "Syntax Error, please provide a proper droplet size. See https://github.com/danetuso/DigitalOceanAPI for a list.");
exit;
}
return $arguments;
}
else
{
foreach (VALID_ARGUMENTS as $v)
{
if($arguments[1] == "--" . $v)
{
$valid = true;
return $arguments;
}
}
if(!$valid)
return null;
}
return $arguments;
}
/**
* Prints out help message.
*/
function printHelp()
{
$buf = "Usage: php -q vmManage.php --arguments\n";
$buf .= "\n";
$buf .= "--list List VMs\n";
$buf .= "--create <hostname> <size> <location> <image> Create a VM with hostname, size, region, and image of droplet\n";
$buf .= " Ex: php -q vmManage.php --create test s-1vcpu-1gb sfo1 ubuntu-16-04-x64\n";
$buf .= "--destroy <ID/Hostname> Kill VM with ID or Hostname\n";
$buf .= "--help Shows this dialogue\n";
$buf .= "\n";
echo $buf;
}
/**
* Displays message to the console (Only in DEBUG_MODE)
*
* @param int $level Level of Message authority
* @param string $output String to be displayed
*
*/
function printMessage($level, $output)
{
if(DEBUG_MODE || $level == 3)
{
if(JSON_OUTPUT && $level == 3)
{
$out = array("error" => $output);
echo json_encode($out);
}
else
{
$buf = "";
switch($level)
{
case 0:
$buf .= "\033[1;32m[OKAY] \033[0m";
break;
case 1:
$buf .= "\033[1;34m[INFO] \033[0m";
break;
case 2:
$buf .= "\033[1;33m[WARN] \033[0m";
break;
case 3:
$buf .= "\033[1;31m[CRIT] \033[0m";
break;
}
$buf .= $output . "\n";
echo $buf;
}
}
}
}
?>