forked from redhat-developer/vscode-quarkus
-
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.
Uses the existing menu to select which Quarkus project to deploy. Uses OpenShift Connector in order to deploy the Quarkus project to OpenShift. OpenShift Connector handles selecting/creating the application and naming the component. Closes redhat-developer#313 Signed-off-by: David Thompson <davthomp@redhat.com>
- Loading branch information
Showing
6 changed files
with
211 additions
and
41 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { commands, Disposable, Extension, extensions, ProgressLocation, window } from "vscode"; | ||
|
||
export const OPENSHIFT_CONNECTOR_EXTENSION_ID: string = 'redhat.vscode-openshift-connector'; | ||
export const OPENSHIFT_CONNECTOR = 'OpenShift Connector extension'; | ||
const DOWNLOAD_TIMEOUT: number = 60000; // Timeout for downloading VSCode OpenShift Connector, in milliseconds | ||
|
||
/** | ||
* Returns true if the OpenShift connector extension is installed, and false otherwise | ||
* | ||
* @returns true if the OpenShift connector extension is installed, and false otherwise | ||
*/ | ||
export function isOpenShiftConnectorInstalled(): boolean { | ||
return !!extensions.getExtension(OPENSHIFT_CONNECTOR_EXTENSION_ID); | ||
} | ||
|
||
/** | ||
* Returns the OpenShift Connector extension API | ||
* | ||
* @throws Error if the extension is not installed | ||
* @returns the OpenShift Connector extension API | ||
*/ | ||
export async function getOpenShiftConnector(): Promise<any> { | ||
if (!isOpenShiftConnectorInstalled()) { | ||
throw new Error(`${OPENSHIFT_CONNECTOR} is not installed`); | ||
} | ||
const openShiftConnector: Extension<any> = extensions.getExtension(OPENSHIFT_CONNECTOR_EXTENSION_ID); | ||
if (openShiftConnector.isActive) { | ||
return openShiftConnector.exports; | ||
} | ||
return extensions.getExtension(OPENSHIFT_CONNECTOR_EXTENSION_ID).activate(); | ||
} | ||
|
||
/** | ||
* Install the OpenShift Connector extension | ||
* | ||
* @returns when the extension is installed | ||
* @throws if the user refuses to install the extension, or if the extension does not get installed within a timeout period | ||
*/ | ||
async function installOpenShiftConnector(): Promise<any> { | ||
const installListenerDisposables: Disposable[] = []; | ||
return Promise.race([ | ||
new Promise((resolve, reject) => { | ||
extensions.onDidChange(() => { | ||
if (isOpenShiftConnectorInstalled()) { | ||
installListenerDisposables.forEach((d: Disposable) => { d.dispose(); }); | ||
resolve(); | ||
} | ||
}, null, installListenerDisposables); | ||
commands.executeCommand("workbench.extensions.installExtension", OPENSHIFT_CONNECTOR_EXTENSION_ID) | ||
.then((_unused: any) => { }, reject); | ||
}), | ||
new Promise((_unused, reject) => { | ||
// Fail install if it takes longer than the timeout period | ||
setTimeout(reject, DOWNLOAD_TIMEOUT, new Error(`${OPENSHIFT_CONNECTOR} installation is taking a while. Cancelling 'Deploy to OpenShift'`)); | ||
}) | ||
]).catch(e => { | ||
installListenerDisposables.forEach((d: Disposable) => { d.dispose(); }); | ||
return Promise.reject(e); | ||
}); | ||
} | ||
|
||
/** | ||
* Install the OpenShift Connector extension and show the progress | ||
* | ||
* @returns when the extension is installed | ||
* @throws if the extension installation fails or times out | ||
*/ | ||
export async function installOpenShiftConnectorWithProgress(): Promise<void> { | ||
return window.withProgress({ location: ProgressLocation.Notification, title: `Installing ${OPENSHIFT_CONNECTOR}...` }, _progress => { | ||
return installOpenShiftConnector(); | ||
}).then(() => { | ||
window.showInformationMessage(`Successfully installed ${OPENSHIFT_CONNECTOR}.`); | ||
return Promise.resolve(); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { commands, Uri, window } from "vscode"; | ||
import { ProjectLabelInfo } from "../../definitions/ProjectLabelInfo"; | ||
import { getOpenShiftConnector, installOpenShiftConnectorWithProgress, isOpenShiftConnectorInstalled, OPENSHIFT_CONNECTOR } from "../../utils/openShiftConnectorUtils"; | ||
import { getQuarkusProject } from "../getQuarkusProject"; | ||
|
||
/** | ||
* Attempts to select a Quarkus project, then deploy it to OpenShift using OpenShift Connector | ||
*/ | ||
export async function deployToOpenShift() { | ||
try { | ||
await installOpenShiftConnectorIfNeeded(); | ||
const quarkusProject: ProjectLabelInfo = await getQuarkusProject(); | ||
const result: string = await commands.executeCommand("openshift.component.createFromRootWorkspaceFolder", // | ||
Uri.parse(quarkusProject.uri) as Uri, // | ||
[] as Uri[], // unused in OpenShift Connector | ||
undefined, // OpenShift context | ||
"java-quarkus"); | ||
if (!result) { | ||
throw new Error(); | ||
} | ||
return window.showInformationMessage(result); | ||
} catch (error) { | ||
return window.showErrorMessage(`Failed to deploy to OpenShift`); | ||
} | ||
} | ||
|
||
/** | ||
* Installs the OpenShift Connector extension if its missing, then returns the extension API | ||
* | ||
* @throws Error if the extension is not installed and something prevents installation | ||
* @returns the OpenShift Connector extension API | ||
*/ | ||
async function installOpenShiftConnectorIfNeeded(): Promise<any> { | ||
if (isOpenShiftConnectorInstalled()) { | ||
return getOpenShiftConnector(); | ||
} | ||
return askToInstallOpenShiftConnector(); | ||
} | ||
|
||
/** | ||
* Resolves to the OpenShift connector extension API, or rejects if the extension is missing and the user doesn't want to install it | ||
* | ||
* @throws Error if the extension is missing and the user can't or doesn't want to install it | ||
* @returns the OpenShift connector extension API | ||
*/ | ||
async function askToInstallOpenShiftConnector(): Promise<any> { | ||
const YES: string = 'Yes'; | ||
const NO: string = 'No'; | ||
const response: string = await window.showInformationMessage(`${OPENSHIFT_CONNECTOR} is needed to deploy to OpenShift. Install it now?`, YES, NO); | ||
if (response === YES) { | ||
try { | ||
await installOpenShiftConnectorWithProgress(); | ||
if (isOpenShiftConnectorInstalled()) { | ||
return getOpenShiftConnector(); | ||
} | ||
} catch (e) { | ||
throw new Error(`${OPENSHIFT_CONNECTOR} installation failed`); | ||
} | ||
} | ||
throw new Error(`${OPENSHIFT_CONNECTOR} needs to be installed to deploy to OpenShift`); | ||
} |