Skip to content

Commit 02f7aa3

Browse files
author
Iskren Hadzhinedev
committed
Add ixp-manager:setup-wizard command for easier installation
1 parent e6bfd25 commit 02f7aa3

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

app/Console/Commands/SetupWizard.php

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
namespace IXP\Console\Commands;
3+
4+
define('strict_types', 1);
5+
6+
/*
7+
* Copyright (C) 2009 - 2024 Internet Neutral Exchange Association Company Limited By Guarantee.
8+
* All Rights Reserved.
9+
*
10+
* This file is part of IXP Manager.
11+
*
12+
* IXP Manager is free software: you can redistribute it and/or modify it
13+
* under the terms of the GNU General Public License as published by the Free
14+
* Software Foundation, version v2.0 of the License.
15+
*
16+
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
17+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19+
* more details.
20+
*
21+
* You should have received a copy of the GNU General Public License v2.0
22+
* along with IXP Manager. If not, see:
23+
*
24+
* http://www.gnu.org/licenses/gpl-2.0.html
25+
*/
26+
use Illuminate\Support\Facades\DB;
27+
use Illuminate\Validation\Rules\Password;
28+
use Illuminate\Support\Carbon;
29+
30+
31+
use IXP\Models\{
32+
CompanyBillingDetail,
33+
CompanyRegisteredDetail,
34+
Customer,
35+
Infrastructure,
36+
User
37+
};
38+
39+
/**
40+
* Artisan command to streamline the initial installation of IXP Manager
41+
*
42+
* @author Iskren Hadzhinedev <[email protected]>
43+
* @package IXP\Console\Commands
44+
* @copyright Copyright (C) 2009 - 2024 Internet Neutral Exchange Association Company Limited By Guarantee
45+
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL V2.0
46+
*/
47+
class SetupWizard extends Command
48+
{
49+
/**
50+
* The name and signature of the console command.
51+
*
52+
* @var string
53+
*/
54+
protected $signature = 'ixp-manager:setup-wizard'
55+
. ' {--N|name= : The name of the admin user}'
56+
. ' {--U|username= : The username of the admin user}'
57+
. ' {--E|email= : The email of the admin user}'
58+
. ' {--A|asn= : The ASN of your IXP}'
59+
. ' {--I|infrastructure= : The name of your primary infrastructure}'
60+
. ' {--C|company-name= : The name of your company}';
61+
62+
/**
63+
* The console command description.
64+
* @var string
65+
*/
66+
protected $description = "Run initial setup for IXP Manager";
67+
68+
/**
69+
* Execute the console command.
70+
*
71+
* @return int
72+
*
73+
* @throws
74+
*/
75+
public function handle(): int
76+
{
77+
if (Customer::count() > 0) {
78+
$this->error('IXP Manager has already been setup. Exiting.');
79+
return 1;
80+
}
81+
82+
$this->info('Starting the setup wizard...');
83+
$data = $this->populateData();
84+
85+
$passhash = password_hash($data['password'], PASSWORD_BCRYPT, ['cost' => 10]);
86+
87+
try {
88+
DB::transaction(function () use ($data, $passhash) {
89+
90+
Infrastructure::create([
91+
'name' => $data['infrastructure'],
92+
'shortname' => $data['infrastructure'],
93+
'isPrimary' => 1,
94+
'created_at' => Carbon::now(),
95+
'updated_at' => Carbon::now(),
96+
]);
97+
98+
$billingDetail = CompanyBillingDetail::create([
99+
'billingContatName' => $data['name'],
100+
'billingEmail' => config('identity.billing_email', $data['email']),
101+
'invoiceMethod' => CompanyBillingDetail::INVOICE_METHOD_EMAIL,
102+
'billingFrequency' => CompanyBillingDetail::BILLING_FREQUENCY_NOBILLING,
103+
'created_at' => Carbon::now(),
104+
'updated_at' => Carbon::now(),
105+
]);
106+
107+
$registrationDetail = CompanyRegisteredDetail::create([
108+
'registeredName' => $data['company_name'],
109+
'created_at' => Carbon::now(),
110+
'updated_at' => Carbon::now(),
111+
]);
112+
113+
$cust = Customer::create([
114+
'name' => $data['company_name'],
115+
'type' => 3,
116+
'shortname' => $data['company_name'],
117+
'autsys' => $data['asn'],
118+
'maxprefixes' => 100,
119+
'peeringemail' => $data['email'],
120+
'peeringpolicy' => 'mandatory',
121+
'nocphone' => config('identity.support_phone', '+1 555 555 5555'),
122+
'noc24hphone' => config('identity.support_phone', '+1 555 555 5555'),
123+
'nocemail' => config('identity.support_email', $data['email']),
124+
'nochours' => config('identity.support_hours', '24/7'),
125+
'nocwww' => config('app.url', 'http://example.com'),
126+
'corpwww' => config('identity.corporate_url', 'http://example.com'),
127+
'datejoin' => Carbon::now(),
128+
'status' => 1,
129+
'activepeeringmatrix' => 1,
130+
'company_registered_detail_id' => $registrationDetail->id,
131+
'company_billing_details_id' => $billingDetail->id,
132+
'abbreviatedName' => $data['company_name'],
133+
'isReseller' => 0,
134+
'created_at' => Carbon::now(),
135+
'updated_at' => Carbon::now(),
136+
]);
137+
138+
$cust->contacts()->create([
139+
'name' => $data['name'],
140+
'email' => $data['email'],
141+
'created_at' => Carbon::now(),
142+
'updated_at' => Carbon::now(),
143+
]);
144+
145+
$user = new User;
146+
$user->name = $data['name'];
147+
$user->username = $data['username'];
148+
$user->password = $passhash;
149+
$user->email = $data['email'];
150+
$user->privs = User::AUTH_SUPERUSER;
151+
$user->disabled = 0;
152+
$user->creator = 'IXP Manager setup wizard';
153+
$user->created_at = Carbon::now();
154+
$user->updated_at = Carbon::now();
155+
156+
$user->save();
157+
$user->customer()->associate($cust);
158+
$user->customers()->attach($cust->id);
159+
$user->currentCustomerToUser()->update(['privs' => User::AUTH_SUPERUSER]);
160+
});
161+
}
162+
catch (\Exception $e) {
163+
$this->error('A database error occurred while setting up IXP Manager:' . $e->getMessage());
164+
return 2;
165+
}
166+
167+
return 0;
168+
}
169+
170+
protected function populateData(): array
171+
{
172+
173+
$data = [
174+
"asn" => $this->option('asn') ?? $this->ask('Enter the ASN of your IXP'),
175+
"company_name" => $this->option('company-name') ?? $this->ask('Enter the name of your company'),
176+
"infrastructure" => $this->option('infrastructure') ?? $this->ask('Enter the name of your primary infrastructure'),
177+
"name" => $this->option('name') ?? $this->ask('Enter the full name(s) of the admin user'),
178+
"username" => $this->option('username') ?? $this->ask('Enter the username of the admin user'),
179+
"email" => $this->option('email') ?? $this->ask('Enter the email of the admin user'),
180+
"password" => $this->secret('Enter the password of the admin user'),
181+
];
182+
if ($data['password'] !== $this->secret('Confirm the password of the admin user')) {
183+
$this->error('Passwords do not match. Exiting.');
184+
exit(1);
185+
}
186+
187+
$passwordRules = Password::min(8)
188+
->mixedCase()
189+
->numbers()
190+
->symbols()
191+
->uncompromised();
192+
$validator = \Validator::make($data, [
193+
'asn' => 'required|integer|between:1,4294967295',
194+
'company_name' => 'required|string',
195+
'infrastructure' => 'required|string',
196+
'name' => 'required|string',
197+
'username' => 'required|string',
198+
'email' => 'required|email',
199+
'password' => ['required', 'string', $passwordRules],
200+
]);
201+
202+
if ($validator->fails()) {
203+
$this->error('The following errors occurred:');
204+
foreach ($validator->errors()->all() as $error) {
205+
$this->error("\t" . $error);
206+
}
207+
exit(2);
208+
}
209+
return $data;
210+
211+
}
212+
}

0 commit comments

Comments
 (0)