Skip to content

Commit

Permalink
Merge pull request #261 from avixFF/add-parameters
Browse files Browse the repository at this point in the history
Add parameters
  • Loading branch information
nikolaymatrosov authored Nov 15, 2022
2 parents bb1b1ac + 947ec37 commit 6b80f19
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ inputs:
vm-subnet-id:
required: true
description: 'The subnet ID where the VM will be created'
vm-public-ip:
required: false
description: 'Public IP address'
vm-platform-id:
required: false
description: 'Compute platform Id'
Expand All @@ -54,6 +57,15 @@ inputs:
docker-compose-path:
required: true
description: 'Path to the `docker-compose.yaml` file inside repo.'
outputs:
instance-id:
description: 'Instance ID'
disk-id:
description: 'Boot disk ID that was created for the instance'
public-ip:
description: 'Public IP address that was assigned to the instance'
created:
description: 'A flag that indicates whether instance was created or updated'
branding:
color: blue
icon: upload-cloud
Expand Down
23 changes: 23 additions & 0 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GetImageLatestByFamilyRequest,
ImageServiceService,
} from '@nikolay.matrosov/yc-ts-sdk/lib/generated/yandex/cloud/compute/v1/image_service';
import {IpVersion} from '@nikolay.matrosov/yc-ts-sdk/lib/generated/yandex/cloud/compute/v1/instance';
import {IpVersion, Instance} from '@nikolay.matrosov/yc-ts-sdk/lib/generated/yandex/cloud/compute/v1/instance';
import {
AttachedDiskSpec_Mode,
CreateInstanceRequest,
Expand Down Expand Up @@ -86,6 +86,7 @@ interface VmParams {
userDataPath: string;
dockerComposePath: string;
subnetId: string;
ipAddress: string;
serviceAccountId: string;
serviceAccountName: string | undefined;
diskType: string;
Expand All @@ -104,6 +105,24 @@ function prepareConfig(filePath: string): string {
return Mustache.render(content, {env: {...process.env}}, {}, {escape: x => x});
}

function getInstanceFromOperation(op: Operation): Instance | undefined {
const v = op.response?.value;
if (v !== undefined) {
return Instance.decode(v);
}
}

function setOutputs(op: Operation): void {
const instance = getInstanceFromOperation(op);

core.setOutput('instance-id', instance?.id);
core.setOutput('disk-id', instance?.bootDisk?.diskId);

if (instance?.networkInterfaces && instance?.networkInterfaces.length > 0) {
core.setOutput('public-ip', instance?.networkInterfaces[0].primaryV4Address?.oneToOneNat?.address);
}
}

async function createVm(
session: Session,
instanceService: Client<typeof InstanceServiceService, {}>,
Expand All @@ -115,6 +134,8 @@ async function createVm(

core.startGroup('Create new VM');

core.setOutput('created', 'true');

let op = await instanceService.create(
CreateInstanceRequest.fromPartial({
folderId: vmParams.folderId,
Expand Down Expand Up @@ -143,6 +164,7 @@ async function createVm(
subnetId: vmParams.subnetId,
primaryV4AddressSpec: {
oneToOneNatSpec: {
address: vmParams.ipAddress,
ipVersion: IpVersion.IPV4,
},
},
Expand All @@ -153,6 +175,7 @@ async function createVm(
);
op = await completion(op, session);
handleOperationError(op);
setOutputs(op);
core.endGroup();
}

Expand All @@ -164,6 +187,8 @@ async function updateMetadata(
): Promise<Operation> {
core.startGroup('Update metadata');

core.setOutput('created', 'false');

let op = await instanceService.updateMetadata(
UpdateInstanceMetadataRequest.fromPartial({
instanceId,
Expand All @@ -175,6 +200,7 @@ async function updateMetadata(
);
op = await completion(op, session);
handleOperationError(op);
setOutputs(op);
core.endGroup();
return op;
}
Expand All @@ -197,6 +223,7 @@ function parseVmInputs(): VmParams {

const zoneId: string = core.getInput('vm-zone-id') || 'ru-central1-a';
const subnetId: string = core.getInput('vm-subnet-id', {required: true});
const ipAddress: string = core.getInput('vm-public-ip');
const platformId: string = core.getInput('vm-platform-id') || 'standard-v3';
const cores: number = parseInt(core.getInput('vm-cores') || '2', 10);
const memory: number = parseMemory(core.getInput('vm-memory') || '1Gb');
Expand All @@ -209,6 +236,7 @@ function parseVmInputs(): VmParams {
diskType,
diskSize,
subnetId,
ipAddress,
zoneId,
platformId,
folderId,
Expand Down

0 comments on commit 6b80f19

Please # to comment.