Iterating over files in Puppet (3.1) - ruby

I am looking for a dry version of the following:
Ideally it could be just an array and a loop. What is the way to go in Puppet?
class hive_cdh4::configs inherits hive_cdh4 {
define hive_configs () {
file { "/etc/hive/conf.rr/hive-site.xml":
owner => root,
group => root,
mode => 664,
ensure => present,
content => template("hive_cdh4/hive-site.xml.erb")
}
file { "/etc/hive/conf.rr/hive-exec-log4j.properties":
owner => root,
group => root,
mode => 664,
ensure => present,
content => template("hive_cdh4/hive-exec-log4j.properties.erb")
}
file { "/etc/hive/conf.rr/hive-log4j.properties":
owner => root,
group => root,
mode => 664,
ensure => present,
content => template("hive_cdh4/hive-log4j.properties.erb")
}
}
}

How about something like this:
define hive_config ($file_name = $title, $format = 'properties') {
file { "/etc/hive/conf.rr/$file_name.$format":
owner => root,
group => root,
mode => '0664',
ensure => present,
content => template("hive_cdh4/$file_name.$format.erb")
}
}
hive_config {'hive-site':}
hive_config {'hive-exec-log4j':}
hive_config {'hive-log4j':
format => 'xml'
}
I tested it quickly in a Vagrant provision, and it seems to work?

Related

laravel save pdf no such file or directory

So I want to save a pdf file to a directory on my local server but it keeps saying that the directory does not exist.
So first of all where would you store PDF files that are not accessible to by externals (so not in the public folder).
So this is my code. The download works perfectly.
public function generatePDF()
{
$this->mailorder = Session::get('order');
$this->cart = Session::get('cart');
$data = [
'id' => $this->mailorder->id,
'client' => $this->mailorder->Contact,
'country' => $this->mailorder->country,
'city' => $this->mailorder->city,
'street' => $this->mailorder->street,
'postal' => $this->mailorder->postal,
'phone' => $this->mailorder->phone,
'email' => $this->mailorder->email,
'dateIn' => $this->mailorder->dateIn,
'dateOut' => $this->mailorder->dateOut,
'subtotal' => $this->mailorder->subtotal,
'tax' => $this->mailorder->tax,
'total' => $this->mailorder->total,
'cart' => $this->mailorder->cart,
'delivery' => $this->mailorder->delivery,
];
$path = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
$pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save('storage/app/public/'.$path.'.pdf');
;
return $pdf->download(''.$path.'.pdf');
}
First of all, you should check if the directory exists with File facade. If it does not exist, you must make the directory.
if(!File::exists($directory_path)) {
File::makeDirectory($directory_path);
}
If the error still occurs, you must force it to make the directory:
if(!File::exists($directory_path)) {
File::makeDirectory($directory_path, $mode = 0755, true, true);
}
After that, you can save the file in that directory.
Second, if you don't want to save the file in the public directory. you must save it in storage.By simply call storage_path($file_path). this way laravel saves the file under storage/app/public directory.
after that, you can get the URL of the file according to this answer.
I figured it out thank you for your answer.
This is my code:
public function generatePDF()
{
$this->mailorder = Session::get('order');
$this->cart = Session::get('cart');
$data = [
'id' => $this->mailorder->id,
'client' => $this->mailorder->Contact,
'country' => $this->mailorder->country,
'city' => $this->mailorder->city,
'street' => $this->mailorder->street,
'postal' => $this->mailorder->postal,
'phone' => $this->mailorder->phone,
'email' => $this->mailorder->email,
'dateIn' => $this->mailorder->dateIn,
'dateOut' => $this->mailorder->dateOut,
'subtotal' => $this->mailorder->subtotal,
'tax' => $this->mailorder->tax,
'total' => $this->mailorder->total,
'cart' => $this->mailorder->cart,
'delivery' => $this->mailorder->delivery,
];
$filename = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
$path = storage_path('pdf/orders');
if(!File::exists($path)) {
File::makeDirectory($path, $mode = 0755, true, true);
}
else {}
$pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save(''.$path.'/'.$filename.'.pdf');
;
return $pdf->download(''.$filename.'.pdf');
}

Logstash Duplicate Data

i have duplicate data in Logstash
how could i remove this duplication?
my input is:
input
input {
file {
path => "/var/log/flask/access*"
type => "flask_access"
max_open_files => 409599
}
stdin{}
}
filter
filter of files is :
filter {
mutate { replace => { "type" => "flask_access" } }
grok {
match => { "message" => "%{FLASKACCESS}" }
}
mutate {
add_field => {
"temp" => "%{uniqueid} %{method}"
}
}
if "Entering" in [api_status] {
aggregate {
task_id => "%{temp}"
code => "map['blockedprocess'] = 2"
map_action => "create"
}
}
if "Entering" in [api_status] or "Leaving" in [api_status]{
aggregate {
task_id => "%{temp}"
code => "map['blockedprocess'] -= 1"
map_action => "update"
}
}
if "End Task" in [api_status] {
aggregate {
task_id => "%{temp}"
code => "event['blockedprocess'] = map['blockedprocess']"
map_action => "update"
end_of_task => true
timeout => 120
}
}
}
Take a look at the image, the same data log, at the same time, and I just sent one log request.
i solve it
i create a unique id by ('document_id') in output section
document_id point to my temp and temp is my unique id in my project
my output changed to:
output {
elasticsearch {
hosts => ["localhost:9200"]
document_id => "%{temp}"
# sniffing => true
# manage_template => false
# index => "%{[#metadata][beat]}-%{+YYYY.MM.dd}"
# document_type => "%{[#metadata][type]}"
}
stdout { codec => rubydebug }
}
Executing tests in my local lab, I've just found out that logstash is sensitive to the number of its config files that are kept in /etc/logstash/conf.d directory.
If config files are more than 1, then we can see duplicates for the same record.
So, try to remove all backup configs from /etc/logstash/conf.d directory and perform logstash restart.

Failed to fetch ec2 uri http://169.254.169.254/

I have this simple puppet manifest
class javaora(include ::archive) {
group { 'serviceusrgroup':
name => 'serviceusrgroup',
ensure => 'present',
}
user { 'jersey':
name => 'jersey',
home => '/home/jersey',
gid => 'serviceusrgroup',
comment => 'User to Run servers',
}
file { ['/usrdata/archive', '/usrdata/apps/java']: ensure => 'directory' }
archive { "/usrdata/archive/${tomcat::jreversion}":
ensure => present,
extract => true,
extract_path => '/usrdata/apps/java',
source => $tomcat::jredownload,
creates => "/usrdata/apps/java/${tomcat::jdkversion}"
}
I am getting error Failed to fetch ec2 uri http://169.254.169.254/latest/meta-data/:403 Forbidden
It also gives me error at line 2 : Syntax error near include ; expected ")"

Hash with arrays - get array element

I get hash that contains user role, controller name and list of the controller actions this role can access to.
access = {
'admin' => [ 'users' => ['edit','delete'],
'messages' => ['show','update']
],
'user' => [ 'index' => ['index','sign-out'],
'messages' => ['show','index']
]
}
How can i check what access['admin']['users']['edit'] exists?
access['admin']['users'].include? 'edit'
However, this may be a problem: you're using ... => ['users'=>['edit','delete'],...]
This will create an array with a hash inside. Example:
{'a'=>'b'} #=> {"a"=>"b"}
['a'=>'b'] #=> [{"a"=>"b"}]
So consider using this:
access = {
'admin' => { 'users' => ['edit','delete'],
'messages' => ['show','update']
},
'user' => { 'index' => ['index','sign-out'],
'messages' => ['show','index']
}
}

How do I save settings as a hash in a external file?

Can I somehow use this
settings = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
in a external file as settings?
How can I include this into my script?
The most common way to store configuration data in Ruby is to use YAML:
settings.yml
user1:
path: /
days: 5
user2:
path: /tmp/
days: 3
Then load it in your code like this:
require 'yaml'
settings = YAML::load_file "settings.yml"
puts settings.inspect
You can create the YAML file using to_yaml:
File.open("settings.yml", "w") do |file|
file.write settings.to_yaml
end
That said, you can include straight Ruby code also, using load:
load "settings.rb"
However, you can't access local variables outside the file, so you would have to change your code to use an instance variable or a global variable:
settings.rb
SETTINGS = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
#settings = { 'foo' => 1, 'bar' => 2 }
Then load it thus:
load "settings.rb"
puts SETTINGS.inspect
puts #settings.inspect
you can also use Marshal
settings = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
data=Marshal.dump(settings)
open('output', 'wb') { |f| f.puts data }
data=File.read("output")
p Marshal.load(data)
A really simple one is to use eval.
config.txt
{
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
program.rb
configuration = eval(File.read("./config.txt"))
puts configuration['user1']

Resources