forked from theonedev/onedev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue #1361 - Able to restore database while server is running Fix issue #1360 - NPE when running reset-admin-password utility
- Loading branch information
1 parent
32abc05
commit 69baf83
Showing
26 changed files
with
813 additions
and
758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 126 additions & 21 deletions
147
server-core/src/main/java/io/onedev/server/ServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,130 @@ | ||
package io.onedev.server; | ||
|
||
public interface ServerConfig { | ||
|
||
/** | ||
* Get http port configured for the server. | ||
* <p> | ||
* @return | ||
* http port of the server, or <i>0</i> if http port is not defined | ||
*/ | ||
int getHttpPort(); | ||
|
||
/** | ||
* Get ssh port configured for the server. | ||
* <p> | ||
* @return | ||
* ssh port of the server | ||
*/ | ||
int getSshPort(); | ||
|
||
String getClusterIp(); | ||
|
||
int getClusterPort(); | ||
import com.google.common.base.Splitter; | ||
import io.onedev.server.persistence.HibernateConfig; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import static io.onedev.commons.utils.FileUtils.loadProperties; | ||
|
||
public class ServerConfig { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ServerConfig.class); | ||
|
||
private static final String PROP_HTTP_PORT = "http_port"; | ||
|
||
private static final String PROP_SSH_PORT = "ssh_port"; | ||
|
||
private static final String PROP_CLUSTER_IP = "cluster_ip"; | ||
|
||
private static final String PROP_CLUSTER_PORT = "cluster_port"; | ||
|
||
private final int httpPort; | ||
|
||
private final int sshPort; | ||
|
||
private final String clusterIp; | ||
|
||
private final int clusterPort; | ||
|
||
public ServerConfig(File installDir) { | ||
File file = new File(installDir, "conf/server.properties"); | ||
Properties props = loadProperties(file); | ||
|
||
String httpPortStr = System.getenv(PROP_HTTP_PORT); | ||
if (StringUtils.isBlank(httpPortStr)) | ||
httpPortStr = props.getProperty(PROP_HTTP_PORT); | ||
if (StringUtils.isNotBlank(httpPortStr)) { | ||
httpPort = Integer.parseInt(httpPortStr.trim()); | ||
} else { | ||
logger.warn(PROP_HTTP_PORT + " not specified, default to 6610"); | ||
httpPort = 6610; | ||
} | ||
|
||
String sshPortStr = System.getenv(PROP_SSH_PORT); | ||
if (StringUtils.isBlank(sshPortStr)) | ||
sshPortStr = props.getProperty(PROP_SSH_PORT); | ||
if (StringUtils.isNotBlank(sshPortStr)) { | ||
sshPort = Integer.parseInt(sshPortStr.trim()); | ||
} else { | ||
logger.warn(PROP_SSH_PORT + " not specified, default to 6611"); | ||
sshPort = 6611; | ||
} | ||
|
||
String clusterIp = System.getenv(PROP_CLUSTER_IP); | ||
if (StringUtils.isBlank(clusterIp)) | ||
clusterIp = props.getProperty(PROP_CLUSTER_IP); | ||
|
||
if (StringUtils.isBlank(clusterIp)) { | ||
HibernateConfig hibernateConfig = new HibernateConfig(installDir); | ||
String dbUrl = StringUtils.substringAfter(hibernateConfig.getUrl(), ":"); | ||
if (dbUrl.startsWith("hsqldb")) { | ||
clusterIp = "127.0.0.1"; | ||
} else { | ||
String dbHost; | ||
int dbPort; | ||
if (dbUrl.startsWith("oracle")) { | ||
List<String> fields = Splitter.on(":").splitToList(dbUrl); | ||
dbHost = StringUtils.stripStart(fields.get(2), "@"); | ||
String dbPortString = fields.get(3); | ||
if (dbPortString.contains("/")) | ||
dbPort = Integer.parseInt(StringUtils.substringBefore(dbPortString, "/")); | ||
else | ||
dbPort = Integer.parseInt(dbPortString); | ||
} else { | ||
String tempStr = StringUtils.substringAfter(dbUrl, "//"); | ||
dbHost = StringUtils.substringBefore(tempStr, ":"); | ||
tempStr = StringUtils.substringAfter(tempStr, ":"); | ||
if (dbUrl.startsWith("sqlserver")) { | ||
dbPort = Integer.parseInt(StringUtils.substringBefore(tempStr, ";")); | ||
} else { | ||
tempStr = StringUtils.substringBefore(tempStr, "/"); | ||
// Fix issue https://code.onedev.io/onedev/server/~issues/1086 | ||
tempStr = StringUtils.substringBefore(tempStr, ","); | ||
dbPort = Integer.parseInt(tempStr); | ||
} | ||
} | ||
|
||
try (Socket socket = new Socket()) { | ||
socket.connect(new InetSocketAddress(dbHost, dbPort)); | ||
clusterIp = socket.getLocalAddress().getHostAddress(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
this.clusterIp = clusterIp; | ||
|
||
String clusterPortStr = System.getenv(PROP_CLUSTER_PORT); | ||
if (StringUtils.isBlank(clusterPortStr)) | ||
clusterPortStr = props.getProperty(PROP_CLUSTER_PORT); | ||
if (StringUtils.isBlank(clusterPortStr)) | ||
clusterPort = 5710; | ||
else | ||
clusterPort = Integer.parseInt(clusterPortStr.trim()); | ||
} | ||
|
||
public int getHttpPort() { | ||
return httpPort; | ||
} | ||
|
||
public int getSshPort() { | ||
return sshPort; | ||
} | ||
|
||
public String getClusterIp() { | ||
return clusterIp; | ||
} | ||
|
||
public int getClusterPort() { | ||
return clusterPort; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.