Search in sources :

Example 1 with PrivateRegistryImageSetting

use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.

the class PublishWebAppOnLinuxDialog method validate.

private void validate() throws InvalidFormDataException {
    if (!AuthMethodManager.getInstance().isSignedIn()) {
        throw new InvalidFormDataException(NEED_SIGN_IN);
    }
    // docker file
    if (Utils.isEmptyString(model.getDockerFilePath())) {
        throw new InvalidFormDataException(INVALID_DOCKER_FILE);
    }
    File dockerFile = Paths.get(model.getDockerFilePath()).toFile();
    if (!dockerFile.exists() || !dockerFile.isFile()) {
        throw new InvalidFormDataException(INVALID_DOCKER_FILE);
    }
    // acr
    PrivateRegistryImageSetting setting = model.getPrivateRegistryImageSetting();
    if (Utils.isEmptyString(setting.getServerUrl()) || !setting.getServerUrl().matches(DOMAIN_NAME_REGEX)) {
        throw new InvalidFormDataException(MISSING_SERVER_URL);
    }
    if (Utils.isEmptyString(setting.getUsername())) {
        throw new InvalidFormDataException(MISSING_USERNAME);
    }
    if (Utils.isEmptyString(setting.getPassword())) {
        throw new InvalidFormDataException(MISSING_PASSWORD);
    }
    String imageTag = setting.getImageTagWithServerUrl();
    if (Utils.isEmptyString(imageTag)) {
        throw new InvalidFormDataException(MISSING_IMAGE_WITH_TAG);
    }
    if (imageTag.endsWith(":")) {
        throw new InvalidFormDataException(CANNOT_END_WITH_COLON);
    }
    final String[] repoAndTag = imageTag.split(":");
    // check repository first
    if (repoAndTag[0].length() < 1 || repoAndTag[0].length() > REPO_LENGTH) {
        throw new InvalidFormDataException(REPO_LENGTH_INVALID);
    }
    if (repoAndTag[0].endsWith("/")) {
        throw new InvalidFormDataException(CANNOT_END_WITH_SLASH);
    }
    final String[] repoComponents = repoAndTag[0].split("/");
    for (String component : repoComponents) {
        if (!component.matches(REPO_COMPONENTS_REGEX)) {
            throw new InvalidFormDataException(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 InvalidFormDataException(TAG_LENGTH_INVALID);
        }
        if (!repoAndTag[1].matches(TAG_REGEX)) {
            throw new InvalidFormDataException(String.format(TAG_INVALID, repoAndTag[1], TAG_REGEX));
        }
    }
    if (repoAndTag.length > 2) {
        throw new InvalidFormDataException(INVALID_IMAGE_WITH_TAG);
    }
    // web app
    if (model.isCreatingNewWebAppOnLinux()) {
        if (Utils.isEmptyString(model.getWebAppName())) {
            throw new InvalidFormDataException(MISSING_WEB_APP);
        }
        if (Utils.isEmptyString(model.getSubscriptionId())) {
            throw new InvalidFormDataException(MISSING_SUBSCRIPTION);
        }
        if (Utils.isEmptyString(model.getResourceGroupName())) {
            throw new InvalidFormDataException(MISSING_RESOURCE_GROUP);
        }
        if (model.isCreatingNewAppServicePlan()) {
            if (Utils.isEmptyString(model.getAppServicePlanName())) {
                throw new InvalidFormDataException(MISSING_APP_SERVICE_PLAN);
            }
        } else {
            if (Utils.isEmptyString(model.getAppServicePlanId())) {
                throw new InvalidFormDataException(MISSING_APP_SERVICE_PLAN);
            }
        }
    } else {
        if (Utils.isEmptyString(model.getWebAppId())) {
            throw new InvalidFormDataException(MISSING_WEB_APP);
        }
    }
    // target package
    if (Utils.isEmptyString(model.getTargetName())) {
        throw new InvalidFormDataException(MISSING_ARTIFACT);
    }
    if (!model.getTargetName().matches(ARTIFACT_NAME_REGEX)) {
        throw new InvalidFormDataException(String.format(INVALID_WAR_FILE, model.getTargetName()));
    }
}
Also used : PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) InvalidFormDataException(com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException) File(java.io.File)

Example 2 with PrivateRegistryImageSetting

use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.

the class PublishWebAppOnLinuxDialog method apply.

private void apply() {
    model.setDockerFilePath(cpAcr.getDockerfilePath());
    // set ACR info
    model.setPrivateRegistryImageSetting(new PrivateRegistryImageSetting(cpAcr.getServerUrl(), cpAcr.getUserName(), cpAcr.getPassword(), cpAcr.getImageTag(), cpAcr.getStartupFile()));
    // set target
    model.setTargetPath(targetPath);
    model.setTargetName(Paths.get(targetPath).getFileName().toString());
    // set web app info
    if (rdoExistingWebApp.getSelection()) {
        // existing web app
        model.setCreatingNewWebAppOnLinux(false);
        IWebApp selectedWebApp = getSelectedWebApp();
        if (selectedWebApp != null) {
            model.setWebAppId(selectedWebApp.id());
            model.setWebAppName(selectedWebApp.name());
            model.setSubscriptionId(selectedWebApp.subscriptionId());
            model.setResourceGroupName(selectedWebApp.resourceGroup());
        } else {
            model.setWebAppId(null);
            model.setWebAppName(null);
            model.setSubscriptionId(null);
            model.setResourceGroupName(null);
        }
    } else if (rdoNewWebApp.getSelection()) {
        // create new web app
        model.setCreatingNewWebAppOnLinux(true);
        model.setWebAppId("");
        model.setWebAppName(cpNew.txtAppName.getText());
        Subscription selectedSubscription = getSelectedSubscription();
        if (selectedSubscription != null) {
            model.setSubscriptionId(selectedSubscription.getId());
        }
        // resource group
        if (cpNew.rdoExistingResourceGroup.getSelection()) {
            // existing RG
            model.setCreatingNewResourceGroup(false);
            ResourceGroup selectedRg = getSelectedResourceGroup();
            if (selectedRg != null) {
                model.setResourceGroupName(selectedRg.getName());
            } else {
                model.setResourceGroupName(null);
            }
        } else if (cpNew.rdoNewResourceGroup.getSelection()) {
            // new RG
            model.setCreatingNewResourceGroup(true);
            model.setResourceGroupName(cpNew.txtNewResourceGroupName.getText());
        }
        // app service plan
        if (cpNew.rdoNewAppServicePlan.getSelection()) {
            model.setCreatingNewAppServicePlan(true);
            model.setAppServicePlanName(cpNew.txtAppServicePlanName.getText());
            Region selectedLocation = getSelectedLocation();
            if (selectedLocation != null) {
                model.setLocationName(selectedLocation.getLabel());
            } else {
                model.setLocationName(null);
            }
            PricingTier selectedPricingTier = getSelectedPricingTier();
            if (selectedPricingTier != null) {
                model.setPricingSkuTier(selectedPricingTier.getTier());
                model.setPricingSkuSize(selectedPricingTier.getSize());
            } else {
                model.setPricingSkuTier(null);
                model.setPricingSkuSize(null);
            }
        } else if (cpNew.rdoExistingAppServicePlan.getSelection()) {
            model.setCreatingNewAppServicePlan(false);
            IAppServicePlan selectedAsp = getSelectedAppServicePlan();
            if (selectedAsp != null) {
                model.setAppServicePlanId(selectedAsp.id());
            } else {
                model.setAppServicePlanId(null);
            }
        }
    }
}
Also used : PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) PricingTier(com.microsoft.azure.toolkit.lib.appservice.model.PricingTier) IAppServicePlan(com.microsoft.azure.toolkit.lib.appservice.service.IAppServicePlan) Region(com.microsoft.azure.toolkit.lib.common.model.Region) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) ResourceGroup(com.microsoft.azure.toolkit.lib.common.model.ResourceGroup) IWebApp(com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)

Example 3 with PrivateRegistryImageSetting

use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.

the class PublishWebAppOnLinuxDialog method execute.

private void execute() {
    Operation operation = TelemetryManager.createOperation(WEBAPP, DEPLOY_WEBAPP_CONTAINER);
    Observable.fromCallable(() -> {
        ConsoleLogger.info("Starting job ...  ");
        operation.start();
        if (basePath == null) {
            ConsoleLogger.error("Project base path is null.");
            throw new FileNotFoundException("Project base path is null.");
        }
        // locate artifact to specified location
        String targetFilePath = model.getTargetPath();
        ConsoleLogger.info(String.format("Locating artifact ... [%s]", targetFilePath));
        // validate dockerfile
        Path targetDockerfile = Paths.get(model.getDockerFilePath());
        ConsoleLogger.info(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 = model.getPrivateRegistryImageSetting();
        ConsoleLogger.info(String.format("Building image ...  [%s]", acrInfo.getImageTagWithServerUrl()));
        DockerClient docker = DefaultDockerClient.fromEnv().build();
        DockerUtil.ping(docker);
        DockerUtil.buildImage(docker, acrInfo.getImageTagWithServerUrl(), targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler());
        // push to ACR
        ConsoleLogger.info(String.format("Pushing to ACR ... [%s] ", acrInfo.getServerUrl()));
        DockerUtil.pushImage(docker, acrInfo.getServerUrl(), acrInfo.getUsername(), acrInfo.getPassword(), acrInfo.getImageTagWithServerUrl(), new DockerProgressHandler());
        // deploy
        if (model.isCreatingNewWebAppOnLinux()) {
            // create new WebApp
            ConsoleLogger.info(String.format("Creating new WebApp ... [%s]", model.getWebAppName()));
            IWebApp app = AzureWebAppMvpModel.getInstance().createAzureWebAppWithPrivateRegistryImage(model);
            if (app != null && app.name() != null) {
                ConsoleLogger.info(String.format("URL:  http://%s.azurewebsites.net/", app.name()));
                AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null));
            }
        } else {
            // update WebApp
            ConsoleLogger.info(String.format("Updating WebApp ... [%s]", model.getWebAppName()));
            IWebApp app = AzureWebAppMvpModel.getInstance().updateWebAppOnDocker(model.getWebAppId(), acrInfo);
            if (app != null && app.name() != null) {
                ConsoleLogger.info(String.format("URL:  http://%s.azurewebsites.net/", app.name()));
            }
        }
        return null;
    }).subscribeOn(SchedulerProviderFactory.getInstance().getSchedulerProvider().io()).subscribe(ret -> {
        ConsoleLogger.info("Job done");
        if (model.isCreatingNewWebAppOnLinux() && AzureUIRefreshCore.listeners != null) {
            AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, null));
        }
        sendTelemetry(true, null);
        operation.complete();
    }, err -> {
        err.printStackTrace();
        ConsoleLogger.error(err.getMessage());
        EventUtil.logError(operation, ErrorType.systemError, new Exception(err), null, null);
        operation.complete();
        sendTelemetry(false, err.getMessage());
    });
}
Also used : Path(java.nio.file.Path) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) DockerProgressHandler(com.microsoft.azuretools.container.DockerProgressHandler) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) FileNotFoundException(java.io.FileNotFoundException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AzureUIRefreshEvent(com.microsoft.azuretools.utils.AzureUIRefreshEvent) InvalidFormDataException(com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException) FileNotFoundException(java.io.FileNotFoundException) IWebApp(com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)

Example 4 with PrivateRegistryImageSetting

use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.

the class PushImageDialog method validate.

private void validate() throws InvalidFormDataException {
    if (model == null) {
        throw new InvalidFormDataException(MISSING_MODEL);
    }
    // docker file
    if (Utils.isEmptyString(model.getDockerFilePath())) {
        throw new InvalidFormDataException(INVALID_DOCKER_FILE);
    }
    File dockerFile = Paths.get(model.getDockerFilePath()).toFile();
    if (!dockerFile.exists() || !dockerFile.isFile()) {
        throw new InvalidFormDataException(INVALID_DOCKER_FILE);
    }
    // acr
    PrivateRegistryImageSetting setting = model.getPrivateRegistryImageSetting();
    if (Utils.isEmptyString(setting.getServerUrl()) || !setting.getServerUrl().matches(DOMAIN_NAME_REGEX)) {
        throw new InvalidFormDataException(MISSING_SERVER_URL);
    }
    if (Utils.isEmptyString(setting.getUsername())) {
        throw new InvalidFormDataException(MISSING_USERNAME);
    }
    if (Utils.isEmptyString(setting.getPassword())) {
        throw new InvalidFormDataException(MISSING_PASSWORD);
    }
    String imageTag = setting.getImageTagWithServerUrl();
    if (Utils.isEmptyString(imageTag)) {
        throw new InvalidFormDataException(MISSING_IMAGE_WITH_TAG);
    }
    if (imageTag.endsWith(":")) {
        throw new InvalidFormDataException(CANNOT_END_WITH_COLON);
    }
    final String[] repoAndTag = imageTag.split(":");
    // check repository first
    if (repoAndTag[0].length() < 1 || repoAndTag[0].length() > REPO_LENGTH) {
        throw new InvalidFormDataException(REPO_LENGTH_INVALID);
    }
    if (repoAndTag[0].endsWith("/")) {
        throw new InvalidFormDataException(CANNOT_END_WITH_SLASH);
    }
    final String[] repoComponents = repoAndTag[0].split("/");
    for (String component : repoComponents) {
        if (!component.matches(REPO_COMPONENTS_REGEX)) {
            throw new InvalidFormDataException(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 InvalidFormDataException(TAG_LENGTH_INVALID);
        }
        if (!repoAndTag[1].matches(TAG_REGEX)) {
            throw new InvalidFormDataException(String.format(TAG_INVALID, repoAndTag[1], TAG_REGEX));
        }
    }
    if (repoAndTag.length > 2) {
        throw new InvalidFormDataException(INVALID_IMAGE_WITH_TAG);
    }
    // target package
    if (Utils.isEmptyString(model.getTargetName())) {
        throw new InvalidFormDataException(MISSING_ARTIFACT);
    }
    if (!model.getTargetName().matches(ARTIFACT_NAME_REGEX)) {
        throw new InvalidFormDataException(String.format(INVALID_ARTIFACT_FILE, model.getTargetName()));
    }
}
Also used : PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) InvalidFormDataException(com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException) File(java.io.File)

Example 5 with PrivateRegistryImageSetting

use of com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting in project azure-tools-for-java by Microsoft.

the class ContainerRegistryMvpModel method createImageSettingWithRegistry.

/**
 * Get Registry Credential.
 */
public PrivateRegistryImageSetting createImageSettingWithRegistry(@NotNull final Registry registry) throws Exception {
    if (!registry.adminUserEnabled()) {
        throw new Exception(ADMIN_USER_NOT_ENABLED);
    }
    final RegistryCredentials credentials = registry.getCredentials();
    if (credentials == null) {
        throw new Exception(CANNOT_GET_CREDENTIAL);
    }
    String username = credentials.username();
    final Map<AccessKeyType, String> passwords = credentials.accessKeys();
    if (Utils.isEmptyString(username) || passwords == null || !passwords.containsKey(AccessKeyType.PRIMARY)) {
        throw new Exception(CANNOT_GET_CREDENTIAL);
    }
    return new PrivateRegistryImageSetting(registry.loginServerUrl(), username, passwords.get(AccessKeyType.PRIMARY), IMAGE_TAG, null);
}
Also used : AccessKeyType(com.microsoft.azure.management.containerregistry.AccessKeyType) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) RegistryCredentials(com.microsoft.azure.management.containerregistry.RegistryCredentials)

Aggregations

PrivateRegistryImageSetting (com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting)21 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)6 DockerClient (com.spotify.docker.client.DockerClient)6 Registry (com.microsoft.azure.management.containerregistry.Registry)5 ISecureStore (com.microsoft.azure.toolkit.ide.common.store.ISecureStore)4 IWebApp (com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)4 InvalidFormDataException (com.microsoft.azuretools.azurecommons.exceptions.InvalidFormDataException)4 FileNotFoundException (java.io.FileNotFoundException)4 Path (java.nio.file.Path)4 PricingTier (com.microsoft.azure.toolkit.lib.appservice.model.PricingTier)3 Gson (com.google.gson.Gson)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 AccessKeyType (com.microsoft.azure.management.containerregistry.AccessKeyType)2 RegistryCredentials (com.microsoft.azure.management.containerregistry.RegistryCredentials)2 DockerProgressHandler (com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler)2 IAppServicePlan (com.microsoft.azure.toolkit.lib.appservice.service.IAppServicePlan)2 Region (com.microsoft.azure.toolkit.lib.common.model.Region)2 ResourceGroup (com.microsoft.azure.toolkit.lib.common.model.ResourceGroup)2 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)2 DockerProgressHandler (com.microsoft.azuretools.container.DockerProgressHandler)2