Skip to content

Commit

Permalink
Added retry run (#31)
Browse files Browse the repository at this point in the history
* wip1

* added retry to machine status

* fix to the parser

* fixing the  index.js

* fixed tests

* pushing the new index.ts
  • Loading branch information
cjlapao authored May 17, 2024
1 parent 5e0de61 commit 79d119b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
28 changes: 28 additions & 0 deletions __tests__/imagehost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ describe('ImageHost', () => {
imageHost = new ImageHost()
})

it('should parse the image URL correctly with strange password', () => {
const imageUrl = 'catalog://root:te@s:t@localhost:55670/arm/build agent template/latest'
imageHost.parse(imageUrl)

expect(imageHost.schema).toBe('catalog')
expect(imageHost.username).toBe('root')
expect(imageHost.password).toBe('te@s:t')
expect(imageHost.host).toBe('localhost')
expect(imageHost.port).toBe('55670')
expect(imageHost.catalogId).toBe('build agent template')
expect(imageHost.architecture).toBe('arm')
expect(imageHost.version).toBe('latest')
})

it('should parse the image URL correctly with strong password', () => {
const imageUrl = 'catalog://root:te@st@localhost:55670/arm/build agent template/latest'
imageHost.parse(imageUrl)

expect(imageHost.schema).toBe('catalog')
expect(imageHost.username).toBe('root')
expect(imageHost.password).toBe('te@st')
expect(imageHost.host).toBe('localhost')
expect(imageHost.port).toBe('55670')
expect(imageHost.catalogId).toBe('build agent template')
expect(imageHost.architecture).toBe('arm')
expect(imageHost.version).toBe('latest')
})

it('should parse the image URL correctly', () => {
const imageUrl = 'catalog://root:test@localhost:55670/arm/build agent template/latest'
imageHost.parse(imageUrl)
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

25 changes: 12 additions & 13 deletions src/image_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ export class ImageHost {
this.schema = schemaParts[0]
imageUrl = schemaParts[1]
}
const userParts = imageUrl.split('@')
if (userParts.length === 1) {
this.host = userParts[0]
imageUrl = userParts[0]
} else {
const user = userParts[0]
const usernameParts = user.split(':')
if (usernameParts.length === 1) {
this.username = usernameParts[0]
} else if (usernameParts.length === 2) {
this.username = usernameParts[0]
this.password = usernameParts[1]

const lastAtSignIndex = imageUrl.lastIndexOf('@')
if (lastAtSignIndex !== -1) {
const user = imageUrl.slice(0, lastAtSignIndex)
const firstIndexOfColon = user.indexOf(':')
if (firstIndexOfColon === -1) {
this.username = user
} else {
this.username = user.slice(0, firstIndexOfColon)
this.password = user.slice(firstIndexOfColon + 1, user.length)
}
imageUrl = userParts[1]

imageUrl = imageUrl.slice(lastAtSignIndex + 1, imageUrl.length)
}

imageUrl.endsWith('/') ? (imageUrl = imageUrl.slice(0, -1)) : imageUrl
Expand Down
2 changes: 1 addition & 1 deletion src/use_cases/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function CloneUseCase(telemetry: Telemetry, client: DevOps): Promis
core.setOutput('vm_name', cloneRequest.clone_name)

const startAfterCreate = core.getInput('start_after_op')
if (startAfterCreate === 'true') {
if (startAfterCreate === 'true' && response.status !== 'running') {
core.info(`Starting virtual machine ${vmId}`)
await client.setMachineAction(vmId, 'start')
await new Promise(resolve => setTimeout(resolve, 3000))
Expand Down
29 changes: 24 additions & 5 deletions src/use_cases/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,31 @@ export async function RunUseCase(telemetry: Telemetry, client: DevOps): Promise<
const lines = command.split('\n')

core.info(`Checking the machine ${machine_name} status`)
const machine = await client.getMachine(machine_name)
let machine = await client.getMachine(machine_name)
core.debug(`Machine ${machine_name} status: ${JSON.stringify(machine)}`)
if (machine.State !== 'running') {
core.setFailed(
`Error executing command on virtual machine ${machine_name}: the current status is not running but instead ${machine.State}`
)
return false
for (let i = 0; i < 20; i++) {
if (i > 0) {
core.info(`Trying to start ${machine_name} [${i}/20]`)
} else {
core.info(`Trying to start ${machine_name}`)
}

await client.setMachineAction(machine_name, 'start')
machine = await client.getMachine(machine_name)
if (machine.State === 'running') {
break
}
core.info(`Machine ${machine_name} is stated, waiting 1s, old status: ${machine.State}`)
await new Promise(resolve => setTimeout(resolve, 1000))
}

if (machine.State !== 'running') {
core.setFailed(
`Error executing command on virtual machine ${machine_name}: the current status is not running but instead ${machine.State}`
)
return false
}
}

// waiting for machine to be ready
Expand Down

0 comments on commit 79d119b

Please sign in to comment.