Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to create a Cloud Shell terminal #34

Closed
jdneo opened this issue Jan 25, 2018 · 33 comments
Closed

Add API to create a Cloud Shell terminal #34

jdneo opened this issue Jan 25, 2018 · 33 comments
Assignees

Comments

@jdneo
Copy link
Member

jdneo commented Jan 25, 2018

Currently the Azure Account Extension support launch to the cloud shell in the vscode.

If I have a extension depend on Azure Account Extension, is there any way that can get the terminal instance that created by Azure Account Extension, then I can send command to this terminal?

@chrmarti
Copy link
Contributor

There is currently no way to do this. What kind of commands do you want to send? Could you use the Azure SDK to achieve the same? The Account extension has API with the credentials object for the Azure SDK.

@jdneo
Copy link
Member Author

jdneo commented Jan 25, 2018

I guess I cannot use Azure SDK to do that. I would like to send some terraform command to the cloud shell.

@vscodebot vscodebot bot closed this as completed Feb 1, 2018
@vscodebot
Copy link

vscodebot bot commented Feb 1, 2018

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

@chrmarti chrmarti reopened this Feb 1, 2018
@chrmarti chrmarti changed the title [Help Wanted]Is there anyway that can get cloud shell terminal created by azure account extension Add API to create a Cloud Shell terminal Feb 1, 2018
@jdneo
Copy link
Member Author

jdneo commented Feb 1, 2018

@chrmarti thanks for reopen it. If this can be supported, there is no need for me to copy-paste your cloud shell code to my extension.

As far as I know, there are several extensions that are using azure cloud shell. It will be great if we can get the terminal instance from azure-account-extension.

@yungez
Copy link

yungez commented Feb 7, 2018

we have same request on cloud shell sdk supporting. We're building vscode ansible extension, ansible is also runnable from cloudshell. we'd like to see features like:

  1. upload files to cloudshell
  2. get cloud shell terminal instance or at least cloud shell connection status.

@chrmarti
Copy link
Contributor

Are you looking for a way to only run commands or do you also need the terminal UI to show the user the output and maybe collect input?

I'm not aware of a way to upload files to the Cloud Shell. @jluk Is there a REST API for that?

@jluk
Copy link

jluk commented Feb 27, 2018

There is not today, but this is something we have been reviewing. Adding @yangl900 as FYI.

@yungez
Copy link

yungez commented Feb 28, 2018

@chrmarti thanks for replying. We need a way to run commands and show user the output.

@chrmarti
Copy link
Contributor

chrmarti commented Mar 7, 2018

I have pushed (not published yet) a draft of such an API. Let me know if that looks like it would cover your use case: https://github.com/Microsoft/vscode-azure-account/blob/1686b7cb6f544732723da70bd87fc164d44ccb93/src/azure-account.api.d.ts#L29

@jdneo
Copy link
Member Author

jdneo commented Mar 9, 2018

Hi @chrmarti I've tested the new API on my machine. And that works! Really appreciated!

About the upload files to Cloud Shell. If it is impossible for now. Could we add more fields about the cloud shell's storage account information in the returned CloudShell object. Then we can do the uploading by ourselves.

What we need are:

  1. the resource group of the storage account
  2. the storage account name
  3. the fileshare name
  4. the storage account key

chrmarti added a commit that referenced this issue Mar 12, 2018
@chrmarti
Copy link
Contributor

I would not feel comfortable exposing these details from the Cloud Shell's configuration. I have instead added a session property to this experimental API that you can use to look up the configuration yourself. You could copy the following code to do so:

  1. Extract a token from the session: https://github.com/Microsoft/vscode-azure-account/blob/b78b7ecba7c395990ad17f906de907835442338e/src/cloudConsole.ts#L321
  2. Fetch the configuration: https://github.com/Microsoft/vscode-azure-account/blob/b78b7ecba7c395990ad17f906de907835442338e/src/cloudConsoleLauncher.ts#L28

Hope that works for you. I have also reduced the number of status values and made the terminal and session properties promises.

@jdneo
Copy link
Member Author

jdneo commented Mar 12, 2018

@chrmarti I agree with what you said. I'll try for this ASAP and tells you if it works on my side. Thanks.

@chrmarti
Copy link
Contributor

@jdneo Just published a bugfix release of the extension that includes this experimental API. We can promote it to 'stable' (moving it up from the 'experimental' property) after this round of feedback.

@jdneo
Copy link
Member Author

jdneo commented Mar 13, 2018

Hi @chrmarti

I changed my code according to your suggestion as following:

...
const accountAPI: AzureAccount = vscode.extensions
                .getExtension<AzureAccount>("ms-vscode.azure-account")!.exports;

const cloudShell: CloudShell =  accountAPI.experimental.createCloudShell("Linux");
this.tfTerminal.terminal = await cloudShell.terminal;

const storageAccount: IStorageAccount = await getStorageAccountforCloudShell(cloudShell);
...

And this is the screenshot when I debug my code:
image

As you can see, though we await for the terminal, but after the promise is resolved, the status of cloudshell is still connecting. Is this by design?

@chrmarti
Copy link
Contributor

Yes, the terminal is available while the connection is still being set up (we show progress messages in the terminal during connection setup). You should be able to access the storage account as soon as the session promise resolves (which is also while the status is still 'Connecting').

Before sending text to the terminal, you would probably wait for the status to change to 'Connected'. (Also note that the terminal will not show until you call .show() on it, which you can do immediately or later, as works best for you.)

@chrmarti
Copy link
Contributor

I could add a waitForConnection function returning a Promise<boolean> that resolves to true when the connection is available and false when the connection failed. That would align with other parts of the API and make it easier to start sending text.

@jdneo
Copy link
Member Author

jdneo commented Mar 13, 2018

Thanks, waitForConnection() looks good to me.

And FYI, just as you said, the the storage account can be accessed as soon as the session promise resolves.

chrmarti added a commit that referenced this issue Mar 13, 2018
@chrmarti
Copy link
Contributor

You could use this code until the next release:

async function waitForConnection(cloudShell: CloudShell) {
	const handleStatus = () => {
		switch(cloudShell.status) {
			case 'Connecting':
				return new Promise<boolean>(resolve => {
					const subs = cloudShell.onStatusChanged(() => {
						subs.dispose();
						resolve(handleStatus());
					});
				});
			case 'Connected':
				return true;
			case 'Disconnected':
				return false;
			default:
				const status: never = cloudShell.status;
				throw new Error(`Unexpected status '${status}'`);
		}
	};
	return handleStatus();
}

@jdneo
Copy link
Member Author

jdneo commented Mar 13, 2018

Thanks for the information.

@jdneo
Copy link
Member Author

jdneo commented Mar 14, 2018

Hi @chrmarti . May I ask when will you remove the experimental property for this API. I'm thinking that if I published the extension using this API, the extension may break when the Azure-Account is updated to remvoe the experimental property. Do you have any suggestions on this?

@chrmarti
Copy link
Contributor

@jdneo I can move it up when you confirm that it works for your use cases.

@yungez Did you have a chance to give it a try?

@jdneo
Copy link
Member Author

jdneo commented Mar 14, 2018

Yes it works fine for my use cases now. Thanks!

@chrmarti
Copy link
Contributor

Took the opportunity of a bug fix release to move this out of experimental.

@chrmarti
Copy link
Contributor

chrmarti commented Apr 3, 2018

@jdneo I have pushed an addition to the API for uploading files to the master branch. Could you check if that will work for you: https://github.com/Microsoft/vscode-azure-account/blob/f0846fc601554ddb3f945e34e6a85551b705fca7/src/azure-account.api.d.ts#L57

@jdneo
Copy link
Member Author

jdneo commented Apr 4, 2018

@chrmarti Awesome! I'm trying to local generate the vsix file and install the extension locally, but it always need me to reload the VS Code, seems the extension cannot be started up. Do you have any idea on that?


Oh, that is because the engines defined in package.json is an unreleased version of vscode. After change it to 1.21.0, everything works fine. I'll check the new API later.

@chrmarti
Copy link
Contributor

chrmarti commented Apr 4, 2018

@jdneo The extension uses new API from 1.22. It should run fine with the 1.22 insiders release of VS Code.

@jdneo
Copy link
Member Author

jdneo commented Apr 4, 2018

Thanks for the information. I was just directly calling the Azure: Upload to Cloud Shell to understand the behavior, but seems nothing happened. 😢

@chrmarti
Copy link
Contributor

chrmarti commented Apr 4, 2018

Is there anything in the console? (Help > Toggle Developer Tools)

@jdneo
Copy link
Member Author

jdneo commented Apr 4, 2018

yes, Bad progress location: undefined

@chrmarti
Copy link
Contributor

chrmarti commented Apr 4, 2018

Maybe you need to rebuild the vsix with the engine set to 1.22 first? I just tried and it works for me.

@jdneo
Copy link
Member Author

jdneo commented Apr 4, 2018

I see. Just as you have said. After rebuilding the vsix and install it in VS Code Insider, everything works fine.

I just have one question:

Seems currently we will always upload files to the $HOME folder. Is it possible that if we can specify the path for each file. For example clouddrive/myprojet/file.txt. Here clouddrive is the Azure File Share mounted by Cloud Shell.

@chrmarti
Copy link
Contributor

chrmarti commented Apr 4, 2018

That seems to be a limitation of the Upload API. @jluk Is there a way to upload files to a different folder than the home folder?

@jluk
Copy link

jluk commented Apr 4, 2018

Not right now, bash supports pushing to $home and PS supports pushing to $clouddrive. We'll be improving this down the road, but this is the case for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants