Ruby on rails - pass params from db to environment config file - ruby-on-rails

im using a gem for a sms service, and i have to put the params in the specific environment config file.
All is ok with static params, but if i call this params from a db, starting the webrick i have an error:
(ActiveRecord::ConnectionNotEstablished)
this is my original code in development.rb
Skuby.setup do |config|
config.method = 'send_sms_classic' #default
config.username = 'myusername'
config.password = 'mypassword'
config.password = Setting.where(campo: 'skebby_password').valore
config.sender_string = 'company' #optional
config.sender_number = '39123456790' #optional
config.charset = 'UTF-8' #skebby default is ISO-8859-1
end
changing something, like:
config.password = Setting.where(campo: 'skebby_password').valore
it goes down!
How can i fix?

Do you think it's ok to do this in your config/environment. I would think that such would be better suited for your config/initializers, within your initializer, your db connection is already setup.
BTW, if you're using different parameters for your environments, I think you should rather use a configuration file, which would set your params per environment and you could then use that.

Related

I can't get the value of refile.rb fron setting.yml in rails

I would like to call value of setting.yml in config/initializers/refile.rb
So I try to set it as follows.But it Does not work as I thought.
When I run SPEC file, the value of Refile.cdn_host is empty.
I don't know well why this value is empty.
can I get some advice?
config/settings/test.yml
cloudfront:
host: 'localhost'
config/initializers/refile.rb
Refile.cdn_host = Settings.cloudfront.host
Incidentally when I change it as follows,
Refile.cdn_host = 'localhost'
it does work without being empty.
You need to load the file first, try
Refile.cdn_host = YAML.load_file("#{Rails.root.to_s}/config/settings/test.yml")['cloudfront']['host']

Configuring grails datasources with username and password from OS environment

I am currently working on a grails application. Previously I have been using Django alot to create webapps. In django you can easily create settings that are collected from the OS environment. I find that to be an easy solution for making sure that you don't check in usernames and passwords into the source code repository.
So what I would like to do is in the DataSource.groovy be able to lift in the username and password from the OS environment. Has anyone done anything like this, or is this not the way to go forward?
If this is not the "grails way", how is it supposed to be done, because having the username and password in the repository just feels wrong?
You can write code in DataSource.groovy and Config.groovy to get env variable and then set username password and other things. I always to it for my production app, sample code is as follows
//Eample is based on url structure like "mysql://username:password#host/database?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8"
dataSource {
String mysqlUrl = System.getenv("DATABASE_URL")
println ">>>>>> Got DATABASE_URL: ${mysqlUrl} <<<<<<<"
URI dbUri = new URI(mysqlUrl);
username = dbUri.userInfo.split(":")[0]
password = dbUri.userInfo.split(":")[1]
String databaseUrl = "jdbc:${dbUri.scheme}://${dbUri.host}${dbUri.path}"
if (dbUri.port > 0) {
databaseUrl += ":${dbUri.port}"
}
String query = dbUri.query ?: "reconnect=true"
query += "&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8"
databaseUrl += "?${query}"
url = databaseUrl
dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
}
This is one of the way I am sure there must be some simpler way to do this.
PS: I find it simple though :)
You can create a environment variable in Windows. Example DBCONNGRAILS. In config.groovy (...\grails-app\conf\config.groovy) you can use something like
def myConnectionString = System.getenv(DBCONNGRAILS)
WARNING: If you get variables from the environment(Windows) there will no problem when loading the value when in development. But when you are working with a production server, there will be cases when you will need to resart the OS for tomcat to be able to pick up the changes. I would recommend externalizing the config files.
You can externalized the configuration in any other place and load/merge it simply in your application configuration.
We use JNDI at work for exactly this scenario. We have context.xml files located within the individual server's Tomcat directories. The context.xml contains the datasource properties and then within the datasource closure (defined in Datasource.groovy), the jndiName property is set accordingly.
More Links about JNDI:
http://grails.github.io/grails-doc/2.3.7/guide/conf.html#JNDIDataSources
http://grails.asia/how-to-make-grails-use-jndi-datasource-with-tomcat

Rails asset pipeline Rails.application.assets.instance_variable_get('#environment') returns nil

I am trying to compile javascript dynamically and then adding it to the sprockets store so it is available. Everywhere I researched suggested the below code to register the javascript:
env = Rails.application.assets.is_a?(Sprockets::Index) ? Rails.application.assets.instance_variable_get('#environment') : Rails.application
Rails.application.config.assets.digests[file_name] = env[file_name].digest_path
in production, Rails.application.assets.instance_variable_get('#environment') always returns nil, is there something I am doing wrong? or should I be adding something else?
Rails.application.assets itself is an instance of Sprockets::Environment
#environment' is a variable of assets_manifest, that belongs to Rails.application, like this:
Rails.application.instance_variable_get('#assets_manifest').instance_variable_get('#environment')
I got similar problems with RAILS 3.2.15,
but it was Rails.application.assets returns nil
quiet_assets.rb:4:in': undefined method logger=' for nil:NilClass (NoMethodError)
the issued line was
Rails.application.assets.logger = Logger.new('logger.log')
I back to Rails console, and find Rails.application.assets just returned nil.
I fix this issue by this step:
include two gem in your Gemfile in case you don't have it.
gem 'sprockets'
gem 'sprockets-rails'
find the file cause the issue , and initial your assets object.
you can also put that in application.rb , in my case I put it in config/initializers/quiet_assets.rb , before I refer to logger.
add this line:
Rails.application.assets = Sprockets::Environment.new
before this issued line:
Rails.application.assets.logger = Logger.new('logger.log')
in application.rb , remember have activate assets pipeline .
config.assets.enabled = true
for production you may need to set config.assets.compile = true
hope this helps somehow
Build it yourself (which is needed for new versions)
env = Sprockets::Railtie.build_environment(Rails.application)

How to run the example code for a gem such as Vacuum?

I'm new to rails, and looking to set up a site that uses Amazon's product API. I'm used to using the API in PHP but keen to move to Ruby if at all possible.
I've been trying the various Amazon product API gems, Ruby/AWS, Amazon-ECS, and now Vacuum. However a problem I am sticking at is how to actually use the code they suggest in their readme files.
For example, Vacuum:
https://github.com/hakanensari/vacuum/
It mentions the following code:
req = Vacuum.new :product_advertising
req.configure do |config|
config.key 'key'
config.secret 'secret'
config.tag 'tag'
end
req.build operation: 'ItemSearch',
search_index: 'Books',
keywords: 'Deleuze'
res = req.get
res.valid? or raise res.code
p res.body
Unsure where to put this code, I set up a controller for the test app and put it there. However it fails on the first line, saying that Vacuum is not defined (it's installed as a gem within the gemfile, and I've run bundle install).
I'm probably doing something stupid yet simple to fix, and would really appreciate any suggestions.
D
Edit:
Thanks to x1a4, the following code should be replace the configure block above:
req.configure do |config|
config.key = 'key'
config.secret = 'secret'
config.tag = 'tag'
end
Thanks to x1a4, the following code should be replace the configure block above:
req.configure do |config|
config.key = 'key'
config.secret = 'secret'
config.tag = 'tag'
end

hiding database password using codec specification in datasource.groovy not working

I am hiding the database password in datasource.groovy by doing
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = "q59YgJCdHw3dshwlsa=="
passwordEncryptionCodec = DESCodec
dbname="mydbname"
}
followed the artilce : http://jira.grails.org/browse/GRAILS-3620
I run the groovy codec class separately to get the encrypted string like this:
groovy grails-app/utils/DESCodec.groovy mypassword_string_text.
But After placing the DESCodec class in the Utility package in grails, when I try to start the server, it wont start but directly shutdown, It starts when I directly put the right password and comment out the codec and encrypted string. I am assuming it is not finding the codec class/anything else I am missing in the config like specifying the codec class requires quotes or any path change or I should change the algorithm, by the way the DESCodec class is the last codec class in the link.
I am working on getting the config to be environment specific and externalized, but I still need the password to be encrypted here, and then decrypted before connecting to the database.
I think the best way to do this is to externalise environment specific configuration (especially passwords, but URL's, email addresses and so on too) and then protect the config file with proper permissions on the file system of the target machine.
In Config.groovy (for example):
grails.config.locations = [
"file:/etc/${appName}/conf/db.properties"
]
and in the config file:
dataSource.username = "root"
dataSource.password = "secret"
I typically do this for production config, but keep dev/test config in the Config.groovy for convenience. When running in dev/test you simply get a warning at startup if it can't find the referenced config file. If it does find it, then it will override what's in Config.groovy
This has the added advantage of not requiring your war file to be recompiled and redeployed if any prod environment configuration changes, you simply change the config file and restart the app.

Resources