A free open source IT asset/license management system
Added console commands
snipe0107221d94cb
diff --git a/app/Console/Commands/PullInventory.php b/app/Console/Commands/PullInventory.phpnew file mode 100644index 0000000000..7353638f37--- /dev/null+++ b/app/Console/Commands/PullInventory.php@@ -0,0 +1,150 @@+<?php++namespace App\Console\Commands;++use App\Models\SyncAdapterInstance;+use App\SyncAdapters\SyncHostFromAdapter;+use Illuminate\Console\Command;+use Throwable;+++class PullInventory extends Command+{+ protected $signature = 'snipeit:pull-inventory {adapter? : The adapter instance slug. Omit to pull every enabled instance.}';++ protected $description = 'Pull host inventory from configured sync-adapter instances and upsert as Snipe-IT assets.';++ public function handle(): int+ {+ $slug = $this->argument('adapter');++ if ($slug !== null) {+ $instance = SyncAdapterInstance::query()->where('slug', $slug)->first();++ if ($instance === null) {+ $available = SyncAdapterInstance::query()->pluck('slug')->implode(', ');+ $this->error(sprintf(+ 'Unknown adapter instance "%s". Available: %s.',+ $slug,+ $available !== '' ? $available : '(none configured)',+ ));++ return self::FAILURE;+ }++ return $this->runInstance($instance);+ }++ // No slug argument means "run every enabled instance". Used by+ // the scheduler entry so the whole install syncs off one cron+ // line without having to enumerate instances at boot time+ // (they're user-created and change at runtime).+ $instances = SyncAdapterInstance::query()->orderBy('slug')->get();++ if ($instances->isEmpty()) {+ $this->info('No sync-adapter instances configured.');++ return self::SUCCESS;+ }++ $anyFailed = false;+ $ranCount = 0;++ foreach ($instances as $instance) {+ $adapter = $instance->adapter();+ if ($adapter === null || !$adapter->isEnabled()) {+ $this->line(sprintf('Skipping %s (not active or not configured).', $instance->slug));++ continue;+ }++ $ranCount++;+ if ($this->runInstance($instance) === self::FAILURE) {+ $anyFailed = true;+ }+ }++ $this->info(sprintf('Ran %d enabled instance(s).', $ranCount));++ return $anyFailed ? self::FAILURE : self::SUCCESS;+ }++ /**+ * Run a single instance. Extracted so the two entry paths (slug+ * arg and iterate-all) share the same body. Returns the shell+ * exit code (SUCCESS or FAILURE) rather than throwing so the+ * iterate-all path can aggregate across many instances.+ */+ private function runInstance(SyncAdapterInstance $instance): int+ {+ $slug = $instance->slug;++ $adapter = $instance->adapter();+ if ($adapter === null) {+ $this->error(sprintf(+ 'Instance "%s" references adapter_type "%s" which is not registered.',+ $slug,+ $instance->adapter_type,+ ));++ return self::FAILURE;+ }++ if (!$adapter->isEnabled()) {+ $this->error(sprintf(+ 'Adapter "%s" is not active or is missing configuration. Set it up under Settings -> Sync Adapters.',+ $slug,+ ));++ return self::FAILURE;+ }++ // CLI PHP usually has no time cap, but being explicit is+ // harmless and matches the interactive controller.+ set_time_limit(0);++ $seen = 0;+ $errors = 0;++ try {+ foreach ($adapter->pull() as $record) {+ try {+ SyncHostFromAdapter::run($record);+ $seen++;+ } catch (Throwable $e) {+ // A single bad record shouldn't take down the run.+ // Log the offending host and keep going.+ $errors++;+ $this->warn(sprintf(+ 'Failed to sync host %s: %s',+ $record->sourceId,+ $e->getMessage(),+ ));+ }+ }+ } catch (Throwable $e) {+ $abortSummary = sprintf('Sync aborted: %s', $e->getMessage());+ $instance->last_synced_at = now();+ $instance->last_sync_result = $abortSummary;+ $instance->save();++ $this->error(sprintf('%s %s', $slug, $abortSummary));++ return self::FAILURE;+ }++ // Same lang key as the UI so both paths render the same phrasing+ // on the settings page.+ $result = trans('admin/settings/general.sync_adapter_sync_complete', [+ 'count' => $seen,+ 'errors' => $errors,+ ]);+ $instance->last_synced_at = now();+ $instance->last_sync_result = $result;+ $instance->save();++ $this->info(sprintf('%s: %s', $slug, $result));++ return $errors > 0 ? self::FAILURE : self::SUCCESS;+ }+}diff --git a/app/Console/Commands/PushInventory.php b/app/Console/Commands/PushInventory.phpnew file mode 100644index 0000000000..90e1a90154--- /dev/null+++ b/app/Console/Commands/PushInventory.php@@ -0,0 +1,132 @@+<?php++namespace App\Console\Commands;++use App\Models\AssetExternalSource;+use App\Models\SyncAdapterInstance;+use App\SyncAdapters\PushableAdapter;+use Illuminate\Console\Command;+use Throwable;+++class PushInventory extends Command+{+ protected $signature = 'snipeit:push-inventory {adapter? : The adapter instance slug. Omit to push every enabled instance that supports push.}';++ protected $description = 'Push Snipe-IT-authoritative field values to configured sync-adapter instances that support pushing.';++ public function handle(): int+ {+ $slug = $this->argument('adapter');++ if ($slug !== null) {+ $instance = SyncAdapterInstance::query()->where('slug', $slug)->first();+ if ($instance === null) {+ $this->error(sprintf('Unknown adapter instance "%s".', $slug));++ return self::FAILURE;+ }++ return $this->runInstance($instance);+ }++ $instances = SyncAdapterInstance::query()->orderBy('slug')->get();+ if ($instances->isEmpty()) {+ $this->info('No sync-adapter instances configured.');++ return self::SUCCESS;+ }++ $anyFailed = false;+ $ranCount = 0;++ foreach ($instances as $instance) {+ $adapter = $instance->adapter();+ if (!$adapter instanceof PushableAdapter) {+ continue;+ }+ if (!$adapter->isEnabled() || !$adapter->canPush()) {+ $this->line(sprintf('Skipping %s (not active, not configured, or push not supported).', $instance->slug));++ continue;+ }++ $ranCount++;+ if ($this->runInstance($instance) === self::FAILURE) {+ $anyFailed = true;+ }+ }++ $this->info(sprintf('Ran push on %d push-enabled instance(s).', $ranCount));++ return $anyFailed ? self::FAILURE : self::SUCCESS;+ }++ /**+ * Run push for one instance. Mirrors the postAdapterPush controller+ * shape: iterate asset_external_sources for this instance in chunks+ * so a large fleet doesn't load everything into memory, invoke+ * $adapter->push() per asset, accumulate counts. Per-asset errors+ * get logged and counted. A single bad asset doesn't abort the run.+ */+ private function runInstance(SyncAdapterInstance $instance): int+ {+ $slug = $instance->slug;++ $adapter = $instance->adapter();+ if (!$adapter instanceof PushableAdapter) {+ $this->error(sprintf('Adapter "%s" does not support push.', $slug));++ return self::FAILURE;+ }+ if (!$adapter->isEnabled()) {+ $this->error(sprintf('Adapter "%s" is not active or is missing configuration.', $slug));++ return self::FAILURE;+ }+ if (!$adapter->canPush()) {+ $this->error(sprintf('Adapter "%s" does not currently support push (vendor gate).', $slug));++ return self::FAILURE;+ }++ set_time_limit(0);++ $pushed = 0;+ $errors = 0;++ try {+ AssetExternalSource::query()+ ->where('source', $slug)+ ->with('asset')+ ->chunkById(200, function ($rows) use ($adapter, &$pushed, &$errors) {+ foreach ($rows as $row) {+ $asset = $row->asset;+ if ($asset === null) {+ continue;+ }++ try {+ $adapter->push($asset);+ $pushed++;+ } catch (Throwable $e) {+ $errors++;+ $this->warn(sprintf(+ 'push: asset %d failed: %s',+ $asset->id,+ $e->getMessage(),+ ));+ }+ }+ });+ } catch (Throwable $e) {+ $this->error(sprintf('%s push aborted: %s', $slug, $e->getMessage()));++ return self::FAILURE;+ }++ $this->info(sprintf('%s: pushed %d asset(s), %d error(s).', $slug, $pushed, $errors));++ return $errors > 0 ? self::FAILURE : self::SUCCESS;+ }+}