From e1f8dfa25a44f83b672f934efadb96cfd50a3eb5 Mon Sep 17 00:00:00 2001 From: Travis Person Date: Mon, 20 Apr 2015 17:21:13 -0700 Subject: [PATCH 1/4] Check to see if the daemon is currently running If the daemon is running we do not want to proceed with an an initialization. Return a client error telling the user to kill the daemon before proceeding with the command. --- cmd/ipfs/init.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 66bdaf671..daab7f36f 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -35,6 +35,17 @@ var initCmd = &cmds.Command{ // name of the file? // TODO cmds.StringOption("event-logs", "l", "Location for machine-readable event logs"), }, + PreRun: func(req cmds.Request) error { + daemonLocked := fsrepo.LockedByOtherProcess(req.Context().ConfigRoot) + + log.Info("checking if daemon is running...") + if daemonLocked { + e := "ipfs daemon is running. please stop it to run this command" + return cmds.ClientError(e) + } + + return nil + }, Run: func(req cmds.Request, res cmds.Response) { force, _, err := req.Option("f").Bool() // if !found, it's okay force == false From cca5212e098eb1684597cadb66a98a024cf221e0 Mon Sep 17 00:00:00 2001 From: Travis Person Date: Mon, 20 Apr 2015 18:35:42 -0700 Subject: [PATCH 2/4] Add sharness test This test to make sure we can't run `ipfs init` while a daemon is currently running. --- test/sharness/t0020-init.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/sharness/t0020-init.sh b/test/sharness/t0020-init.sh index 002b9865e..8b564f23c 100755 --- a/test/sharness/t0020-init.sh +++ b/test/sharness/t0020-init.sh @@ -44,4 +44,15 @@ test_expect_success "ipfs init output looks good" ' test_cmp expected actual_init ' +test_launch_ipfs_daemon + +test_expect_failure "ipfs init should not run while daemon is running" ' + ipfs init 2> daemon_running_output + echo "Error: ipfs daemon is running. please stop it to run this command" > daemon_running_expected + echo "Use \'ipfs init --help\' for information about this command" >> daemon_running_expected + test_cmp daemon_running_expected daemon_running_output +' + +test_kill_ipfs_daemon + test_done From e7756e45f9a07249fb71e0f84c1599e77bd29430 Mon Sep 17 00:00:00 2001 From: Travis Person Date: Mon, 20 Apr 2015 19:33:41 -0700 Subject: [PATCH 3/4] Problem with initializing daemon on same port At this point I think this test should be moved out of this file --- test/sharness/t0020-init.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/sharness/t0020-init.sh b/test/sharness/t0020-init.sh index 8b564f23c..b494c805b 100755 --- a/test/sharness/t0020-init.sh +++ b/test/sharness/t0020-init.sh @@ -44,13 +44,14 @@ test_expect_success "ipfs init output looks good" ' test_cmp expected actual_init ' +test_init_ipfs + test_launch_ipfs_daemon test_expect_failure "ipfs init should not run while daemon is running" ' - ipfs init 2> daemon_running_output - echo "Error: ipfs daemon is running. please stop it to run this command" > daemon_running_expected - echo "Use \'ipfs init --help\' for information about this command" >> daemon_running_expected - test_cmp daemon_running_expected daemon_running_output + ipfs init 2> daemon_running_err && + expect="Error: ipfs daemon is running. please stop it to run this command" && + cat daemon_running_err | grep $expect ' test_kill_ipfs_daemon From 676fd50ca094fcb205e79bfe4d3c29edc17bcdb6 Mon Sep 17 00:00:00 2001 From: Travis Person Date: Mon, 20 Apr 2015 23:46:59 -0700 Subject: [PATCH 4/4] Correctly written test for ipfs init with daemon --- test/sharness/t0020-init.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/sharness/t0020-init.sh b/test/sharness/t0020-init.sh index b494c805b..5ee134a36 100755 --- a/test/sharness/t0020-init.sh +++ b/test/sharness/t0020-init.sh @@ -48,10 +48,10 @@ test_init_ipfs test_launch_ipfs_daemon -test_expect_failure "ipfs init should not run while daemon is running" ' - ipfs init 2> daemon_running_err && - expect="Error: ipfs daemon is running. please stop it to run this command" && - cat daemon_running_err | grep $expect +test_expect_success "ipfs init should not run while daemon is running" ' + test_must_fail ipfs init 2> daemon_running_err && + EXPECT="Error: ipfs daemon is running. please stop it to run this command" && + grep "$EXPECT" daemon_running_err ' test_kill_ipfs_daemon