Puppet PostgresQL management, Error: Puppet::Parser::AST:: - postgresql

I'm using Vagrant and I'm trying to provision a VM using puppet. In the Vagrant file I configured VM db as follow:
config.vm.define "db" do |db|
db.vm.hostname = "db"
db.vm.network "private_network", ip: "10.11.1.201", virtualbox__intnet: true
db.vm.provider "virtualbox" do |v|
v.memory = 1024
end
db.vm.network "forwarded_port", guest: 22, host: 2221, id: 'ssh', auto_correct: true
db.vm.network "forwarded_port", guest: 5432, host: 2222
db.ssh.forward_agent = true
config.vm.provision :shell do |shell|
shell.inline = "mkdir -p /etc/puppet/modules;
puppet module install puppetlabs-postgresql"
end
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/manifests"
puppet.manifest_file = "site.pp"
puppet.module_path = "puppet/modules"
end
end
end
As you can see I set the download of the modules using a vagrant shell command before the puppet provisioner runs. In this way I have downloaded the puppetlabs-postgresql module from puppet labs. I do not want to manage my database by creating classes in my site.pp file located in /puppet/manifests/site.pp. I want to have a module call database in /puppet/modules/database. What I have done so far is create an init.pp file in /puppet/modules/database. Below is the content of my init.pp file :
class database {
class { 'postgresql::server':
ip_mask_allow_all_users => '0.0.0.0/0',
listen_addresses => '*',
ipv4acls => ['hostssl all johndoe 192.168.0.0/24 cert'],
postgres_password => 'TPSrep0rt!',
}
}
And then in my /puppet/manifests/site.pp file i have included the database class as below :
node 'db' {
include database
}
After the "vagrant up" command I get the error:
Error: Puppet::Parser::AST::Resource failed with error ArgumentError: Could not find declared class postgresql::server at /tmp/vagrant-puppet/modules-d1208595f982e4ac16b287f9bd398c89/database/manifests/init.pp:8 on node db.lan
==> db: Wrapped exception:
==> db: Could not find declared class postgresql::server
==> db: Error: Puppet::Parser::AST::Resource failed with error ArgumentError: Could not find declared class postgresql::server at /tmp/vagrant-puppet/modules-d1208595f982e4ac16b287f9bd398c89/database/manifests/init.pp:8 on node db.lan
What is the correct way to make use of the postgresql classes?

It is weird feeling to read your code, especially the class database part.
can you set hieradata template for puppet module postgresql
something likes:
postgresql::server:
ip_mask_allow_all_users: '0.0.0.0/0'
listen_addresses: '*'
ipv4acls:
- 'hostssl all johndoe 192.168.0.0/24 cert'
postgres_password: 'TPSrep0rt!'
in node define,
node 'db' {
include postgresql::server
}
manage the facters in pp file directly is not good practice.

Related

Save monolog in mongodb in symfony 4

I want to add monolog in mongodb with default handler(MongoDBHandler) in Symfony 4.
my monolog.yaml file in dev folder
monolog:
handlers:
mongo:
type: mongo
mongo:
id: monolog.logger.mongo
host: '%env(MONGODB_URL)%'
database: '%env(MONGODB_DB)%'
collection: logs
my services.yaml
services:
monolog.logger.mongo:
class: Monolog\Handler\MongoDBHandler
arguments: ['#doctrine_mongodb']
my doctrine_mongodb.yaml
doctrine_mongodb:
auto_generate_proxy_classes: '%kernel.debug%'
auto_generate_hydrator_classes: '%kernel.debug%'
connections:
default:
server: '%env(MONGODB_URL)%'
options:
db: '%env(MONGODB_DB)%'
log:
server: '%env(MONGODB_URL)%'
options:
db: '%env(MONGODB_DB)%'
connect: true
default_database: '%env(MONGODB_DB)%'
document_managers:
log:
auto_mapping: false
logging: false
But doesn't work.
one of the errors:
Cannot autowire service "monolog.logger.mongo": argument "$database"
of method "Monolog\Handler\MongoDBHandler::__construct()" is
type-hinted "string", you should configure its value explicitly.
While i use database option in monolog config.
Is there any document?
Another way to enable mongodb for monolog is:
monolog:
handlers:
mongo:
type: mongo
mongo:
host: '%env(MONGODB_URL)%'
user: myuser
pass: mypass
database: '%env(MONGODB_DB)%'
collection: logs
, So it mean you need to remove id field and add user and pass instead.
If you use doctrine mongodb already, it's possible to re-use it's connection, avoiding more ENV vars to separate the DSN:
monolog:
handlers:
mongo:
type: mongo
mongo:
id: "doctrine_mongodb.odm.default_connection"
database: "%env(MONGODB_DB)%"
collection: MyLogDocument # Keeping this the same, allows you to simply use a doctrine repository to access the documents in your app if needed
level: debug
I get the following error:
Attempted to load class "MongoClient" from the global namespace.
Did you forget a "use" statement?
protected function getMonolog_Handler_MongoService()
{
$this->privates['monolog.handler.mongo'] = $instance = new \Monolog\Handler\MongoDBHandler(new \MongoClient('mongodb://admin:pass#localhost:27017'), 'monolog', 'logs', 100, true);
$instance->pushProcessor(($this->privates['monolog.processor.psr_log_message'] ?? ($this->privates['monolog.processor.psr_log_message'] = new \Monolog\Processor\PsrLogMessageProcessor())));
return $instance;
}

Why can I use entity manager in controller and not in Command?

I have created a service for getting the entity manager in a command
I don't understand why I can use the entity manager in regular Controller and not in my Command.
I have declared my service in "service.yml"
common.doctrine:
class: AppBundle\Services\GetDoctrineService
arguments: [ '#doctrine.orm.entity_manager' ]
public: true
I wrote it in "GetDoctrineService.php"
<?php
namespace AppBundle\Services;
use Doctrine\ORM\EntityManager;
class GetDoctrineService
{
protected $em;
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->em = $em;
}
public function getRepository(string $repo) {
return $this->em->getRepository($repo);
}
}
In my command, I am importing it and calling it in the "Command way"
$em = $this->getApplication()->getKernel()->getContainer()->get('common.doctrine');
$foo = $em->getRepository(Entity::FOO)->findAll();
I changed the db host to "localhost" in "parameters.yml"
# This file is auto-generated during the composer install
parameters:
#database_host: db-name
database_host: localhost
When I installed postgres with a command, the file "pg_hba.conf" wasn't created (I am working with Ubuntu)
I have this error while i use my command :
Message: "An exception occured in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?"
The problem was in the configuration files:
using 'localhost' was a mistake: we should have use the dn-name for the host

Accessing config variables from other config files

I am having problems using in a config file a config var set in another config file. E.g.
// file - config/local.js
module.exports = {
mongo_db : {
username : 'TheUsername',
password : 'ThePassword',
database : 'TheDatabase'
}
}
// file - config/connections.js
module.exports.connections = {
mongo_db: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: sails.config.mongo_db.username,
password: sails.config.mongo_db.password,
database: sails.config.mongo_db.database
},
}
When I 'sails lift', I get the following error:
user: sails.config.mongo_db.username,
^
ReferenceError: sails is not defined
I can access the config variables in other places - e.g, this works:
// file - config/bootstrap.js
module.exports.bootstrap = function(cb) {
console.log('Dumping config: ', sails.config);
cb();
}
This dumps all the config settings to the console - I can even see the config settings for mongo_db in there!
I so confuse.
You can't access sails inside of config files, since Sails config is still being loaded when those files are processed! In bootstrap.js, you can access the config inside the bootstrap function, since that function gets called after Sails is loaded, but not above the function.
In any case, config/local.js gets merged on top of all the other config files, so you can get what you want this way:
// file - config/local.js
module.exports = {
connections: {
mongo_db : {
username : 'TheUsername',
password : 'ThePassword',
database : 'TheDatabase'
}
}
}
// file - config/connections.js
module.exports.connections = {
mongo_db: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017
},
}
If you really need to access one config file from another you can always use require, but it's not recommended. Since Sails merges config files together based on several factors (including the current environment), it's possible you'd be reading some invalid options. Best to do things the intended way: use config/env/* files for environment-specific settings (e.g. config/env/production.js), config/local.js for settings specific to a single system (like your computer) and the rest of the files for shared settings.

psql: FATAL: role "vagrant" does not exist [duplicate]

This question already has answers here:
PostgreSQL error: Fatal: role "username" does not exist
(19 answers)
Closed 8 years ago.
I created a vagrant instance and I am getting this error every time I try and do psql in the terminal How would Fix it. the error is as states:
psql: FATAL: role "vagrant" does not exist
I thought vagrant takes care of this? This is my vagrant file:
Vagrant.require_plugin "vagrant-omnibus"
Vagrant.require_plugin "vagrant-berkshelf"
Vagrant.configure(2) do |config|
# Box config
config.vm.box = 'precise64'
config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
# Plugin config
config.omnibus.chef_version = :latest
config.berkshelf.enabled = true
# Network config
config.vm.network :forwarded_port, guest: 3000, host: 3000
# Virtual config
config.vm.provider(:virtualbox) do |vb|
vb.customize [
"modifyvm", :id,
"--memory", "1024",
"--cpus", "4"
]
end
# Provisioner config
config.vm.provision :chef_solo do |chef|
chef.add_recipe 'apt'
chef.add_recipe 'postgresql::client'
chef.add_recipe 'postgresql::server'
chef.add_recipe 'build-essential'
chef.add_recipe 'rvm::system'
chef.add_recipe 'git'
chef.json = {
:postgresql => {
:version => '9.3'
},
"postgresql" => {
"password" => {
"postgres" => "kshgfi3ret3hihjfbkivtbo3ity835"
}
},
"database" => {
"create" => ["aisisplatform"]
},
:git => {
:prefix => "/usr/local"
},
:rvm => {
'rubies' => [ 'ruby-2.1.0' ],
'default_ruby' => 'ruby-2.1.0',
'vagrant' => {
:system_chef_solo => '/usr/bin/chef-solo'
}
},
}
end
end
You don't have vagrant user in postgres, and when you run psql, it tries to login as vagrant user (the same name as OS user). You may try something like:
psql -U postgres -h localhost
to login as postgres user, with password specified in your Vagrantfile for postgresq user.
Then, you have several options:
Export PGUSER and PGHOST environment variables to set user and host (psql without parameters will use these values). You may also want to use .pgpass file to avoid entering password on each psql execute.
Modify Vagrantfile to create vagrant user in postgres with password

Vagrant not working properly

I have a vagrant file that uses chef to help install things:
Vagrant.configure(2) do |config|
config.vm.box = 'opscode-ubuntu-12.04_chef-11.4.0'
config.vm.box_url = 'https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_chef-11.4.0.box'
config.vm.network :forwarded_port, guest: 3000, host: 3000
config.vm.provider(:virtualbox) do |vb|
vb.customize [
"modifyvm", :id,
"--memory", "1024",
"--cpus", "4"
]
end
config.vm.provision :shell, inline: %Q{
sudo apt-get install -y postgresql-client
}
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe :apt
chef.add_recipe 'postgresql::server'
chef.add_recipe 'build-essential'
chef.add_recipe 'rvm::vagrant'
chef.add_recipe 'rvm::system'
chef.add_recipe 'git'
chef.json = {
:postgresql => {
:version => '9.3'
},
"postgresql" => {
"password" => {
"postgres" => "kshgfi3ret3hihjfbkivtbo3ity835"
}
},
"database" => {
"create" => ["aisisplatform"]
},
:git => {
:prefix => "/usr/local"
},
:rvm => {
'rubies' => [ 'ruby-2.1.0' ],
'default_ruby' => 'ruby-2.1.0',
'vagrant' => {
:system_chef_solo => '/usr/bin/chef-solo'
}
},
}
end
end
There are a few issues with this:
Through out the vagrant up start up I get warnings like this:
/tmp/vagrant-chef-1/chef-solo-1/cookbooks/rvm/libraries/rvm_chef_user_environment.rb:32: warning: class variable access from toplevel
The next issue is that things don't work properly, some times the vm sees PostgreSQL, through the psql command, sometimes it doesn't know what it is and states it's not installed. when it does see it, it states that psql: FATAL: role "vagrant" does not exist
The final issue is that when the vm boots up there is well over 200 updates of both regular and security. I would like this to be taken care of when the vm is set up the first time, via vagrant up. I tried doing:
config.vm.provision :shell, inline: %Q{
sudo apt-get update
sudo apt-get upgrade -y
}
But when the script runs I get TON of errors about stdn and so on and so forth. So, what do I do to fix these? what's wrong with my vagrant file?
I adapted the vagrant file to use the omnibus and berkshelf plugins. The former will ensure chef is at the desired version and the latter keeps the cookbooks up-to-date.
I also noted the "class variable access" warnings, whose root cause is presumably buried in the rvm cookbook. I didn't look deeper because my Vagrant run completed without error.
Example
$ tree
.
├── Berksfile
└── Vagrantfile
Berksfile
site :opscode
cookbook "apt"
cookbook "postgresql"
cookbook "build-essential"
cookbook "rvm", :github => "fnichol/chef-rvm"
cookbook "git"
Vagrantfile
Vagrant.require_plugin "vagrant-omnibus"
Vagrant.require_plugin "vagrant-berkshelf"
Vagrant.configure(2) do |config|
# Box config
config.vm.box = 'precise64'
config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
# Plugin config
config.omnibus.chef_version = :latest
config.berkshelf.enabled = true
# Network config
config.vm.network :forwarded_port, guest: 3000, host: 3000
# Virtual config
config.vm.provider(:virtualbox) do |vb|
vb.customize [
"modifyvm", :id,
"--memory", "1024",
"--cpus", "4"
]
end
# Provisioner config
config.vm.provision :chef_solo do |chef|
chef.add_recipe 'apt'
chef.add_recipe 'postgresql::client'
chef.add_recipe 'postgresql::server'
chef.add_recipe 'build-essential'
chef.add_recipe 'rvm::system'
chef.add_recipe 'git'
chef.json = {
:postgresql => {
:version => '9.3'
},
"postgresql" => {
"password" => {
"postgres" => "kshgfi3ret3hihjfbkivtbo3ity835"
}
},
"database" => {
"create" => ["aisisplatform"]
},
:git => {
:prefix => "/usr/local"
},
:rvm => {
'rubies' => [ 'ruby-2.1.0' ],
'default_ruby' => 'ruby-2.1.0',
'vagrant' => {
:system_chef_solo => '/usr/bin/chef-solo'
}
},
}
end
end
Notes:
A standard Ubuntu image can be used. The omnibus plugin will install Chef 11.10 automatically
The "rvm::vagrant" recipe was removed as unncessary. Chef is installed using the omnibus installer and will therefore have it's own embedded ruby version
Used the "postgresql::client" recipe instead of a shell provisioner.