-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
executable file
·173 lines (157 loc) · 4.13 KB
/
helpers.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
<?php
use App\Server;
use App\User;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
if (!function_exists('cached_provider_data')) {
/**
* Get cached provider data from json files
*
* @return array
*/
function cached_provider_data($provider)
{
switch ($provider) {
case DIGITAL_OCEAN:
return Cache::rememberForever(
'digital-ocean-data',
function () {
return json_decode(
file_get_contents(
base_path('provider-data/digital-ocean.json')
)
);
}
);
case VULTR:
return Cache::rememberForever('vultr-data', function () {
return json_decode(
file_get_contents(base_path('provider-data/vultr.json'))
);
});
case LINODE:
return Cache::rememberForever('linode-data', function () {
return json_decode(
file_get_contents(
base_path('provider-data/linode.json')
)
);
});
}
return [];
}
}
if (!function_exists('str_root_password')) {
/**
* Returns a random root password
*
* @return string
*
* */
function str_root_password()
{
return str_shuffle(str_random(10) . '{}[]%&');
}
}
if (!function_exists('get_ram')) {
/**
* Get RAM in GB
*
* @param integer $value
*
* @return string
*/
function get_ram($value)
{
if ($value < 1024) {
return "{$value}MB";
}
$inGb = floor($value / 1024);
return "{$inGb}GB";
}
}
if (!function_exists('get_disk')) {
/**
* Get RAM in GB
*
* @param integer $value
*
* @return string
*/
function get_disk($value)
{
$inGb = floor($value / 1024);
return "{$inGb}GB";
}
}
if (!function_exists('get_region_name')) {
/**
* Get the name of a region depending
* on the provider passed
*
* @return string
*/
function get_region_name(string $provider, $id)
{
$data = cached_provider_data($provider);
if ($provider === DIGITAL_OCEAN) {
return collect($data->regions)->first(function ($region) use ($id) {
return $region->slug = $id;
})->name;
}
if ($provider === VULTR) {
return collect($data->regions)->first(function ($region) use ($id) {
return $region->DCID = $id;
})->name;
}
if ($provider === LINODE) {
return ucfirst(
collect($data->regions)->first(function ($region) use ($id) {
return $region->id = $id;
})->id
);
}
return null;
}
}
if (!function_exists('github_api')) {
/**
* Get an http client for github api interaction
*
* @return \Guzzle\Client
*/
function github_api($apiToken = null)
{
return new HttpClient([
'base_uri' => 'https://api.github.com.',
'headers' => [
'Authorization' =>
'token ' .
($apiToken
? $apiToken
: auth()->user()->source_control['github'])
]
]);
}
}
if (!function_exists('gitlab_api')) {
/**
* Get an http client for github api interaction
*
* @return \Guzzle\Client
*/
function gitlab_api($apiToken = null)
{
return new HttpClient([
'base_uri' => 'https://gitlab.com/api/v4/',
'headers' => [
'Authorization' =>
'Bearer ' .
($apiToken
? $apiToken
: auth()->user()->source_control['gitlab'])
]
]);
}
}