One place for hosting & domains

      Manage

      How To Manage Remote Servers with Ansible eBook


      Download the Complete eBook!

      How To Manage Remote Servers with Ansible eBook in EPUB format

      How To Manage Remote Servers with Ansible eBook in PDF format

      Introduction to the eBook

      This book is designed to introduce you to using Ansible to manage your servers. You’ll learn how to install and configure Ansible on a control node, and then how to use it to configure and run commands on remote servers. You’ll also learn how to collect tasks into complete Playbooks to automate server setup from start to finish.

      This book is based on the How To Manage Remote Servers with Ansible tutorial series found on DigitalOcean Community. The topics that it covers include how to:

      1. Become familiar with configuration management tools and processes, and the benefits of using them to manage your infrastructure.

      2. Install and configure Ansible on an Ubuntu 20.04 control node, ensuring that your servers are properly set up and that you are able to execute remote instructions through Ansible.

      3. Build inventory files and organize your servers into groups to selectively control how and where Ansible commands are run.

      4. Run Ad Hoc commands to execute individual tasks on one, or multiple remote servers.

      5. Package individual commands into Playbooks that you can use to automate the provisioning of multiple servers, and how to run specific sets of tasks in Playbooks using tags.

      Each chapter is usable on its own as a reference, or as part of a progressive guide to learning how to manage your servers with Ansible. If you’re familiar with a topic, or are more interested in a particular section, feel free to jump to the chapter that best suits your purpose.

      Download the eBook

      You can download the eBook in either the EPUB or PDF format by following the links below.

      Download the Complete eBook!

      How To Manage Remote Servers with Ansible eBook in EPUB format

      How To Manage Remote Servers with Ansible eBook in PDF format



      Source link

      How To Use Migrations to Create and Manage Database Tables in Laravel



      Part of the Series:
      How To Build a Links Landing Page in PHP with Laravel and Docker Compose

      Laravel is an open-source PHP framework that provides a set of tools and resources to build modern PHP applications. In this project-based tutorial series, you’ll build a Links Landing Page application with the Laravel framework, using a containerized PHP development environment managed by Docker Compose.

      At the end, you’ll have a one-page website built with Laravel and managed via Artisan commands where you can share relevant links to an audience on social channels and presentations.

      Laravel database migrations allow developers to quickly bootstrap, destroy, and recreate an application’s database, without the need to log into the database console or run any SQL queries.

      In this guide, you’ll create a database migration to set up the table where you’ll save the application links. In order to do that, you’ll use the Artisan command-line tool that comes with Laravel by default. At the end, you will be able to destroy and recreate your database tables as many times as you want, using only artisan commands.

      To get started, first make sure you’re in the application’s root directory and your Docker Compose development environment is up and running:

      • cd ~/landing-laravel
      • docker-compose up -d

      Output

      landing-laravel_app_1 is up-to-date landing-laravel_nginx_1 is up-to-date landing-laravel_db_1 is up-to-date

      Next, create a database migration to set up the links table. Laravel Migrations allow developers to programmatically create, update, and destroy database tables, working as a version control system for your database schema.

      To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder. This class will contain a default boilerplate code.

      Remember to use docker-compose exec app to run the command on the app service container, where PHP is installed:

      • docker-compose exec app php artisan make:migration create_links_table

      Output

      Created Migration: 2020_11_18_165241_create_links_table

      Notice that the migration name is generated based on the current date and time, and the name provided as argument to the make:migration command. For that reason, your migration file name will differ slightly.

      Open the generated migration class using your editor of choice:

      • nano database/migrations/2020_11_18_165241_create_links_table

      Next, update the up method to include the table columns you’ll need to store the app data.

      Replace the current content of your migration class with the following code. The highlighted values are the only lines that need adding, so if you prefer, you can also only copy those highlighted lines and include them into your Schema::create definition:

      database/migrations/2020_10_12_171200_create_links_table.php

      <?php
      
      use IlluminateDatabaseMigrationsMigration;
      use IlluminateDatabaseSchemaBlueprint;
      use IlluminateSupportFacadesSchema;
      
      class CreateLinksTable extends Migration
      {
          /**
           * Run the migrations.
           *
           * @return void
           */
          public function up()
          {
              Schema::create('links', function (Blueprint $table) {
                  $table->id();
                  $table->string('url', 200);
                  $table->text('description');
                  $table->boolean('enabled')->default(true);
                  $table->timestamps();
              });
          }
      
          /**
           * Reverse the migrations.
           *
           * @return void
           */
          public function down()
          {
              Schema::dropIfExists('links');
          }
      }
      

      In addition to the default fields that are included in the table definition that is automatically generated with the Artisan command, you’re including three new fields in this table:

      • url : A string field to save the link URL.
      • description : A text field to save the link description.
      • enabled : A field to store the state of the link, whether it’s enabled or not. The boolean Schema type will generate a tinyint unsigned field to store a value of either 0 of 1.

      Save your migration file when you’re done adding these fields. Next, run the migration with:

      • docker-compose exec app php artisan migrate

      Output

      Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (152.46ms) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (131.12ms) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (101.06ms) Migrating: 2020_11_18_165241_create_links_table Migrated: 2020_11_18_165241_create_links_table (60.20ms)

      You’ll notice that other migrations were also executed along with the create_links_table. That is because the default Laravel installation comes with migrations for users (with a users table and a password_resets table) and for queued jobs (with a failed_jobs table). Because our demo application won’t use these features, it is safe to remove those migrations now; however, you may also opt to leave them in place if you are working on an application of your own and you plan on developing it further. All migration files are located at database/migrations in the app’s root folder.

      For more detailed information on database migrations, please refer to our guide on How To Use Database Migrations and Seeders to Abstract Database Setup in Laravel.

      In the next part of this series, you’ll create a custom Artisan command to list, insert, and delete entries in the app’s links table.



      Source link

      How To Create Artisan Commands To Manage Database Records in Laravel



      Part of the Series:
      How To Build a Links Landing Page in PHP with Laravel and Docker Compose

      Laravel is an open-source PHP framework that provides a set of tools and resources to build modern PHP applications. In this project-based tutorial series, you’ll build a Links Landing Page application with the Laravel framework, using a containerized PHP development environment managed by Docker Compose.

      At the end, you’ll have a one-page website built with Laravel and managed via Artisan commands where you can share relevant links to an audience on social channels and presentations.

      If you followed along with this series so far, your database tables should be all set. However, you still need to implement a way to let users insert new entries in the links table.

      To limit the scope of this series while also making the application fully-functional, you’ll set up Artisan commands to create and delete links in the database. Artisan is the command line tool that comes with Laravel, offering a number of utilities to speed up the development process, from generating boilerplate code to deleting and re-creating the application’s database.

      Using the command line interface to manage your application can be an alternative to web forms and secured areas, since it requires a user to be logged on the server in order to execute such commands instead of being authenticated from a browser. If you decide later on to create a secured area for your application, you can create web forms to allow a registered user to submit a new link to the database.

      Artisan commands are often used to perform application tasks that should run in the background, either manually or automatically via a scheduling mechanism such as Crontab. They can also be used to facilitate prototyping new application features that need to be configured dynamically, depending on input from an authorized user.

      To get started, create a new Artisan command using the make:command helper:

      • docker-compose exec app php artisan make:command LinkNew

      Output

      Console command created successfully.

      This will create a file named LinkNew.php, located at the app/Console/Commands directory. In this class, which extends from the IlluminateConsoleCommand parent class, you’ll need to implement a handle method that will be executed when this command is called. To define the signature of the command, you’ll set the $signature protected property to link:new.

      Open the new file using your text or code editor of choice. Here, we’ll use nano:

      • nano app/Console/Commands/LinkNew.php

      A few different things will need to happen in the handle method so that you are able to save a new link to the database. First, you’ll prompt for the user’s input in order to obtain the link URL.

       $url = $this->ask('Link URL:');
      

      Then, you’ll use the filter_var function to validate that the input obtained from the user is a valid URL. If the link is invalid, you’ll show an error and exit the application with status code 1, which means the application exited in error.

              if (!filter_var($url, FILTER_VALIDATE_URL)) {
                  $this->error("Invalid URL. Exiting...");
                  return 1;
              }
      

      If the link is valid, you’ll continue and ask for the link description using the same method as before.

       $description = $this->ask('Link Description:');
      

      You’ll then ask for a final confirmation that all data is correct, using the confirm helper. If the user confirms, the link is finally inserted in the database. You’ll use the Link Eloquent model created in a previous part of this series to interact with the database.

              $this->info("New Link:");
              $this->info($url . ' - ' . $description);
      
              if ($this->confirm('Is this information correct?')) {
                  $link = new Link();
                  $link->url = $url;
                  $link->description = $description;
                  $link->save();
      
                  $this->info("Saved.");
              }
      

      The application exits with a 0, representing a success status (0 errors).

      return 0;
      

      The following code contains the full implementation of these steps. Replace the current content in your LinkNew class with:

      app/Console/Commands/LinkNew.php

      <?php
      
      namespace AppConsoleCommands;
      
      use AppModelsLink;
      use IlluminateConsoleCommand;
      use IlluminateSupportFacadesDB;
      
      class LinkNew extends Command
      {
          /**
           * The name and signature of the console command.
           *
           * @var string
           */
          protected $signature="link:new";
      
          /**
           * The console command description.
           *
           * @var string
           */
          protected $description = 'Create a New Link';
      
          /**
           * Create a new command instance.
           *
           * @return void
           */
          public function __construct()
          {
              parent::__construct();
          }
      
          /**
           * Execute the console command.
           *
           * @return int
           */
          public function handle()
          {
              $url = $this->ask('Link URL:');
      
              if (!filter_var($url, FILTER_VALIDATE_URL)) {
                  $this->error("Invalid URL. Exiting...");
                  return 1;
              }
      
              $description = $this->ask('Link Description:');
      
              $this->info("New Link:");
              $this->info($url . ' - ' . $description);
      
              if ($this->confirm('Is this information correct?')) {
                  $link = new Link();
                  $link->url = $url;
                  $link->description = $description;
                  $link->save();
      
                  $this->info("Saved.");
              }
      
              return 0;
          }
      }
      

      Save and close the file when you’re done.

      To execute the command and insert a new link in the database, run:

      • docker-compose exec app php artisan link:new

      Output

      Link URL:: > https://digitalocean.com/community Link Description:: > DigitalOcean Community New Link: https://digitalocean.com/community - DigitalOcean Community Is this information correct? (yes/no) [no]: > yes Saved.

      Feel free to add a few more links if you want to.

      Next, you’ll need to create a new Artisan command to show the list of all links.You can call it link:list. Create the new command with:

      • docker-compose exec app php artisan make:command LinkList

      Open the command class using your text or code editor of choice:

      • nano app/Console/Commands/LinkList.php

      Within the handle method of this command, you’ll query for all rows in the links table. You can use the Link model to access the underlying database query methods that Eloquent provides. To exhibit the results nicely in the command line, you can use the table output helper:

              $headers = [ 'id', 'url', 'description' ];
              $links = Link::all(['id', 'url', 'description'])->toArray();
              $this->table($headers, $links);
      
              return 0;
      

      The following code contains the full implementation of the link:list command. Replace the content in your LinkList.php file with :

      app/Console/Commands/LinkList.php

      <?php
      
      namespace AppConsoleCommands;
      
      use AppModelsLink;
      use IlluminateConsoleCommand;
      
      class LinkList extends Command
      {
          /**
           * The name and signature of the console command.
           *
           * @var string
           */
          protected $signature="link:list";
      
          /**
           * The console command description.
           *
           * @var string
           */
          protected $description = 'List links saved in the database';
      
          /**
           * Create a new command instance.
           *
           * @return void
           */
          public function __construct()
          {
              parent::__construct();
          }
      
          /**
           * Execute the console command.
           *
           * @return int
           */
          public function handle()
          {
              $headers = [ 'id', 'url', 'description' ];
              $links = Link::all(['id', 'url', 'description'])->toArray();
              $this->table($headers, $links);
      
              return 0;
          }
      }
      

      Save and close the file when you are done.

      To run this command and show a list of all links already inserted in the linkstable, run:

      • docker-compose exec app php artisan link:list

      Output

      +----+------------------------------------+--------------+ | id | url | description | +----+------------------------------------+--------------+ | 1 | https://digitalocean.com/community | DO Community | | 2 | https://laravel.com | Laravel | +----+------------------------------------+--------------+

      Finally, you’ll create a command to delete links:

      • docker-compose exec app php artisan make:command LinkDelete

      Output

      Console command created successfully.

      Open the new file using your text or code editor of choice:

      • nano app/Console/Commands/LinkDelete.php

      You can name this command link:delete. To know which link must be deleted, you’ll need to require that users provide an additional argument when calling the command: the ID of the link. This is also set within the $signature variable, which defines how your command is called and what arguments, mandatory or not, should be provided:

      protected $signature="link:delete {link_id}";
      

      The handle method for this command will implement a few different instructions. First, you’ll obtain the Link ID that should have been provided within the command call.

      $link_id = $this->argument('link_id');
      

      Then, you’ll obtain the referenced link from the database, using the Eloquent method find that is available through your Link model.

      $link = Link::find($link_id);
      

      When the find method doesn’t find a database record with that ID, it will return null. You’ll check if that is the current value contained in the $link variable, and return an error in that case. The program will exit in error (code 1).

              if ($link === null) {
                  $this->error("Invalid or non-existent link ID.");
                  return 1;
              }
      

      When $link is not null, the command continues execution. You then use the confirm helper to ask for a user confirmation.

      if ($this->confirm('Are you sure you want to delete this link? ' . $link->url)) {
          // deletes link
      }
      

      When the user confirms the action by typing yes and hitting ENTER, you’ll call the delete method from the Link Eloquent model to delete the specified link from the database.

                  $link->delete();
                  $this->info("Link deleted.");
      

      The following code contains the full implementation for the list:delete command. Replace the content in your LinkDelete.php file with the following:

      app/Console/Commands/LinkDelete.php

      <?php
      
      namespace AppConsoleCommands;
      
      use AppModelsLink;
      use IlluminateConsoleCommand;
      use IlluminateSupportFacadesDB;
      
      class LinkDelete extends Command
      {
          /**
           * The name and signature of the console command.
           *
           * @var string
           */
          protected $signature="link:delete {link_id}";
      
          /**
           * The console command description.
           *
           * @var string
           */
          protected $description = 'Deletes a link from the database.';
      
          /**
           * Create a new command instance.
           *
           * @return void
           */
          public function __construct()
          {
              parent::__construct();
          }
      
          /**
           * Execute the console command.
           *
           * @return int
           */
          public function handle()
          {
              $link_id = $this->argument('link_id');
              $link = Link::find($link_id);
      
              if ($link === null) {
                  $this->error("Invalid or non-existent link ID.");
                  return 1;
              }
      
              if ($this->confirm('Are you sure you want to delete this link? ' . $link->url)) {
                  $link->delete();
                  $this->info("Link deleted.");
              }
      
              return 0;
          }
      }
      

      Save and close the file when you’re done.

      Now when you want to delete a link from your links table, you’ll first need to obtain the link’s ID with artisan link:list, as demonstrated earlier on. Once you know the ID of a link, you can run the artisan link:delete command with:

      • docker-compose exec app php artisan link:delete LINK_ID

      Output

      Are you sure you want to delete this link? https://laravel.com (yes/no) [no]: > yes Link deleted.

      You’re now able to insert, list, and delete links in the application’s database, using Artisan commands executed from a command-line interface. In the next part of this series, you’ll set up the front end of your application using Blade templates and the Bulma CSS framework.



      Source link