Skip to content

Commit

Permalink
Issue #535
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrojdeCTL committed Oct 25, 2018
1 parent 51138ec commit 3d807d4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 89 deletions.
164 changes: 76 additions & 88 deletions mdw-common/src/com/centurylink/mdw/dataaccess/DataAccess.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/*
* Copyright (C) 2017 CenturyLink, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright (C) 2017 CenturyLink, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.centurylink.mdw.dataaccess;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import com.centurylink.mdw.app.ApplicationContext;
import com.centurylink.mdw.cli.Checkpoint;
import com.centurylink.mdw.cli.Import;
import com.centurylink.mdw.config.PropertyManager;
import com.centurylink.mdw.constant.PropertyNames;
import com.centurylink.mdw.dataaccess.file.GitDiffs;
import com.centurylink.mdw.dataaccess.file.LoaderPersisterVcs;
import com.centurylink.mdw.dataaccess.file.MdwBaselineData;
import com.centurylink.mdw.dataaccess.file.RuntimeDataAccessVcs;
import com.centurylink.mdw.dataaccess.file.VersionControlGit;
import com.centurylink.mdw.dataaccess.file.WrappedBaselineData;
import com.centurylink.mdw.spring.SpringAppContext;
import com.centurylink.mdw.util.DesignatedHostSslVerifier;
import com.centurylink.mdw.util.log.LoggerUtil;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import com.centurylink.mdw.app.ApplicationContext;
import com.centurylink.mdw.cli.Checkpoint;
import com.centurylink.mdw.cli.Import;
import com.centurylink.mdw.config.PropertyManager;
import com.centurylink.mdw.constant.PropertyNames;
import com.centurylink.mdw.dataaccess.file.GitDiffs;
import com.centurylink.mdw.dataaccess.file.LoaderPersisterVcs;
import com.centurylink.mdw.dataaccess.file.MdwBaselineData;
import com.centurylink.mdw.dataaccess.file.RuntimeDataAccessVcs;
import com.centurylink.mdw.dataaccess.file.VersionControlGit;
import com.centurylink.mdw.dataaccess.file.WrappedBaselineData;
import com.centurylink.mdw.spring.SpringAppContext;
import com.centurylink.mdw.util.DesignatedHostSslVerifier;
import com.centurylink.mdw.util.log.LoggerUtil;
import com.centurylink.mdw.util.log.StandardLogger;

public class DataAccess {

public final static int schemaVersion60 = 6000;
public final static int schemaVersion60 = 6000;
public final static int schemaVersion61 = 6001;
public final static int currentSchemaVersion = schemaVersion61;
public static int supportedSchemaVersion = currentSchemaVersion;
Expand Down Expand Up @@ -127,7 +127,7 @@ public synchronized static VersionControl getAssetVersionControl(File rootDir) t
if (url != null && branch != null) {
String user = PropertyManager.getProperty(PropertyNames.MDW_GIT_USER);
String password = PropertyManager.getProperty(PropertyNames.MDW_GIT_PASSWORD);
if (user != null) {
if (user != null) {
VersionControlGit vcGit = (VersionControlGit) assetVersionControl;
File gitLocal = new File(gitLocalPath);
vcGit.connect(url, user, password, gitLocal);
Expand All @@ -136,19 +136,19 @@ public synchronized static VersionControl getAssetVersionControl(File rootDir) t
if (gitTrustedHost != null)
DesignatedHostSslVerifier.setupSslVerification(gitTrustedHost);

String assetPath = vcGit.getRelativePath(rootDir);
logger.info("Loading assets from path: " + assetPath);

String assetPath = vcGit.getRelativePath(rootDir);
logger.info("Loading assets from path: " + assetPath);

if (!gitLocal.isDirectory()) {
if (!gitLocal.mkdirs())
throw new DataAccessException("Git loc " + gitLocalPath + " does not exist and cannot be created.");
}
if (!vcGit.localRepoExists()) {
logger.severe("**** WARNING: Git location " + gitLocalPath + " does not contain a repository. Cloning: " + url);
vcGit.cloneNoCheckout();
if (PropertyManager.getBooleanProperty(PropertyNames.MDW_GIT_AUTO_CHECKOUT, true)) {
logger.info("Performing checkout...");
vcGit.hardCheckout(branch);
logger.severe("**** WARNING: Git location " + gitLocalPath + " does not contain a repository. Cloning: " + url);
vcGit.cloneNoCheckout();
if (PropertyManager.getBooleanProperty(PropertyNames.MDW_GIT_AUTO_CHECKOUT, true)) {
logger.info("Performing checkout...");
vcGit.hardCheckout(branch);
}
}

Expand Down Expand Up @@ -176,18 +176,6 @@ public synchronized static VersionControl getAssetVersionControl(File rootDir) t
+ "\n(" + gitLocal.getAbsolutePath() + ")");
logger.info("Differences:\n============\n" + diffs);
}

boolean gitAutoPull = "true".equalsIgnoreCase(PropertyManager.getProperty(PropertyNames.MDW_GIT_AUTO_PULL));
if (gitAutoPull) {
// force checkout all assets (equivalent to doing an asset import)
boolean gitHardReset = PropertyManager.getBooleanProperty(PropertyNames.MDW_ASSET_SYNC_GITRESET, false);
try (DbAccess dbAccess = new DbAccess()) {
logger.severe("**** Performing Git Auto-Pull (Overwrites existing assets): " + vcGit + " (branch: " + branch + ")");
Import importer = new Import(gitLocal, vcGit, branch, gitHardReset, dbAccess.getConnection());
importer.setAssetLoc(assetPath);
importer.importMDWGit();
}
}
}
}
else {
Expand All @@ -210,39 +198,39 @@ public synchronized static VersionControl getAssetVersionControl(File rootDir) t
}
}
return assetVersionControl;
}

public static void updateAssetRefs() throws DataAccessException {
StandardLogger logger = LoggerUtil.getStandardLogger();
if (assetVersionControl != null && !PropertyManager.getBooleanProperty(PropertyNames.MDW_GIT_AUTO_PULL, false)) {
// Automatically update ASSET_REF DB table in case application doesn't do an Import - safety measure
File assetLoc = ApplicationContext.getAssetRoot();
VersionControlGit vcGit = (VersionControlGit) assetVersionControl;
try (DbAccess dbAccess = new DbAccess()) {
String ref = vcGit.getCommit();
if (ref != null) { // avoid attempting update for local-only resources
logger.info("Auto-populating ASSET_REF table...");
Checkpoint cp = new Checkpoint(assetLoc, vcGit, vcGit.getCommit(), dbAccess.getConnection());
cp.updateRefs();
}
else
logger.info("ASSET_REF table not auto-populated during startup due to: null Git commit");
}
catch (SQLException e) {
throw new DataAccessException(e.getErrorCode(), e.getMessage(), e);
}
catch (IOException e) {
throw new DataAccessException(e.getMessage(), e);
}
}
else {
String message = "";
if (assetVersionControl == null) message = " AssetVersionControl is null";
if ("true".equals(PropertyManager.getProperty(PropertyNames.MDW_GIT_AUTO_PULL))) message = message.length() == 0 ? " mdw.git.auto.pull=true" : ", mdw.git.auto.pull=true";
logger.info("ASSET_REF table not auto-populated during startup due to: " + message);
}
}

}

public static void updateAssetRefs() throws DataAccessException {
StandardLogger logger = LoggerUtil.getStandardLogger();
if (assetVersionControl != null && !PropertyManager.getBooleanProperty(PropertyNames.MDW_GIT_AUTO_PULL, false)) {
// Automatically update ASSET_REF DB table in case application doesn't do an Import - safety measure
File assetLoc = ApplicationContext.getAssetRoot();
VersionControlGit vcGit = (VersionControlGit) assetVersionControl;
try (DbAccess dbAccess = new DbAccess()) {
String ref = vcGit.getCommit();
if (ref != null) { // avoid attempting update for local-only resources
logger.info("Auto-populating ASSET_REF table...");
Checkpoint cp = new Checkpoint(assetLoc, vcGit, vcGit.getCommit(), dbAccess.getConnection());
cp.updateRefs();
}
else
logger.info("ASSET_REF table not auto-populated during startup due to: null Git commit");
}
catch (SQLException e) {
throw new DataAccessException(e.getErrorCode(), e.getMessage(), e);
}
catch (IOException e) {
throw new DataAccessException(e.getMessage(), e);
}
}
else {
String message = "";
if (assetVersionControl == null) message = " AssetVersionControl is null";
if ("true".equals(PropertyManager.getProperty(PropertyNames.MDW_GIT_AUTO_PULL))) message = message.length() == 0 ? " mdw.git.auto.pull=true" : ", mdw.git.auto.pull=true";
logger.info("ASSET_REF table not auto-populated during startup due to: " + message);
}
}

public static BaselineData getBaselineData() throws DataAccessException {
try {
return new WrappedBaselineData(new MdwBaselineData()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.centurylink.mdw.constant.OwnerType;
import com.centurylink.mdw.constant.WorkAttributeConstant;
import com.centurylink.mdw.dataaccess.DatabaseAccess;
import com.centurylink.mdw.dataaccess.db.CommonDataAccess;
import com.centurylink.mdw.model.JsonObject;
import com.centurylink.mdw.model.Jsonable;
import com.centurylink.mdw.model.Response;
Expand Down

0 comments on commit 3d807d4

Please sign in to comment.