Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmurray committed Oct 22, 2018
2 parents 6a8c877 + d9fa17f commit d27f0d8
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It works. Might not be elegant, but it works!
Depending on your mood, you can either [download the compiled version](https://github.com/jpmurray/doddns/raw/master/builds/doddns) or [build it yourself](https://laravel-zero.com/#/usage?id=building-a-standalone-application), then add it to your `$PATH` and run the setup command... And you're good to go!

### Updating?
If you've pulled or downloaded a new version, be sure to run `doddns migrate` to make sure your local database is up to date!
If you've pulled or downloaded a new version, be sure to run `doddns setup` and choose the upgrade option to make sure your local database is up to date! You can also forgo the menu to upgrade directly using `doddns setup -U`.

### Crontab
If you want doddns to autoupdate with your current IP address, you should add an ntry to your cron tab like so: `* * * * * php /path-to-doddns/doddns schedule:run >> /dev/null 2>&1`.
Expand All @@ -27,8 +27,10 @@ After that, doddns will try to update every hours by itself.

You can then use the `doddns` command to see a list of possible actions:

- `doddns setup`: will create local database and asks for DO personal acces token.
- `doddns setup`: Shows a menu to either make first setup / start from cratch, or run upgrade commands after a version bump.
- `doddns set-token {token}`: will set your DO personal access token, overwriting any existing value.
- `doddns last-known-ip`: will output the last known IP that has been found / used.
- `doddns version`: output the installed and available version, and make a basic check if you are up to date or not.
- `doddns records:list`: list any added record that doddns tries to update.
- `doddns records:add`: add a record to update to the database from exiting DO domains.
- `doddns records:remove`: removes a record from doddns' database.
Expand Down
1 change: 1 addition & 0 deletions VERSION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.4.0
50 changes: 50 additions & 0 deletions app/Commands/LastKnownIP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Commands;

use App\Helpers\SettingsHelper;
use LaravelZero\Framework\Commands\Command;

class LastKnownIP extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'last-known-ip';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Show the last known IP address that has been used.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(SettingsHelper $settings)
{
if (!$settings->hasLastKnownIP()) {
$this->error("Can't find required field in database. If you have updated lately, you should run doddns migrate.");
return;
}

$this->showLastKnownIp($settings);
}

private function showLastKnownIp($settings)
{
$last_known_ip = $settings->getLastKnownIP();

if (is_null($last_known_ip)) {
$this->info("Nothing. Please run the doddns:records update command at last once.");
return;
}

$this->info($last_known_ip);
}
}
41 changes: 38 additions & 3 deletions app/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Setup extends Command
*
* @var string
*/
protected $signature = 'setup';
protected $signature = 'setup {--U|upgrade : Run the upgrade command without menu prompt.}';

/**
* The description of the command.
Expand All @@ -30,10 +30,45 @@ class Setup extends Command
*/
public function handle(SettingsHelper $settings)
{
if ($this->confirm("This will destroy any existing doddns configuration. Is that ok?")) {
$this->createDatabase();

if ($this->option('upgrade')) {
$option = 2;
} else {
$option = $this->menu('What do you want to do?', [
'First time setup', //0
'Restart from scratch', //1
'Ugprade to latest version', //2
])->open();
}

if (is_null($option)) {
$this->info("Doing nothing.");
return;
}

if ($option == 2) {
$this->upgrade();
return;
}

$this->makeSetup();
}

private function upgrade()
{
$this->callSilent('migrate');
$this->info("Upgraded!");
}

private function makeSetup()
{
if (!$this->confirm("This will destroy any existing doddns configuration. Is that ok?")) {
$this->info("Doing nothing.");
return;
}

$this->createDatabase();

$token = $this->ask("What is your Digital Ocean peronal access token?");

if ($settings->hasToken()) {
Expand Down
37 changes: 33 additions & 4 deletions app/Commands/UpdateRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Commands;

use App\Helpers\DigitalOceanHelper;
use App\Helpers\SettingsHelper;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
Expand All @@ -25,25 +26,53 @@ class UpdateRecords extends Command
*/
protected $description = 'Update saved records to current IP address.';

/**
* Helper to interact with Digital Ocean API.
*/
protected $digitalocean;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(DigitalOceanHelper $digitalocean)
public function handle(DigitalOceanHelper $digitalocean, SettingsHelper $settings)
{
$this->digitalocean = $digitalocean;

$current_ip = Ip::get();
$has_ip_changed = $this->checkIfIPChanged($current_ip, $settings->getLastKnownIP());

if (!$has_ip_changed) {
$this->info("IP address has not changed. Doing nothing.");
return;
}

$this->updateIPAddress($current_ip);
}

private function updateIPAddress($current_ip)
{
DB::table('settings')->update(
['last_known_ip' => $current_ip]
);

$records_to_update = DB::table('records')->get();

$records_to_update->each(function ($record) use ($current_ip, $digitalocean) {
$digitalocean->domainRecord->update($record->domain, $record->record_id, $record->record_name, $current_ip);
$records_to_update->each(function ($record) use ($current_ip) {
$this->digitalocean->domainRecord->update($record->domain, $record->record_id, $record->record_name, $current_ip);

DB::update('update records set record_updated_at = ? where id = ?', [Carbon::now()->toDatetimeString(), $record->id]);

$this->info("Updated ({$record->record_type}) {$record->record_name} of {$record->domain} to : {$current_ip}");
});

$this->info("DONE!");
$this->info("IP updated to {$current_ip}!");
}

private function checkIfIPChanged($current_ip, $last_known_ip)
{
return $current_ip != $last_known_ip;
}

/**
Expand Down
43 changes: 43 additions & 0 deletions app/Commands/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Commands;

use LaravelZero\Framework\Commands\Command;

class Version extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'version';

/**
* The description of the command.
*
* @var string
*/
protected $description = "Display the current doddns' version";

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$current_version = config('app.version');
$latest_version = file_get_contents("https://raw.githubusercontent.com/jpmurray/doddns/master/VERSION.md");

$this->info("Installed version: {$current_version}");
$this->info("Latest version available: {$latest_version}");

if ($current_version == $latest_version) {
$this->line("You are up to date!");
} else {
$this->info("A new version is available!");
$this->line("You should, depending your case, pull the latest from repository, or visit https://github.com/jpmurray/doddns to download the latest build.");
}
}
}
14 changes: 12 additions & 2 deletions app/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function getToken()
return $this->getSetting('token');
}

public function hasLastKnownIP()
{
return $this->hasSetting('last_known_ip');
}

public function getLastKnownIP()
{
return $this->getSetting('last_known_ip');
}

/**
* @param $key
* @return mixed
Expand Down Expand Up @@ -67,8 +77,8 @@ private function hasSetting($key)
private function setupSettings()
{
// We bail if the settings are already loaded
if($this->settings) {
return;
if ($this->settings) {
return;
}

if (!is_file(config('database.connections.sqlite.database'))) {
Expand Down
Binary file modified builds/doddns
100644 → 100755
Binary file not shown.
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Changelog

# X (Next release)
- Nothing yet.

# 1.4.0
- Records the latest IP that has been found / use while running the update command.
- Adds a new command to see the latest known ip: `doddns last-known-ip`.
- Updates IP with Digital Ocean only when the current IP is different than the last known IP.
- Answering no to the database overwrite on setup actually stops the process.
- `doddns setup` now show a menu with different options: first time setup, start from scratch and upgrade.
- `doddns version` outputs installed and available version, and checks if you are up to date.

# 1.3.0
- Now with more dependency injection thanks to @victorlap !
Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
|
*/

'version' => "1.3.0",
'version' => "1.4.0",

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddLatestIpToSettings extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('settings', function (Blueprint $table) {
$table->string('last_known_ip')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('last_known_ip');
});
}
}

0 comments on commit d27f0d8

Please sign in to comment.