CakePHP 3 Cache File Permissions - cakephp

I have a CakePHP 3 app with shells that I run from crontab.
When I run the shells through crontab, it creates cache files owned by the user running the crontab, which is not the user that runs apache...
Sometimes when I run the crontab the cached models are owned by apache and the shell fails, sometimes when I visit a page the models are owned by ec2-user and the page fails...
I posted a question on github, https://github.com/cakephp/cakephp/issues/11265#issuecomment-333951638
I was told to modify the chmod option for the cache config, I tried the following but it didn't work...
/**
* Configure the cache adapters.
*/
'Cache' => [
'default' => [
'className' => 'File',
'path' => CACHE,
'url' => env('CACHE_DEFAULT_URL', null),
'chmod' => 777
],
Any ideas on how I can make the default file permissions 777 on the cake cache files?

I would suggest having the chron run as the correct user OR having the chron task change owner and keep permissions as set but if you really want to have it all as it is and just change the permissions then you can use the mask option which I assume is what they meant.
Cache Config Options
Set using the following:
'Cache' => [
'default' => [
'mask' => 0777,
// other config options
],
]

#KaffineAddict is correct but make sure you do not wrap the value of mask in quotes as this can cause the values to not give correct permissions.
'Cache' => [
'default' => [
'mask' => 0777,
// other config options
],
]

Related

Update permissions to a file not deployed by puppet

I would like to know how to update the permissions for a file that is not getting deployed by puppet. The file is actually getting deployed by an RPM. I have tried the following with no luck:
file { '/some/directory/myfile.conf' :
ensure => 'file',
replace => 'no',
owner => 'someuser',
group => 'somegroup',
mode => '0644'
}
This actually removes the content of the file and leaves an empty file. However it sets the right permissions and mode. I would like to keep the content. I am using puppet 2.7.3.

Codeigniter 3 - override db hostname in model

I'm using a small CI codebase to run a simple feature in a larger codebase (a mix of Zend and Syfmony).
That master system uses a bootstrap file to set various global variables that I've accessed by creating a library file. it all works great and I can access the parent system, functions and global vars within my CI app.
BUT... That parent system stores DB connection info in sever related override files (So if code is deployed on server test.server, then config file test.server is loaded, and all relevant vars are set to that server.
What I need to do is tell my DB config file to access those vars.
OR, in my model, set hostname, user and pass vars.
I've tried:
$this->db->hostname = GLOBAL_VAR_HOSTNAME;
but that is not working.
So can I pass any data from my library file into a config file, so I can set:
$db['default'] = array(
'hostname' => GLOBAL_VAR_HOSTNAME,
'username' => GLOBAL_VAR_USERNAME,
etc etc
Setting up all the DB info in the standard database config file as new servers are added all the time and they tech leads want to use the existing system for sharing vars.
I'm going for a coffee...
You can setup manual connections like this:
$this->load->database(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci',
'dbdriver' => 'mysqli',
));
var_dump($this->db->get('posts')->result());
Or you can create another connection completly:
$database = $this->load->database(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydb',
'dbdriver' => 'mysqli',
), TRUE);
var_dump($database->get('posts')->result());

Cake 3.0 Cache Configuration for OPcache and APCu

In cakephp 3.0 what is the right way to configure Cache based on cake built-in caching engines?
I'm using PHP 5.6 with Zend OPcache And APCu.
Zend Opcache is a built in opcode cache, it isn't something you need to configure, APCu however will need to be compiled against your webserver and enabled with APC emulation
in config/app.php you need to enter the following:
// Using a fully namespaced name.
'long' => [
'className' => 'Cake\Cache\Engine\ApcEngine',
'duration' => '+1 week',
'probability' => 100,
'path' => CACHE . 'long' . DS,
]
You can create different configs for different cache times.
Then to cache an object you simply:
$data = Cache::remember('my_cache_key', function () {
return Service::expensiveCall();
});
Or use the functions Cache::read and Cache::write

Share configuration file at CakePHP between Config files (routles.php, mail.php...)

I have a site running in localhost as a development environment and in a server for production.
There are some differences in some configuration files between both and I every time I have to update the changes in the server I have to be careful not to overwrite some files which are different.
I would like to be able to simplify this process by creating just one single file with the correct configuration for each environment.
I need to read that file currently in this Config files:
app/Config/email.php
app/Config/routes.php
And ideally, if possible in:
app/Vendor/Vendor_name/vendor_file.php
Is it possible somehow?
I have tried to use Configure::read and Configure::write but it seems it can not be done inside email settings such as public $smtp or in the routes file.
Thaks.
The routes file is simply a php file with calls to the router. You could very simply split it up into multiple files and load them yourself:
app/Config/
routes.php
routes_dev.php
routes_production.php
routes.php would then load the proper routes file.
<?php
if ($env == 'dev') {
include 'routes_dev.php';
} else {
include 'routes_production.php';
}
The email config is also just a php file. You could write a function to set the proper default config based on the environment.
class EmailConfig {
public function __construct() {
if ($env == 'dev') {
$this->default = $this->dev;
}
}
public $default = array(
'host' => 'mail.example.com',
'transport' => 'Smtp'
);
public $dev = array(
'host' => 'mail2.example.com',
'transport' => 'Smtp'
);
}
As for vendor files, that's a case by case basis.
If you have a deployment system, it might be better to actually have separate files for each environment (maybe even a full config directory) and rename them after the deployment build is complete, making Cake and your code none the wiser.
The way I used to handle this situation was to add environment variables to the apache virtualhost configuration.
SetEnv cake_apps_path /var/www/apps/
SetEnv cake_libs_path /var/www/libs/
This allowed me to then pull $_SERVER['cake_apps_path'] and $_SERVER['cake_libs_path']. Then each developer can set his own variables in his own virtualhost config, and you add that to the server's virtualhost config, and you're done. Each developer can have their own pathing.

Cakelog is writing to multiple files, but I want it to write to only the one I specify

I want to log payment gateway errors in payment.log. So add this to bootstrap:
CakeLog::config('payment', array(
'engine' => 'FileLog',
'file' => 'payment',
));
and when a problem occurred:
CakeLog::write('payment', 'The is a problem!');
but, the above command will log This is a problem! in both payment.log and error.log, while log into first file is enough.
Also, if any other problems occurred in other controllers (like users), it will log into both files. while it should just log in error.log
(I mean payment errors should log into payment.log and any other problems should write into error.log)
Where's the mistake?
Thanks.
It looks like you are missing the scopes option in the config maybe?
CakeLog::config('payments', array(
'engine' => 'File',
'scopes' => array('payment', 'order')
));
Then you need to specify the scope where you want to write to:
CakeLog::write('warning', 'Stuff is broken here', 'payment');
It may take some fiddling to get it just right, but this should help.

Resources