Skip to content

Commit

Permalink
Add ./tower app:update cli command to update Cockpit to the latest …
Browse files Browse the repository at this point in the history
…or specific version
  • Loading branch information
aheinze committed Aug 11, 2022
1 parent b0a7460 commit 14f51d6
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
# Release Notes


## 2.2.1 (2022-08-10)

- Add `./tower app:update` cli command to update Cockpit to the latest or specific version

## 2.2.1 (2022-08-10)

- Fix linked tree content item display resolver
Expand Down
5 changes: 0 additions & 5 deletions modules/App/bootstrap.php
Expand Up @@ -32,8 +32,3 @@
$this->on('app.user.disguise', function(array &$user) {
unset($user['password'], $user['apiKey'], $user['_reset_token']);
});

$this->on('app.cli.init', function($cli) {
$app = $this;
include(__DIR__.'/cli.php');
});
9 changes: 0 additions & 9 deletions modules/App/cli.php

This file was deleted.

6 changes: 3 additions & 3 deletions modules/Content/views/index.php
Expand Up @@ -85,15 +85,15 @@
</div>

<kiss-grid cols="2" gap="small">
<kiss-card class="kiss-padding-small kiss-bgcolor-contrast" theme="bordered">
<kiss-card class="kiss-padding kiss-bgcolor-contrast" theme="bordered">
<span class="kiss-size-xsmall"><?=t('Models')?></span>
<div class="kiss-size-2 kiss-text-bold">{{ (singletons.length + lists.length) }}</div>
</kiss-card>
<div class="kiss-padding-small" v-if="lists.length">
<div class="kiss-padding" v-if="lists.length">
<span class="kiss-size-xsmall kiss-color-muted"><?=t('Lists')?></span>
<div class="kiss-size-2 kiss-color-muted">{{ lists.length }}</div>
</div>
<div class="kiss-padding-small" v-if="singletons.length">
<div class="kiss-padding" v-if="singletons.length">
<span class="kiss-size-xsmall kiss-color-muted"><?=t('Singletons')?></span>
<div class="kiss-size-2 kiss-color-muted">{{ singletons.length }}</div>
</div>
Expand Down
106 changes: 106 additions & 0 deletions modules/System/Command/App/Update.php
@@ -0,0 +1,106 @@
<?php

namespace System\Command\App;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class Update extends Command {

protected static $defaultName = 'app:update';
protected $app = null;

public function __construct(\Lime\App $app) {
$this->app = $app;
parent::__construct();
}

protected function configure(): void {
$this
->setHelp('This command updates Cockpit to the latest version')
->addArgument('target', InputArgument::OPTIONAL, 'What is the target release (e.g. core or pro')
->addArgument('version', InputArgument::OPTIONAL, 'Cockpit version');
}

protected function execute(InputInterface $input, OutputInterface $output): int {

$version = $input->getArgument('version') ?? 'master';
$target = $input->getArgument('target') ?? 'core';

if (!in_array($target, ['core', 'pro'])) {
$target = 'core';
}

if (!is_writable(APP_DIR)) {
$output->writeln("<error>[x] app root is not writable!</error>");
return Command::FAILURE;
}

$zipUrl = "https://files.getcockpit.com/releases/{$version}/cockpit-{$target}.zip";

$output->writeln("Download Cockpit release from ${zipUrl}...");

try {
$this->update($zipUrl, "cockpit-{$target}");
} catch (\Exception $e) {
$output->writeln("<error>[x] {$e->getMessage()}</error>");
return Command::FAILURE;
}

$output->writeln('<info>[✓]</info> Cockpit updated!');
return Command::SUCCESS;
}

protected function update(string $zipUrl, string $zipRoot = '/'): bool {

$targetPath = APP_DIR;

$fs = $this->app->helper('fs');
$tmppath = $this->app->path('#tmp:');
$zipname = null;
$zipRoot = trim($zipRoot, '/');

// download

$zipname = basename($zipUrl);

if (!file_put_contents("{$tmppath}/{$zipname}", $handle = @fopen($zipUrl, 'r'))) {
throw new \Exception("Couldn't download {$zipUrl}!");
}

@fclose($handle);

// extract zip contents

@mkdir("{$tmppath}/update-{$zipname}", 0777);
$zip = new \ZipArchive;

if ($zip->open("{$tmppath}/{$zipname}") === true) {

if (!$zip->extractTo("{$tmppath}/update-{$zipname}")) {
throw new \Exception('Extracting zip file failed!');
}

$zip->close();

} else {
throw new \Exception('Open zip file failed!');
}


$fs->delete("{$tmppath}/update-{$zipname}/{$zipRoot}/config");
$fs->delete("{$tmppath}/update-{$zipname}/{$zipRoot}/storage");

// copy

$fs->copy("{$tmppath}/update-{$zipname}/{$zipRoot}", $targetPath);

// cleanup
$fs->delete("{$tmppath}/{$zipname}");
$fs->delete("{$tmppath}/update-{$zipname}");

return true;
}
}
@@ -1,6 +1,6 @@
<?php

namespace App\Command\Cache;
namespace System\Command\Cache;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -27,4 +27,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('<info>[✓]</info> Tmp folder was flushed!');
return Command::SUCCESS;
}
}
}
@@ -1,6 +1,6 @@
<?php

namespace App\Command\Spaces;
namespace System\Command\Spaces;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -62,4 +62,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln("<info>[✓]</info> Space <info>{$name}</info> created!");
return Command::SUCCESS;
}
}
}
@@ -1,6 +1,6 @@
<?php

namespace App\Command\i18n;
namespace System\Command\i18n;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
5 changes: 5 additions & 0 deletions modules/System/bootstrap.php
Expand Up @@ -18,6 +18,11 @@
} catch(Throwable $e) {}
});

$this->on('app.cli.init', function($cli) {
$app = $this;
include(__DIR__.'/cli.php');
});

// system api
$this->module('system')->extend([

Expand Down
10 changes: 10 additions & 0 deletions modules/System/cli.php
@@ -0,0 +1,10 @@
<?php

if (!isset($cli, $app) || PHP_SAPI !== 'cli') {
return;
}

$cli->add(new System\Command\App\Update($app));
$cli->add(new System\Command\Cache\Flush($app));
$cli->add(new System\Command\Spaces\Create($app));
$cli->add(new System\Command\i18n\CreateTranslation($app));

0 comments on commit 14f51d6

Please sign in to comment.