How to run db.runCommand in newer versions of mongoid.
Example:
MyModel.collection.database.command(eval: "db.runCommand ( { compact: 'sessions' } )" )
used to work in older version of mongoid but it now throws "DcSite.collection.database.command(eval: "db.runCommand ( { compact: 'sessions' } )" )" error.
by
TheR
You can just use the command method directly. For example:
class Band
include Mongoid::Document
end
Band.create!
Band.collection.database.command(compact: "bands")
The command method executes a runCommand under the hood, and parses the given hash to send to the database.
Here are the docs on the command method: https://www.mongodb.com/docs/ruby-driver/master/api/Mongo/Database.html#command-instance_method
Related
We have vagrant file with trigger like
DB_NAME="mydb"
TIME=(Time.now.strftime("%Y%m%d%H%M%S"))
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"])
config.trigger.before [:destroy, :provision] do |trigger|
trigger.info = "Dumping database to /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"
trigger.run_remote = {inline: "mysqldump --add-drop-table -u #{DB_USERNAME} -p#{DB_PASSWORD} #{DB_NAME} > /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"}
end
in /vagrant/config/schema/ we have backup files like:
20181116160919_mydb.sql
How to find in ruby all files like *_mydb.sql and / or return name of latest one created?
We want automatize db backup on destroy, provision & up.
EDIT:
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"]).sort
SQL_BACKUPS.reverse.each do |filename|
puts "#{filename}"
end
return lists with sql files
ps, I don't have Experience with Ruby.
I know you have timestamped files but, if one doesn't have a timestamp or a way to distinguish the creation times, the method below is what I used for non-timestamped files.
File::Stat has a method ctime(returns the creation time), So it can be done like.
SQL_BACKUPS=Dir["./config/schema/*_#{DB_NAME}.sql"].map { |f| {name: f, ctime: File::Stat.new(f).ctime } }
sorted = SQL_BACKUPS.sort_by { |f| f[:ctime] }
sorted.last.name # gives the one that was created the last.
I would like to get help with the code in ruby filter.
logstash version is 5.0.0 alpha4.
I am testing the code in ruby filter as below but I am getting _rubyexception.
ruby {
code => "
event['newfield'] = 'test'
"
}
ruby filter is defined inside filter { }.
The logstash.log shows as below,
:timestamp=>"2016-08-03T15:26:47.291000+0900", :message=>"Ruby exception occurred: undefined method[]=' for 2016-08-03T06:26:46.829Z test %{message}:LogStash::Event", :level=>:error}
I cant find the reason why ruby filter is unable to use event object.
I appreciate if I could get some help to cope this issue.
Thanks,
Yu
There is a new event API in Logstash 5.0.0 and now you need to set fields on the event as follows:
ruby {
code => "
event.set('newfield', 'test')
"
}
For get the value from field name:
ruby {
code => "
value = (event.get('field_name'))
"
}
How can I run a 'sub function' from a script in command line? Example:
#script_1.sh
main_function() {
sub_function() {
echo "hello world"
}
}
I tried to source this file and call the function from another script:
#script_2.sh
source script_1.sh
sub_function
But I get
script_2.sh: line 3: sub_function: command not found
while I expected to just get hello world.
Thus defined the sub_function will be defined after function is called.
So:
#script_1.sh
function() {
sub_function() {
#cmd
}
}
#script_2.sh
source script_1.sh
function
sub_function
... should work ... except you should rename function, as it's a reserved word
The step missing in your question is to invoke function first - its action is to define sub_function.
Note that sub_function doesn't 'belong' to function in any way - its definition is just a side-effect of running function.
P.S. I assume you aren't really trying to call it function - that's a reserved word in bash.
I am using the msutter DSC module for puppet. While reading through the source code, I come across code like this (in dsc_configuration_provider.rb):
def create
Puppet.debug "\n" + ps_script_content('set')
output = powershell(ps_script_content('set'))
Puppet.debug output
end
What file defines the powershell function or method? Is it a ruby builtin? A puppet builtin? Inherited from a class? I know that it is being used to send text to powershell as a command and gathering results, but I need to see the source code to understand how to improve its error logging for my purposes, because certain powershell errors are being swallowed and no warnings are being printed to the Puppet log.
These lines in file dsc_provider_helpers.rb may be relevant:
provider.commands :powershell =>
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end
Surely this defines where the Powershell executable is located, but gives no indication how it is called and how its return value is derived. Are stdout and stderr combined? Am I given the text output or just the error code? etc.
This is core Puppet logic. When a provider has a command, like
commands :powershell => some binary
That is hooked up as a function powershell(*args).
You can see it with other providers like Chocolatey:
commands :chocolatey => chocolatey_command
def self.chocolatey_command
if Puppet::Util::Platform.windows?
# must determine how to get to params in ruby
#default_location = $chocolatey::params::install_location || ENV['ALLUSERSPROFILE'] + '\chocolatey'
chocopath = ENV['ChocolateyInstall'] ||
('C:\Chocolatey' if File.directory?('C:\Chocolatey')) ||
('C:\ProgramData\chocolatey' if File.directory?('C:\ProgramData\chocolatey')) ||
"#{ENV['ALLUSERSPROFILE']}\chocolatey"
chocopath += '\bin\choco.exe'
else
chocopath = 'choco.exe'
end
chocopath
end
Then other locations can just call chocolatey like a function with args:
chocolatey(*args)
How can I add conditional prereqs to dist.ini for each platform (Windows/Non windows) I want the module to support?
For example in perl code I could do:
if ( $^0 eq 'MSWin32' ){
require Win32::Foo;
}else{
require Bar::Baz;
}
How do I cater to each system/platform like this in dist.ini so that the proper prereqs are installed via cpan/cpanm?
You can't do it in dist.ini, since an ini file doesn't really have any way to do conditional logic. But one way might be to write your own Dist::Zilla plugin, something like this:
package Dist::Zilla::Plugin::MyPrereqs; # pick a better name
use Moose;
with 'Dist::Zilla::Role::PrereqSource';
sub register_prereqs {
my $self = shift;
my %prereqs;
if ( $^0 eq 'MSWin32' ) {
$prereqs{'Win32::Foo'} = '0.12'; # min. version
} else {
$prereqs{'Bar::Baz'} = '1.43';
}
$self->zilla->register_prereqs( %prereqs );
}
If you generalize this to take some platform-dependent lists of prereqs within dist.ini, it would make a good CPAN release.
Use Dist::Zilla::Plugin::OSPrereqs. For your example it would look like:
[OSPrereqs / MSWin32]
Win32::Foo = 0.12
[OSPrereqs / !MSWin32]
Bar::Baz = 1.43