Skip to content

Commit

Permalink
Updated output variable to use managed image location (#9137)
Browse files Browse the repository at this point in the history
* Updated output varialbe to use managed image location as opposed to the image name

* add additional output variable

* update metadata to match patch version and remove debug code

* spacing issues

* wording and function name updates
  • Loading branch information
grayjeremy authored and hiyadav committed Jan 14, 2019
1 parent 5b51aad commit ccdc046
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"loc.input.help.skipTempFileCleanupDuringVMDeprovision": "During deprovisioning of VM, skip clean-up of temporary files uploaded to VM. Refer [here](https://www.packer.io/docs/builders/azure.html#skip_clean)",
"loc.input.label.imageUri": "Image URL or Name",
"loc.input.help.imageUri": "Provide a name for the output variable which will store generated machine image VHD url for un-managed VM image or image name for managed VM image.",
"loc.input.label.imageId": "Azure Resource Id",
"loc.input.help.imageId": "Provide a name for the output variable which will store the azure resource id for the newly created image. This is for managed images only.",
"loc.messages.OSTypeNotSupported": "This OS type is not supported for image creation: %s. Please use either windows or linux.",
"loc.messages.CopyTemplateToTempFailed": "Could not copy built-in template from source %s to temp location %s",
"loc.messages.TaskNotFound": "Task.json file could not be found: %s",
Expand All @@ -59,6 +61,7 @@
"loc.messages.ExecutingPackerBuild": "Running packer build command. Waiting for it to finish...",
"loc.messages.PackerBuildCompleted": "packer build completed.",
"loc.messages.ImageURIOutputVariableNotFound": "Could not get vhd image URI (unmanaged image) from packer execution. Output variable will not be set.",
"loc.messages.ImageIDOutputVariableNotFound": "Could not get managed image id from packer execution. Output variable will not be set. Note: This variable should not be used with un-managed VM images.",
"loc.messages.ManagedImageNameOutputVariableNotFound": "Could not get managed image name from packer execution. Output variable will not be set.",
"loc.messages.CustumTemplateOutputVariableNotFound": "Could not get managed image name or vhd image URI (unmanaged image) from packer execution. Output variable will not be set.",
"loc.messages.BuiltInTemplateNotFoundErrorMessagePathName": "Built-in template for OS type: %s ",
Expand Down
2 changes: 2 additions & 0 deletions Tasks/PackerBuildV1/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export var PackerLogTokenStorageLocation = "StorageAccountLocation";
export var PackerLogTokenManagedResourceGroupName = "ManagedImageResourceGroupName";
export var PackerLogTokenManagedImageName = "ManagedImageName";
export var PackerLogTokenManagedImageLocation = "ManagedImageLocation";
export var PackerLogTokenManagedImageId = "ManagedImageId";

export var OutputVariableImageUri = "imageUri";
export var OutputVariableImageId = "imageId";

export var CurrentSupportedPackerVersionString = "1.2.4";
export var PackerDownloadUrlFormat = "https://releases.hashicorp.com/packer/%s/packer_%s_%s.zip"
Expand Down
37 changes: 31 additions & 6 deletions Tasks/PackerBuildV1/src/operations/packerBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export async function run(packerHost: packerHost): Promise<any> {
var taskParameters = packerHost.getTaskParameters();
var outputVariablesParser: definitions.IOutputParser;
if (taskParameters.templateType === constants.TemplateTypeCustom) {
outputVariablesParser = new OutputVariablesParser([constants.PackerLogTokenImageUri, constants.PackerLogTokenStorageLocation, constants.PackerLogTokenManagedImageName, constants.PackerLogTokenManagedResourceGroupName, constants.PackerLogTokenManagedImageLocation]);
outputVariablesParser = new OutputVariablesParser([constants.PackerLogTokenImageUri, constants.PackerLogTokenStorageLocation, constants.PackerLogTokenManagedImageName, constants.PackerLogTokenManagedResourceGroupName, constants.PackerLogTokenManagedImageLocation, constants.PackerLogTokenManagedImageId]);
}
else {
if (!taskParameters.isManagedImage) {
outputVariablesParser = new OutputVariablesParser([constants.PackerLogTokenImageUri, constants.PackerLogTokenStorageLocation]);
} else {
outputVariablesParser = new OutputVariablesParser([constants.PackerLogTokenManagedImageName, constants.PackerLogTokenManagedResourceGroupName, constants.PackerLogTokenManagedImageLocation]);
outputVariablesParser = new OutputVariablesParser([constants.PackerLogTokenManagedImageName, constants.PackerLogTokenManagedResourceGroupName, constants.PackerLogTokenManagedImageLocation, constants.PackerLogTokenManagedImageId]);
}
}

Expand All @@ -50,20 +50,32 @@ export async function run(packerHost: packerHost): Promise<any> {
function setOutputVariables(packerHost: packerHost, outputs: Map<string, string>): void {
var taskParameters = packerHost.getTaskParameters();
var imageUri;
var imageId;
var managedImageName;

if (!utils.IsNullOrEmpty(taskParameters.imageId) && !(taskParameters.templateType == constants.TemplateTypeBuiltin && !taskParameters.isManagedImage)) {
imageId = getValueFromOutputs(constants.PackerLogTokenManagedImageId, outputs);

if (!utils.IsNullOrEmpty(imageId)) {
tl.debug("Setting image Id variable which contains the managed image Id to: " + imageId);
tl.setVariable(taskParameters.imageId, imageId);
} else {
throw tl.loc("ImageIDOutputVariableNotFound");
}
}

if (!utils.IsNullOrEmpty(taskParameters.imageUri)) {
if (taskParameters.templateType === constants.TemplateTypeBuiltin) {
if (!taskParameters.isManagedImage) {
imageUri = outputs.get(constants.PackerLogTokenImageUri);
imageUri = getValueFromOutputs(constants.PackerLogTokenImageUri, outputs);
if (!utils.IsNullOrEmpty(imageUri)) {
tl.debug("Setting image URI variable to: " + imageUri);
tl.setVariable(taskParameters.imageUri, imageUri);
} else {
throw tl.loc("ImageURIOutputVariableNotFound");
}
} else {
imageUri = outputs.get(constants.PackerLogTokenManagedImageName);
imageUri = getValueFromOutputs(constants.PackerLogTokenManagedImageName, outputs);
if (!utils.IsNullOrEmpty(imageUri)) {
tl.debug("Setting image URI variable which contains the managed image name to: " + imageUri);
tl.setVariable(taskParameters.imageUri, imageUri);
Expand All @@ -72,8 +84,8 @@ function setOutputVariables(packerHost: packerHost, outputs: Map<string, string>
}
}
} else {
imageUri = outputs.get(constants.PackerLogTokenImageUri);
managedImageName = outputs.get(constants.PackerLogTokenManagedImageName);
imageUri = getValueFromOutputs(constants.PackerLogTokenImageUri, outputs);
managedImageName = getValueFromOutputs(constants.PackerLogTokenManagedImageName, outputs);

if (!utils.IsNullOrEmpty(managedImageName)) {
tl.debug("Setting image URI variable which contains the managed image name to: " + managedImageName);
Expand All @@ -88,4 +100,17 @@ function setOutputVariables(packerHost: packerHost, outputs: Map<string, string>
}
}
}
}

function getValueFromOutputs (key: string, outputs: Map<string, string>): string {
var value;
try {
value = outputs.get(key);
}
catch (ex)
{
// do not throw as this is try get
}

return value;
}
2 changes: 2 additions & 0 deletions Tasks/PackerBuildV1/src/taskParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class TaskParameters {
public skipTempFileCleanupDuringVMDeprovision: boolean = true;

public imageUri: string;
public imageId: string;

public graphCredentials: msRestAzure.ApplicationTokenCredentials;

Expand Down Expand Up @@ -86,6 +87,7 @@ export default class TaskParameters {
this.additionalBuilderParameters = JSON.parse(tl.getInput("additionalBuilderParameters"));
this.skipTempFileCleanupDuringVMDeprovision = tl.getBoolInput("skipTempFileCleanupDuringVMDeprovision", false);
this.imageUri = tl.getInput(constants.OutputVariableImageUri, false);
this.imageId = tl.getInput(constants.OutputVariableImageId, false);
}
catch (error) {
throw (tl.loc("TaskParametersConstructorFailed", error));
Expand Down
10 changes: 10 additions & 0 deletions Tasks/PackerBuildV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@
"defaultValue": "",
"helpMarkDown": "Provide a name for the output variable which will store generated machine image VHD url for un-managed VM image or image name for managed VM image.",
"groupName": "Output"
},
{
"name": "imageId",
"type": "string",
"label": "Azure Resource Id",
"required": false,
"defaultValue": "",
"helpMarkDown": "Provide a name for the output variable which will store the azure resource id for the newly created image. This is for managed images only.",
"groupName": "Output"
}
],
"dataSourceBindings": [
Expand Down Expand Up @@ -294,6 +303,7 @@
"ExecutingPackerBuild": "Running packer build command. Waiting for it to finish...",
"PackerBuildCompleted": "packer build completed.",
"ImageURIOutputVariableNotFound": "Could not get vhd image URI (unmanaged image) from packer execution. Output variable will not be set.",
"ImageIDOutputVariableNotFound": "Could not get managed image id from packer execution. Output variable will not be set. Note: This variable should not be used with un-managed VM images.",
"ManagedImageNameOutputVariableNotFound": "Could not get managed image name from packer execution. Output variable will not be set.",
"CustumTemplateOutputVariableNotFound": "Could not get managed image name or vhd image URI (unmanaged image) from packer execution. Output variable will not be set.",
"BuiltInTemplateNotFoundErrorMessagePathName": "Built-in template for OS type: %s ",
Expand Down
10 changes: 10 additions & 0 deletions Tasks/PackerBuildV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@
"defaultValue": "",
"helpMarkDown": "ms-resource:loc.input.help.imageUri",
"groupName": "Output"
},
{
"name": "imageId",
"type": "string",
"label": "ms-resource:loc.input.label.imageId",
"required": false,
"defaultValue": "",
"helpMarkDown": "ms-resource:loc.input.help.imageId",
"groupName": "Output"
}
],
"dataSourceBindings": [
Expand Down Expand Up @@ -294,6 +303,7 @@
"ExecutingPackerBuild": "ms-resource:loc.messages.ExecutingPackerBuild",
"PackerBuildCompleted": "ms-resource:loc.messages.PackerBuildCompleted",
"ImageURIOutputVariableNotFound": "ms-resource:loc.messages.ImageURIOutputVariableNotFound",
"ImageIDOutputVariableNotFound": "ms-resource:loc.messages.ImageIDOutputVariableNotFound",
"ManagedImageNameOutputVariableNotFound": "ms-resource:loc.messages.ManagedImageNameOutputVariableNotFound",
"CustumTemplateOutputVariableNotFound": "ms-resource:loc.messages.CustumTemplateOutputVariableNotFound",
"BuiltInTemplateNotFoundErrorMessagePathName": "ms-resource:loc.messages.BuiltInTemplateNotFoundErrorMessagePathName",
Expand Down

0 comments on commit ccdc046

Please sign in to comment.