From 7a13f688e75cc3bb1f68def69d2c396564a3ae4f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 30 Mar 2020 17:40:21 +1100 Subject: [PATCH] Setup Workflow (#3565) * Fixes for setup --- .env.ci | 2 +- .github/workflows/phpunit.yml | 1 + app/Http/Controllers/BaseController.php | 5 +- app/Http/Controllers/SetupController.php | 47 ++++++++++--------- app/Jobs/Account/CreateAccount.php | 17 ++++++- app/Jobs/Company/CreateCompanyToken.php | 2 +- .../SendVerificationNotification.php | 11 ++++- app/Utils/SystemHealth.php | 5 +- 8 files changed, 58 insertions(+), 32 deletions(-) diff --git a/.env.ci b/.env.ci index 8379aa8781e6..f5e7b3aa22bc 100644 --- a/.env.ci +++ b/.env.ci @@ -18,4 +18,4 @@ DB_PASSWORD=ninja DB_HOST=127.0.0.1 NINJA_ENVIRONMENT=development COMPOSER_AUTH='{"github-oauth": {"github.com": "${{ secrets.GITHUB_TOKEN }}"}}' -TRAVIS=true \ No newline at end of file +TRAVIS=true diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index b3ea42dc829e..17f173d8fa97 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -23,6 +23,7 @@ jobs: NINJA_ENVIRONMENT: development MULTI_DB_ENABLED: false TRAVIS: true + MAIL_DRIVER: log services: mariadb: diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 2a9f55bf7a28..d2b8acc6e0a4 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -11,7 +11,7 @@ namespace App\Http\Controllers; -use App\Libraries\Account; +use App\Models\Account; use App\Models\Company; use App\Models\Design; use App\Models\User; @@ -305,8 +305,7 @@ class BaseController extends Controller if ((bool)$this->checkAppSetup() !== false) { $data = []; - if (Ninja::isSelfHost()) { - $account = Account::all()->first(); + if (Ninja::isSelfHost() && $account = Account::all()->first()) { $data['report_errors'] = $account->report_errors; } else { $data['report_errors'] = true; diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index e03a365b47e0..7c8b02381b46 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers; use App\Http\Requests\Setup\CheckDatabaseRequest; +use App\Http\Requests\Setup\CheckMailRequest; use App\Http\Requests\Setup\StoreSetupRequest; use App\Jobs\Account\CreateAccount; use App\Models\Account; @@ -39,16 +40,17 @@ class SetupController extends Controller public function doSetup(StoreSetupRequest $request) { + $_ENV['APP_KEY'] = config('app.key'); $_ENV['APP_URL'] = $request->input('url'); $_ENV['APP_DEBUG'] = $request->input('debug') ? 'true' : 'false'; $_ENV['REQUIRE_HTTPS'] = $request->input('https') ? 'true' : 'false'; $_ENV['DB_TYPE'] = 'mysql'; $_ENV['DB_HOST1'] = $request->input('host'); - $_ENV['DB_DATABASE1'] = $request->input('db_username'); - $_ENV['DB_USERNAME1'] = $request->input('db_password'); + $_ENV['DB_DATABASE1'] = $request->input('database'); + $_ENV['DB_USERNAME1'] = $request->input('db_username'); $_ENV['DB_PASSWORD1'] = $request->input('db_password'); $_ENV['MAIL_DRIVER'] = $request->input('mail_driver'); - $_ENV['MAIL_PORT'] = $request->input('port'); + $_ENV['MAIL_PORT'] = $request->input('mail_port'); $_ENV['MAIL_ENCRYPTION'] = $request->input('encryption'); $_ENV['MAIL_HOST'] = $request->input('mail_host'); $_ENV['MAIL_USERNAME'] = $request->input('mail_username'); @@ -62,7 +64,6 @@ class SetupController extends Controller $_ENV['SELF_UPDATER_MAILTO_ADDRESS'] = $request->input('mail_address'); $_ENV['SELF_UPDATER_MAILTO_NAME'] = $request->input('mail_name'); $_ENV['DB_CONNECTION'] = 'db-ninja-01'; - $_ENV['APP_DEBUG'] = false; $config = ''; @@ -76,16 +77,24 @@ class SetupController extends Controller $config .= "{$key}={$val}\n"; } + /* Write the .env file */ $filePath = base_path().'/.env'; $fp = fopen($filePath, 'w'); fwrite($fp, $config); fclose($fp); + + /* We need this in some environments that do not have STDIN defined */ + define('STDIN',fopen("php://stdin","r")); + /* Make sure no stale connections are cached */ + \DB::purge('db-ninja-01'); + + /* Run migrations */ Artisan::call('optimize'); - Artisan::call('migrate'); - Artisan::call('db:seed'); - Artisan::call('horizon'); + Artisan::call('migrate', ['--force' => true]); + Artisan::call('db:seed', ['--force' => true]); + /* Create the first account. */ if (Account::count() == 0) { $account = CreateAccount::dispatchNow($request->all()); } @@ -101,14 +110,11 @@ class SetupController extends Controller public function checkDB(CheckDatabaseRequest $request): Response { - if (Account::count() == 0) { /** This may not work, because we don't have 'account's table. */ - } - $status = SystemHealth::dbCheck($request); info($status); - if (gettype($status) == 'array' && $status['success'] === true) { + if (is_array($status) && $status['success'] === true) { return response([], 200); } @@ -120,23 +126,20 @@ class SetupController extends Controller * * @return Response */ - public function checkMail(): Response + public function checkMail(CheckMailRequest $request) { - // if (Account::count() == 0) {} /** This may not work, because we don't have 'account's table. */ - try { - SystemHealth::testMailServer(); + $response_array = SystemHealth::testMailServer($request); - $randomStatus = rand(0, 1); - - if ($randomStatus) + if(count($response_array) == 0) return response([], 200); - - } catch (\Exception $e) { - info(['action' => 'SetupController::checkMail()', 'message' => $e->getMessage(),]); + else + return response()->json($response_array, 200); - return response([], 400); + + } catch (\Exception $e) { + return response()->json(['message' => $e->getMessage()], 400); } } } diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index 65c6a4ccbd44..0101089f89b6 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -20,19 +20,32 @@ use Symfony\Component\HttpFoundation\Response; class CreateAccount { + use Dispatchable; + protected $request; + public function __construct(array $sp660339) { $this->request = $sp660339; } + public function handle() { - if (config('ninja.environment') == 'selfhost' && Account::all()->count() > 1) { + + if(config('ninja.environment') == 'selfhost' && Account::all()->count() == 0) { + $this->create(); + }elseif (config('ninja.environment') == 'selfhost' && Account::all()->count() > 1) { return response()->json(array('message' => Ninja::selfHostedMessage()), 400); } elseif (!Ninja::boot()) { return response()->json(array('message' => Ninja::parse()), 401); } + + } + + private function create() + { + $sp794f3f = Account::create($this->request); $sp794f3f->referral_code = Str::random(32); @@ -58,5 +71,7 @@ class CreateAccount $spaa9f78->fresh(); $sp035a66->notification(new NewAccountCreated($spaa9f78, $sp035a66))->ninja(); return $sp794f3f; + } + } diff --git a/app/Jobs/Company/CreateCompanyToken.php b/app/Jobs/Company/CreateCompanyToken.php index 1a68f66f2a51..db9e0a13f61c 100644 --- a/app/Jobs/Company/CreateCompanyToken.php +++ b/app/Jobs/Company/CreateCompanyToken.php @@ -55,7 +55,7 @@ class CreateCompanyToken implements ShouldQueue $ct = CompanyToken::create([ 'user_id' => $this->user->id, - 'account_id' => $this->company->account->id, + 'account_id' => $this->user->account->id, 'token' => Str::random(64), 'name' => $this->custom_token_name ?: $this->user->first_name. ' '. $this->user->last_name, 'company_id' => $this->company->id, diff --git a/app/Listeners/SendVerificationNotification.php b/app/Listeners/SendVerificationNotification.php index 3b02a80c79f8..8c405603cac2 100644 --- a/app/Listeners/SendVerificationNotification.php +++ b/app/Listeners/SendVerificationNotification.php @@ -46,8 +46,15 @@ class SendVerificationNotification implements ShouldQueue { MultiDB::setDB($event->company->db); - $event->user->notify(new VerifyUser($event->user)); + try { - Ninja::registerNinjaUser($event->user); + $event->user->notify(new VerifyUser($event->user)); + + Ninja::registerNinjaUser($event->user); + + } + catch(\Exception $e){ + + } } } diff --git a/app/Utils/SystemHealth.php b/app/Utils/SystemHealth.php index b9f1ca1429f1..d1c72b1ac7f6 100644 --- a/app/Utils/SystemHealth.php +++ b/app/Utils/SystemHealth.php @@ -137,19 +137,20 @@ class SystemHealth Mail::to(config('mail.from.address')) ->send(new TestMailServer('Email Server Works!', config('mail.from.address'))); } catch (\Exception $e) { + \Log::error($e->getMessage()); return $e->getMessage(); } if (count(Mail::failures()) > 0) { + \Log::error(print_r(Mail::failures(),1)); return Mail::failures(); } - return []; + return response()->json(['message'=>'Success'],200); } private static function checkEnvWritable() { return is_writable(base_path().'/.env'); - //return @fopen(base_path().'/.env', 'w'); } }