I am learning sinatra, and I am trying to create simple website. This is my web directory tree:
├── app.rb
│
├── admin
│ └── views
│ └── admin.rb
├── models
├── static
│
└── views
and now I want render views just for admin. In other words: I have 2 views folder in different locations, admin for admin controller and views, and another views is for homepage.
Add config.ru file in root application folder
require './app'
require './admin/admin'
# run MyApp
run Rack::URLMap.new("/" => MyApp.new, "/admin" => AdminApp.new)
In app.rb
require 'sinatra'
require 'haml'
class MyApp < Sinatra::Base
get "/app" do
haml :app
end
end
In admin.rb
# admin.rb
class AdminApp < Sinatra::Base
get "/" do
haml :index
end
end
Finally in console rackup -p PORTNUMBER example
rackup -p 4000
Update
Reference to Gist
Related
A default install of Middleman 4.1.10 with i18n 0.7.0 on Ruby 2.3.1p112 generates nested duplicate locales folders with broken links for the non-default language.
E.g, if Swedish is the default language, the translated english files get generated under /en/en/, with duplicates under /en/sv/, and files with broken links in swedish under /en/. The site´s primary language´s files get created correctly under /sv/. Changing the default language to English renders the same error, but then under /sv/sv/, with correct files and links under /en/ instead.
I stumbled on this when evaluating Middleman by converting an existing bi-lingual site. The same path/filename/link pattern is also reproducible on a default installation of Middleman with minimal configuration, as shown here.
I really can´t figure out if I went wrong somewhere, or if this is a bug in Middleman´s I18n module. Found nothing even remotely reminding of this by googling, on Stackoverflow, or Middleman´s documentation or bug reports, which makes me suspect I´m missing something embarrasingly obvious.
This is my setup:
config.rb
page '/*.xml', layout: false
page '/*.json', layout: false
page '/*.txt', layout: false
set :relative_links, true
activate :i18n, :mount_at_root => :sv
Yes, I think I've tested all the other possible documented combinations of :i18n without any result. Manually setting the order between languages by :langs => [:en, :sv] only renders the same erroneous directory structure under sv.
locales/en.yml
---
en:
index: Home
news: News
paths:
index: index
news: news
locales/sv.yml
---
sv:
index: Hem
news: Nyheter
paths:
index: index
news: nyheter
source/layouts/layout.erb
<!doctype html>
<html>
<head>
<title></title>
<!-- The only change from defaults: -->
<%= link_to t(:index), t("paths.index") + ".html" %>
<%= link_to t(:news), t("paths.news") + ".html" %>
<%= stylesheet_link_tag :site %>
<%= javascript_include_tag :all %>
</head>
<body class="<%= page_classes %>">
<%= yield %>
</body>
</html>
source/localizable/en/index.html.erb
---
title: English index
---
English index
source/localizable/en/news.html.erb
---
title: English news
---
English news
source/localizable/sv/index.html.erb
---
title: Swedish index
---
Swedish index
source/localizable/sv/news.html.erb
---
title: Swedish news
---
Swedish news
Obviously I expected this:
├── en
│ ├── index.html
│ └── news.html
│
└── sv
├── index.html
└── nyheter.html
But got this:
├── en
│ ├── en
│ │ ├── index.html # Both files with correct english translations and
│ │ └── news.html # links, but only linked in the /en/en/ directory
│ │
│ ├── index.html # Both files with swedish translations
│ ├── nyheter.html # and functioning links in swedish
│ │
│ └── sv
│ ├── index.html # Both files with swedish translations and
│ └── news.html # links within the /en/sv/ directory
│
└── sv
├── index.html # Both files with correct swedish
└── nyheter.html # translations and links
No one has been able to explain this inside my company so if you are able to solve this KUDOS to you!
Inside my puppet repo I have setup as follows:
environment/ops/modules/papertrail
├── files
│ ├── elasticsearch_log_files.yml
│ ├── log_files.yml
│ └── remote_syslog.conf
|
└── manifests
├── elasticsearch.pp
└──init.pp
└── templates
└── elasticsearch_log_files.yml.erb
MY elasticsearch.pp file contains the following:
class papertrail::elasticsearch inherits papertrail {
$source = "puppet:///modules/papertrail"
file { "/etc/log_files.yml" :
mode => 0644,
owner => root,
group => root,
ensure => present,
source => "$source/elasticsearch_log_files.yml",
}
}
Now when I try to change the last line to:
"$source/elasticsearch_log_files.yml.erb",
or
"$source/templates/elasticsearch_log_files.yml",
Puppet errors out and says that it can't locate the file:
Error: /Stage[main]/Papertrail::Elasticsearch/File[/etc/log_files.yml]: Could not evaluate: Could not retrieve information from environment ops source(s) puppet:///modules/papertrail/elasticsearch_log_files.yml.erb
What is strange is that when I use the following stanza to just include the yml file instead of erb it works fine and the file gets populated on the target:
"$source/elasticsearch_log_files.yml",
How can I include my erb? I have dynamic variables that I need to assign to the configuration file log_files.yml and I am so far unable to do so =(
This is solved. I didn't add the template directory to my git commit so once added with git add . it worked.
How can I quickly run a custom function on any node with puppet apply ?
Let's say I have this file (test.pp)
file { "/root/files/f":
content => test('test'),
}
And this ruby file (test.rb) which only log and return the first agument.
require 'logger'
module Puppet::Parser::Functions
newfunction(:test, :type => :rvalue
) do |args|
log = Logger.new(STDOUT)
log.level = Logger::INFO
log.info(args[0])
args[0]
end
end
How does one call the ruby function test with a puppet apply test.pp?
My problem is that is take like 5minutes to run my entire puppet agent -t and I need to add a function to puppet so I would like to test it quickly instead of waiting 5minutes each time.
This will certainly work, but it's hard to maintain.
A more maintainable way to do this is to add your function to a module, then install that module on your master.
For example, create a new with the name of your module (eg. test_function):
mkdir -p test_function/lib/puppet/parser/functions
You should have the following tree in your test_function module
├── lib
│ └── puppet
│ ├── parser
│ │ └── functions
│ │ ├── test.rb
Add your test.rb code to the test.rb file
Copy this module to your master's module path (this is probably /etc/puppet/modules depending on your Puppet version, use puppet module list to find out)
After that, the puppet master will read its module list and dynamically add-in all functions it finds.
More documentation about it here:
https://docs.puppetlabs.com/guides/plugins_in_modules.html
https://docs.puppetlabs.com/guides/custom_functions.html
I just found out.
All you need to do is to add the test.rb file directly into /var/lib/puppet/lib/puppet/parser/functions/test.rb and the puppet apply will "see" the function.
I have lots of scripts need to include the same header.
What's the better way to include them in every script
# -*- encoding : utf-8 -*-
#!/usr/bin/ruby
require 'yaml'
require "json"
require 'pry'
require "selenium-webdriver"
require 'nokogiri'
require 'active_support/all'
require 'yaml'
require 'date'
require 'awesome_print'
require 'mongo'
require File.expand_path('./form_action.rb', File.dirname(__FILE__))
require File.expand_path('./schedule_manager.rb', File.dirname(__FILE__))
require File.expand_path('../common_helper.rb', File.dirname(__FILE__))
require File.expand_path('../mongodb_helper.rb', File.dirname(__FILE__))
require File.expand_path('../webdriver_helper.rb', File.dirname(__FILE__))
require File.expand_path('../lib/db_manager.rb', File.dirname(__FILE__))
require File.expand_path('../lib/app_util.rb', File.dirname(__FILE__))
require File.expand_path('../lib/schedule_manager_base.rb', File.dirname(__FILE__))
require File.expand_path('../lib/class_template.rb', File.dirname(__FILE__))
include Mongo
include MongodbHelper
include AppUtil
Project folder structure
I will include the common_includes in client1.rb,
But the SIBLINGS not work.
├── common_helper.rb
├── common_includes.rb
├── config
│ ├── database.yml
│ └── schedule.rb
├── client1
│ ├── config.yml
│ ├── client1.rb
│ └── schedule_manager.rb
└── webdriver_helper.rb
├── lib
│ ├── app_util.rb
│ ├── class_template.rb
│ ├── db_manager.rb
│ └── schedule_manager_base.rb
Put smth like following:
REQUIRES = %w(yaml json pry selenium-webdriver nokogiri active_support/all yaml date awesome_print mongo)
SIBLINGS = %w(form_action schedule_manager)
COMMON = %w(common_helper mongodb_helper webdriver_helper db_manager.rb lib/app_util lib/schedule_manager_base lib/class_template.rb)
INCLUDES = %i(Mongo MongodbHelper AppUtil)
REQUIRES.each &method :require
SIBLINGS.each do |f|
File.expand_path("./#{f}.rb", File.dirname(__FILE__))
end
COMMON.each do |f|
File.expand_path("../#{f}.rb", File.dirname(__FILE__))
end
module CommonIncludes
def self.included base
INCLUDES.each { |inc| base.include Kernel.const_get(inc) }
end
end
into a very file common_includes.rb and everywhere you need it, call:
require 'common_includes'
include CommonIncludes
This is more or less common approach to require/include in case you need to specify the file list explicitly one by one.
NB To manage external dependencies, everyone wants to use bundler, as mentioned Mike Slutsky around.
UPD Don’t hesitate to use Dir["./*.rb"] to collect all the files for a given directory.
If I am understanding your question correctly, you are looking for a way that you can include all these headers without having to copy and paste them all over the place.
One way you can achieve this is to put all these headers into one file, and in your scripts just include that file:
For example, in headers.rb:
# -*- encoding : utf-8 -*-
#!/usr/bin/ruby
require 'yaml'
require "json"
require 'pry'
require "selenium-webdriver"
require 'nokogiri'
require 'active_support/all'
require 'yaml'
require 'date'
require 'awesome_print'
require 'mongo'
require File.expand_path('./form_action.rb', File.dirname(__FILE__))
require File.expand_path('./schedule_manager.rb', File.dirname(__FILE__))
require File.expand_path('../common_helper.rb', File.dirname(__FILE__))
require File.expand_path('../mongodb_helper.rb', File.dirname(__FILE__))
require File.expand_path('../webdriver_helper.rb', File.dirname(__FILE__))
require File.expand_path('../lib/db_manager.rb', File.dirname(__FILE__))
require File.expand_path('../lib/app_util.rb', File.dirname(__FILE__))
require File.expand_path('../lib/schedule_manager_base.rb', File.dirname(__FILE__))
require File.expand_path('../lib/class_template.rb', File.dirname(__FILE__))
include Mongo
include MongodbHelper
include AppUtil
Then your scripts:
require 'path/to/headers'
I'm just getting started with puppet (and vagrant) to set up the development environment for our team, which consists of 8+ developers, each of which have their particular bash configuration, etc. I've got all the software installed on the system to quickly deploy new development virtual machines, but I'm not sure the best way to set up the development environment for each particular user in an automated way (we will end up having several development environments and it would be convenient to write this once and be done).
For example, I'd like to set up a user joe, clone Joe's configuration repo from github, and then run a script in that github repository to set up the environment for Joe. Any suggestions for how to do this for Joe as well as Jimmy, James, Julie, Jane, Jim, Jake, and Jimbo?
In case its any help, the development machines will almost certainly be ubuntu systems.
In addition to #Matt's suggestion, I created a custom puppet module that instantiates the configuration environment for each individual based on their github preferences. The resulting puppet module users looks something like this:
users/
├── manifests
│ ├── init.pp # base level configurations for all users
│ ├── jake.pp # custom setup for jake
│ ├── james.pp # custom setup for james
│ ├── jane.pp # custom setup for jane
│ ├── jim.pp # custom setup for jim
│ ├── jimbo.pp # custom setup for joe
│ ├── jimmy.pp # custom setup for jimmy
│ ├── joe.pp # custom setup for julie
│ └── julie.pp # custom setup for jimbo
└── templates
The relevant tidbit is in the custom setup files for each user. For example, here's what jim.pp might look like:
class users::jim {
# make sure that all base configuration in init.pp is set up first
require users
# add the user here
user { 'jim':
# comment => 'Dean Malmgren',
home => '/home/jim',
shell => '/bin/bash',
uid => 201,
managehome => 'true',
groups => ['sudo', 'vagrant'],
provider => 'useradd',
password => '$6$kxHLEuHW$78m3zHVLu0XUUDaU4bT.PEg./FfcloJiWml',
}
# clone the repository the first time
exec { 'jim-clone-dotfiles':
command => 'git clone git://github.com/jim/dotfiles.git && python dotfiles/create_softlinks.py',
cwd => '/home/jim',
creates => '/home/jim/dotfiles',
user => 'jim',
group => 'jim',
require => [ Package['git'] ],
}
# fetch and update if jim decides to update his dotfiles repo
exec { 'jim-update-dotfiles':
command => 'git merge --ff-only origin/master && python create_softlinks.py',
cwd => '/home/jim/dotfiles',
unless => 'git fetch && git diff --exit-code origin/master',
user => 'jim',
group => 'jim',
require => Exec['jim-clone-dotfiles'],
}
}
You could use a puppet fact in the vagrant file to set the username and pass this through to your puppet manifests. Something like the following:
Vagrant.configure("2") do |config|
config.vm.provision :puppet do |puppet|
puppet.facter = {
"user_name" => ENV['USER']
}
end
end
This would pass the current logged in username through to puppet and then within your manifest files you could use the variable "$user_name" within your git commands to checkout the correct users repo and do any other related tasks.