|
| 1 | +import { Gitpod } from '../src/client'; |
| 2 | +import { findMostUsedEnvironmentClass, EnvironmentState } from '../src/lib/environment'; |
| 3 | +import { EnvironmentSpec } from '../src/resources/environments/environments'; |
| 4 | +import { verifyContextUrl } from './scm-auth'; |
| 5 | +import { generateKeyPairSync } from 'crypto'; |
| 6 | +import { Client, SFTPWrapper } from 'ssh2'; |
| 7 | +import { withCleanup } from './cleanup'; |
| 8 | +import * as sshpk from 'sshpk'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Examples: |
| 12 | + * - yarn ts-node examples/fs-access.ts |
| 13 | + * - yarn ts-node examples/fs-access.ts https://github.com/gitpod-io/empty |
| 14 | + */ |
| 15 | +async function main() { |
| 16 | + const contextUrl = process.argv[2]; |
| 17 | + |
| 18 | + await withCleanup(async (disposables) => { |
| 19 | + const client = new Gitpod({ |
| 20 | + logLevel: 'info', |
| 21 | + }); |
| 22 | + |
| 23 | + const envClass = await findMostUsedEnvironmentClass(client); |
| 24 | + if (!envClass) { |
| 25 | + console.error('Error: No environment class found. Please create one first.'); |
| 26 | + process.exit(1); |
| 27 | + } |
| 28 | + console.log(`Found environment class: ${envClass.displayName} (${envClass.description})`); |
| 29 | + |
| 30 | + console.log('Generating SSH key pair'); |
| 31 | + const { publicKey: pemPublicKey, privateKey: pemPrivateKey } = generateKeyPairSync('rsa', { |
| 32 | + modulusLength: 2048, |
| 33 | + publicKeyEncoding: { |
| 34 | + type: 'spki', |
| 35 | + format: 'pem', |
| 36 | + }, |
| 37 | + privateKeyEncoding: { |
| 38 | + type: 'pkcs8', |
| 39 | + format: 'pem', |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + // Convert PEM keys to OpenSSH format |
| 44 | + const keyObject = sshpk.parseKey(pemPublicKey, 'pem'); |
| 45 | + const publicKey = keyObject.toString('ssh'); |
| 46 | + |
| 47 | + const privateKeyObject = sshpk.parsePrivateKey(pemPrivateKey, 'pem'); |
| 48 | + const privateKey = privateKeyObject.toString('ssh'); |
| 49 | + |
| 50 | + console.log('Creating environment with SSH access'); |
| 51 | + const keyId = 'fs-access-example'; |
| 52 | + const spec: EnvironmentSpec = { |
| 53 | + desiredPhase: 'ENVIRONMENT_PHASE_RUNNING', |
| 54 | + machine: { class: envClass.id }, |
| 55 | + sshPublicKeys: [ |
| 56 | + { |
| 57 | + id: keyId, |
| 58 | + value: publicKey, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }; |
| 62 | + |
| 63 | + if (contextUrl) { |
| 64 | + await verifyContextUrl(client, contextUrl, envClass.runnerId); |
| 65 | + spec.content = { |
| 66 | + initializer: { |
| 67 | + specs: [ |
| 68 | + { |
| 69 | + contextUrl: { |
| 70 | + url: contextUrl, |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + console.log('Creating environment'); |
| 79 | + const { environment } = await client.environments.create({ spec }); |
| 80 | + disposables.add(() => client.environments.delete({ environmentId: environment.id })); |
| 81 | + |
| 82 | + const env = new EnvironmentState(client, environment.id); |
| 83 | + disposables.add(() => env.close()); |
| 84 | + |
| 85 | + console.log('Waiting for environment to be running'); |
| 86 | + await env.waitUntilRunning(); |
| 87 | + |
| 88 | + console.log('Waiting for SSH key to be applied'); |
| 89 | + await env.waitForSshKeyApplied(keyId, publicKey); |
| 90 | + |
| 91 | + console.log('Waiting for SSH URL'); |
| 92 | + const sshUrl = await env.waitForSshUrl(); |
| 93 | + |
| 94 | + console.log(`Setting up SSH connection to ${sshUrl}`); |
| 95 | + // Parse ssh://username@host:port format |
| 96 | + const urlParts = sshUrl.split('://')[1]; |
| 97 | + if (!urlParts) { |
| 98 | + throw new Error('Invalid SSH URL format'); |
| 99 | + } |
| 100 | + |
| 101 | + const [username, rest] = urlParts.split('@'); |
| 102 | + if (!username || !rest) { |
| 103 | + throw new Error('Invalid SSH URL format: missing username or host'); |
| 104 | + } |
| 105 | + |
| 106 | + const [host, portStr] = rest.split(':'); |
| 107 | + if (!host || !portStr) { |
| 108 | + throw new Error('Invalid SSH URL format: missing host or port'); |
| 109 | + } |
| 110 | + |
| 111 | + const port = parseInt(portStr, 10); |
| 112 | + if (isNaN(port)) { |
| 113 | + throw new Error('Invalid SSH URL format: invalid port number'); |
| 114 | + } |
| 115 | + |
| 116 | + const ssh = new Client(); |
| 117 | + disposables.add(() => ssh.end()); |
| 118 | + |
| 119 | + await new Promise<void>((resolve, reject) => { |
| 120 | + ssh.on('ready', resolve); |
| 121 | + ssh.on('error', reject); |
| 122 | + |
| 123 | + ssh.connect({ |
| 124 | + host, |
| 125 | + port, |
| 126 | + username, |
| 127 | + privateKey, |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + console.log('Creating SFTP client'); |
| 132 | + const sftp = await new Promise<SFTPWrapper>((resolve, reject) => { |
| 133 | + ssh.sftp((err, sftp) => { |
| 134 | + if (err) reject(err); |
| 135 | + else resolve(sftp); |
| 136 | + }); |
| 137 | + }); |
| 138 | + disposables.add(() => sftp.end()); |
| 139 | + |
| 140 | + console.log('Writing test file'); |
| 141 | + const testContent = 'Hello from Gitpod TypeScript SDK!'; |
| 142 | + await new Promise<void>((resolve, reject) => { |
| 143 | + sftp.writeFile('test.txt', Buffer.from(testContent), (err) => { |
| 144 | + if (err) reject(err); |
| 145 | + else resolve(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + const content = await new Promise<string>((resolve, reject) => { |
| 150 | + sftp.readFile('test.txt', (err, data) => { |
| 151 | + if (err) reject(err); |
| 152 | + else resolve(data.toString()); |
| 153 | + }); |
| 154 | + }); |
| 155 | + console.log(`File content: ${content}`); |
| 156 | + }); |
| 157 | +} |
| 158 | + |
| 159 | +main().catch((error) => { |
| 160 | + console.error('Error:', error); |
| 161 | + process.exit(1); |
| 162 | +}); |
0 commit comments