Skip to content

Commit

Permalink
Merge pull request #1109 from scm-manager/feature/os_specific_homes
Browse files Browse the repository at this point in the history
OS Specific Homes
  • Loading branch information
pfeuffer committed Apr 27, 2020
2 parents fd68648 + 50253da commit 0bddfae
Show file tree
Hide file tree
Showing 11 changed files with 520 additions and 136 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Removed the `requires` attribute on the `@Extension` annotation and instead create a new `@Requires` annotation ([#1097](https://github.com/scm-manager/scm-manager/pull/1097))
- Use os specific locations for scm home directory ([#1109](https://github.com/scm-manager/scm-manager/pull/1109))
- Use Library/Logs/SCM-Manager on OSX for logging ([#1109](https://github.com/scm-manager/scm-manager/pull/1109))

### Fixed
- Protocol URI for git commands under windows ([#1108](https://github.com/scm-manager/scm-manager/pull/1108))
Expand Down
2 changes: 2 additions & 0 deletions docs/Home.md
Expand Up @@ -83,6 +83,8 @@ repositories over http.
### SCM Manager 2

- [Getting started](v2/getting-started.md)
- [Base directory](v2/basedirectory.md)
- [Logging](v2/logging.md)
- [Configuration for Intellij IDEA](v2/intellij-idea-configuration.md)
- [SCM v2 Test Cases](v2/test-cases.md)
- [Table of decisions made during development](v2/decision-table.md)
Expand Down
45 changes: 45 additions & 0 deletions docs/v2/basedirectory.md
@@ -0,0 +1,45 @@
# Base Directory

The SCM-Manager base directory aka. home directory,
contains all data which is created by SCM-Manager such as repositories and configurations.
The location of the base directory depends on your operating system and type of installation.

| Type of Installation | Base directory |
|----------------------|----------------|
| Docker | /var/lib/scm |
| RPM | /var/lib/scm |
| DEB | /var/lib/scm |
| Unix | ~/.scm |
| Mac OS X | ~/Library/Application Support/SCM-Manager |
| Windows | %APPDATA%\SCM-Manager |

## Change base directory location

The location of the base directory can be changed by using one of the following ways.
The preferences are the following: Properties file over system property over environment variable.

### Environment variable

By setting the environment variable **SCM_HOME** e.g.:

```bash
export SCM_HOME=/home/scm
/opt/scm-server/bin/scm-server
```

For rpm and deb installations the variable can be changed via the file `/etc/default/scm-server`.

## System property

The path can be changed by setting the system property **scm.home** e.g.:

```bash
-Dscm.home=/home/scm
```
## Properties file

If SCM-Manager finds a file called `scm.properties` on the class path it reads the property `scm.home` e.g.:

```properties
scm.home=/home/scm
```
32 changes: 32 additions & 0 deletions docs/v2/logging.md
@@ -0,0 +1,32 @@
# Logging

SCM-Manager logs information which can be useful, if the system does not behave as expected.
The logging behavior depends on your operating system and installation.

| Type of Installation | Logging |
|----------------------|---------|
| Docker | stdout |
| RPM | /var/log/scm |
| DEB | /var/log/scm |
| Unix | $BASEDIR/logs |
| Mac OS X | ~/Library/Logs/SCM-Manager |
| Windows | $BASEDIR\logs |

The location of the **$BASEDIR** can be found [here](basedirectory).

## Configuration

The logging behaviour of SCM-Manager can be configured via an xml file.
The syntax and properties can be found [here](http://logback.qos.ch/manual/configuration.html).
The location of the file depends also on the type of installation.

| Type of Installation | Path |
|----------------------|---------|
| Docker | /opt/scm-server/conf/logging.xml |
| RPM | /etc/scm/logging.xml |
| DEB | /etc/scm/logging.xml |
| Unix | $EXTRACT_PATH/scm-server/conf/logging.xml |
| Mac OS X | $EXTRACT_PATH/scm-server/conf/logging.xml |
| Windows | $EXTRACT_PATH/scm-server/conf/logging.xml |

**$EXTRACT_PATH** is the path were you etract the content of the package.
142 changes: 142 additions & 0 deletions scm-core/src/main/java/sonia/scm/BaseDirectory.java
@@ -0,0 +1,142 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package sonia.scm;

import com.google.common.base.Strings;
import sonia.scm.util.SystemUtil;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Properties;

/**
* Determines the base directory for SCM-Manager.
* This class should not be used directory, use {@link SCMContextProvider#getBaseDirectory()} instead.
*
* @since 2.0.0
*/
final class BaseDirectory {

/** Environment variable for the SCM-Manager base directory */
static final String ENVIRONMENT_VARIABLE = "SCM_HOME";

/** Java system property for the SCM-Manager base directory */
static final String SYSTEM_PROPERTY = "scm.home";

/** Classpath resource for the SCM-Manager base directory */
@SuppressWarnings("java:S1075") // it is already configurable
static final String CLASSPATH_RESOURCE = "/scm.properties";

/** Property name in resource file */
static final String RESOURCE_PROPERTY = "scm.home";

private final Platform platform;
private final String classPathResource;
private final Map<String,String> environment;
private final Properties systemProperties;

BaseDirectory(Platform platform, String classPathResource, Map<String, String> environment, Properties systemProperties) {
this.platform = platform;
this.classPathResource = classPathResource;
this.environment = environment;
this.systemProperties = systemProperties;
}

/**
* Returns the determined base directory.
*
* @return base directory
*/
@SuppressWarnings("java:S5304") // it is safe to use environment in this case
static Path get() {
return new BaseDirectory(
SystemUtil.getPlatform(),
CLASSPATH_RESOURCE,
System.getenv(),
System.getProperties()
).find();
}

Path find() {
String directory = getFromResource();
if (Strings.isNullOrEmpty(directory)) {
directory = getFromSystemProperty();
}
if (Strings.isNullOrEmpty(directory)) {
directory = getFromEnvironmentVariable();
}
if (Strings.isNullOrEmpty(directory)) {
directory = getOsSpecificDefault();
}

return Paths.get(directory);
}

private String getFromResource() {
try (InputStream input = BasicContextProvider.class.getResourceAsStream(classPathResource))
{
if (input != null)
{
Properties properties = new Properties();
properties.load(input);
return properties.getProperty(RESOURCE_PROPERTY);
}
}
catch (IOException ex)
{
throw new ConfigurationException("could not load properties form resource " + CLASSPATH_RESOURCE, ex);
}
return null;
}

private String getFromEnvironmentVariable() {
return environment.get(ENVIRONMENT_VARIABLE);
}

private String getFromSystemProperty() {
return systemProperties.getProperty(SYSTEM_PROPERTY);
}

private String getOsSpecificDefault() {
if (platform.isMac()) {
return getOsxDefault();
} else if (platform.isWindows()) {
return getWindowsDefault();
}
return systemProperties.getProperty("user.home") + "/.scm";
}

private String getOsxDefault() {
return systemProperties.getProperty("user.home") + "/Library/Application Support/SCM-Manager";
}

private String getWindowsDefault() {
return environment.get("APPDATA") + "\\SCM-Manager";
}

}

0 comments on commit 0bddfae

Please sign in to comment.