Laravel SSH to server not stored in config file - php

I have a project that involves server management and I need to execute some SSH commands.
In Laravel I have the SSH utility (remote), but I have to put the configuration in a file.
I need to connect with the credentials stored in a model from the database.
Any ideas how can I do this?
Something like this:
$connArray = array(
"server" => "8.8.8.8",
"port" => "22",
"user" => "root",
"pass" => "123456"
);
SSH::into($connArray)->run(array(
'cd /var/www',
'git pull origin master',
));

You set edit configuration at runtime:
Create a new connection
(You can safely omit this part, Laravel will create the configuration entry automatically for you, but you might need to create it just for your developers to remember that some configurations are being set during runtime).
'connections' => array(
'runtime' => array(
'host' => '',
'username' => '',
'password' => '',
'key' => '',
'keyphrase' => '',
'root' => '/var/www',
),
),
Set them and do whatever you need:
Config::set('remote.connections.runtime.server', '8.8.8.8');
Config::set('remote.connections.runtime.port', '22');
Config::set('remote.connections.runtime.user', 'root');
Config::set('remote.connections.runtime.pass', '123456');
SSH::into('runtime')->run(array(
'cd /var/www',
'git pull origin master',
));

Related

Elastic Beanstalk deploy a docker environment from the API

I am at my whits end trying to solve this problem. Our ultimate goal is to deploy a custom docker container of Mautic. I have no problem doing this from their website interface. I've solved all my config problems and it works great. But I need to do this automatically from an API. Customers are going to sign up for our service, and we want to deploy Mautic for them instantly (or as instant as AWS can work).
I'm new to elastic beanstalk and AWS. But what I understand is I need to create an environment and deploy my Dockerrun.aws.json file to it. But I cannot find anywhere in the API that I can specify a file to deploy or even an S3 bucket to use (like you can from the interface). I had hoped by saving a template and using that, it would work, but I just get an empty Docker instance with no container launched.
Here's an example of my PHP api call
$eb = new ElasticBeanstalkClient(array(
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => array(
'key' => '...',
'secret' => '...'
)
));
$newEnvironment = $eb->createEnvironment(array(
'ApplicationName' => 'test',
'TemplateName' => 'foo2',
'EnvironmentName' => '...',
'EnvironmentTier' => array(
'Type' => 'Standard',
'Name' => 'WebServer'
),
'OptionSettings' => array(
[
'Namespace' => 'aws:autoscaling:launchconfiguration',
'OptionName' => 'EC2KeyName',
'Value' => '...'
],
[
'Namespace' => 'aws:rds:dbinstance',
'OptionName' => 'DBUser',
'Value' => '...'
],
[
'Namespace' => 'aws:rds:dbinstance',
'OptionName' => 'DBPassword',
'Value' => '...'
]
)
));
The template foo2 was saved from an environment that has a fully running Mautic docker container.
The problem is, this creates an environment and the RDS resource I need, but does not run my docker container.
Is what I want possible? Or do I have to find another avenue?
Thanks
Figured it out. What I was looking for was $eb->createApplicationVersion(...) which I can use to specify an S3 bucket with my Dockerrun.aws.json file. Documentation
Then I can specify that VersionLabel in my createEnvironment() call like so.
$newEnvironment = $eb->createEnvironment(array(
'ApplicationName' => 'test',
'TemplateName' => 'foo2',
// Right here
'VersionLabel` => 'fooVersion',
'EnvironmentName' => '...',
'EnvironmentTier' => array(
'Type' => 'Standard',
'Name' => 'WebServer'
),
'OptionSettings' => array(
[
'Namespace' => 'aws:autoscaling:launchconfiguration',
'OptionName' => 'EC2KeyName',
'Value' => '...'
],
[
'Namespace' => 'aws:rds:dbinstance',
'OptionName' => 'DBUser',
'Value' => '...'
],
[
'Namespace' => 'aws:rds:dbinstance',
'OptionName' => 'DBPassword',
'Value' => '...'
]
)
));
Or I can just create a version through the Dashboard. Documentation

The storage configuration for OAuth2 is missing when 'config_cache_enabled' is set to true

I'm using zf2 apigility in my web application. With production mode, if config_cache_enabled is true in config/application.config.php, I get this message error when requesting access_token:
The storage configuration for OAuth2 is missing
If I set it to false, I get my access token.
So my problem is to have config_cache_enabled set to true and a successful request for getting the access token in production mode, due to best performance when configuration is cached. How to do that ?
This is my zf-mvc-auth configuration :
'zf-mvc-auth' => array(
'authentication' => array(
'adapters' => array(
'CustomStorage' => array(
'adapter' => 'ZF\\MvcAuth\\Authentication\\OAuth2Adapter',
'storage' => array(
'storage' => 'Application\\Adapter\\OAuth\\CustomPdoAdapter',
'route' => '/oauth',
),
),
),
),
),
This is my oauth2.local.php :
'zf-oauth2' => array(
'db' => array(
'dsn' => 'mysql:dbname=mydatabase;host=localhost',
'username' => 'root',
'password' => '',
),
'allow_implicit' => true,
'access_lifetime' => 3600,
'enforce_state' => true,
'storage' => 'Application\Adapter\OAuth\CustomPdoAdapter',
'storage_settings' => array(
'user_table' => 'users',
),
'options' => array(
'always_issue_new_refresh_token' => true,
),
),
I think it is well configured.
Did you setup your zf-mvc-auth correctly. In the module.config.php you can read that you have to define a storage key. There is also written how you can do this:
To specify the storage instance, you may use one of two approaches:
Specify a "storage" subkey pointing to a named service or an array
of named services to use.
Specify an "adapter" subkey with the value "pdo" or "mongo", and
include additional subkeys for configuring a ZF\OAuth2\Adapter\PdoAdapter
or ZF\OAuth2\Adapter\MongoAdapter, accordingly. See the zf-oauth2
documentation for details.
If you are on production mode and "config_cache_enabled" it's true, you need to delete files on data/cache folder

How to configure cakephp to use memcached

I'm trying to use an external cache engine, memcached, to power my CakePHP app.
I have an AWS EC2 instance running the app and also an AWS ElastiCache Cluster with one node using memcached. The memcache and memcached php modules are also installed and enabled.
The configuration in the app.php file is as follows:
'Cache' => [
'default' => [
'className' => 'File',
'path' => CACHE,
],
'elastic' => [
'className' => 'Cake\Cache\Engine\MemcachedEngine',
'compress' => false,
'duration' => '+2 minutes',
'groups' => [],
'host' => 'yyy.euw1.cache.amazonaws.com:11211',
'username' => null,
'password' => null,
'persistent' => false,
'prefix' => 'cake_',
'probability' => 100,
'serialize' => 'php',
'servers' => ['yyy.euw1.cache.amazonaws.com:11211'],
'options' => [],
'lock' => true
]
To select whether or not to query the database, this condition is used:
if (($car = Cache::read('car', 'elastic')) === false) {
$car = $this->Cars->get();
Cache::write('car', $car, 'elastic');
}
Unfortunately, after a long page load I get this error:
"elastic cache was unable to write to DebugKit\Cache\Engine\DebugEngine cache"
Does anyone knows the origin of this error? Can someone guide me through the configuration of memcached for cakephp, using an external cache engine?
Thank you upfront!
Thank you for your reply.
This issue is now closed. We had to allow IP access between EC2 and CloudCache Cluster.

Apache Solr with Php

I am trying to setup apache solr search for one of my project. I have installed solr 3.6 in my development server and it is reachable with
http://127.0.0.1:8080/solr/admin/
I tried to put the sample application available in php manual but it is setting username and password for solr. I am not sure where can I get this information from. I have also tried below code from net but I was getting 500 error whenever I run it
$options = array (
'hostname' => '127.0.0.1',
);
//$client = new SolrClient($options, "4.0"); // use 4.0 for any version of Solr 4.x, ignore this parameter for previous versions
$doc = new SolrInputDocument();
$doc->addField('id', 100);
$doc->addField('title', 'Hello Wolrd');
$doc->addField('description', 'Example Document');
$doc->addField('cat', 'Foo');
$doc->addField('cat', 'Bar');
$response = $client->addDocument($doc);
$client->commit();
/* ------------------------------- */
$query = new SolrQuery();
$query->setQuery('hello');
$query->addField('id')
->addField('title')
->addField('description')
->addField('cat');
$queryResponse = $client->query($query);
$response = $queryResponse->getResponse();
print_r( $response->response->docs );
Please help
your solr version is 3.6 so you should assign $client = new SolrClient($options);
$client = new SolrClient($options, "4.0"); // I see that in your code you commented this line which is required thinking that it is only for solt 4.x in your case, you should uncomment it and only remove the "4.0" in order to create a client.
To create a client :
$options = array (
'host' => "localhost",
'port' => 8983, //port is required
'path' => '/solr/collection1', //collection1 or core are mandatory it can be just /solr/
);
$client = new SolrClient($options); //for solr 3.x
According to the example code, you are not setting the port in your $options array. Should be:
var $options = array(
'hostname' => '127.0.0.1',
'port' => '8080',
);
That could be the cause of the 500 error.
you need to add 3 things host, port, and webapp. I hope you have included the service.php file in that.
var $options = array(
'hostname' => '127.0.0.1',
'port' => '8080', //Port number where solr runs.
'webapp' => '/solr/', //path of the webapp.
);
Master site configuration
For settings.php file in your master site you do not have to change much. Leave your $databases array as it as. Master site will store all the user names, passwords, and sessions.
For settings.php file in your master site you do not have to change much. Leave your $databases array as it as. Master site will store all the user names, passwords, and sessions.
$databases = array(
'default' =>
array(
'default' =>
array(
'database' => 'drupal1',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
Slave sites configuration
The slave sites shall be connected to the Master site's database for certain tables, specially the ones that include user information. For settings.php file in your slave sites you need to specify master site database and call its users and other tables. We can do so by adding configuration settings within the "prefix" key of the $databases array.
$databases = array(
'default' =>
array(
'default' =>
array(
'database' => 'drupal2',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => array(
'default' => 'drupal2.',
'users' => 'drupal1.',
'sessions' => 'drupal1.',
'role' => 'drupal1.',
'authmap' => 'drupal1.',
'users_roles' => 'drupal1.',
),
),
),
);
For more detailed instruction visit - How to set up and use Apache Solr in php

How can I access CakePHP log files on Heroku?

I've deployed a CakePHP application to Heroku. CakePHP writes its logs in APP_ROOT/app/tmp/logs/error.log and APP_ROOT/app/tmp/logs/debug.log by default but since there's no way to get a shell to a running Heroku web dyno, I can't see the content of those files.
As I understand it, the heroku logs command returns everything which has been dumped to STDERR and STDOUT. If I'm right about that, is there a way to force CakePHP to send its logs to STDOUT?
The Heroku PHP Buildpack tails the Apache and PHP log files as a background process as part of the dyno setup. See below.
cat >>boot.sh <<EOF
for var in \`env|cut -f1 -d=\`; do
echo "PassEnv \$var" >> /app/apache/conf/httpd.conf;
done
touch /app/apache/logs/error_log
touch /app/apache/logs/access_log
tail -F /app/apache/logs/error_log &
tail -F /app/apache/logs/access_log &
export LD_LIBRARY_PATH=/app/php/ext
export PHP_INI_SCAN_DIR=/app/www
echo "Launching apache"
exec /app/apache/bin/httpd -DNO_DETACH
EOF
In a fork of that build pack, I added in my own lines in the appropriate positions, then configured my app to use my custom build pack.
touch /app/www/tmp/logs/error.log
tail -F /app/www/app/tmp/logs/error.log &
But this didn't work. In fact, setting aside CakePHP specifics, I don't see any PHP or Apache log contents in the heroku logs either.
I think the following might work. Make sure you're using CakePHP 2.3.9.
App::uses('ConsoleOutput', 'Console');
CakeLog::config('default', array(
'engine' => 'ConsoleLog',
'stream' => new ConsoleOutput('php://stdout')
));
CakeLog::config('stdout', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info'),
'stream' => new ConsoleOutput('php://stdout')
));
CakeLog::config('stderr', array(
'engine' => 'ConsoleLog',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => new ConsoleOutput('php://stderr')
));
CakeLog::config('debug', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info', 'debug'),
'format' => 'debug %s: %s',
'stream' => new ConsoleOutput('php://stdout')
));
CakeLog::config('error', array(
'engine' => 'ConsoleLog',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'format' => 'error %s: %s',
'stream' => new ConsoleOutput('php://stderr')
));
From the Heroku documentation: https://devcenter.heroku.com/articles/php-logging#cakephp
In your application configuration, instruct CakePHP to use the ConsoleLog engine for your logger setups:
CakeLog::config('default', array(
'engine' => 'ConsoleLog',
));
You can then use the regular logging methods.
CakeLog::warning("Hello, this is a test message!");
Refer to the Logging section of the CakePHP manual for more information.
In the latest versions of CakePHP (>= 3.6.0), the config/app.php file comes pre-configured to use an environment variable override for the stock debug and error logs. These vars should contain a DSN-style string defining the same attributes you would normally place in your config/app.php file.
In the Heroku dashboard under Settings (or via the heroku cli tool), you can add the following ENV vars:
LOG_DEBUG_URL = file:///?className=Cake%5CLog%5CEngine%5CConsoleLog&levels[]=notice&levels[]=info&levels[]=debug
LOG_ERROR_URL = file:///?className=Cake%5CLog%5CEngine%5CConsoleLog&levels[]=warning&levels[]=error&levels[]=critical&levels[]=alert&levels[]=emergency
The above two lines replicate the stock CakePHP logging, but redirect all output to the console instead of files.
you can use: 'className' => 'Cake\Log\Engine\ConsoleLog'
/**
* Configures logging options
*/
'Log' => [
'debug' => [
'className' => 'Cake\Log\Engine\ConsoleLog',
'path' => LOGS,
'file' => 'debug',
'levels' => ['notice', 'info', 'debug'],
'url' => env('LOG_DEBUG_URL', null),
],
'error' => [
'className' => 'Cake\Log\Engine\ConsoleLog',
'path' => LOGS,
'file' => 'error',
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
'url' => env('LOG_ERROR_URL', null),
],
],

Categories