forked from rodrigopedra/laravel-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalancer.php
144 lines (125 loc) · 3.07 KB
/
Balancer.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
<?php
namespace App;
use Carbon\Carbon;
use App\Jobs\SyncBalancer;
use App\Callbacks\Dispatch;
use App\Jobs\ProvisionBalancer;
use App\Jobs\UpdateStackDnsRecords;
use App\Jobs\DeleteServerOnProvider;
use App\Callbacks\MarkAsProvisioned;
use Illuminate\Database\Eloquent\Model;
use App\Scripts\SyncBalancer as SyncBalancerScript;
use App\Contracts\Provisionable as ProvisionableContract;
class Balancer extends Model implements ProvisionableContract
{
use Provisionable;
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'self_signs' => 'boolean',
];
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'private_key', 'sudo_password',
];
/**
* Determine if the given user can SSH into the balancer.
*
* @param \App\User $user
* @return bool
*/
public function canSsh(User $user)
{
return $user->canAccessProject($this->project);
}
/**
* Sync the balancer's configuration with the current stacks.
*
* @param int $delay
* @return void
*/
public function sync($delay = 0)
{
Jobs\SyncBalancer::dispatch($this)->delay($delay);
}
/**
* Sync the balancer's configuration with the current stacks.
*
* @return \App\Task
*/
public function syncNow()
{
return $this->run(new SyncBalancerScript($this));
}
/**
* Determine if the balancer should self-sign TLS certificates.
*
* @return bool
*/
public function selfSignsCertificates()
{
return $this->tls === 'self-signed';
}
/**
* Dispatch the job to provision the balancer.
*
* @return void
*/
public function provision()
{
ProvisionBalancer::dispatch($this);
$this->update(['provisioning_job_dispatched_at' => Carbon::now()]);
}
/**
* Run the provisioning script on the balancer.
*
* @return \App\Task|null
*/
public function runProvisioningScript()
{
if ($this->isProvisioning()) {
return;
}
$this->markAsProvisioning();
return $this->runInBackground(new Scripts\ProvisionBalancer($this), [
'then' => [
MarkAsProvisioned::class,
new Dispatch(SyncBalancer::class)
],
]);
}
/**
* Delete the model from the database.
*
* @return bool|null
*
* @throws \Exception
*/
public function delete()
{
if ($this->address) {
UpdateStackDnsRecords::dispatch(
$this->project, $this->address->public_address
);
}
DeleteServerOnProvider::dispatch(
$this->project, $this->providerServerId()
);
$this->address()->delete();
$this->tasks()->delete();
parent::delete();
}
}