From 6b9edcf119ae503025bfdd7ea3e5dabf781c1279 Mon Sep 17 00:00:00 2001 From: DarkSun Date: Fri, 11 Dec 2020 05:02:41 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E9=80=89=E9=A2=98[tech]:=2020201210=20Set?= =?UTF-8?q?=20up=20a=20hyperconverged=20infrastructure=20with=20open=20sou?= =?UTF-8?q?rce=20tools?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sources/tech/20201210 Set up a hyperconverged infrastructure with open source tools.md --- ...d infrastructure with open source tools.md | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 sources/tech/20201210 Set up a hyperconverged infrastructure with open source tools.md diff --git a/sources/tech/20201210 Set up a hyperconverged infrastructure with open source tools.md b/sources/tech/20201210 Set up a hyperconverged infrastructure with open source tools.md new file mode 100644 index 0000000000..c7a72c2fdf --- /dev/null +++ b/sources/tech/20201210 Set up a hyperconverged infrastructure with open source tools.md @@ -0,0 +1,147 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Set up a hyperconverged infrastructure with open source tools) +[#]: via: (https://opensource.com/article/20/12/hyperconverged-infrastructure) +[#]: author: (Sahina Bose https://opensource.com/users/sahina) + +Set up a hyperconverged infrastructure with open source tools +====== +Run a full-featured virtualization environment with oVirt and Gluster +and without burning a hole in your pocket. +![Computer laptop in space][1] + +A modern infrastructure's needs are ever-growing. As demand grows for scaling up applications, our existing servers and storage are no longer enough. This is the point when most businesses look at setting up a virtualized environment on-premises or turning to public cloud infrastructure. But the cost of setting up your own environment or running it on the public cloud can be daunting. + +Fortunately, it is entirely possible to run a full-featured virtualization environment with completely open source products without burning a hole in your pocket. + +A hyperconverged environment is one where compute, storage, and network resources are all on the same servers, managed by a single interface. As I will show in this article, you can run a full-featured virtualized infrastructure with optimal use of servers by using [oVirt][2] and [Gluster][3] to deploy a hyperconverged solution. + +### Prerequisites + +You will need the following hardware infrastructure: + + * Three servers with: + * 32GB RAM or more + * Minimum 256GB storage (for anything more than a proof of concept, you will need more storage, think in terabytes) + * Dual network interface controller (NIC) and at least one 10GiB port + * Network switch + * Configured media access control (MAC) reservation and Domain Name System (DNS) entry + + + +The software components you need for this solution are: + + * [**oVirt**][2], an open source virtualization software management solution with all the enterprise features you need, such as host, virtual machine (VM) and network management, high availability, and live migration + * [**GlusterFS**][3], an open source scalable distributed storage that can run highly available petabyte-scale storage on commodity hardware. + + + +### How it works + +These two open source solutions work together to provide a hyperconverged solution. Each server provides both hypervisor and storage capabilities, with the VM disk images stored on the Gluster volume. + +The Gluster volume allows storage from multiple servers to present as a single, unified namespace to the client. In this solution, the Gluster volume is configured from storage on the three servers as a replica-3 volume, which means every bit of data that's written is replicated to two other servers to provide redundancy and resiliency against server outages. + +Gluster volumes can also be configured to run as [arbiter volumes][4], which have lower storage requirements than replica-3 volumes, as data is copied to only one other server while metadata is written on a third server. Both of these volume configurations ensure that the VMs continue to run without downtime if one of the servers goes down. + +[Libvirt][5] and [qemu-kvm][6] are used to create the VM images and run them on the servers. The oVirt engine manages creating, scheduling, and monitoring of VMs. Since the storage is shared across all three servers, the VMs can run on any of the three servers, allowing for live migration and high availability. + +oVirt has two main components: + + * **oVirt Engine** is a Java-based web application that manages the server and virtual machines. The engine can either run on a standalone server or a VM within the same infrastructure that it manages. + * **Vdsm** is a Python service that runs on every server and interacts with the engine and the subcomponents on the server. + + + +In the hyperconverged deployment, the oVirt engine runs as a VM managing the servers that it runs on. In this mode, it's also known as "Hosted Engine." + +![Hosted Engine architecture][7] + +(Sahina Bose, [CC BY-SA 4.0][8]) + +### Putting it all together + +With so many components involved, you may find the thought of setting everything up daunting. However, installation is easy with the oVirt Node ISO, which contains all the required packages and a [Cockpit][9]-based installation wizard that guides you through the steps. + + 1. Download and install the latest [oVirt Node ISO][10] on all three servers. + + 2. Navigate to the Cockpit server administration URL on any of the servers at `https://:9090`. + + 3. Follow the steps for the Hosted Engine installation with Gluster. + +![Hosted Engine installation UI][11] + +(Sahina Bose, [CC BY-SA 4.0][8]) + +These steps guide you through: + + * Setting up networks and hosts + +![Network and hosts setup][12] + +(Sahina Bose, [CC BY-SA 4.0][8]) + +A minimum of three hosts is required to run a highly available environment. The wizard prompts you to separate the storage network from the management network. + + * Setting up the Gluster volume + +![Setting up the Gluster volume][13] + +(Sahina Bose, [CC BY-SA 4.0][8]) + +By default, three Gluster volumes are created to hold the VM disk images. The engine volume is dedicated to holding the Hosted Engine disk image. Separating this helps with easy backup and recovery. + +![Engine volume setup][14] + +(Sahina Bose, [CC BY-SA 4.0][8]) + +Most of the configuration is automated, but you need to ensure you provide the underlying disk information to the user interface. + + * Deploying the oVirt engine as Hosted Engine + +![Engine volume deployment][15] + +(Sahina Bose, [CC BY-SA 4.0][8]) + +Once the storage has been configured, you must provide the details for the Hosted Engine VM—the fully qualified domain name ([FQDN][16]) and the MAC address reserved for the VM—to complete the deployment. + + + + +### Learn more + +It's this simple to set up a fully functional hyperconverged environment with a management interface you can use to manage and monitor your virtual machines, storage, and network. For further information, please refer to the [oVirt-Gluster hyperconvergence guide][17]. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/hyperconverged-infrastructure + +作者:[Sahina Bose][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/sahina +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space) +[2]: https://www.ovirt.org/ +[3]: https://www.gluster.org/ +[4]: https://docs.gluster.org/en/v3/Administrator%20Guide/arbiter-volumes-and-quorum/ +[5]: https://en.wikipedia.org/wiki/Libvirt +[6]: https://en.wikipedia.org/wiki/QEMU +[7]: https://opensource.com/sites/default/files/uploads/ovirt_hostedengine.jpg (Hosted Engine architecture) +[8]: https://creativecommons.org/licenses/by-sa/4.0/ +[9]: https://opensource.com/article/20/11/cockpit-server-management +[10]: https://www.ovirt.org/download/node.html +[11]: https://opensource.com/sites/default/files/uploads/ovirt_hostedenginesetup.png (Hosted Engine installation UI) +[12]: https://opensource.com/sites/default/files/uploads/ovirt_networks-hosts-setup.png (Network and hosts setup) +[13]: https://opensource.com/sites/default/files/uploads/ovirt_gluster-setup.png (Setting up the Gluster volume) +[14]: https://opensource.com/sites/default/files/uploads/ovirt_engine-volume.png (Engine volume setup) +[15]: https://opensource.com/sites/default/files/blocks/ovirt_engine-volume-deploy.png (Engine volume deployment) +[16]: https://en.wikipedia.org/wiki/Fully_qualified_domain_name +[17]: https://www.ovirt.org/documentation/gluster-hyperconverged/Gluster_Hyperconverged_Guide.html From 755127d1179f1f1cf45db1da4aded8d75da3049b Mon Sep 17 00:00:00 2001 From: DarkSun Date: Fri, 11 Dec 2020 05:02:54 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=80=89=E9=A2=98[tech]:=2020201210=20Prac?= =?UTF-8?q?tice=20coding=20in=20Groovy=20by=20writing=20a=20game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sources/tech/20201210 Practice coding in Groovy by writing a game.md --- ...tice coding in Groovy by writing a game.md | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 sources/tech/20201210 Practice coding in Groovy by writing a game.md diff --git a/sources/tech/20201210 Practice coding in Groovy by writing a game.md b/sources/tech/20201210 Practice coding in Groovy by writing a game.md new file mode 100644 index 0000000000..a2a42ab56c --- /dev/null +++ b/sources/tech/20201210 Practice coding in Groovy by writing a game.md @@ -0,0 +1,173 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Practice coding in Groovy by writing a game) +[#]: via: (https://opensource.com/article/20/12/groovy) +[#]: author: (Chris Hermansen https://opensource.com/users/clhermansen) + +Practice coding in Groovy by writing a game +====== +Groovy streamlines Java's syntax, making it an easy language for +experienced Java coders to adopt. +![Business woman on laptop sitting in front of window][1] + +You see the same basic concepts in most programming languages, such as: + + * Variables: Places where information is stored + * Expressions: Ways to calculate things + * Statements: How state changes are expressed in a program + + + +Once you understand these concepts, you can start figuring out what makes one language different from another. For example, most languages have a "way of doing things" supported by their design, and those ways can be quite different from one program to another. These include modularity (grouping related functionality together), declarative vs. imperative, object-orientation, low- vs. high-level syntactic features, and so on. + +An example that is familiar to many programmers is "ceremony," that is, the amount of work required to set the scene before tackling a problem. The Java programming language is said to have a significant ceremony requirement stemming from its design, which requires all code to be defined within a class. + +But back to the basics. Programming languages usually share similarities like the ones mentioned above, so once you know one, you can start learning the basics of another to appreciate its differences. One way to expand on the languages you know is by using a basic test program to begin learning. That's what we're doing in this series of articles, using a "guess the number" program, in which the computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the correct answer. + +The "guess the number" program exercises several concepts in programming languages: + + * Variables + * Input + * Output + * Conditional evaluation + * Loops + + + +It's also a great practical experiment to learn a new programming language. + +### Guess the number in Groovy + +[Groovy][2] is dynamically typed, feels like a scripting language, and has good support for interactive use. It was designed to leverage the Java Virtual Machine (JVM) ecosystem and has a high level of source compatibility with Java, along with its streamlined and high-level syntax features. + +Here is my implementation of the "guess the number" game in Groovy (with line numbers so I can review some of the specific features): + + +``` + 1      int randomNumber = (new [Random][3]()).nextInt(100) + 1 +        + 2      println "the secret number is $randomNumber" +        + 3      print 'Guess a number: ' + 4      [System][4].in.withReader { reader -> + 5          [String][5] response + 6          while (response = reader.readLine()) { + 7              if (! response.isNumber()) + 8                  print "that's not a number! try again: " + 9              else { +10                  int guess = response as [Integer][6] +11                  if (guess < randomNumber) +12                      print 'too low, try again: ' +13                  else if (guess > randomNumber) +14                      print 'too high, try again: ' +15                  else { +16                      println "that's right" +17                      break +18                  } +19              } +20          } +21      } +``` + +For those familiar with Java or C, the syntax will seem broadly familiar (unlike in Python, for example). In Groovy statements such as `if-then-else` or `while`, the `then`, `else`, and `while` parts take either a statement or a group of statements enclosed within `{` and `}`. Java programmers will note that Groovy provides streamlined support for scripting—no need for wrapping the program in a class definition. + +Groovy is object-oriented and has access to the Java standard library.  In line 1 of my code, to generate a random number, it creates a new instance of the Random class and calls the nextInt()  method on it with an argument of 100, which will give a number between 0 and 99; and so we add one to this result. + +One more thing to note: Groovy doesn't require semicolons to terminate lines when it can figure out the end of a statement from the presence of a newline. + +The second statement prints out the number that was generated, for debugging purposes. Worth mentioning here is Groovy's active efforts to hide Java ceremony; in Java, this would be written: + + +``` +`System.out.println("The secret number is " + randomNumber);` +``` + +The Groovy compiler takes away the need to prepend `System.out` in front of `println`. Groovy also makes parentheses in method invocations optional as long as the arguments can be clearly identified (but a method that takes no arguments still needs the `()` following the method name). + +Another cool Groovy feature, visible in line 2, is the use of a `GString`. Note the `$` in front of `randomNumber` within the string, which causes the value of the variable `randomNumber` to be interpolated into the string. An expression can be interpolated using `${ }`. + +In line 3, Groovy permits the use of single quotes instead of double quotes to delineate a `String` literal. Double quotes are required for a `GString`. This line prints a prompt for the person running the script. + +I want to be able to read input from the console (typically a terminal window). Java defines the `System` static class and the `in` static field as a way to get at stuff the user is typing in the console. Statement 4 calls the `withReader` method on the `System.in` field to set up and tear down a line-oriented String reader—another example of Groovy's streamlining. `withReader` takes a closure as its argument—a kind of "anonymous code block," similar to a lambda in Java. When `withReader` calls the supplied closure, it passes the reader as an argument to the closure. The corresponding parameter is defined in the closure with the fragment `reader ->`. + +This closure business is a really interesting feature in Groovy. Although it's similar to a lambda, it does not have lambda's limitations, such as only being able to refer to final or equivalent-to-final variables in the enclosing scope, so there's no need to worry much about the definition of `randomNumber` external to the closure. + +The closure, which begins with the `{ reader ->` on line 4 and ends with the `}` on line 21, is the chunk of code that processes the user's input. + +Within that closure is code that mostly looks familiar. Line 5 declares the `String` variable `response` (which is local to the closure). Line 6 starts a `while` loop that iterates as long as the user continues to guess, terminating if the line is empty or the user signals an end-of-file (say with a **Ctrl+D** in a Linux terminal window). This `while` loop ends with the `}` on line 20. + +Note the expression tested by the `while` loop is: + + +``` +`response = reader.readLine()` +``` + +Groovy has an extended definition of "truth." An expression that yields `true` is true, but so is an expression that yields a non-zero numeric value, non-empty string value, or non-null value. This expression will yield an empty string if the user presses return, or a `null` if the user types a **Ctrl+D**, which `System.in` turns into a `null`. + +Lines 7 and 8 check to see if the user entered a `String` value that can be converted to a number and will complain if not. Instances of the Groovy `String` class (i.e., all Strings) have a method called `isNumber()` that can be used for this kind of test. Compare this usage to the typical Java approach, which uses a `try-catch` block. + +Lines 9 through 19 process the value entered by the user. Line 10 converts the `String` value entered to an `Integer` using Groovy's `as` keyword, which can handle many coercions and conversions. The rest of the code tests whether the converted integer value is too low, too high, or just right. Note again the use of single quotes vs. double quotes, particularly in the `println` call on line 19, where double quotes are used to delimit the `String` literal because of the single quote within. + +### Sample output + +How well did this do in demonstrating the concepts I outlined earlier? + + * Variables: Yup, Groovy has those; see the `int` variables and `String` variables. + * Input: I used `System.in` as predefined console input and a reader to get values from that. + * Output: I used Groovy's `println` (Java's `System.out.println`) to print messages on the console. + * Conditional evaluation: I explored Groovy's `if-then-else` as well as Groovy truth. + * Loops: I used these in Groovy's `while` loop. + + + +I also used some Java standard library functionality, such as the `isNumeric()` method on `String` instances and the `withReader()` method on `System.in`. I used a closure, kind of like a Java lambda. + +Run the program to play the game: + + +``` +$ groovy guess.groovy +the secret number is 29 +Guess a number: 50 +too high, try again: 25 +too low, try again: 37 +too high, try again: 31 +too high, try again: 28 +too low, try again: 29 +that's right +$ +``` + +I feel I should mention that the optimal way to play this game is to use a binary search strategy: Calculate in your head the mean of the most recent "too high" and the most recent "too low" and use that as your next guess. + +One thing I didn't cover is comments. Like Java, Groovy comments can begin with a `//` and run to the end of the line or can be wrapped in `/* */`. I could comment out the debug `println` with a `//` in front to make it harder to cheat. + +### Learn more + +If you want to learn Groovy, start with the official [Groovy website][2]. I find [Hubert A. Klein Ikkink's musings][7] on Groovy, Grails, and Java to be extremely worthwhile. There are also many fine books about Groovy; even if they are somewhat dated, they are still worth reading. I like Venkat Subramaniam's [_Programming Groovy 2: Dynamic productivity for the Java developer_][8]. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/groovy + +作者:[Chris Hermansen][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/clhermansen +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating) +[2]: https://groovy-lang.org/ +[3]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+random +[4]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+system +[5]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string +[6]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+integer +[7]: https://www.mrhaki.com/ +[8]: https://www.bookdepository.com/Programming-Groovy-2-Venkat-Subramaniam/9781937785307 From 4c1a9ad74b0fd7c78bfc31ee00bbbe054e08e160 Mon Sep 17 00:00:00 2001 From: DarkSun Date: Fri, 11 Dec 2020 05:03:09 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E9=80=89=E9=A2=98[tech]:=2020201209=20Make?= =?UTF-8?q?=20medit=20your=20next=20Linux=20terminal=20text=20editor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sources/tech/20201209 Make medit your next Linux terminal text editor.md --- ...it your next Linux terminal text editor.md | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 sources/tech/20201209 Make medit your next Linux terminal text editor.md diff --git a/sources/tech/20201209 Make medit your next Linux terminal text editor.md b/sources/tech/20201209 Make medit your next Linux terminal text editor.md new file mode 100644 index 0000000000..39ecb25ea7 --- /dev/null +++ b/sources/tech/20201209 Make medit your next Linux terminal text editor.md @@ -0,0 +1,70 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Make medit your next Linux terminal text editor) +[#]: via: (https://opensource.com/article/20/12/medit) +[#]: author: (Seth Kenlon https://opensource.com/users/seth) + +Make medit your next Linux terminal text editor +====== +This classic text editor offers all the basics and some exciting extra +features that let you customize your experience. +![Person drinking a hot drink at the computer][1] + +There’s [XEDIT][2], jEdit, NEdit, [gedit][3], and, as it turns out, [medit][4].  + +I had not heard about medit until I started searching for editors I hadn’t yet tried, but I’m glad to have discovered it. If you’re looking for a classic gedit experience (circa Gnome 2), then medit is, probably unintentionally, an excellent and modern approximation. It’s also got many additional features, such as the ability to write plugins in Python, Lua, or C, and a means to integrate even shell scripts into its menu system. All the other usual features are here, too—a tabbed interface, an on-demand shell, indentation management, syntax highlighting, and so on. + +### Installing medit + +You can download medit from [mooedit.sourceforge.net][5]. It’s confirmed to work on Linux and Windows. If you’re using Linux, you may also find it in your repository. I installed my copy from [slackbuilds.org][6] on Slackware. + +![Medit terminal showing examples of Bash script in editor][7] + +### Using medit + +Medit advertises itself as an editor "for programming and around programming," and in fact, started its life out as part of a larger project called [GAP (Groups, Algorithms, Programming)][8]. Most of its features are aimed at typical developer expectations. For instance, in the **Edit** menu, there are options to increase and decrease indentation, a common task for any programmer trying to indicate scope visually (and a literal requirement for Python programmers), and options to comment or uncomment blocks of text. + +Some features can be useful for general users, too. Medit has an easy-to-use tabbed interface (both at the top of the window and as a pop-up list along the side), a side panel for quick filesystem browsing, the ability to bookmark places in a file, and so on. It also has syntax highlighting for both programming languages, as well as markup and markdown languages, so it’s a useful editor for code and prose alike. + +### Color schemes + +When editing plain text with no syntax associated with it or a format like Asciidoc, for which medit has no preset highlighting scheme, the editor assumes your system default. I use a dark theme, so medit displays white text on a dark gray background. + +For syntax highlighting, though, the text becomes colorful depending on the part each word plays in its structured language. At first, I was a little frustrated at some of the choices medit made; many of the colors were too dark against my dark background to be legible, and I didn’t feel that all the important elements were unique enough. The answer to this problem, should you disagree with medit’s choices, is in **Preferences**, where you can change the color theme. I changed mine to the Tango colors, which rendered a solarized color array that stood out nicely against my dark editor background and even added color to elements that were kept white under the medit theme. + +![Medit terminal showing examples of Bash script in editor using Tango color scheme against dark background][9] + +### Pop-up Python + +At the bottom of the medit window, there’s a pop-up terminal for quick access to a shell. This is a nice feature, but frankly, after you’ve experienced Emacs and [Kate][10], it feels pretty common. What surprised me about medit was its pop-up Python console, which launches from the **Tools** menu with **moo** and **gtk** modules pre-imported. In other words, when you launch medit’s Python shell, you can look through the Python and GTK modules that medit itself is partly built upon. It’s a great feature and one that might inspire you to write a plugin (the Terminal pop-up is a plugin written in Python, so you can also look through its code to get a feel for how a plugin is written). + +### Classic editing + +Medit is a great GTK-based editor with all the important basic features and several tantalizing extras to help inspire you to extend the application and make it your own. As it accepts C, Python, Lua, and Bash, there are several entry points for doing just that. If you’re looking for a no-nonsense editor for your writing, whether it’s code or markdown or something in between, give medit a chance. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/medit + +作者:[Seth Kenlon][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/seth +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coffee_tea_laptop_computer_work_desk.png?itok=D5yMx_Dr (Person drinking a hot drink at the computer) +[2]: https://opensource.com/article/20/12/xedit +[3]: https://opensource.com/article/20/12/gedit +[4]: http://mooedit.sourceforge.net/ +[5]: https://sourceforge.net/projects/mooedit/files/medit/ +[6]: https://slackbuilds.org/repository/14.2/development/medit +[7]: https://opensource.com/sites/default/files/uploads/medit-31_days_medit-opensource.png (Medit terminal showing examples of Bash script in editor) +[8]: https://www.gap-system.org/ +[9]: https://opensource.com/sites/default/files/uploads/medit-tango-colour-31_days_medit-opensource.png (Medit terminal showing examples of Bash script in editor using Tango color scheme against dark background) +[10]: https://opensource.com/article/20/12/kate-text-editor From d5466d1fdac927bc569ae2249cd3a2fa84fecf6a Mon Sep 17 00:00:00 2001 From: DarkSun Date: Fri, 11 Dec 2020 05:03:23 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E9=80=89=E9=A2=98[tech]:=2020201209=20Prog?= =?UTF-8?q?ram=20a=20simple=20game=20with=20Elixir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sources/tech/20201209 Program a simple game with Elixir.md --- ...01209 Program a simple game with Elixir.md | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 sources/tech/20201209 Program a simple game with Elixir.md diff --git a/sources/tech/20201209 Program a simple game with Elixir.md b/sources/tech/20201209 Program a simple game with Elixir.md new file mode 100644 index 0000000000..8304970ff0 --- /dev/null +++ b/sources/tech/20201209 Program a simple game with Elixir.md @@ -0,0 +1,141 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Program a simple game with Elixir) +[#]: via: (https://opensource.com/article/20/12/elixir) +[#]: author: (Moshe Zadka https://opensource.com/users/moshez) + +Program a simple game with Elixir +====== +Learn Elixir by programming a "guess the number" game and comparing the +language against ones you know. +![A die with rainbow color background][1] + +To you learn a new programming language, it's good to focus on the things most programming languages have in common: + + * Variables + * Expressions + * Statements + + + +These concepts are the basis of most programming languages. Because of these similarities, once you know one programming language, you can start figuring another one out by recognizing its differences. + +Another good tool for learning a new language is starting with a standard program. This allows you to focus on the language, not the program's logic. We're doing that in this article series using a "guess the number" program, in which the computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the number correctly. + +The "guess the number" program exercises several concepts in programming languages: + + * Variables + * Input + * Output + * Conditional evaluation + * Loops + + + +It's a great practical experiment to learn a new programming language. + +### Guess the number in Elixir + +The [Elixir][2] programming language is a dynamically typed functional language designed for building stable and maintainable applications. It runs on top of the same virtual machine as [Erlang][3] and shares many of its strengths—but with slightly easier syntax. + +You can explore Elixir by writing a version of the "guess the number" game. + +Here is my implementation: + + +``` +defmodule Guess do +  def guess() do +     random = Enum.random(1..100) +     IO.puts "Guess a number between 1 and 100" +     Guess.guess_loop(random) +  end +  def guess_loop(num) do +    data = IO.read(:stdio, :line) +    {guess, _rest} = Integer.parse(data) +    cond do +      guess < num -> +        IO.puts "Too low!" +        guess_loop(num) +      guess > num -> +        IO.puts "Too high!" +        guess_loop(num) +      true -> +        IO.puts "That's right!" +    end +  end +end + +Guess.guess() +``` + +To assign a value to a variable, list the variable's name followed by the `=` sign. For example, the statement `random = 0` assigns a zero value to the `random` variable. + +The script starts by defining a **module**. In Elixir, only modules can have named functions in them. + +The next line defines the function that will serve as the entry point, `guess()`, which: + + * Calls the `Enum.random()` function to get a random integer + * Prints the game prompt + * Calls the function that will serve as the loop + + + +The rest of the game logic is implemented in the `guess_loop()` function. + +The `guess_loop()` function uses [tail recursion][4] to loop. There are several ways to do looping in Elixir, but using tail recursion is a common one. The last thing `guess_loop()` does is call _itself_. + +The first line in `guess_loop()` reads the input from the user. The next line uses `parse()` to convert the input to an integer. + +The `cond` statement is Elixir's version of a multi-branch statement. Unlike `if/elif` or `if/elsif` in other languages, Elixir does not treat the first nor the last branch in a different way. + +This `cond` statement has a three-way branch: The guess can be smaller, bigger, or equal to the random number. The first two options output the inequality's direction and then tail-call `guess_loop()`, looping back to the beginning. The last option outputs `That's right`, and the function finishes. + +### Sample output + +Now that you've written your Elixir program, you can run it to play the "guess the number" game. Every time you run the program, Elixir will pick a different random number, and you can guess until you find the correct number: + + +``` +$ elixir guess.exs +Guess a number between 1 and 100 +50 +Too high +30 +Too high +20 +Too high +10 +Too low +15 +Too high +13 +Too low +14 +That's right! +``` + +This "guess the number" game is a great introductory program for learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts of the languages and compare their details. + +Do you have a favorite programming language? How would you write the "guess the number" game in it? Follow this article series to see examples of other programming languages that might interest you. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/elixir + +作者:[Moshe Zadka][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/moshez +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/dice_tabletop_board_gaming_game.jpg?itok=y93eW7HN (A die with rainbow color background) +[2]: https://elixir-lang.org/ +[3]: https://www.erlang.org/ +[4]: https://en.wikipedia.org/wiki/Tail_call From 52d9db43d26ca89096a6a58e5b95f52a7edf779d Mon Sep 17 00:00:00 2001 From: DarkSun Date: Fri, 11 Dec 2020 05:03:38 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E9=80=89=E9=A2=98[tech]:=2020201209=20How?= =?UTF-8?q?=20I=20use=20Stream=20Deck=20on=20Linux=20with=20open=20source?= =?UTF-8?q?=20tools?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sources/tech/20201209 How I use Stream Deck on Linux with open source tools.md --- ...am Deck on Linux with open source tools.md | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 sources/tech/20201209 How I use Stream Deck on Linux with open source tools.md diff --git a/sources/tech/20201209 How I use Stream Deck on Linux with open source tools.md b/sources/tech/20201209 How I use Stream Deck on Linux with open source tools.md new file mode 100644 index 0000000000..e8a362c407 --- /dev/null +++ b/sources/tech/20201209 How I use Stream Deck on Linux with open source tools.md @@ -0,0 +1,121 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (How I use Stream Deck on Linux with open source tools) +[#]: via: (https://opensource.com/article/20/12/stream-deck) +[#]: author: (Lorna Mitchell https://opensource.com/users/lornajane) + +How I use Stream Deck on Linux with open source tools +====== +Stream Deck is not just a keypad full of shiny buttons; it's a +customizable workhorse that makes it easier to manage your video +streams. +![Working from home at a laptop][1] + +Most of us have gotten a lot more familiar with working from home this year. In my role as a developer advocate, this has meant a lot less travel and a lot more video work, including [streaming on Twitch][2]. + +As I transitioned to working and streaming video from home in spring 2020, I decided to get a [Stream Deck][3], but I wasn't exactly sure what I'd use it for. + +The Stream Deck is a keypad that makes it easier to manage your video streaming. You can customize its buttons to do things like switching scenes, adjusting audio, inserting media, interacting with viewers, and much more. I got the 32-button version because that's all that was in stock anywhere when I bought it. + +![Lorna's video streaming setup][4] + +(Lorna Mitchell, [CC BY-SA 4.0][5]) + +Six months in, it's become my best desk accessory. I want to introduce you to it and share my current setup. + +### Stream Deck software for Linux + +Stream Deck offers several software options for driving the device, but I'm on Ubuntu Linux, so the official Stream Deck tools aren't an option. I found a Python graphical user interface (GUI), but it really just mapped shell scripts onto buttons; since I don't use a mouse, I found the interface pretty awkward. + +Instead, I glued together a lovely selection of open source libraries and created [a rather weird and knobbly Golang application][6] that perfectly suits my needs. That sounds like the hard way to drive some perfectly good hardware. Perhaps it is, but now that it's finished, it fits me perfectly, and I love it! I'm going to claim it's not purely a "works-on-my-machine" solution because it works on both of my Thinkpads running Ubuntu. + +### Meet my Stream Deck + +![Stream Deck][7] + +(Lorna Mitchell, [CC BY-SA 4.0][5]) + +My setup is still changing weekly, but here's how it looks in November 2020. The top row is [Open Broadcaster Software][8] (OBS) integration; the second row is mainly bot/chat integration and window-focusing with a bonus screenshot button on the end; row three holds the lighting controls and a little bit of Twitch API integration; and the bottom row has sound effects and the all-important mute button. If you'd like a bit more detail, read on. + +#### Start with streaming + +It's a Stream Deck; the clue is in the name! So I began by adding some integration with OBS, the program I use to stream. OBS has a [plugin to support WebSockets][9], and there was already a [library for Golang][10], which really helped. Crucially, the library also handles events, so I have buttons that select scenes and a little indicator to show which scene is active—even if I change scenes via OBS rather than the Stream Deck. + +Using the Stream Deck to change scenes is amazing when you're screen-sharing because it means you don't have to focus on another window while you're in the middle of something. + +#### Window management + +Window focus—keeping all the right things visible and everything else not visible—is definitely a huge challenge when streaming. Let's just say that I had to rotate a lot of keys in the first few months of my streaming journey! + +There's a lot to look at, between OBS, probably some chat/moderation/events, the stream itself… you get the idea. My setup's buttons focus on the applications I use the most while streaming (they call a mixture of [wmctrl][11] and [gtk-launch][12]), and it has made things much easier to operate. I have one button for OBS, one for stream manager, and one for my featured chat dashboard (the last two are web pages packaged to make them standalone apps using [Nativefier][13]). Now my streams are more about the topics I have in mind, rather than mostly showing Alt+Tab. + +I can absolutely imagine using this for other day-to-day applications, such as if you regularly rotate among a set of applications and want the ability to conjure one up immediately. I'm using browser tabs in a known order for this purpose, but I feel I have missed an opportunity for a productivity hack here. + +#### The bot and the clipboard + +I stream a bunch of different technologies, and my geeky audience asks a lot of questions and recommends a lot of tools, which is brilliant. However, that means I spent a lot of time finding and focusing on the chat window (and the text box within that window) to share links. To improve this, I switched the channel bot to [Nightbot][14], a chatbot that adds moderation and other features to a stream's chat function. Many bots are available, but I chose this one because it has API integration and buttons to help me get things into chat when I need them. + +The first two buttons cue things up. I often paste URLs into my streams, but I don't want to risk accidentally pasting a password. So the cue button first cues up what I have in my clipboard (shout out to [Clipboard for Go][15] for the clipboard integration) and shows it to me. The other cue button reads the next line from a file I keep of things I might want to share on a stream—maybe I want to mention an event where I'm speaking that I think the crowd would like, or I know I'm going to promote a particular tool on the stream that day—and shows me what it's going to send. I can press the cue button again to see another line from the file. + +The Send button causes the bot to "chat" the cued-up text into my channel. This is a much better setup than searching for a chat window when you are mid-spiel about a topic. + +#### Lights and sounds + +I'm not the only streamer with funky, changeable light colors in the background of my streaming location, and having control over them is very useful. The shelf that sits behind me has a custom groove for a strip of neopixels. My Stream Deck has buttons to turn the lights on and off and implement some color presets. Everything works over [MQTT][16], and I use the [Eclipse Paho client in Go][17] to control it all. Another button on the Stream Deck turns my video light on and off so that I can get everything set up and cue the lights when I'm ready to stream, rather than blinding myself when I'm not live. I will happily admit to flipping the LED strip colors as a sort of video meeting party trick or even when nobody is watching! Buttons to press and colorful lights… what's not to like? + +I also have sound effects on my Stream Deck, but I haven't made cute icons for those buttons yet. Technology-wise, I'm running [CasterSoundboard][18], which supports the [Open Sound Control][19] protocol, and using the [hypebeast/osc][20] library in my application to wrangle it. I have a few different sounds; they make me (and hopefully others!) smile. I have seen some really engaging sound effects on streams, but I think I'm at a beginner stage so far! + +#### Non-streaming buttons + +There are not many other features on my Stream Deck, but those are the ones I use the most, by far, usually when I'm not streaming. The first is a screenshot button. I write a lot of technical documentation, and sometimes that means I need to create screenshots. Having a one-press button that captures the current window in two seconds is very useful, and I am so happy with it. The button activates the command I'd use for this: + + +``` +/usr/bin/gnome-screenshot -w -d 2 +``` + +Finally, something I never want to part with: a microphone mute button. It mutes at the system level via [pulsemixer][21], and it shows an indicator of the device's current state. I'm writing this in 2020, when video calling is my primary human contact. Since many platforms don't have a way to hover or click to get to the mute icon, having control of my sound device like this is fabulous. Lots of video calling applications will pick up these settings and show you as muted when you are and unmuting when you want to make an important point. Sometimes the smallest things are the most life-changing! + +### Give it a go + +There are many features I use daily, and not just when I'm streaming. I have had a lot of fun dreaming up improvements and features for the Stream Deck and sometimes streaming while I implement them. + +There are some features that I wouldn't want to live without now. If you're on the fence about getting a device like this, I'd recommend giving it a go. There are lots of great libraries to help you make it your own. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/stream-deck + +作者:[Lorna Mitchell][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/lornajane +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/wfh_work_home_laptop_work.png?itok=VFwToeMy (Working from home at a laptop) +[2]: https://twitch.tv/lornajanetv +[3]: https://www.elgato.com/en/gaming/stream-deck +[4]: https://opensource.com/sites/default/files/uploads/lorna.jpg (Lorna's video streaming setup) +[5]: https://creativecommons.org/licenses/by-sa/4.0/ +[6]: https://github.com/lornajane/streamdeck-tricks +[7]: https://opensource.com/sites/default/files/uploads/streamdeck.jpg (Stream Deck) +[8]: https://obsproject.com/ +[9]: https://github.com/Palakis/obs-websocket +[10]: https://github.com/christopher-dG/go-obs-websocket +[11]: https://en.wikipedia.org/wiki/Wmctrl +[12]: https://developer.gnome.org/gtk3/stable/gtk-launch.html +[13]: https://github.com/jiahaog/Nativefier +[14]: https://nightbot.tv/ +[15]: https://github.com/atotto/clipboard +[16]: https://mqtt.org/ +[17]: https://github.com/eclipse/paho.mqtt.golang +[18]: https://github.com/JupiterBroadcasting/CasterSoundboard +[19]: http://opensoundcontrol.org/osc +[20]: https://github.com/hypebeast/go-osc +[21]: https://pypi.org/project/pulsemixer/