Windows, Ec2ServerCreate - Cannot set JSON attributes when running knife from Rails application - ruby-on-rails

I am able to launch new instance at AWS from Ruby on Rails application (Chef::Knife::Ec2ServerCreate.new()). Works fine till I try to set JSON attributes. When I set them from command line and invoke knife.bat, it works.
Looking at ec2_server_create shows that command line option --json-attributes is mapped to symbol :json_attributes. I tried to set it using following code:
Chef::Config[:knife][:json_attributes] = "{'NodeName' : 'Node001'}"
and I get error:TypeError (no implicit conversion of Symbol into Integer):
As soon as I comment this single line, instance is created, registered with chef server and recipe is running.
Any suggestion how to set json attributes of first chef-client run?
PS
Sample code shows constant value for attribute, but actual value would be dynamically created.
Error message and code line where error occurs:
/chef/knife/core/bootstrap_context.rb:188:in `[]=': no implicit conversion of Symbol into Integer (TypeError)
Looking into source you can find:
def first_boot
(#config[:first_boot_attributes] || {}).tap do |attributes|
if #config[:policy_name] && #config[:policy_group]
attributes[:policy_name] = #config[:policy_name]
attributes[:policy_group] = #config[:policy_group]
else
attributes[:run_list] = #run_list #THIS LINE CAUSES EXCEPTION
end
attributes.merge!(:tags => #config[:tags]) if #config[:tags] && !#config[:tags].empty?
end
end
I set run list.

The issue is that json_attributes needs to be a hash, not a string. It isn't the #run_list that is failing, it is "{'NodeName' : 'Node001'}"[:run_list] which when you see in on place is probably a bit clearer.

Related

How to fix an 'Invalid tag name' in WSDL request using Zeep?

When trying to access a WSDL service, I get the following error:
ValueError: Invalid tag name 'Foo\\Bar\\Baz\\Etc\\V3Port'
The port which is provided through the WSDL-link actually has single backslashes: 'Foo\Bar\Baz\Etc\V3Port'
The ValueError gets raised when zeep calls the apihelpers.pxi method in the lxml library.
Any idea how I can fix this?
(BTW: the script worked fine when I used it 2 months ago. The WSDL-link hasn't changed)
I have found a solution
Before, it was sufficient to do:
from zeep import Client
Now, apparently, I need to explicitly add:
from lxml import etree
Everything works as before now.
Zeep does not like like "/" in values also. It gives invalid tag name error any time it finds any character in value that it does not like. For all such errors, you need to modify as_qname function in utils.py located in root folder of zeep library.
In my case, it was complaining for "/" in value, which is a valid value. I had to add below line to as_qname function.
value = value.replace("/", "-")
Below are first few lines of as_qname after modification
def as_qname(value: str, nsmap, target_namespace=None) -> etree.QName:
"""Convert the given value to a QName"""
value = value.strip() # some xsd's contain leading/trailing spaces
value = value.replace("/", "-") # Added by me.
if ":" in value:
prefix, local = value.split(":")

rails 4 - active job - wrong number of arguments (0 for 1) even though im passing 1 argument

Here I call the job - passing in procedure:
ProcedureDayAfterJob.set(wait_until: reminder_for_jo).perform_later(procedure)
Here is the job itself - taking procedure as argument:
class ProcedureDayAfterJob < ActiveJob::Base
queue_as :mailers
def perform(procedure)
return if procedure.upcoming?
Admin::NotificationMailer.procedure_day_after(procedure).deliver_later
end
end
Here is method in mailer - taking procedure as argument:
def procedure_day_after(procedure)
#procedure = procedure
mail(
# hiding who im sending to
)
end
And here is the error I'm getting:
2017-12-15T19:25:57.365Z 9222 TID-oxvg7jsso ERROR: !!! ERROR HANDLER
THREW AN ERROR !!!
2017-12-15T19:25:57.365Z 9222 TID-oxvg7jsso ERROR: wrong number of
arguments (0 for 1)
2017-12-15T19:25:57.365Z 9222 TID-oxvg7jsso ERROR:
/Users/foreverlabs/foreverlabs/app/jobs/procedure_day_after_job.rb:4:in
`perform'
I'm passing in the argument to each method so can anybody figure out what's going on?
I have seen this happen due to a typo in the initializer for the Service/Class being called within the job.
It is usually misleading with the single line errors where the only helpful info is the line number and file it is erroring on. With that, and looking at your queue log, it shows it is coming from the 4th line which is inside the perform method itself, and where the code has been redacted.
Check the class you are calling on this line to see if "initialize" is misspelled somehow (I do it from time to time myself),because doing such would cause the constructor to disable, as well as cause the class to expect zero arguments.
I'm sure you have fixed this, but hopefully this will help someone else in the future.

lua - invalid argument type

I am a newbie to Lua. Currently getting the following error message:
invalid argument type for argument -model (should be the model checkpoint
to use for sampling)
Usage: [options] <model>
I am sure it is something pretty easy to solve, but cannot manage to find the solution.
The 'model' is a file lm_checkpoint_epoch50.00_2.7196.t7, which is in the directory
/home/ubuntu/xxx/nn/cv
I am running the program from the parent directory (/home/ubuntu/xxx/nn)
I have tried out the following options to run the program (from one directory above the one the model is saved):
th sample.lua - model lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua /cv/lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua - /cv/model lm_chelm_checkpoint_epoch50.00_2.5344.t7
Also, the program has a torch.CmdLine() object where :argument equals '/cv/lm_checkpoint_epoch50.00_2.7196.t7'. The program prints the parameters, so that you see the following output on the screen:
Options
<model> /cv/lm_checkpoint_epoch50.00_2.7196.t7
so it finds a value for argument 'model', which is picked up from the .lua file, not the parameter in the command line. This file is a valid mode.
Pretty lost, hope someone relates to this issue. Thanks.
found the issue - it was a bug as smhx suggested. I inadvertently changed the source code from:
require 'torch'
cmd = torch.CmdLine()
cmd:argument('-model','model checkpoint to use for sampling')
Note that there is no argument in the source code. To:
cmd:argument('-model','/cv/model lm_chelm_checkpoint_epoch50.00_2.5344.t7'
'model checkpoint to use for sampling')
So the argument must be passed through the command line, not the source code. With parameters, it is different - you can include them in the source code.
So if I change back the source code and run the following from the command line:
th sample.lua cv/lm_chelm_checkpoint_epoch50.00_2.5344.t7
it works.

Read the content of a file and display with line numbers on the view

I am trying to write code to read a Ruby script entered by the user and stored it in a temp file, then pass that temp file to jruby -c temp_file to do syntax validation.
If any errors are found, I have to show the errors, along with the script entered by the user with line numbers.
How would I do this?
If there is a syntax error you see the failing line number:
blah.rb:2: syntax error, unexpected '.'
So just split your script on newline (\n) and thats your error line (1 index based of course).
If I understand correctly what you are trying to achieve, you can use jruby-parser (install gem install jruby-parser) with JRuby to find line numbers where errors occur:
require 'jruby-parser'
# Read script file into a String
script = File.open('hello.rb', 'r').read
begin
JRubyParser.parse(script)
rescue Exception => e
# Display line number
puts e.position.start_line
# Display error message
puts e.message
end

Cannot update the model using a delayed_job

I am trying to upload a video to facebook using koala and then saving the video id returned form facebook.
The upload works great but when the method tries to update the attribute I get a weird error:
failed with NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.delete - 0 failed attempts
this is the code inside the method I am trying to run as a delayed_job:
#first line alone works
result = graph.put_video(file_path)
unless result.nil?
#this line fails
self.update_attributes(:fbid => result["id"])
end
Any ideas?
Update: it only happens on windows and Mac not in linux

Resources