Chef :: remote_file unable to download file from FTP - ruby

I am developing a recipe to download some file from FTP server. I am successfully able authenticate the FTP server and fetching the list of the files. But when my chef is executed to download the files I am getting error.
Here is code
fileList.each do |file|
remote_file "C:\\" + file do
source URI::encode("ftp://user:password#server/folder/" + file)
action :create_if_missing
end
end
and here is error's log
PS C:\chef-repo> chef-apply .\cookbooks\visireview-sqlserver-config\recipes\ftp_restore_db.rb
[2016-02-17T17:14:20+05:00] WARN: ftp://user:password#server/folder/file.bak was an invalid URI. Trying to escape invalid characters
[2016-02-17T17:14:20+05:00] FATAL: Stacktrace dumped to C:/chef/cache/chef-stacktrace.out
[2016-02-17T17:14:20+05:00] FATAL: Chef::Exceptions::InvalidRemoteFileURI: "ftp://user:password#server/folder/file.bak" is not a valid `source` parameter for remote_file. `source` must be an absolute URI or an array of URIs.
Please spare some of your previous time and help me to get rid of this issue.
Thanks

I think you replaced too much of the real content. Probably something in the username, password, or hostname is causing a problem with the URI parsing.
> URI.parse("ftp://user:password#server/folder/file.bak").absolute?
true
Get that working with whatever values you actually have and it should be fine.

Related

compute sha-256 using chef

I have a code to compare sha-256 value from the file and artifactory which throws error if the checksum does not match. But I want to compute checksum in my own recipe rather than reading it from artifactory. Below is the code I have so far and trying to find out if there are any functions or methods which I can use in my recipe to compute sha-256 for a file.
Thanks in advance
only_if { node['abc'] }
not_if { ::File.exist?(checksum_file) and ::File.read(checksum_file).strip==coordinates['checksum'].strip }
message 'The previously deployed checksum is not aligned with the actual value'
level :debug
notifies :create, 'remote_file[download file]', :immediately
end```
chef is built on top of ruby, so we can unleash the power of ruby within recipes.
if you would like to compute the sha-256 checksome for a file, the following ruby code might be handy
require 'digest'
Digest::SHA256.file('/path/to/file').hexdigest
the snippet in your post, does not specify which chef resource are you using, although it feels like you are using the the log resource.
since you mentioned that you are interested in downloading the file from artifactory, remote_file resource can do that and it has a checksum property:
checksum: Optional, see use_conditional_get. The SHA-256 checksum of the file. Use to prevent a file from being re-downloaded. When the local file matches the checksum, Chef Infra Client does not download it.

Jenkins archive single file not working

I'm trying to archive a zip file in Jenkins using the archiveArtifacts step like this:
archiveArtifacts 'publish\\archive.zip'
But I keep getting this error:
Archiving artifacts
ERROR: No artifacts found that match the file pattern "publish\archive.zip". Configuration error?
ERROR: ‘publish\archive.zip’ doesn’t match anything: ‘publish’ exists but not ‘publish\archive.zip’
The file definitely exists. If a tack an asterisk onto the end of the path like this:
archiveArtifacts 'publish\\archive.zip*'
the step works fine and the file is archived but why is this necessary?
My best guess is that you have non readable char at the end of the filename or elsewhere in your script. I used to get some confusing script errors when writing scripts on windows and putting them on a linux environment. In IntelliJ you can change theese via file->line seperators->unix.

How can I avoid "Zip end of central directory signature not found (Zip::Error)" with rubyzip?

I'm reading a lot of zip file with rubyzip.
However this error message is always showing in only specific file even it is zip file.
/app/vendor/bundle/ruby/2.3.0/gems/rubyzip-1.2.1/lib/zip/central_directory.rb:143:in `get_e_o_c_d': Zip end of central directory signature not found (Zip::Error)
I guess this error occures in rubyzip.
How can I manage this error?
Here is my code.
url = 'http://example.zip'
zipfilename = open(url)
Zip::File.open(zipfilename, :allow_redirections => :all) do |zip_file|
entry = zip_file.glob("*ixbrl.htm").first
stream = entry.get_input_stream.read
puts stream
end
Thank you!
I faced this error when I try to extract data from a uploaded .xlsx file in my application. On my context, the .xlsx file was corrupted, so my solution was save the content (I usually handle the buffer file after the upload) as a csv file (in my application I dont need to worry about the file extension), fixing the content by force it to encode as utf-8, and extract it's data after it. here is a example of the code, I'm using roo-xls gem to handle .xls files and roo gem to handle .csv and .xlsx files.
I ran into the same error. Also only reproducible on Heroku. The error was fixed after I added an unzip buildpack (second, after Ruby).
https://github.com/davidlibrera/heroku-buildpack-unzip
If you're using a google docs xlsx file try checking if the access of the sheet link is public.

How to output additional error message on password failure with Ruby

I am trying to help improve this open source repository: https://github.com/michenriksen/birdwatcher/ . At the moment if the user puts in the wrong postgreSQL password, the repository outputs:
[-] Sequel::DatabaseConnectionError: PG::ConnectionBad: FATAL: password authentication failed for user "birdwatcher"
FATAL: password authentication failed for user "birdwatcher"
Which isn't particularly useful, as it does not give the user any indication of what to do to fix the error (a gem uninistall and reinstall doesn't fix it either). In addition to the output currently generated, I would like it to output:
Please edit the ~/.birdwatcherrc file with the correct password, or delete the file entirely, then run birdwatcher again.
Currently by looking through the repository I have isolated the following code:
def bootstrap_database!
task "Preparing database...", true do
Sequel.extension :migration, :core_extensions
#database = Sequel.connect(configuration.get!(:database_connection_uri))
Sequel::Migrator.run(#database, DB_MIGRATIONS_PATH)
Sequel::Model.db = #database
Sequel::Model.plugin :timestamps
bootstrap_models!
load_default_workspace!
end
end
( https://github.com/michenriksen/birdwatcher/blob/master/lib/birdwatcher/console.rb )
Which appears to be the related section? However I cannot find anything that looks as if it would be generating the error message. Does anyone have an idea how to go about finding this, and adding the error message in? Perhaps there is default error handling in one of the called function libraries which is doing this and I could put in a call to add the additional message when the particular error is thrown?
Will it be as simple as puts "Please edit the ~/.birdwatcherrc file with the correct password, or delete the file entirely, then run birdwatcher again." or will I need something more advanced?
Any help much appreciated. I've never authored a Ruby gem myself, so I'm not 100% sure where and how to edit, other than it appears as though it should be a simple task once I know how. The author has obviously put a lot of effort into the repository already, so I'd like to help them make it better if possible
The error is probably raise when the connection is configured or the first query is run. That happens in this two lines:
#database = Sequel.connect(configuration.get!(:database_connection_uri))
Sequel::Migrator.run(#database, DB_MIGRATIONS_PATH)
Just wrap the line that raise the error with a rescue block:
begin
# line that raise the exception
rescue Sequel::DatabaseConnectionError
puts 'Please edit the ~/.birdwatcherrc file with the correct password, or delete the file entirely, then run birdwatcher again.'
raise # re-raises the original method
end

Getting random "read_nonblock': end of file reached (EOFError)" with Net::HTTP.start

When I execute the following code...
http = Net::HTTP.start('jigsaw.w3.org')
http.request_post('/css-validator/validator', ' ', 'Content-type' => "multipart/form-data")
...then I very often get the following error:
EOFError: end of file reached
from /Users/josh/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock'
Is this only me? What could be the problem? Sometimes it seems to work, but most of the time it doesn't.
The problem seems to be on the side of the host:
Loading http://jigsaw.w3.org/css-validator/DOWNLOAD.html manually in a browser results most of the time in "no data received" at the moment.
I'm trying to set up the downloadable command line version of the validator on my local machine and use this. More info here: How can I validate CSS on internal web pages?

Resources