Skip to content

Commit d544e9d

Browse files
fix scheduling hack
1 parent 475d499 commit d544e9d

File tree

6 files changed

+65
-85
lines changed

6 files changed

+65
-85
lines changed

src/Sandbox.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,37 @@ export class Sandbox {
183183
hostToken: customSession?.hostToken,
184184
});
185185

186+
if (customSession?.git) {
187+
const netrc = await session.commands.runBackground([
188+
`mkdir -p ~/private`,
189+
`cat > ~/private/.netrc <<EOF
190+
machine ${customSession.git.provider}
191+
login ${customSession.git.username || "x-access-token"}
192+
password ${customSession.git.accessToken}
193+
EOF`,
194+
`chmod 600 ~/private/.netrc`,
195+
`cd ~`,
196+
`ln -sfn private/.netrc .netrc`,
197+
]);
198+
netrc.onOutput(console.log);
199+
console.log(await netrc.open());
200+
await netrc.waitUntilComplete();
201+
202+
const config = await session.commands.runBackground([
203+
`cat > ~/private/.gitconfig <<EOF
204+
[user]
205+
name = ${customSession.git.name || customSession.id}
206+
email = ${customSession.git.email}
207+
EOF`,
208+
`chmod 600 ~/private/.gitconfig`,
209+
`cd "~"`,
210+
`ln -sfn private/.gitconfig .gitconfig`,
211+
]);
212+
config.onOutput(console.log);
213+
console.log(await config.open());
214+
await config.waitUntilComplete();
215+
}
216+
186217
return session;
187218
}
188219

src/Sandboxes.ts

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import {
1616
} from "./utils/api";
1717

1818
import {
19-
CreateSandboxGitSourceOpts,
2019
CreateSandboxOpts,
21-
CreateSandboxTemplateSourceOpts,
2220
PaginationOpts,
2321
SandboxInfo,
2422
SandboxListOpts,
@@ -67,54 +65,13 @@ export class Sandboxes {
6765

6866
constructor(private apiClient: Client) {}
6967

70-
private async createGitSandbox(
71-
opts: CreateSandboxGitSourceOpts & StartSandboxOpts
72-
) {
73-
const sandbox = await this.createTemplateSandbox({
74-
...opts,
75-
source: "template",
76-
id: opts.templateId || this.defaultTemplateId,
77-
});
78-
79-
// We do not want users to pass gitAccessToken on global user, because it
80-
// can be read by other users
81-
const sessionCreateOptions: SessionCreateOptions = {
82-
id: "clone-repo-user",
83-
permission: "write",
84-
};
85-
if (opts.config) {
86-
sessionCreateOptions.git = {
87-
accessToken: opts.config.accessToken,
88-
email: opts.config.email,
89-
name: opts.config.name,
90-
};
91-
}
92-
93-
const session = await sandbox.connect(sessionCreateOptions);
94-
95-
await session.commands.run([
96-
"rm -rf .git",
97-
"git init",
98-
`git remote add origin ${opts.url}`,
99-
"git fetch origin",
100-
`git checkout -b ${opts.branch}`,
101-
`git reset --hard origin/${opts.branch}`,
102-
]);
103-
104-
await opts.setup?.(session);
105-
106-
session.disconnect();
107-
108-
return sandbox;
109-
}
110-
11168
private async createTemplateSandbox(
112-
opts: CreateSandboxTemplateSourceOpts & StartSandboxOpts
69+
opts?: CreateSandboxOpts & StartSandboxOpts
11370
) {
114-
const templateId = opts.id || this.defaultTemplateId;
115-
const privacy = opts.privacy || "unlisted";
116-
const tags = opts.tags || ["sdk"];
117-
const path = opts.path || "/SDK";
71+
const templateId = opts?.id || this.defaultTemplateId;
72+
const privacy = opts?.privacy || "unlisted";
73+
const tags = opts?.tags || ["sdk"];
74+
const path = opts?.path || "/SDK";
11875

11976
// Always add the "sdk" tag to the sandbox, this is used to identify sandboxes created by the SDK.
12077
const tagsWithSdk = tags.includes("sdk") ? tags : [...tags, "sdk"];
@@ -175,7 +132,6 @@ export class Sandboxes {
175132
*/
176133
public async fork(sandboxId: string, opts?: StartSandboxOpts) {
177134
return this.create({
178-
source: "template",
179135
id: sandboxId,
180136
...opts,
181137
});
@@ -210,22 +166,10 @@ export class Sandboxes {
210166
}
211167

212168
/**
213-
* Create a sandbox from a template or git repository. By default we will create a sandbox from the default template.
169+
* Create a sandbox from a template. By default we will create a sandbox from the default universal template.
214170
*/
215-
async create(
216-
opts: CreateSandboxOpts & StartSandboxOpts = { source: "template" }
217-
): Promise<Sandbox> {
218-
switch (opts.source) {
219-
case "git": {
220-
return this.createGitSandbox(opts);
221-
}
222-
case "template": {
223-
return this.createTemplateSandbox(opts);
224-
}
225-
default: {
226-
throw new Error("Invalid source");
227-
}
228-
}
171+
async create(opts?: CreateSandboxOpts & StartSandboxOpts): Promise<Sandbox> {
172+
return this.createTemplateSandbox(opts);
229173
}
230174

231175
/**

src/Session/git.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ export class Git {
6060
* Push all changes to git
6161
*/
6262
async push() {
63-
await this.commands.run(["git push"]);
63+
await this.commands.run(["git push -u origin HEAD"]);
64+
}
65+
66+
async clone(opts: { url: string; branch: string }) {
67+
await this.commands.run([
68+
"rm -rf .git",
69+
"git init",
70+
`git remote add origin ${opts.url}`,
71+
"git fetch origin",
72+
`git checkout -b ${opts.branch}`,
73+
`git reset --hard origin/${opts.branch}`,
74+
]);
6475
}
6576
}

src/bin/commands/build.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ export const buildCommand: yargs.CommandModule<
148148
updateSpinnerMessage(index, "Starting sandbox...", sandboxId)
149149
);
150150

151+
// This is a hack, we need to tell the global scheduler that the VM is running
152+
// in a different cluster than the one it'd like to default to.
153+
const baseUrl = apiClient
154+
.getConfig()
155+
.baseUrl?.replace("api", "global-scheduler");
156+
157+
await fetch(
158+
`${baseUrl}/api/v1/cluster/${sandboxId}?preferredManager=${cluster}`
159+
).then((res) => res.json());
160+
151161
const startResponse = await startVm(clusterApiClient, sandboxId, {
152162
vmTier: VMTier.fromName("Micro"),
153163
});

src/types.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ export interface SessionCreateOptions {
185185
id: string;
186186
permission?: "read" | "write";
187187
git?: {
188+
provider: string;
189+
username?: string;
188190
accessToken?: string;
189191
email: string;
190192
name?: string;
@@ -201,32 +203,14 @@ export type SandboxSession = {
201203
env?: Record<string, string>;
202204
};
203205

204-
export type CreateSandboxTemplateSourceOpts = CreateSandboxBaseOpts & {
205-
source: "template";
206+
export type CreateSandboxOpts = CreateSandboxBaseOpts & {
206207
/**
207208
* What template to fork from, this is the id of another sandbox. Defaults to our
208209
* [universal template](https://codesandbox.io/s/github/codesandbox/sandbox-templates/tree/main/universal).
209210
*/
210211
id?: string;
211212
};
212213

213-
export type CreateSandboxGitSourceOpts = CreateSandboxBaseOpts & {
214-
source: "git";
215-
url: string;
216-
branch: string;
217-
templateId?: string;
218-
config?: {
219-
accessToken: string;
220-
email: string;
221-
name?: string;
222-
};
223-
setup?: (session: Session) => Promise<void>;
224-
};
225-
226-
export type CreateSandboxOpts =
227-
| CreateSandboxTemplateSourceOpts
228-
| CreateSandboxGitSourceOpts;
229-
230214
export type SandboxOpts = {
231215
id: string;
232216
bootupType: PitcherManagerResponse["bootupType"];

src/utils/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function getDefaultTemplateId(apiClient: Client): string {
5656
return "7ngcrf";
5757
}
5858

59-
return "pt_CW2BcBSPRoGb3AS6Fv7oXs";
59+
return "pt_TRkPA6ypYZ7cRLQYgCmRHs";
6060
}
6161

6262
export function handleResponse<D, E>(

0 commit comments

Comments
 (0)