-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
UnbindFromClusterCommand.java
140 lines (125 loc) · 4.92 KB
/
UnbindFromClusterCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.dbms;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.neo4j.causalclustering.core.state.ClusterStateDirectory;
import org.neo4j.causalclustering.core.state.ClusterStateException;
import org.neo4j.commandline.admin.AdminCommand;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.commandline.arguments.Arguments;
import org.neo4j.dbms.DatabaseManagementSystemSettings;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.StoreLockException;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.util.Validators;
import org.neo4j.server.configuration.ConfigLoader;
public class UnbindFromClusterCommand implements AdminCommand
{
private static final Arguments arguments = new Arguments().withDatabase();
private Path homeDir;
private Path configDir;
private OutsideWorld outsideWorld;
UnbindFromClusterCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
this.homeDir = homeDir;
this.configDir = configDir;
this.outsideWorld = outsideWorld;
}
public static Arguments arguments()
{
return arguments;
}
private static Config loadNeo4jConfig( Path homeDir, Path configDir, String databaseName )
{
Config config = ConfigLoader.loadConfig( Optional.of( homeDir.toFile() ),
Optional.of( configDir.resolve( "neo4j.conf" ).toFile() ) );
Map<String,String> additionalConfig = new HashMap<>();
additionalConfig.put( DatabaseManagementSystemSettings.active_database.name(), databaseName );
return config.with( additionalConfig );
}
private static List<Class<?>> settings()
{
return Arrays.asList( GraphDatabaseSettings.class, DatabaseManagementSystemSettings.class );
}
@Override
public void execute( String[] args ) throws IncorrectUsage, CommandFailed
{
try
{
Config config = loadNeo4jConfig( homeDir, configDir, arguments.parse( "database", args ) );
File dataDirectory = config.get( DatabaseManagementSystemSettings.data_directory );
Path pathToSpecificDatabase = config.get( DatabaseManagementSystemSettings.database_path ).toPath();
Validators.CONTAINS_EXISTING_DATABASE.validate( pathToSpecificDatabase.toFile() );
confirmTargetDirectoryIsWritable( pathToSpecificDatabase );
ClusterStateDirectory clusterStateDirectory = new ClusterStateDirectory( dataDirectory );
clusterStateDirectory.initialize( outsideWorld.fileSystem() );
deleteClusterStateIn( clusterStateDirectory.get().toPath() );
}
catch ( StoreLockException e )
{
throw new CommandFailed( "Database is currently locked. Please shutdown Neo4j.", e );
}
catch ( IllegalArgumentException e )
{
throw new IncorrectUsage( e.getMessage() );
}
catch ( UnbindFailureException | CannotWriteException | IOException | ClusterStateException e )
{
throw new CommandFailed( e.getMessage(), e );
}
}
private void confirmTargetDirectoryIsWritable( Path pathToSpecificDatabase )
throws CommandFailed, CannotWriteException, IOException
{
try ( Closeable ignored = StoreLockChecker.check( pathToSpecificDatabase ) )
{
// empty
}
}
private void deleteClusterStateIn( Path target ) throws UnbindFailureException
{
try
{
FileUtils.deleteRecursively( target.toFile() );
}
catch ( IOException e )
{
throw new UnbindFailureException( e );
}
}
private class UnbindFailureException extends Exception
{
UnbindFailureException( Exception e )
{
super( e );
}
}
}