WebFund 2024F: Tutorial 1: Difference between revisions
Created page with "'''This tutorial is not yet finalized.''' In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern. First, with the sync version, try the following: * Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression]. * Give it an additional argument that causes ma..." |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern. | In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern. | ||
First, with the sync version, try the following: | First, with the sync version, try the following: | ||
* Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression]. | * Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression]. | ||
* Give it an additional argument that causes matched lines to be output to a new file. You'll need to use | * Give it an additional argument that causes matched lines to be output to a new file. You'll need to use writeTextFileSync() from [https://docs.deno.com/api/deno/file-system the Deno file system API]. When finished, output "All done!" to the console. | ||
* Make the output lines [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sorted] in alphabetical order. | * Make the output lines [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sorted] in alphabetical order. | ||
Then, do all of the above with the async version. Make sure to use | Then, do all of the above with the async version. Make sure to use writeTextFile(), not writeTextFileSync()! | ||
You may find the following methods useful when working with arrays:[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join()]. | You may find the following methods useful when working with arrays:[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join()]. | ||
Line 73: | Line 71: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Getting Started with Deno== | ==Getting Started with Deno and Openstack== | ||
For this class you will be using [http://deno.com/ Deno] and a web broswer. While these are both cross-platform, we can provide the most support for running these applications inside the class [http://www.ubuntu.com/ Ubuntu] Openstack cloud instances. | |||
To get going with OpenStack, follow the [https://carleton.ca/scs/tech-support/scs-open-stack/ SCS guide to OpenStack]. Inside the COMP2406 project, create your own instance, with the name of your instance being your Carleton username plus "-comp2406-1" (so, "janesmith-comp2406-1"). If you need no make another instance, increment the number. You should normally have only one instance running; please delete non-functioning instances. | |||
(I don't expect you to mess up your instance but mistakes happen!) | |||
Should should select the latest "comp2406-2024f-" image as the starting image. This one will have deno already installed in the student account. If you use another image, you'll have to manually install deno and add its binary to the current path. | |||
Once you have an openstack VM setup (with a floating IP assigned), you'll need to access it. The way you should do it is by using ssh (secure shell). | |||
==Connecting to your OpenStack VM via ssh== | |||
There are two basic ways to connect to an openstack VM: via a terminal, or via a remote desktop. Both will use ssh, but will use it in different ways. | |||
If you | If you do remote code editing using a local editor (such as Emacs or [https://visualstudio.microsoft.com/ Visual Studio Code]), under the hood it will be connecting to the VM via ssh (basically through a terminal). | ||
Because of firewalls, you cannot connect to openstack VMs from the open Internet directly; you have to do it via the Carleton network. You can do this either through | |||
* [https://carleton.ca/its/vpn-access-to-campus/ The Carleton network VPN], or by | |||
* Connecting via ssh to another Linux system that allows ssh from the Internet. | |||
SCS supports access to its Linux servers [https://carleton.ca/scs/tech-support/scs-theta-linux-network/ using their SCS accounts]. So if your SCS username is JaneDoe, you can run in a terminal on your system: | |||
ssh janedoe@access.scs.carleton.ca | |||
and, after typing in your password, you can access this machine. | |||
For this class you don't want to use access directly; however, you can use it to "jump" to your openstack VM (avoiding the need for a VPN. So the basic ways to connect, if your username is janedoe and your VM's IP address is 134.117.33.1: | |||
ssh student@134.117.33.1 <--- while connected to the Carleton VPN | |||
or | |||
ssh -J janedoe@access.scs.carleton.ca student@134.117.33.1 <--- with no VPN | |||
In this latter case, you'll be typing in your SCS password and your VM's password, which gets annoying quickly. | |||
'''Default password:''' The default password for openstack VMs is "student" (for the student account). You'll have to change it when you first log in. | |||
'''Avoiding passwords:''' For a guide to how to use ssh without passwords see [https://carleton.ca/scs/2019/passwordless-ssh-with-key-pairs/ SCS's guide]. | |||
'''ProxyJump config:''' If you put the following in a <tt>.ssh/options</tt> file on your machine, something like this: | |||
Host jumphost | |||
User janedoe # replace with your SCS username | |||
HostName access.scs.carleton.ca | |||
Host comp2406 | |||
User student | |||
Hostname 134.117.33.1 # replace with your VM's IP address | |||
ProxyJump jumphost | |||
Then you can just type the following to access the VM without being on the Carleton VPN: | |||
ssh comp2406 | |||
==Hello, World!== | |||
In your editor of choice, create a file <tt>hello.js</tt> in your Documents folder with the following contents: | In your editor of choice, create a file <tt>hello.js</tt> in your Documents folder in your openstack VM with the following contents: | ||
console.log("Hello World!"); | console.log("Hello World!"); | ||
You can now run this file by | You can now run this file by typing: | ||
cd Documents | cd Documents # if that is where you put your hello.js | ||
deno hello.js | deno run hello.js | ||
And you should see <tt>Hello, World!</tt> output to your terminal. | And you should see <tt>Hello, World!</tt> output to your terminal. |
Latest revision as of 00:37, 13 September 2024
In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern.
First, with the sync version, try the following:
- Make the search pattern into a regular expression.
- Give it an additional argument that causes matched lines to be output to a new file. You'll need to use writeTextFileSync() from the Deno file system API. When finished, output "All done!" to the console.
- Make the output lines sorted in alphabetical order.
Then, do all of the above with the async version. Make sure to use writeTextFile(), not writeTextFileSync()!
You may find the following methods useful when working with arrays:push(), pop(), shift(), unshift(), and join().
Code
simplegrep_sync.js
if (Deno.args.length < 2) {
console.error('Not enough parameters given. Try this: ' +
'"deno run --allow-read simplegrep_sync term filename.txt"');
Deno.exit(1);
}
var searchterm = Deno.args[0];
var filename = Deno.args[1];
var rawContents = Deno.readTextFileSync(filename);
var lines = rawContents.split('\n');
for (var i = 0; i < lines.length; i++) {
if (lines[i].indexOf(searchterm) > -1) {
console.log(lines[i]);
}
}
simplegrep_async.js
if (Deno.args.length < 2) {
console.error('Not enough parameters given. Try this: ' +
'"deno run --allow-read simplegrep_async term filename.txt"');
Deno.exit(1);
}
const searchterm = Deno.args[0];
const filename = Deno.args[1];
const returnMatches = function(rawContents) {
const lines = rawContents.split('\n');
lines.forEach(function(theLine) {
if (theLine.indexOf(searchterm) > -1) {
console.log(theLine);
}
});
}
const readPromise = Deno.readTextFile(filename);
try {
const rawContents = await readPromise;
returnMatches(rawContents);
} catch (error) {
console.error(`Error reading file: ${error.message}`);
Deno.exit(1);
}
Getting Started with Deno and Openstack
For this class you will be using Deno and a web broswer. While these are both cross-platform, we can provide the most support for running these applications inside the class Ubuntu Openstack cloud instances.
To get going with OpenStack, follow the SCS guide to OpenStack. Inside the COMP2406 project, create your own instance, with the name of your instance being your Carleton username plus "-comp2406-1" (so, "janesmith-comp2406-1"). If you need no make another instance, increment the number. You should normally have only one instance running; please delete non-functioning instances.
(I don't expect you to mess up your instance but mistakes happen!)
Should should select the latest "comp2406-2024f-" image as the starting image. This one will have deno already installed in the student account. If you use another image, you'll have to manually install deno and add its binary to the current path.
Once you have an openstack VM setup (with a floating IP assigned), you'll need to access it. The way you should do it is by using ssh (secure shell).
Connecting to your OpenStack VM via ssh
There are two basic ways to connect to an openstack VM: via a terminal, or via a remote desktop. Both will use ssh, but will use it in different ways.
If you do remote code editing using a local editor (such as Emacs or Visual Studio Code), under the hood it will be connecting to the VM via ssh (basically through a terminal).
Because of firewalls, you cannot connect to openstack VMs from the open Internet directly; you have to do it via the Carleton network. You can do this either through
- The Carleton network VPN, or by
- Connecting via ssh to another Linux system that allows ssh from the Internet.
SCS supports access to its Linux servers using their SCS accounts. So if your SCS username is JaneDoe, you can run in a terminal on your system:
ssh janedoe@access.scs.carleton.ca
and, after typing in your password, you can access this machine.
For this class you don't want to use access directly; however, you can use it to "jump" to your openstack VM (avoiding the need for a VPN. So the basic ways to connect, if your username is janedoe and your VM's IP address is 134.117.33.1:
ssh student@134.117.33.1 <--- while connected to the Carleton VPN or ssh -J janedoe@access.scs.carleton.ca student@134.117.33.1 <--- with no VPN
In this latter case, you'll be typing in your SCS password and your VM's password, which gets annoying quickly.
Default password: The default password for openstack VMs is "student" (for the student account). You'll have to change it when you first log in.
Avoiding passwords: For a guide to how to use ssh without passwords see SCS's guide.
ProxyJump config: If you put the following in a .ssh/options file on your machine, something like this:
Host jumphost User janedoe # replace with your SCS username HostName access.scs.carleton.ca Host comp2406 User student Hostname 134.117.33.1 # replace with your VM's IP address ProxyJump jumphost
Then you can just type the following to access the VM without being on the Carleton VPN:
ssh comp2406
Hello, World!
In your editor of choice, create a file hello.js in your Documents folder in your openstack VM with the following contents:
console.log("Hello World!");
You can now run this file by typing:
cd Documents # if that is where you put your hello.js deno run hello.js
And you should see Hello, World! output to your terminal.
You can also run deno interactively by simply running deno with no arguments. You'll then get a prompt where you can enter any code that you like and see what it does. To exit this environment, type Control-D.
Note that when run interactively, we say that deno is running a read-eval-print loop (REPL). It reads input, evaluates it, and then prints the results. This structure is very old in computer science, going back to the first LISP interpreters from the early 1960's.