Cloning repository from ruby - ruby

I'm creating a Sinatra app that will pull in data from a remote git repository.
I've taken a look at the ruby-git gem, but I get cannot load such file -- git on the line of require 'git'. The gem is installed and in my Gemfile.
I'm not sure if this is the correct way about going about this, but essentially I want the app to checkout a git repository for pulling in data.

Ended up using the system command:
system("git clone #{$config['repository']} content")

Related

Cloud Foundry Ruby Offline Buildpack

I'm trying to get a Ruby Offline buildpack working and am running into this error when deploying an application.
2015-03-31T14:28:56.21-0600 [STG/0] OUT -------> Buildpack version 1.3.0
2015-03-31T14:28:56.22-0600 [STG/0] OUT !
2015-03-31T14:28:56.22-0600 [STG/0] OUT ! No such file or directory - /var/vcap/data/dea_next/admin_buildpacks/fbc8ba1a-0f89-4cd4-bdc6-6b189b326ab6_f80a090fe58f5bba36a6d25dfe86220f7dfcc41d/compile-extensions/bin/translate_dependency_url https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/bundler-1.7.12.tgz
2015-03-31T14:28:56.22-0600 [STG/0] OUT !
Environment:
Linux CentOS 6
Here are the steps I followed:
download the ZIP from https://github.com/cloudfoundry/ruby-buildpack
unzip locally
cd ruby-buildpack
rvm use 2.1.5
BUNDLE_GEMFILE=cf.Gemfile bundle
BUNDLE_GEMFILE=cf.Gemfile bundle exec buildpack-packager cached
cf create-buildpack sjw_ruby_buildpack ruby_buildpack-cached-v1.3.0.zip 11
These instructions came from https://github.com/cloudfoundry-incubator/buildpack-packager/blob/master/doc/disconnected_environments.md
I then deployed the Ruby app as follows:
switched to ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-linux]
bundle install
bundle package --all
cf push -b sjw_ruby_buildpack
Failed with the above error message.
This was answered on the Cloud Foundry Dev Group
https://groups.google.com/a/cloudfoundry.org/forum/?utm_medium=email&utm_source=footer#!topic/vcap-dev/9KhpUfuGprk
Here's the relevant section:
It seems like git submodule update --init isn't working, which is actually to be expected if you're using the "Download ZIP" button at the homepage of the ruby-buildpack repo. It sounds like that's how you got the buildpack (" - download the ZIP from https://github.com/cloudfoundry/ruby-buildpack), but Github doesn't provide you with the .git file when you download the source code this way.
To modify the prebuilt buildpack, you could download the ruby_buildpack-cached-v1.3.0.zip file from here, unzip it, modify it, re-zip it and run the cf create-buildpack command to upload it to your Cloud Foundry deployment.
If you need to build it entirely locally, you can try cloning the repo instead:
git clone https://github.com/cloudfoundry/ruby-buildpack.git
cd ruby-buildpack
rm ruby_buildpack-cached-v1.3.0.zip
git submodule update --init
BUNDLE_GEMFILE=cf.Gemfile bundle exec buildpack-packager cached
cf create-buildpack sjw_ruby_buildpack ruby_buildpack-cached-v1.3.0.zip 11
In particular, you should expect the git submodule update --init command NOT to return "fatal: Not a git repository (or any of the parent directories): .git".
Once that's done, try pushing your app again, specifying the buildpack with the -b flag like before, and let us know what happens.

Gemfile git branch for Beanstalk unable to bundle install

In my Gemfile I have
gem 'slim', :git => 'git://github.com/brennancheung/slim.git', :branch => 'angularjs_support'
which is a branch of the slim gem required for me to run AngularJS correctly with my views. I've pushed my code to my beanstalk application but am unable to bundle install according to the logs shown below...
sh: git: command not found
Git error: command `git clone 'git://github.com/brennancheung/slim.git'
"/usr/share/ruby/1.9/gems/1.9.1/cache/bundler/git/slim-700ed452e752ccb6baf9de9d0a46fbded8bb2da5"
--bare --no-hardlinks` in directory /var/app/ondeck has failed.
I'm new to Beanstalk and have no idea how to fix this. Any help on how to get bundle to install successfully would be greatly appreciated. Thanks.
Since git is not installed on by default on EC2 instance, you would have to find a workaround solution:
a. Install git on instance with configuration file and command.
It is the most obvious way to solve the problem, although not be the most efficient.
b. Clone slim repository into your project, so it will be deployed together.
Seems that slim is not being actively developed lately, so having the copy in your project might not be a bad idea. It protects you from github.com being down, yet you will have extra files to carry around.
c. Use configuration file and commands to pull the data from github.com directly with http.
Too many files to work with, and also dependency on third party service.
d. Use a combination of above. Clone slim repository and copy files to S3. Use configuration and commands to copy files from S3 to your instance.
It seems like the most elegant and efficient way to solve the problem.
It might look something like:
$ cat .ebextensions/myapp.config
commands:
10-copy-slim-from-s3
command: "aws s3 cp s3://mybucket/slim slim --recursive"

rake gen_deploy rejected in Octopress

I installed Octopress in GitHub Pages.
And I clone the repository.
$ git clone git#github.com:my-name/my-name.github.io.git
$ git checkout source
And
$ rake setup_github_pages
I input my repository name.
And
$ rake gen_deploy
I got error
! [rejected] master -> master (non-fast-forward)
my solution
I resolve this problem, in GitHub delete my-name.github.io.git, and make same name repository and
$ rake gen_deploy
But I don't want to delete repository
What is the best solution?
Without deleting the repository
Please keep in mind this is not considered best practice, but it may work for you.
The solution is to force a push on the master branch.
Edit the Rakefile and look for this line:
system "git push origin #{deploy_branch}"
Alter the line by adding a plus (+) before the #{deploy_branch} tag:
system "git push origin +#{deploy_branch}"
Run the command
rake deploy
It should succeed.
Undo the edit you made to the Rakefile!
Idea for this solution came from reading this: https://stackoverflow.com/a/9629458/1369730
I have the same problem when hosting my Octopress blog on github pages. I Googled a lot and finally solved this problem.
Just change the directory.
cd octopress/_deploy
git pull origin master
cd ..
rake deploy
Then it's fixed.

Missing File in Gem after Build

TL;DR:
Don't run bundle within an existing git repository. Weird things will happen without any error messages.
Original Question:
I've built a gem by adapting the steps in this tutorial:
http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/
As a final step, I've run gem build .gemspec
This succeeds, but when I install the gem I find the critical file, the one which contains my code, isn't in the gem. Another file in the same (lib) directory, "version.rb", does exist in the gem.
I don't know how to start debugging this...how does bundler/gem build decide which files to include in the gem?
Edit:
My workflow is:
gem build <project_name>.gemspec
gem unpack <project_name>
=> confirm file does not exist in <unpacked>/lib/
gem install <project name>
=> confirm file structure in ~/home/stefan/.rvm/... contains gem, but does not contain desired file
Edit 2 / Resolution:
I was finally able to get this working by committing all my code to a remote repository, creating a clean clone, and building the gem. The new gem included all the required files.
A bit of history...I originally created the code and committed it before thinking about making a gem (this is my first gem). I then used bundle inside the original repository, which didn't complain, but was probably the reason for the weirdness.
One of the things bundler did for you is start a local git repo to version-manage your gem code. Check that you have added the file in git
git add lib/gem_name/missing_file.rb
Bundler generated gems use git internally to track "membership" of source files for the gem. You can see this in the .gemspec where it uses backticks to call out to git and generate a file list:
gem.files = `git ls-files`.split($/)
Note this also means you should pay attention to what you list in .gitignore

Using Rake or Gem for building scripts?

I want to build scripts that automatize things for me.
Here is an example of what I want to do:
Create new rails app (rails new application_name --database=mysql)
Jump to folder
Initialize git (git init; git add .; git commit -m "first commit"; git remote add name address; git push name master)
Create heroku project (heroku create; git push heroku master)
Etc...
I've got a lot of such scripts (not just rails related) I want to build.
Should these kind of steps be coded with Rake or Gem?
From what I have understood rake is usually getting the tasks from the current folder's Rakefile. If i want to make operations universal, is it better to create a gem?
The thing is that I want to be able to call it from whatever directory I'm in.
What are the pros and cons with each?
Going with rake or a gem is fine. If you want to centralize your rake tasks (assuming you are on OSX or Some Linux/*nix variant) you can create them in your home directory:
~/.rake/*.rake
Rake will look there for tasks to run if in a directory w/ no Rakefile.
Also, consider (again, if you are on some sort of *nix platform) just creating shell aliases to your commands.
Edit:
Another consideration specific to your Rails work is to leverage Application Templates. Here is a link to a good screencast.
Some of what you want could be accomplished with shell aliases, some with gems, some with rake.
Using Brian's suggestion, I added the following to ~/.rake/git.rake:
namespace :git do
desc "Init, add, initial commit"
task :init do
`git init .`
`git add .`
`git commit -m 'Initial commit'`
end
end
Then in any directory I can run "rake git:init" and it will do all the intial setup. The remote add is a little harder because the remote name would be a variable (could be provided via a shell variable or a Readline prompt).
For creating a rails app, I'd add an alias to ~/.bash_profile:
alias new_mysql="rails new $ARGV --database=mysql"
Then run "new_mysql myRailsProject".
For the most part I would think just running a bunch of command line scripts would be a shell alias rather than a Rake task.
You should be using rake tasks for this stuff. Bates has a screencast showing how to accomplish what your trying to get done. Basically you create a custom task and after you can call rake my_task and it will execute your script.

Resources