protected function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('sql-file'); $filesystem = new Filesystem(); if (!$filesystem->isFile($file)) { throw new FileNotFoundException("We could not find the file you wanted to send to the remote server"); } $vars = $input->getArguments(); $keyLocation = $vars['ssh-secret']; if (!$filesystem->isFile($keyLocation)) { $output->writeln(sprintf('<error>Oh NO! We couldn\'t find the private key located here: %s</error>', $keyLocation)); $pattern = '/^~\\//i'; if (preg_match($pattern, $keyLocation) === 1) { $output->writeln('<error>Maybe use the key absolute path?</error>'); } $output->writeln('<error>We are going to bail and let you fix this. </error>'); return; } $auth = ['key' => $vars['ssh-secret']]; $output->writeln("Connecting to remote host"); $remote = new Connection('remote', $vars['ssh-host'], $vars['ssh-user'], $auth); if ($remote->getGateway()) { $output->writeln("Connection established. Transferring file " . $file); } $fileName = $filesystem->name($file); $remoteFile = $fileName; $remote->put($file, $remoteFile); $output->writeln("File transfered. Importing into database"); $mysqlCommandFormat = "mysql -u %s -p'%s' %s < %s"; $mysqlImportCommand = sprintf($mysqlCommandFormat, $vars['db-user'], $vars['db-password'], $vars['db-name'], $remoteFile); $mysqlDropDbCommand = sprintf("mysql -u %s -p'%s' -e 'DROP DATABASE %s;'", $vars['db-user'], $vars['db-password'], $vars['db-name']); $mysqlCreateDbCommand = sprintf("mysql -u %s -p'%s' -e 'CREATE DATABASE %s;'", $vars['db-user'], $vars['db-password'], $vars['db-name']); $remote->run($mysqlDropDbCommand, function ($line) use($output) { }); $remote->run($mysqlCreateDbCommand, function ($line) use($output) { }); $remote->run($mysqlImportCommand, function ($line) use($output) { $output->writeln($line); }); /* * Cleanup the remote machine */ $remote->run('rm ' . $remoteFile, function ($line) { }); $output->writeln("Remote Importer is ALL DONE !!!"); }
/** * Upload a local file to the server. * * @param string $local * @param string $remote * @return void * @static */ public static function put($local, $remote) { \Illuminate\Remote\Connection::put($local, $remote); }