Chef : Create a process as another user - ruby

So I have some code for running a batch file as a specific user. This was my attempt to automate the following syntax
runas /user:thisguy "C:\ThisGuysScript.bat"
so it looks like this in Ruby
Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
So I try to put this in a recipe in chef and disaster strikes
require 'win32/process'
::Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
Is failing with the following error
[Tue, 30 Oct 2012 15:57:03 +0000] FATAL: ArgumentError: You must supply a name when declaring a user resource
So it seems to not realise that I want to use the win32 flavour process. Chef seems to override the win32 module (I know recipes are the opscode DSL rather than really ruby right?)
Anyone been able to get this working? Or the same function with a different implementation. Checked out the windows cookbook but didn't spot much

It sounds like you want to make an LWRP for creating a process on a windows machine.
The error you are getting means you have something like
user do # Missing name
gid 500
home "..."
end
the correct syntax is
user "apache" do # or whatever the user name should be
# ...
end
If you don't have the above in your cookbook, it is possible that the included file has a variable named user which would also cause this issue.
To answer your subquestion, Chef is straight ruby with some functions made available and a frame work to run things. Note, there are several stages in a chef run. I think you are having issues in the compilation stage.
Making an LWRP seems like the way to go. If you don't want to go that far you could do something like.
ruby_block "Firing process lazers" do
require 'win32/process'
::Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
end

Related

SMS Fu Ruby gem throwing very strange YAML error

I'm trying to use the Ruby SMS Fu gem to send automated SMS messages to someone from my email address. Also, I'm not using Rails; this is a simple Ruby script that's getting some information from a site somewhere and then sending messages to someone based on said information. Unfortunately, I'm running into a very strange error when I try to use SMS Fu in my script:
/Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:377:in `parse': (<unknown>): found character that cannot start any token while scanning for the next token at line 7 column 12 (Psych::SyntaxError)
from /Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:377:in `parse_stream'
from /Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:325:in `parse'
from /Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:252:in `load'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:55:in `config_yaml'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:61:in `carriers'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:77:in `carrier'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:73:in `carrier_email'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:85:in `sms_address'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:43:in `deliver'
from ./my_script.rb:69:in `<main>'
What I've got going in my script looks like this:
#!/usr/bin/env ruby
require "active_support" # action_mailer, one of sms_fu's dependencies, throws
# an error without this
require "sms_fu"
# gather information from our site...
# Pony delivery via SMTP
PONY_CONFIG = {
:via => :smtp,
:via_options => {
:address => "smtp.gmail.com",
:port => "587",
:user_name => "me#example.com",
:password => "pass",
:authentication => :plain,
:enable_starttls_auto => true,
:domain => "localhost.localdomain"
}}
sms_fu = SMSFu::Client.configure(:delivery => :pony, :pony_config => PONY_CONFIG)
sms_fu.deliver("123456789", "some carrier", "my message", from: "me#example") # this line triggers the error
The odd thing is that this error is complaining about parsing YAML, but according to SMS Fu's README file, I only need to worry about setting up a YAML file if I'm using Rails, which I'm not. However, it seems that, upon examining SMS Fu's sources, it is hard-coded to work with a YAML file in the SMSFu.config_yaml method, which is subsequently used indirectly or directly by other methods in SMS Fu, all the way back to SMSFu::Client#deliver, which is the one causing the error here.
So, I guess my question is: how can I get this error to stop happening so that I can just send my SMS message without all the YAML hassle?
EDIT:
Forgot to give some version / environment info: I'm running this script on OS X 10.12.2 and I'm using Ruby 2.4.0 (although this Ruby version is manifest in the error above).

How to use Fog Library to create CDN in AWS

I am new in the DevOps world and my company uses Fog library to deploy EC2 instances for our Dev Environment. One of my company's products needs a CDN and I am trying to figure out how I can automate CDN using the same Fog Library.
I found info at fog.io and here is the code I put in makeCDN.rb (with a .sh wrapper to deploy it).
#!/usr/bin/ruby
require 'fog'
# create a connection to the service
cdn = Fog::CDN.new({
:provider => 'AWS',
:aws_access_key_id => 'fake_key_id',
:aws_secret_access_key => '2345fake_access_key6789'
})
cdn.post_distribution({
'CustomOrigin' => {
'DNSName' => 'hostname.domain.org', #example name
'HTTPPort' => '80',
'OriginProtocolPolicy' => 'match-viewer',
'DefaultRootObject' => '/',
'Enabled' => 'true',
}
})
So, I am unsure what I am doing wrong but the error I am getting is:
/home/eztheog/.rvm/gems/ruby-1.9.3-p547#fogDev/gems/excon-0.38.0/lib/excon/middlewares/expects.rb:10:in
`response_call': Expected(201) <=> Actual(400 Bad Request) (Excon::Errors::BadRequest)
response => #<Excon::Response:0x00000001d73b78 #data={:body=>"<?xml version=\"1.0\"?>\n<ErrorResponse
xmlns=\"http://cloudfront.amazonaws.com/doc/2010-11-01/\"><Error>
<Type>Sender</Type><Code>MalformedXML</Code><Message>1 validation error
detected: Value null at 'distributionConfig.enabled' failed to satisfy
constraint: Member must not be null</Message></Error>
<RequestId>c2b33cda-abee-11e4-8115-b584e1255c70</RequestId>
</ErrorResponse>", :headers=>{"x-amzn-RequestId"=>"c2b33cda-abee-11e4-8115-b584e1255c70",
"Content-Type"=>"text/xml", "Content-Length"=>"371", "Date"=>"Tue, 03
Feb 2015 21:51:07 GMT"}, :status=>400, :remote_ip=>"205.251.242.229",
:local_port=>39733, :local_address=>"10.100.6.203"}, #body="<?xml
version=\"1.0\"?>\n<ErrorResponse
xmlns=\"http://cloudfront.amazonaws.com/doc/2010-11-01/\"><Error>
<Type>Sender</Type><Code>MalformedXML</Code><Message>1 validation error
detected: Value null at 'distributionConfig.enabled' failed to satisfy
constraint: Member must not be null</Message></Error>
<RequestId>c2b33cda-abee-11e4-8115-b584e1255c70</RequestId>
</ErrorResponse>", #headers={"x-amzn-RequestId"=>"c2b33cda-abee-11e4-
8115-b584e1255c70", "Content-Type"=>"text/xml", "Content-Length"=>"371",
"Date"=>"Tue, 03 Feb 2015 21:51:07 GMT"}, #status=400,
#remote_ip="205.251.242.229", #local_port=39733,
#local_address="10.100.6.203">
I have found information here but am unsure how to parse the info into the ruby file.
There seems to be little blog stuff that I can find to figure out how to do this.
Can anyone point me the right direction?
I found this gist that explains the context.
So, the RubyDoc.info link (in question) said that the Enabled Boolean was an Option. But, in AWS it is not (or so it seems based on the error I got).
But, to resolve this, the :Enabled => true has to be OUTSIDE of the cdn.distribution block.
Hope this helps anyone who is looking for this in the future!
Also, this might be a BUG because the Fog library says that this feature is OPTIONAL but it seems mandatory by AWS.

Ruby: gem win32-service: Create a service with Admin privileges

I am using the win32-service gem to create a Windows service using Ruby (1.9.3-p429, MRI).
This snippet of code works.
require 'rubygems'
require 'win32/service'
include Win32
SERVICE_NAME = 'myservice'
# Create a new service
Service.create({
:service_name => SERVICE_NAME,
:service_type => Service::WIN32_OWN_PROCESS,
:description => 'A custom service I wrote just for fun',
:start_type => Service::AUTO_START,
:error_control => Service::ERROR_NORMAL,
:binary_path_name => 'c:\Ruby193\bin\ruby.exe -C c:\ c:\myservice.rb',
:load_order_group => 'Network',
#:service_start_name => 'Administrator',
#:password => 'adminpasswd',
:display_name => SERVICE_NAME
})
Service.start SERVICE_NAME
The problem I have is that the service needs to run with Administrator privileges, but the entity which calls this code snippet runs as the Local System Account, and that is the default privilege.
I can open up up the Services GUI (services.msc) and go in and stop the service, raise the privileges via the "Log On" tab of the service (myservice) properties, and use Administrator/adminpasswd as the user/password. It then runs the service with sufficient privileges.
However, when I try to call Service.create with the :service_start_name and :password set to exactly the same values (by uncommenting the lines in the code snippet) as I used in the Services tab, it doesn't work. This server is an Amazon EC2 server running Windows 2008r2 Datacenter Edition and is not a part of any Windows domain that I know of (because I started it).
What do I need to do differently to get this Windows service running with Administrator privileges?
The underlying CreateService Windows API function requires an account domain on the lpServiceStartName field, so you probably need to set the :service_start_name field to 'domain\Administrator', where the account domain is usually the computer name.
Borodin gave me a clue for this answer. When I went back to the Services GUI to reconfigure the service, I noticed that although I had typed 'Administrator' as the user name, the user name which actually showed up in the panel was '.\Administrator'. Keeping Borodin's comment in mind, it looks like I could specify '.' as the domain.
So...the code which actually worked was:
require 'rubygems'
require 'win32/service'
include Win32
SERVICE_NAME = 'myservice'
# Create a new service
Service.create({
:service_name => SERVICE_NAME,
:service_type => Service::WIN32_OWN_PROCESS,
:description => 'A custom service I wrote just for fun',
:start_type => Service::AUTO_START,
:error_control => Service::ERROR_NORMAL,
:binary_path_name => 'c:\Ruby193\bin\ruby.exe -C c:\ c:\myservice.rb',
:load_order_group => 'Network',
:service_start_name => '.\Administrator',
:password => 'adminpasswd',
:display_name => SERVICE_NAME
})
Service.start SERVICE_NAME

Sending mails automatically through action mailer Rails 3.1

I have to send weekly emails to all the user about the latest things happening. I am using ActionMailer to accomplish other mailing task however I have no clue how to automate the weekly emails.
Update
I found whenever gem which could be used to schedule cron jobs. I guess this could be used to send weekly emails which I intend to. Still looking how to make it work with ActionMailer will update once I find the solution
Update 2
This is what I have done so far using whenever gem:-
in schedule.rb
every 1.minute do
runner "User.weekly_update", :environment => 'development'
end
in users_mailer.rb
def weekly_mail(email)
mail(:to => email, :subject => "Weekly email from footyaddicts")
end
in users.rb
def self.weekly_update
#user = User.all
#user.each do |u|
UsersMailer.weekly_mail(u.email).deliver
end
end
If i try to run User.weekly_update from the console I am able to get the mails. I am testing in development mode and using rvm. I checked my crontab file and it has got the right stuff.
However I am not getting any mails automatically from the app. Any clue what might be wrong?
Thanks,
OK so it turns out to be a path issue with whenever gem, and the problem was created when I installed another version of ruby.
In my machine the new ruby version is installed in /usr/local/bin/ruby. In my rails app I had to go to the file script/rails and replace #!/usr/bin/env ruby with #!/usr/local/bin/ruby.
I found this out by visiting cron.log file which showed this error message :- /usr/bin/env: ruby: No such file or directory
I made a cron.log file to log the cron error this is what I did in my schedule.rb code written in the question :-
every 2.minutes do
runner "User.weekly_update", :environment => 'development', :output => 'log/cron.log'
end
I am getting periodic mails now.
It seems like you haven't configured ActionMailer settings.
First check out the logs from console, whether the mailing process is working(paste your logs).
If yes then do following steps.
add this in your gemfile.
gem 'tlsmail'
run
bundle install
write these configuration setting in your config/environments/development.rb file
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:enable_starttls_auto => true,
:authentication => :login,
:user_name => "<address>#gmail.com",
:password => "<password>"
}
config.action_mailer.raise_delivery_errors = true
add your working password/email against user_name and password.
Don't forget to restart server.

Rails namespaces with only numbers

I'm building an api in rails, and I have been told that I need to scope the site so it has versions.
Something like /1.0/do_something.
From what I've read, I do this by making a routes.rb that looks like the following:
API::Application.routes.draw do
scope :path => "1.0", :module => "1.0" do
post "do_something" => "controller#method"
# Everything else. Glob is saved in params[:r]
match '*r', :to => 'errors#e404'
end
end
But when I rake routes, I get the following error:
rake aborted!
missing :action
What am I doing wrong?
You cannot have a module called "1.0" for obvious reasons. Consider calling this V1 instead.

Resources