Skip to content

Commit

Permalink
Merge pull request #23 from fly-apps/minor-enhancements
Browse files Browse the repository at this point in the history
Minor Enhancements
  • Loading branch information
Johannes-Werbrouck authored May 5, 2023
2 parents 3949b7b + d211f74 commit 3bd7ee6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 152 deletions.
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
<a href="https://supportukrainenow.org/"><img src="https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct.svg" width="100%"></a>
# Fly-Laravel

------
Fly-Laravel was created by Fly.io and is a quick way to get a Laravel app running on Fly.io. It was built using [Laravel Zero](https://laravel-zero.com).

<p align="center">
<img title="Laravel Zero" height="100" src="https://raw.githubusercontent.com/laravel-zero/docs/master/images/logo/laravel-zero-readme.png" />
</p>
## Usage

<p align="center">
<a href="https://github.com/laravel-zero/framework/actions"><img src="https://github.com/laravel-zero/laravel-zero/actions/workflows/tests.yml/badge.svg" alt="Build Status"></img></a>
<a href="https://packagist.org/packages/laravel-zero/framework"><img src="https://img.shields.io/packagist/dt/laravel-zero/framework.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/laravel-zero/framework"><img src="https://img.shields.io/packagist/v/laravel-zero/framework.svg?label=stable" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/laravel-zero/framework"><img src="https://img.shields.io/packagist/l/laravel-zero/framework.svg" alt="License"></a>
</p>
Fly-Laravel assumes that you have flyctl installed, and that you have it connected to your [Fly.io](https://www.fly.io) account. If you need help with this, check out https://fly.io/docs/speedrun/.

<h4> <center>This is a <bold>community project</bold> and not an official Laravel one </center></h4>
To get an app running on Fly.io, run `./vendor/bin/fly-laravel launch`. This will create (among other things) a `fly.toml` file which holds the entire app configuration. For more info about fly.toml app configuration, check out [the docs](https://fly.io/docs/reference/configuration/).

Laravel Zero was created by [Nuno Maduro](https://github.com/nunomaduro) and [Owen Voke](https://github.com/owenvoke), and is a micro-framework that provides an elegant starting point for your console application. It is an **unofficial** and customized version of Laravel optimized for building command-line applications.

- Built on top of the [Laravel](https://laravel.com) components.
- Optional installation of Laravel [Eloquent](https://laravel-zero.com/docs/database/), Laravel [Logging](https://laravel-zero.com/docs/logging/) and many others.
- Supports interactive [menus](https://laravel-zero.com/docs/build-interactive-menus/) and [desktop notifications](https://laravel-zero.com/docs/send-desktop-notifications/) on Linux, Windows & MacOS.
- Ships with a [Scheduler](https://laravel-zero.com/docs/task-scheduling/) and a [Standalone Compiler](https://laravel-zero.com/docs/build-a-standalone-application/).
- Integration with [Collision](https://github.com/nunomaduro/collision) - Beautiful error reporting

------

## Documentation

For full documentation, visit [laravel-zero.com](https://laravel-zero.com/).

## Support the development
**Do you like this project? Support it by donating**

- PayPal: [Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L)
- Patreon: [Donate](https://www.patreon.com/nunomaduro)
For ease of use, you could configure a shell alias, like this: `alias fly-laravel='php vendor/bin/fly-laravel'`.
To make this available everywhere, you can add this to your shell configuration file in your home directory, like `~/.zshrc` or `~/.bashrc`. Don't forget to restart your shell after.

## License

Laravel Zero is an open-source software licensed under the MIT license.
Fly-Laravel is an open-source software licensed under the MIT license.
64 changes: 64 additions & 0 deletions app/Commands/DeployCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Commands;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Process\Exceptions\ProcessFailedException;
use Illuminate\Support\Facades\Process;
use LaravelZero\Framework\Commands\Command;

class DeployCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'deploy {--open}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Deploy an app on Fly.io. Add the --open flag to open the app after deploying.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try
{
$result = Process::run("fly deploy")->throw();
$this->line($result->output());

//if --open is added, run 'fly open' before quitting.
if ($this->option('open'))
{
$this->info("TODO: open the fly.io app. ");
}
}
catch (ProcessFailedException $e)
{
$this->error($e->result->errorOutput());
return Command::FAILURE;
}

//finalize
return Command::SUCCESS;
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}
47 changes: 0 additions & 47 deletions app/Commands/InspireCommand.php

This file was deleted.

140 changes: 67 additions & 73 deletions app/Commands/LaunchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace App\Commands;

use App\Providers\AppServiceProvider;
use App\Services\GenerateFlyToml;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Process\Exceptions\ProcessFailedException;
use Illuminate\Support\Facades\Process;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
use function PHPUnit\Framework\throwException;

class LaunchCommand extends Command
{
Expand All @@ -23,7 +22,7 @@ class LaunchCommand extends Command
*
* @var string
*/
protected $description = 'LaunchCommand an application on Fly.io';
protected $description = 'Launch an application on Fly.io';

/**
* Execute the console command.
Expand All @@ -32,80 +31,75 @@ class LaunchCommand extends Command
*/
public function handle()
{
// 1. create a fly app, including asking the app name and setting a region

$appName = $this->ask("Choose an app name (leave blank to generate one)");
if (!$appName) $appName = "--generate-name"; //not putting this as the default answer in $this->ask so '--generate-name' is not displayed in the prompt

$result = Process::run("flyctl apps create -o personal --machines $appName");

if ($result->failed())
{
$this->error($result->errorOutput());
return Command::FAILURE;
try {
// 1. create a fly app, including asking the app name and setting a region
$currentDirectoryName = array_reverse(explode("/", getcwd()))[0];
//turn "ExampleApp" into "example-app"
strtolower(preg_replace("([A-Z])", "-$0", lcfirst($currentDirectoryName)));
$appName = $this->ask("Choose an app name (use '--generate-name' to generate one)", $currentDirectoryName);
if (!$appName) $appName = "--generate-name"; //not putting this as the default answer in $this->ask so '--generate-name' is not displayed in the prompt

if (!preg_match("/^[a-z0-9-]+$/", $appName))
{
$this->error("App names are only allowed to contain lowercase, numbers and hyphens.");
return Command::FAILURE;
}

$result = Process::run("flyctl apps create -o personal --machines $appName")->throw();

$this->info($result->output());

// 2. detect Node and PHP versions
$result = Process::run("node -v");
if (!preg_match('/v(\d+)./', $result->output(), $matches)) {
$this->error('could not detect Node version');
return Command::FAILURE;
}

$nodeVersion = $matches[1];
$this->line("Detected Node version: $nodeVersion");

// Determine and include in-line the PHP version
$phpVersion = (new \App\Services\GetPhpVersion)->get($this);

// 3. Generate fly.toml file
(new GenerateFlyToml($appName, $nodeVersion, $phpVersion))->get($this);

// 4. Copy over .fly folder, .dockerignore and DockerFile

Process::run("cp -r " . __DIR__ . "/../../resources/templates/.fly/ .fly")->throw();
$this->line('Added folder .fly in project root');

Process::run("cp -r " . __DIR__ . "/../../resources/templates/.dockerignore .dockerignore")->throw();
$this->line('Added .dockerignore in project root');

// The dockerfile is hardcoded and copied over from resources/templates/Dockerfile
if (file_exists('Dockerfile')) {
$this->line("Existing Dockerfile found, using that instead of the default Dockerfile.");
} else {
copy(__DIR__ . '/../../resources/templates/Dockerfile', 'Dockerfile');
$this->line("Added Dockerfile in project root");
}

// 5. set the APP_KEY secret
$APP_KEY = "base64:" . base64_encode(random_bytes(32)); // generate random app key, and encrypt it
Process::run("fly secrets set APP_KEY=$APP_KEY -a $appName --stage")->throw();
$this->line('Set APP_KEY as secret.');

// 6. ask if user wants to deploy. If so, call the DeployCommand. Else, finalize here.
if ($this->confirm("Do you want to deploy your app?")) {
$this->call(DeployCommand::class);
}
}
$this->info($result->output());

// 2. detect Node and PHP versions

$result = Process::run("node -v");
if (!preg_match('/v(\d+)./', $result->output(), $matches))
{
$this->error('could not detect Node version');
return Command::FAILURE;
}

$nodeVersion = $matches[1];
$this->line("Detected Node version: $nodeVersion");

// Determine and include in-line the PHP version
$phpVersion = (new \App\Services\GetPhpVersion)->get( $this );

// 3. Generate fly.toml file
(new \App\Services\GenerateFlyToml( $appName, $nodeVersion, $phpVersion ))->get( $this );

// 4. Copy over .fly folder, .dockerignore and DockerFile

$result = Process::run("cp -r " . __DIR__ . "/../../resources/templates/.fly/ .fly");
if ($result->successful()) $this->line('Added folder .fly in project root');
else
{
$this->error($result->output());
return Command::FAILURE;
}

$result = Process::run("cp -r " . __DIR__ . "/../../resources/templates/.dockerignore .dockerignore");
if ($result->successful()) $this->line('Added .dockerignore in project root');
else
{
$this->error($result->output());
return Command::FAILURE;
}

// The dockerfile is hardcoded and copied over from resources/templates/Dockerfile
if (file_exists('Dockerfile'))
{
$this->line("Existing Dockerfile found, using that instead of the default Dockerfile.");
}
else
{

copy(__DIR__ . '/../../resources/templates/Dockerfile', 'Dockerfile');
$this->line("Added Dockerfile");
}

// 5 set the APP_KEY secret
$APP_KEY = "base64:" . base64_encode(random_bytes(32)); // generate random app key, and encrypt it
$result = Process::run("fly secrets set APP_KEY=$APP_KEY -a $appName --stage");
if ($result->successful()) $this->line('Set APP_KEY as secret.');
else
catch (ProcessFailedException $e)
{
$this->error($result->output());
$this->error($e->result->errorOutput());
return Command::FAILURE;
}

//finalize
$this->info("App '$appName' is ready to go!" );
// finalize
$this->info("App '$appName' is ready to go! Run 'fly-laravel deploy' to deploy it.");
return Command::SUCCESS;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Commands\DeployCommand;
use App\Commands\LaunchCommand;
use Illuminate\Support\ServiceProvider;

Expand All @@ -15,6 +16,7 @@ public function boot(): void
if ($this->app->runningInConsole()) {
$this->commands([
LaunchCommand::class,
DeployCommand::class
]);
}
}
Expand Down

0 comments on commit 3bd7ee6

Please sign in to comment.