Skip to content

Commit

Permalink
upload playbook via storage (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
yungezz authored Mar 23, 2018
1 parent 16c7358 commit c17bbcb
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 12 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# Change Log
All notable changes to the "vscode-ansible" extension will be documented in this file.

## 0.1.5
Make below improvement:
- Cloud Shell terminal reusable. Issue #42.
- Show hover up document linking in role structure. Issue #88.
- Improve code snippets triggering, no need of `ctril + space`. Issue #90.
- Add new host option in run playbook via ssh command. Issue #96.
- Add configuration to enable/disable auto completion. Issue #93.


Thank you for reporting issue and provide valuable feedback!
- @azwanson for Issue #88, Issue #89.
- @moltar for Issue #90.
- @tristan947 for Issue #93.
- @zikalino for Issue #96.

## 0.1.4
Fix below issues/bugs:
- Issue #75 , #63 , #71 . Hover and symbol failed when yaml file format invalid.
- Issue #67 . Execute playbook in subfolder inside docker failed.
- Issue #77 . Execute playbook failed when workspace path contains white space.
- Issue #80 . When remote ssh server default directory is not home dir, executing playbook failed.

## 0.1.3
- Support running playbook remotely via SSH.
- Add yaml language server support to provide below playbook authoring experience:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ This extension collects telemetry data to help improve our products. Please read
```
telemetry.enableTelemetry = false
```

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
Expand All @@ -134,3 +135,7 @@ contact [[email protected]](mailto:[email protected]) with any additio
Please see below documents to learn how to contribute:
- [How to build, run and debug from source](https://github.com/VSChina/vscode-ansible/wiki/How-to-Contribute)
- [Coding Guidelines](https://github.com/VSChina/vscode-ansible/wiki/Coding-Guidelines)


## Release Notes and Thank you
Please see our [releases](https://github.com/VSChina/vscode-ansible/releases) to see detail in each release, and `Thank you`. Or check [CHANGELOG](./CHANGELOG.md).
109 changes: 108 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"dependencies": {
"adal-node": "0.1.22",
"azure-arm-resource": "^3.0.0-preview",
"azure-storage": "^2.8.1",
"fs-extra": "^4.0.2",
"fuzzaldrin-plus": "^0.6.0",
"ms-rest": "^2.3.1",
Expand All @@ -187,4 +188,4 @@
"ws": "^3.3.2",
"yamljs": "^0.3.0"
}
}
}
60 changes: 60 additions & 0 deletions src/azureStorageHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

import * as storage from 'azure-storage';
import { FileService } from 'azure-storage';
import * as path from 'path';

const DIRECTORY_NAME = 'ansible-playbooks';

export function uploadFilesToAzureStorage(localFileName: string, storageAccountName: string, storageAccountKey: string, fileShareName: string): Promise<void> {
const client = storage.createFileService(storageAccountName, storageAccountKey);

return createFileShare(client, fileShareName)
.then(() => {
return createDirectory(client, fileShareName, DIRECTORY_NAME);
})
.then(() => {
return createFile(client, fileShareName, DIRECTORY_NAME, path.basename(localFileName), localFileName);
})
.catch((err) => { throw err; });
}

function createFileShare(client: FileService, fileShareName: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
client.createShareIfNotExists(fileShareName, (err, result, response) => {
if (err) {
reject(err);
} else {
resolve();
}
});
})
}

function createDirectory(client: FileService, fileShareName: string, dirname: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
client.createDirectoryIfNotExists(fileShareName, dirname, (err, result, response) => {
if (err) {
reject(err);
} else {
resolve();
}
})
})
}

function createFile(client: FileService, fileShare: string, dirName: string, src: string, dest: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
client.createFileFromLocalFile(fileShare, dirName, src, dest, (err, result, response) => {
if (err) {
reject(err);
} else {
resolve();
}
})
})
}

export function getCloudShellPlaybookPath(fileShareName: string, playbook: string): string {
return './clouddrive/' + DIRECTORY_NAME + '/' + path.basename(playbook);
}
28 changes: 26 additions & 2 deletions src/cloudConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { window, commands, MessageItem, OutputChannel, Terminal, env } from 'vscode';
import { AzureAccount, AzureSession } from './azure-account.api';
import { getUserSettings, provisionConsole, Errors, resetConsole, delay, runInTerminal } from './cloudConsoleLauncher';
import { getUserSettings, provisionConsole, Errors, resetConsole, delay, runInTerminal, getStorageAccountKey } from './cloudConsoleLauncher';
import * as nls from 'vscode-nls';
import * as path from 'path';
import * as opn from 'opn';
Expand Down Expand Up @@ -81,6 +81,30 @@ export async function openCloudConsole(api: AzureAccount, os: OS, files, outputC
return requiresSetUp();
}

// get storage account from user settings
const storageProfile = result.userSettings.storageProfile;
const storageAccountSettings =
storageProfile.storageAccountResourceId.substr(1,
storageProfile.storageAccountResourceId.length).split("/");
const storageAccount = {
subscriptionId: storageAccountSettings[1],
resourceGroup: storageAccountSettings[3],
provider: storageAccountSettings[5],
storageAccountName: storageAccountSettings[7],
};

const fileShareName = result.userSettings.storageProfile.fileShareName;

// Getting the storage account key
let storageAccountKey: string;
await getStorageAccountKey(
storageAccount.resourceGroup,
storageAccount.subscriptionId,
result.token.accessToken,
storageAccount.storageAccountName).then((keys) => {
storageAccountKey = keys.body.keys[0].value;
});

let consoleUri: string;
const armEndpoint = result.token.session.environment.resourceManagerEndpointUrl;
const inProgress = delayed(() => window.showInformationMessage(localize('azure-account.provisioningInProgress', "Provisioning {0} in Cloud Shell may take a few seconds.", os.shellName)), 2000);
Expand Down Expand Up @@ -148,7 +172,7 @@ export async function openCloudConsole(api: AzureAccount, os: OS, files, outputC

progress.cancel();
terminal.show();
return terminal;
return [terminal, storageAccount.storageAccountName, storageAccountKey, fileShareName, storageAccount.resourceGroup];
})().catch(err => {
progress.cancel();
TelemetryClient.sendEvent('cloudshell', { 'status': CloudShellStatus.Failed, 'error': CloudShellErrors.ProvisionFailed, 'detailerror': err });
Expand Down
18 changes: 18 additions & 0 deletions src/cloudConsoleLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ async function createTerminal(accessToken: string, armEndpoint: string, userSett
});
}

export async function getStorageAccountKey(
resourceGroup: string,
subscriptionId: string,
accessToken: string,
storageAccountName: string) {
return request({
uri: `https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Storage/storageAccounts/${storageAccountName}/listKeys?api-version=2017-06-01`,
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${accessToken}`,
},
simple: false,
resolveWithFullResponse: true,
json: true,
});
}

export async function resetConsole(accessToken: string, armEndpoint: string) {
const response = await request({
uri: getConsoleUri(armEndpoint),
Expand Down
Loading

0 comments on commit c17bbcb

Please sign in to comment.