-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
362 lines (305 loc) · 14.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
namespace PaperspaceConsoleTest
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Paperspace;
class Program
{
private const string PaperspaceNetSampleMachineName = "Paperspace.Net Sample Machine";
static async Task Main(string[] args)
{
// Get configuration
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true)
.Build();
var client = new PaperspaceClient(config["PAPERSPACE_API_KEY"]);
var machine = await GetOrCreateMachine(client);
await MachineCycleSample(client, machine);
await ResourceDelegationSample(client, machine);
await DestroyMachine(client, machine);
await ScriptFullLifecycleSample(client);
await NetworksSample(client);
await UsersSample(client);
await JobsSample(client);
Console.WriteLine("All done!!");
}
static async Task<Machine> GetOrCreateMachine(IPaperspaceClient client)
{
// ----
// List all Windows 10 templates
// ----
var templates = await client.Templates.List(new TemplateFilter() { Label = "Windows 10" });
Console.WriteLine("Listing Templates...");
Console.WriteLine(JsonConvert.SerializeObject(templates, Formatting.Indented));
var template = templates.FirstOrDefault(t => t.OS == "Windows 10 (Server 2019) - Licensed");
if (template == null)
{
throw new InvalidOperationException("Unable to locate Windows 10 template.");
}
// ----
// Check Availability for a GPU machine
// ----
Console.WriteLine("Checking GPU Availability...");
var availability = await client.Machines.Availability(Region.EastCoast_NY2, MachineType.GPUPlus);
Console.WriteLine(JsonConvert.SerializeObject(templates, Formatting.Indented));
// ----
// List Machines
// ----
Console.WriteLine("Listing Machines...");
var machines = await client.Machines.List(new MachineFilter()
{
Name = PaperspaceNetSampleMachineName
});
// ----
// If a Paperspace.Net Sample Machine didn't exist, create a new Machine, otherwise start the existing.
// ----
var newMachine = machines.FirstOrDefault(m => m.Name == PaperspaceNetSampleMachineName);
if (newMachine == null)
{
Console.WriteLine("Creating new machine...");
newMachine = await client.Machines.Create(new CreateMachineRequest
{
Region = Region.EastCoast_NY2,
MachineType = MachineType.Air,
Size = 50,
BillingType = BillingType.Hourly,
MachineName = PaperspaceNetSampleMachineName,
TemplateId = template.Id,
});
}
else
{
await client.Machines.Start(newMachine.Id);
}
Console.WriteLine($"Machine with the id of {newMachine.Id} is now {newMachine.State}");
newMachine = await client.Machines.Waitfor(newMachine.Id, MachineState.Ready, 5000, 0, (m) => Console.WriteLine(m.State));
Console.WriteLine($"Machine with the id of {newMachine.Id} is now {newMachine.State}");
return newMachine;
}
/// <summary>
/// Demonstrates cycling a machine: List -> Stop -> Start -> Restart -> Utilization
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
static async Task MachineCycleSample(IPaperspaceClient client, Machine machine)
{
Console.WriteLine("Listing Machines...");
var machines = await client.Machines.List();
Console.WriteLine(JsonConvert.SerializeObject(machines, Formatting.Indented));
// ----
// Stop the machine we created.
// ----
Console.WriteLine($"Stopping {machine.Id}...");
await client.Machines.Stop(machine.Id);
machines = await client.Machines.List();
machine = await client.Machines.Waitfor(machine.Id, MachineState.Off, 5000, 0, (m) => Console.WriteLine(m.State));
Console.WriteLine($"Machine with the id of {machine.Id} is now {machine.State}");
// ----
//Start the machine we created.
// ----
Console.WriteLine($"Starting {machine.Id}...");
await client.Machines.Start(machine.Id);
machines = await client.Machines.List();
machine = await client.Machines.Waitfor(machine.Id, MachineState.Ready, 5000, 0, (m) => Console.WriteLine(m.State));
Console.WriteLine($"Machine with the id of {machine.Id} is now {machine.State}");
// ----
// Restart the machine we created.
// ----
Console.WriteLine($"Restarting {machine.Id}...");
await client.Machines.Restart(machine.Id);
machines = await client.Machines.List();
// Note: Restart events are added immediately, but the machine state still reports a state of 'Ready'
// either wait for the Restart event to be in progress, or wait awhile.
await Task.Delay(15000);
machine = await client.Machines.Waitfor(machine.Id, MachineState.Ready, 5000, 0, (m) => Console.WriteLine(m.State));
Console.WriteLine($"Machine with the id of {machine.Id} is now {machine.State}");
// ----
// Get machine utilization.
// ----
var currentDate = DateTime.Now;
var utilization = await client.Machines.Utilization(machine.Id, currentDate.ToString("yyyy-MM"));
Console.WriteLine(JsonConvert.SerializeObject(utilization, Formatting.Indented));
}
/// <summary>
/// Demonstrates destroying a machine.
/// </summary>
/// <param name="client"></param>
/// <param name="machine"></param>
/// <returns></returns>
static async Task DestroyMachine(IPaperspaceClient client, Machine machine)
{
// ----
// List machines and then destroy the machine we created.
// ----
Console.WriteLine("Listing Machines...");
var machines = await client.Machines.List();
Console.WriteLine(JsonConvert.SerializeObject(machines, Formatting.Indented));
Console.WriteLine($"Destroying {machine.Id}...");
await client.Machines.Destroy(machine.Id);
Console.WriteLine("Listing Machines...");
machines = await client.Machines.List();
Console.WriteLine(JsonConvert.SerializeObject(machines, Formatting.Indented));
}
/// <summary>
/// Demonstrates the Networks Client
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
static async Task NetworksSample(IPaperspaceClient client)
{
Console.WriteLine("Listing Networks...");
var networks = await client.Networks.List();
Console.WriteLine(JsonConvert.SerializeObject(networks, Formatting.Indented));
}
/// <summary>
/// Demonstrates a full Script lifecycle: Create -> List -> Show -> Text -> Destroy
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
static async Task ScriptFullLifecycleSample(IPaperspaceClient client)
{
// ----
// Create a new Script
// ----
Console.WriteLine("Creating new script...");
var newScript = await client.Scripts.Create(new CreateScriptRequest
{
ScriptName = "My Script",
ScriptText = "echo Hello, World!",
ScriptDescription = "A startup script",
IsEnabled = true,
RunOnce = false
});
Console.WriteLine($"A new script with the id of {newScript.Id} has been created.");
Console.WriteLine($"Listing Scripts...");
var scripts = await client.Scripts.List();
Console.WriteLine(JsonConvert.SerializeObject(scripts, Formatting.Indented));
// ----
// Show the script we created
// ----
Console.WriteLine($"Showing script {newScript.Id}...");
var script = await client.Scripts.Show(newScript.Id);
Console.WriteLine(JsonConvert.SerializeObject(script, Formatting.Indented));
// ----
// Get the text of the script we created
// ----
Console.WriteLine($"Showing script text of {newScript.Id}...");
var text = await client.Scripts.Text(newScript.Id);
Console.WriteLine(text);
// ----
// Destroy script we created
// ----
Console.WriteLine($"Destroying {newScript.Id}...");
await client.Scripts.Destroy(newScript.Id);
scripts = await client.Scripts.List();
Console.WriteLine(JsonConvert.SerializeObject(scripts, Formatting.Indented));
// ----
// Create a new Script using a filename
// ----
Console.WriteLine("Creating new script...");
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("echo Foo, Bar, Baz!")))
{
newScript = await client.Scripts.Create(new CreateScriptRequest
{
ScriptName = "My Script",
ScriptFile = "./foobar.ps1",
ScriptDescription = "A startup script",
IsEnabled = true,
RunOnce = false
}, ms);
Console.WriteLine($"Showing script text of {newScript.Id}...");
text = await client.Scripts.Text(newScript.Id);
Console.WriteLine(text);
Console.WriteLine($"Destroying {newScript.Id}...");
await client.Scripts.Destroy(newScript.Id);
}
}
/// <summary>
/// Demonstrates the Users Client
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
static async Task UsersSample(IPaperspaceClient client)
{
Console.WriteLine("Listing Users...");
var users = await client.Users.List();
Console.WriteLine(JsonConvert.SerializeObject(users, Formatting.Indented));
}
/// <summary>
/// Demonstrates the ResourceDelegations Client
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
static async Task ResourceDelegationSample(IPaperspaceClient client, Machine machine)
{
Console.WriteLine("Creating Resource Delegation...");
var resourceDelegations = await client.ResourceDelegations.Create(new CreateResourceDelegationRequest()
{
Machine = new CreateResourceDelegationMachine()
{
Ids = new List<string>() { machine.Id }
}
});
Console.WriteLine(JsonConvert.SerializeObject(resourceDelegations, Formatting.Indented));
}
static async Task JobsSample(IPaperspaceClient client)
{
Console.WriteLine("Listing Job Machine Types...");
var machineTypes = await client.Jobs.MachineTypes();
Console.WriteLine(JsonConvert.SerializeObject(machineTypes, Formatting.Indented));
// ----
// Create a new Job using jess/tetris
// ----
Console.WriteLine("Creating a Job...");
var job = await client.Jobs.Create(new CreateJobRequest()
{
Name = "Tetris",
Container = "jess/tetris:latest",
Command = "echo Hello, World! && echo 'Hello Paperspace!' > /artifacts/hello.txt",
MachineType = MachineType.C2,
Project = "Mah Tetris Project"
});
Console.WriteLine("Listing Jobs...");
var jobs = await client.Jobs.List();
Console.WriteLine(JsonConvert.SerializeObject(jobs, Formatting.Indented));
job = await client.Jobs.Waitfor(job.Id, JobState.Stopped, 2000, 0, (j) => Console.WriteLine(j.State));
// ----
// Retrieve the logs associated with the job.
// ----
Console.WriteLine("Getting Job Logs...");
var logs = await client.Jobs.Logs(job.Id);
Console.WriteLine(JsonConvert.SerializeObject(logs, Formatting.Indented));
// ----
// Get artifacts
// ----
Console.WriteLine("Listing Artifacts...");
var artifacts = await client.Jobs.ArtifactsList(job.Id);
Console.WriteLine(JsonConvert.SerializeObject(artifacts, Formatting.Indented));
Console.WriteLine("Get Artifacts...");
var artifactsData = await client.Jobs.ArtifactsGet(job.Id);
Console.WriteLine(JsonConvert.SerializeObject(artifactsData, Formatting.Indented));
Console.WriteLine("Destroy Artifacts...");
await client.Jobs.ArtifactsDestroy(job.Id);
// ----
// Clone
// ----
Console.WriteLine("Cloning job...");
var clonedJob = await client.Jobs.Clone(job.Id);
await client.Jobs.Waitfor(clonedJob.Id, JobState.Stopped, 2000, 0, (j) => Console.WriteLine(j.State));
// ----
// Cleanup
// ----
Console.WriteLine("Destroy the jobs...");
await client.Jobs.Destroy(clonedJob.Id);
await client.Jobs.Destroy(job.Id);
}
}
}