Permalink
Browse files

Merge pull request #23 from Ragnarson/18271043_shelly_setup

set up application on new computer [#18271043]
  • Loading branch information...
2 parents 384d420 + 135606e commit b5d873d250c33b1767660e062ce750d7bdf98833 @grk grk committed Mar 19, 2012
Showing with 145 additions and 5 deletions.
  1. +8 −0 lib/shelly/app.rb
  2. +42 −5 lib/shelly/cli/main.rb
  3. +15 −0 spec/shelly/app_spec.rb
  4. +80 −0 spec/shelly/cli/main_spec.rb
View
@@ -25,6 +25,14 @@ def git_remote_exist?
IO.popen("git remote").read.include?(code_name)
end
+ def git_fetch_remote
+ system("git fetch #{code_name} > /dev/null 2>&1")
+ end
+
+ def git_add_tracking_branch
+ system("git checkout -b #{code_name} --track #{code_name}/master > /dev/null 2>&1")
+ end
+
def remove_git_remote
system("git remote rm #{code_name} > /dev/null 2>&1")
end
View
@@ -16,9 +16,9 @@ class Main < Command
check_unknown_options!(:except => :rake)
# FIXME: it should be possible to pass single symbol, instead of one element array
- before_hook :logged_in?, :only => [:add, :status, :list, :start, :stop, :logs, :delete, :ip, :logout, :execute, :rake]
- before_hook :inside_git_repository?, :only => [:add]
- before_hook :cloudfile_present?, :only => [:logs, :stop, :start, :ip, :execute, :rake]
+ before_hook :logged_in?, :only => [:add, :status, :list, :start, :stop, :logs, :delete, :ip, :logout, :execute, :rake, :setup]
+ before_hook :inside_git_repository?, :only => [:add, :setup]
+ before_hook :cloudfile_present?, :only => [:logs, :stop, :start, :ip, :execute, :rake, :setup]
map %w(-v --version) => :version
desc "version", "Display shelly version"
@@ -82,8 +82,7 @@ def add
@app.databases = options["databases"] || ask_for_databases
@app.create
- git_remote = @app.git_remote_exist?
- if !git_remote or (git_remote and yes?("Git remote #{@app} exists, overwrite (yes/no): "))
+ if overwrite_remote?(@app)
say "Adding remote #{@app} #{@app.git_url}", :green
@app.add_git_remote
else
@@ -186,6 +185,39 @@ def start
say_error "You have no access to '#{@app}' cloud defined in Cloudfile"
end
+ desc "setup", "Set up clouds"
+ def setup
+ say "Investigating Cloudfile"
+ cloudfile = Cloudfile.new
+ cloudfile.clouds.each do |cloud|
+ begin
+ app = App.new(cloud)
+ say "Adding #{app} cloud", :green
+ app.git_url = app.attributes["git_info"]["repository_url"]
+ if overwrite_remote?(app)
+ say "git remote add #{app} #{app.git_url}"
+ app.add_git_remote
+ say "git fetch production"
+ app.git_fetch_remote
+ say "git checkout -b #{app} --track #{app}/master"
+ app.git_add_tracking_branch
+ else
+ say "You have to manually add remote:"
+ say "`git remote add #{app} #{app.git_url}`"
+ say "`git fetch production`"
+ say "`git checkout -b #{app} --track #{app}/master`"
+ end
+
+ say_new_line
+ rescue Client::NotFoundException => e
+ raise unless e.resource == :cloud
+ say_error "You have no access to '#{app}' cloud defined in Cloudfile"
+ end
+ end
+
+ say "Your application is set up.", :green
+ end
+
desc "stop", "Stop the cloud"
method_option :cloud, :type => :string, :aliases => "-c", :desc => "Specify cloud"
def stop
@@ -336,6 +368,11 @@ def valid_databases?(databases)
databases.all? { |kind| kinds.include?(kind) }
end
+ def overwrite_remote?(app)
+ git_remote = app.git_remote_exist?
+ !git_remote or (git_remote and yes?("Git remote #{app} exists, overwrite (yes/no): "))
+ end
+
def ask_for_password(options = {})
options = {:with_confirmation => true}.merge(options)
loop do
View
@@ -67,6 +67,21 @@
end
end
+ describe "git_fetch_remote" do
+ it "should try to remove existing git remote" do
+ @app.should_receive(:system).with("git fetch foo-staging > /dev/null 2>&1")
+ @app.git_fetch_remote
+ end
+ end
+
+ describe "git_add_tracking_branch" do
+ it "should try to remove existing git remote" do
+ @app.should_receive(:system).with("git checkout -b foo-staging --track foo-staging/master > /dev/null 2>&1")
+ @app.git_add_tracking_branch
+ end
+ end
+
+
describe "git_remote_exist" do
it "should return true if git remote exist" do
io = mock(:read => "origin\nfoo-staging")
@@ -39,6 +39,7 @@
shelly rake TASK # Run rake task
shelly redeploy # Redeploy application
shelly register [EMAIL] # Register new account
+ shelly setup # Set up clouds
shelly start # Start the cloud
shelly stop # Stop the cloud
shelly user <command> # Manage collaborators
@@ -770,6 +771,85 @@ def response
end
end
+ describe "#setup" do
+ before do
+ Shelly::App.stub(:inside_git_repository?).and_return(true)
+ @client.stub(:token).and_return("abc")
+ @client.stub(:app).and_return("git_info" => {"repository_url" => "git_url"})
+ @app = Shelly::App.new("foo-staging")
+ @app.stub(:git_remote_exist?).and_return(false)
+ @app.stub(:system)
+ Shelly::App.stub(:new).and_return(@app)
+ File.open("Cloudfile", 'w') {|f| f.write("foo-staging:\n") }
+ end
+
+ it "should ensure user has logged in" do
+ hooks(@main, :setup).should include(:logged_in?)
+ end
+
+ it "should ensure that user is inside git repo" do
+ hooks(@main, :setup).should include(:inside_git_repository?)
+ end
+
+ it "should ensure that cloudfile is present" do
+ hooks(@main, :setup).should include(:cloudfile_present?)
+ end
+
+ it "should show info about adding remote and branch" do
+ $stdout.should_receive(:puts).with("Investigating Cloudfile")
+ $stdout.should_receive(:puts).with(green "Adding foo-staging cloud")
+ $stdout.should_receive(:puts).with("git remote add foo-staging git_url")
+ $stdout.should_receive(:puts).with("git fetch production")
+ $stdout.should_receive(:puts).with("git checkout -b foo-staging --track foo-staging/master")
+ $stdout.should_receive(:puts).with(green "Your application is set up.")
+ invoke(@main, :setup)
+ end
+
+ it "should add git remote" do
+ @app.should_receive(:add_git_remote)
+ invoke(@main, :setup)
+ end
+
+ it "should fetch remote" do
+ @app.should_receive(:git_fetch_remote)
+ invoke(@main, :setup)
+ end
+
+ it "should add tracking branch" do
+ @app.should_receive(:git_add_tracking_branch)
+ invoke(@main, :setup)
+ end
+
+ context "when remote exists" do
+ before do
+ @app.stub(:git_remote_exist?).and_return(true)
+ end
+
+ context "and user answers yes" do
+ it "should overwrite remote" do
+ @app.should_receive(:add_git_remote)
+ @app.should_receive(:git_fetch_remote)
+ @app.should_receive(:git_add_tracking_branch)
+ fake_stdin(["yes"]) do
+ invoke(@main, :setup)
+ end
+ end
+ end
+
+ context "and user answers no" do
+ it "should display commands to perform manually " do
+ @app.should_not_receive(:add_git_remote)
+ @app.should_not_receive(:git_fetch_remote)
+ @app.should_not_receive(:git_add_tracking_branch)
+ fake_stdin(["no"]) do
+ invoke(@main, :setup)
+ end
+ end
+ end
+
+ end
+ end
+
describe "#delete" do
before do
Shelly::App.stub(:inside_git_repository?).and_return(true)

0 comments on commit b5d873d

Please sign in to comment.