From 426079628f0c9928d838a75867ad442cf2fc66db Mon Sep 17 00:00:00 2001 From: Roy Razon Date: Sun, 6 Aug 2023 17:00:00 +0300 Subject: [PATCH] fix ls showing other resources fixed #159 --- packages/core/src/commands/ls.ts | 2 +- packages/core/src/driver/driver.ts | 1 + packages/driver-azure/src/driver/index.ts | 43 +++--- packages/driver-gce/src/driver/index.ts | 46 ++++--- packages/driver-kube-pod/src/driver/driver.ts | 127 +++++++++--------- packages/driver-lightsail/src/driver/index.ts | 8 +- 6 files changed, 120 insertions(+), 107 deletions(-) diff --git a/packages/core/src/commands/ls.ts b/packages/core/src/commands/ls.ts index 516ed359..b2dc0c40 100644 --- a/packages/core/src/commands/ls.ts +++ b/packages/core/src/commands/ls.ts @@ -6,6 +6,6 @@ const ls = async ({ }: { machineDriver: MachineDriver log: Logger -}) => machineDriver.listDeletableResources() +}) => machineDriver.listMachines() export default ls diff --git a/packages/core/src/driver/driver.ts b/packages/core/src/driver/driver.ts index 07275230..c6d85781 100644 --- a/packages/core/src/driver/driver.ts +++ b/packages/core/src/driver/driver.ts @@ -40,6 +40,7 @@ export type MachineDriver< stdio: PartialStdioOptions, ) => Promise<{ code: number } | { signal: string }> + listMachines: () => AsyncIterableIterator listDeletableResources: () => AsyncIterableIterator> deleteResources: (wait: boolean, ...resource: Resource[]) => Promise machineStatusCommand: (machine: MachineBase) => Promise diff --git a/packages/driver-azure/src/driver/index.ts b/packages/driver-azure/src/driver/index.ts index 36b8dd4a..fce35864 100644 --- a/packages/driver-azure/src/driver/index.ts +++ b/packages/driver-azure/src/driver/index.ts @@ -92,12 +92,8 @@ const machineFromVm = ( const machineDriver = ( { store, client: cl }: DriverContext, -): MachineDriver => ({ - customizationScripts: CUSTOMIZE_BARE_MACHINE, - friendlyName: 'Microsoft Azure', - getMachine: async ({ envId }) => await cl.getInstance(envId).then(vm => machineFromVm(vm)), - - listDeletableResources: () => asyncMap( +): MachineDriver => { + const listMachines = () => asyncMap( rg => cl.getInstanceByRg(rg.name as string).then(vm => { if (vm) { return machineFromVm(vm) @@ -110,23 +106,32 @@ const machineDriver = ( } }), cl.listResourceGroups() - ), + ) - deleteResources: async (wait, ...resources) => { - await Promise.all(resources.map(({ type, providerId }) => { - if (type === machineResourceType) { - return cl.deleteResourcesResourceGroup(providerId, wait) - } - throw new Error(`Unknown resource type "${type}"`) - })) - }, + return ({ + customizationScripts: CUSTOMIZE_BARE_MACHINE, + friendlyName: 'Microsoft Azure', + getMachine: async ({ envId }) => await cl.getInstance(envId).then(vm => machineFromVm(vm)), + + listMachines, + listDeletableResources: listMachines, + + deleteResources: async (wait, ...resources) => { + await Promise.all(resources.map(({ type, providerId }) => { + if (type === machineResourceType) { + return cl.deleteResourcesResourceGroup(providerId, wait) + } + throw new Error(`Unknown resource type "${type}"`) + })) + }, - resourcePlurals: {}, + resourcePlurals: {}, - ...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }), + ...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }), - machineStatusCommand: async () => machineStatusNodeExporterCommand, -}) + machineStatusCommand: async () => machineStatusNodeExporterCommand, + }) +} const flags = { region: Flags.string({ diff --git a/packages/driver-gce/src/driver/index.ts b/packages/driver-gce/src/driver/index.ts index 147b1c86..9b11fb29 100644 --- a/packages/driver-gce/src/driver/index.ts +++ b/packages/driver-gce/src/driver/index.ts @@ -50,32 +50,34 @@ const machineFromInstance = ( } } -const machineDriver = ( - { store, client }: DriverContext, -): MachineDriver => ({ - friendlyName: 'Google Cloud', - - getMachine: async ({ envId }) => { - const instance = await client.findInstance(envId) - return instance && machineFromInstance(instance) - }, +const machineDriver = ({ store, client }: DriverContext): MachineDriver => { + const listMachines = () => asyncMap(machineFromInstance, client.listInstances()) - listDeletableResources: () => asyncMap(machineFromInstance, client.listInstances()), - deleteResources: async (wait, ...resources) => { - await Promise.all(resources.map(({ type, providerId }) => { - if (type === 'machine') { - return client.deleteInstance(providerId, wait) - } - throw new Error(`Unknown resource type: "${type}"`) - })) - }, + return ({ + friendlyName: 'Google Cloud', - resourcePlurals: {}, + getMachine: async ({ envId }) => { + const instance = await client.findInstance(envId) + return instance && machineFromInstance(instance) + }, - ...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }), + listMachines, + listDeletableResources: listMachines, - machineStatusCommand: async () => machineStatusNodeExporterCommand, -}) + deleteResources: async (wait, ...resources) => { + await Promise.all(resources.map(({ type, providerId }) => { + if (type === 'machine') { + return client.deleteInstance(providerId, wait) + } + throw new Error(`Unknown resource type: "${type}"`) + })) + }, + + resourcePlurals: {}, + ...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }), + machineStatusCommand: async () => machineStatusNodeExporterCommand, + }) +} const flags = { 'project-id': Flags.string({ diff --git a/packages/driver-kube-pod/src/driver/driver.ts b/packages/driver-kube-pod/src/driver/driver.ts index 5b35d693..c4611931 100644 --- a/packages/driver-kube-pod/src/driver/driver.ts +++ b/packages/driver-kube-pod/src/driver/driver.ts @@ -66,72 +66,77 @@ export const machineConnection = async ( const machineDriver = ( { client, log }: DriverContext, -): MachineDriver => ({ - friendlyName: 'Kubernetes single Pod', +): MachineDriver => { + const listMachines = () => asyncMap(machineFromDeployment, client.listProfileDeployments()) - getMachine: async ({ envId }) => { - const deployment = await client.findMostRecentDeployment({ envId, deleted: false }) - return deployment && machineFromDeployment(deployment) - }, + return ({ + friendlyName: 'Kubernetes single Pod', + + getMachine: async ({ envId }) => { + const deployment = await client.findMostRecentDeployment({ envId, deleted: false }) + return deployment && machineFromDeployment(deployment) + }, - listDeletableResources: () => asyncMap(machineFromDeployment, client.listProfileDeployments()), + listMachines, + listDeletableResources: listMachines, + + deleteResources: async (wait, ...resources) => { + await Promise.all(resources.map(({ type, providerId }) => { + if (type === 'machine') { + return client.deleteEnv(providerId, { wait }) + } + throw new Error(`Unknown resource type: "${type}"`) + })) + }, - deleteResources: async (wait, ...resources) => { - await Promise.all(resources.map(({ type, providerId }) => { - if (type === 'machine') { - return client.deleteEnv(providerId, { wait }) + resourcePlurals: {}, + + spawnRemoteCommand: async (machine, command, stdio) => { + const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment) + const { stdin, stdout, stderr } = expandStdioOptions(stdio) + const opts = { + pod: extractName(pod), + container: pod.spec?.containers[0]?.name as string, + command: command.length > 0 ? command : ['sh'], + tty: [stdin, stdout, stderr].every(isTTY), + stdin, + stdout, + stderr, } - throw new Error(`Unknown resource type: "${type}"`) - })) - }, - - resourcePlurals: {}, - - spawnRemoteCommand: async (machine, command, stdio) => { - const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment) - const { stdin, stdout, stderr } = expandStdioOptions(stdio) - const opts = { - pod: extractName(pod), - container: pod.spec?.containers[0]?.name as string, - command: command.length > 0 ? command : ['sh'], - tty: [stdin, stdout, stderr].every(isTTY), - stdin, - stdout, - stderr, - } - return await client.exec(opts) - }, - - connect: machine => machineConnection(client, machine as DeploymentMachine, log), - - machineStatusCommand: async machine => { - const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment) - const apiServiceAddress = await client.apiServiceClusterAddress() - if (!apiServiceAddress) { - log.warn('API service not found for cluster') - return undefined - } - const [apiServiceHost, apiServicePort] = apiServiceAddress - - return ({ - contentType: 'application/vnd.kubectl-top-pod-containers', - recipe: { - type: 'docker', - command: ['top', 'pod', '--containers', '--no-headers', extractName(pod)], - image: 'rancher/kubectl:v1.26.7', - network: 'host', - tty: false, - env: { - KUBERNETES_SERVICE_HOST: apiServiceHost, - KUBERNETES_SERVICE_PORT: apiServicePort.toString(), + return await client.exec(opts) + }, + + connect: machine => machineConnection(client, machine as DeploymentMachine, log), + + machineStatusCommand: async machine => { + const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment) + const apiServiceAddress = await client.apiServiceClusterAddress() + if (!apiServiceAddress) { + log.warn('API service not found for cluster') + return undefined + } + const [apiServiceHost, apiServicePort] = apiServiceAddress + + return ({ + contentType: 'application/vnd.kubectl-top-pod-containers', + recipe: { + type: 'docker', + command: ['top', 'pod', '--containers', '--no-headers', extractName(pod)], + image: 'rancher/kubectl:v1.26.7', + network: 'host', + tty: false, + env: { + KUBERNETES_SERVICE_HOST: apiServiceHost, + KUBERNETES_SERVICE_PORT: apiServicePort.toString(), + }, + bindMounts: [ + '/var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount', + ], }, - bindMounts: [ - '/var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount', - ], - }, - }) - }, -}) + }) + }, + }) +} export const flags = { namespace: Flags.string({ diff --git a/packages/driver-lightsail/src/driver/index.ts b/packages/driver-lightsail/src/driver/index.ts index 61af8ab9..65aafe57 100644 --- a/packages/driver-lightsail/src/driver/index.ts +++ b/packages/driver-lightsail/src/driver/index.ts @@ -48,6 +48,8 @@ const machineDriver = ({ }: DriverContext): MachineDriver => { const keyAlias = region + const listMachines = () => asyncMap(machineFromInstance, client.listInstances()) + return { friendlyName: 'AWS Lightsail', customizationScripts: CUSTOMIZE_BARE_MACHINE, @@ -57,11 +59,9 @@ const machineDriver = ({ return instance && machineFromInstance(instance) }, + listMachines, listDeletableResources: () => { - const machines = asyncMap( - machineFromInstance, - client.listInstances(), - ) + const machines = listMachines() const snapshots = asyncMap( ({ name }) => ({ type: 'snapshot' as ResourceType, providerId: name as string }),