use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.
the class SettingPanel method apply.
/**
* Function triggered by any content change events.
*/
@Override
public void apply(PushImageRunConfiguration pushImageRunConfiguration) {
pushImageRunConfiguration.setDockerFilePath(containerSettingPanel.getDockerPath());
// set ACR info
pushImageRunConfiguration.setPrivateRegistryImageSetting(new PrivateRegistryImageSetting(containerSettingPanel.getServerUrl().replaceFirst("^https?://", "").replaceFirst("/$", ""), containerSettingPanel.getUserName(), containerSettingPanel.getPassword(), containerSettingPanel.getImageTag(), ""));
final ISecureStore secureStore = AzureStoreManager.getInstance().getSecureStore();
secureStore.savePassword(PRIVATE_DOCKER_REGISTRY, containerSettingPanel.getServerUrl(), containerSettingPanel.getUserName(), containerSettingPanel.getPassword());
// set target
pushImageRunConfiguration.setTargetPath(getTargetPath());
pushImageRunConfiguration.setTargetName(getTargetName());
}
use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.
the class WebAppOnLinuxDeployConfiguration method validate.
/**
* Configuration value Validation.
*/
@Override
public void validate() throws ConfigurationException {
checkAzurePreconditions();
if (Utils.isEmptyString(deployModel.getDockerFilePath()) || !Paths.get(deployModel.getDockerFilePath()).toFile().exists()) {
throw new ConfigurationException(INVALID_DOCKER_FILE);
}
// acr
PrivateRegistryImageSetting setting = deployModel.getPrivateRegistryImageSetting();
if (Utils.isEmptyString(setting.getServerUrl()) || !setting.getServerUrl().matches(DOMAIN_NAME_REGEX)) {
throw new ConfigurationException(MISSING_SERVER_URL);
}
if (Utils.isEmptyString(setting.getUsername())) {
throw new ConfigurationException(MISSING_USERNAME);
}
if (Utils.isEmptyString(setting.getPassword())) {
throw new ConfigurationException(MISSING_PASSWORD);
}
String imageTag = setting.getImageTagWithServerUrl();
if (Utils.isEmptyString(imageTag)) {
throw new ConfigurationException(MISSING_IMAGE_WITH_TAG);
}
if (imageTag.endsWith(":")) {
throw new ConfigurationException(CANNOT_END_WITH_COLON);
}
final String[] repoAndTag = imageTag.split(":");
// check repository first
if (repoAndTag[0].length() < 1 || repoAndTag[0].length() > REPO_LENGTH) {
throw new ConfigurationException(REPO_LENGTH_INVALID);
}
if (repoAndTag[0].endsWith("/")) {
throw new ConfigurationException(CANNOT_END_WITH_SLASH);
}
final String[] repoComponents = repoAndTag[0].split("/");
for (String component : repoComponents) {
if (!component.matches(REPO_COMPONENTS_REGEX)) {
throw new ConfigurationException(String.format(REPO_COMPONENT_INVALID, component, REPO_COMPONENTS_REGEX));
}
}
// check when contains tag
if (repoAndTag.length == 2) {
if (repoAndTag[1].length() > TAG_LENGTH) {
throw new ConfigurationException(TAG_LENGTH_INVALID);
}
if (!repoAndTag[1].matches(TAG_REGEX)) {
throw new ConfigurationException(String.format(TAG_INVALID, repoAndTag[1], TAG_REGEX));
}
}
if (repoAndTag.length > 2) {
throw new ConfigurationException(INVALID_IMAGE_WITH_TAG);
}
// web app
if (deployModel.isCreatingNewWebAppOnLinux()) {
if (Utils.isEmptyString(deployModel.getWebAppName())) {
throw new ConfigurationException(MISSING_WEB_APP);
}
if (Utils.isEmptyString(deployModel.getSubscriptionId())) {
throw new ConfigurationException(MISSING_SUBSCRIPTION);
}
if (Utils.isEmptyString(deployModel.getResourceGroupName())) {
throw new ConfigurationException(MISSING_RESOURCE_GROUP);
}
if (deployModel.isCreatingNewAppServicePlan()) {
if (Utils.isEmptyString(deployModel.getAppServicePlanName())) {
throw new ConfigurationException(MISSING_APP_SERVICE_PLAN);
}
} else {
if (Utils.isEmptyString(deployModel.getAppServicePlanId())) {
throw new ConfigurationException(MISSING_APP_SERVICE_PLAN);
}
}
} else {
if (Utils.isEmptyString(deployModel.getWebAppId())) {
throw new ConfigurationException(MISSING_WEB_APP);
}
}
// target package
if (Utils.isEmptyString(deployModel.getTargetName())) {
throw new ConfigurationException(MISSING_ARTIFACT);
}
if (!deployModel.getTargetName().matches(ARTIFACT_NAME_REGEX)) {
throw new ConfigurationException(String.format(INVALID_WAR_FILE, deployModel.getTargetName()));
}
}
use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.
the class WebAppOnLinuxDeployState method executeSteps.
@Override
@AzureOperation(name = "docker.deploy_image.state", type = AzureOperation.Type.ACTION)
public IAppService executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
processHandler.setText("Starting job ... ");
final String basePath = project.getBasePath();
if (basePath == null) {
processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
final String targetFilePath = deployModel.getTargetPath();
processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
final Path targetDockerfile = Paths.get(deployModel.getDockerFilePath());
processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
final PrivateRegistryImageSetting acrInfo = deployModel.getPrivateRegistryImageSetting();
processHandler.setText(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl()));
final DockerClient docker = DefaultDockerClient.fromEnv().build();
DockerUtil.ping(docker);
DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
// push to ACR
processHandler.setText(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler(processHandler));
// deploy
if (deployModel.isCreatingNewWebAppOnLinux()) {
// create new WebApp
processHandler.setText(String.format("Creating new WebApp ... [%s]", deployModel.getWebAppName()));
final IWebApp app = AzureWebAppMvpModel.getInstance().createAzureWebAppWithPrivateRegistryImage(deployModel);
if (app != null && app.name() != null) {
processHandler.setText(String.format("URL: http://%s.azurewebsites.net/", app.name()));
updateConfigurationDataModel(app);
AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null));
}
return app;
} else {
// update WebApp
processHandler.setText(String.format("Updating WebApp ... [%s]", deployModel.getWebAppName()));
final IWebApp app = AzureWebAppMvpModel.getInstance().updateWebAppOnDocker(deployModel.getWebAppId(), acrInfo);
if (app != null && app.name() != null) {
processHandler.setText(String.format("URL: http://%s.azurewebsites.net/", app.name()));
}
return app;
}
}
use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.
the class PushImageRunConfiguration method validate.
/**
* Validate input value.
*/
@Override
public void validate() throws ConfigurationException {
if (dataModel == null) {
throw new ConfigurationException(MISSING_MODEL);
}
if (Utils.isEmptyString(dataModel.getDockerFilePath()) || !Paths.get(dataModel.getDockerFilePath()).toFile().exists()) {
throw new ConfigurationException(INVALID_DOCKER_FILE);
}
// acr
PrivateRegistryImageSetting setting = dataModel.getPrivateRegistryImageSetting();
if (Utils.isEmptyString(setting.getServerUrl()) || !setting.getServerUrl().matches(DOMAIN_NAME_REGEX)) {
throw new ConfigurationException(MISSING_SERVER_URL);
}
if (Utils.isEmptyString(setting.getUsername())) {
throw new ConfigurationException(MISSING_USERNAME);
}
if (Utils.isEmptyString(setting.getPassword())) {
throw new ConfigurationException(MISSING_PASSWORD);
}
String imageTag = setting.getImageTagWithServerUrl();
if (Utils.isEmptyString(imageTag)) {
throw new ConfigurationException(MISSING_IMAGE_WITH_TAG);
}
if (imageTag.endsWith(":")) {
throw new ConfigurationException(CANNOT_END_WITH_COLON);
}
final String[] repoAndTag = imageTag.split(":");
// check repository first
if (repoAndTag[0].length() < 1 || repoAndTag[0].length() > REPO_LENGTH) {
throw new ConfigurationException(REPO_LENGTH_INVALID);
}
if (repoAndTag[0].endsWith("/")) {
throw new ConfigurationException(CANNOT_END_WITH_SLASH);
}
final String[] repoComponents = repoAndTag[0].split("/");
for (String component : repoComponents) {
if (!component.matches(REPO_COMPONENTS_REGEX)) {
throw new ConfigurationException(String.format(REPO_COMPONENT_INVALID, component, REPO_COMPONENTS_REGEX));
}
}
// check when contains tag
if (repoAndTag.length == 2) {
if (repoAndTag[1].length() > TAG_LENGTH) {
throw new ConfigurationException(TAG_LENGTH_INVALID);
}
if (!repoAndTag[1].matches(TAG_REGEX)) {
throw new ConfigurationException(String.format(TAG_INVALID, repoAndTag[1], TAG_REGEX));
}
}
if (repoAndTag.length > 2) {
throw new ConfigurationException(INVALID_IMAGE_WITH_TAG);
}
// target package
if (Utils.isEmptyString(dataModel.getTargetName())) {
throw new ConfigurationException(MISSING_ARTIFACT);
}
if (!dataModel.getTargetName().matches(ARTIFACT_NAME_REGEX)) {
throw new ConfigurationException(String.format(INVALID_ARTIFACT_FILE, dataModel.getTargetName()));
}
}
use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.
the class PushImageRunState method executeSteps.
@Override
public String executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
processHandler.setText("Starting job ... ");
String basePath = project.getBasePath();
if (basePath == null) {
processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
String targetFilePath = dataModel.getTargetPath();
processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
PrivateRegistryImageSetting acrInfo = dataModel.getPrivateRegistryImageSetting();
processHandler.setText(String.format("Building image ... [%s]", acrInfo.getImageTagWithServerUrl()));
DockerClient docker = DefaultDockerClient.fromEnv().build();
DockerUtil.ping(docker);
String image = DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
// push to ACR
processHandler.setText(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler(processHandler));
return image;
}
Aggregations