Ron and Ella Wiki Page

Extremely Serious

Page 31 of 33

Chocolatey Package Manager

Introduction

If you are familiar with package manager of linux (e.g. rpm, apt-get, yum, dpkg, …) or mac (i.e. brew) here is the windows version.

Powershell Behind Proxy (if necessary)
Setting default Powershell TLS Protocol (if necessary)

Requirements

• Windows 7+ / Windows Server 2003+
• PowerShell v2+
• .NET Framework 4+ (the installation will attempt to install .NET 4.0 if you do not have it installed)

Installation using Powershell

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Proxy configuration (if necessary)

choco config set proxy <locationandport>
choco config set proxyUser <username> #optional
choco config set proxyPassword <passwordThatGetsEncryptedInFile> # optional
choco config set proxyBypassList "'<bypasslist, comma separated>'" # optional, Chocolatey v0.10.4 required
choco config set proxyBypassOnLocal true # optional, Chocolatey v0.10.4 required

Example

choco config set proxy http://localhost:8888
choco config set proxyUser bob
choco config set proxyPassword 123Sup#rSecur3
choco config set proxyBypassList "'http://localhost,http://this.location/'" #0.10.4 required
choco config set proxyBypassOnLocal true #0.10.4 required

Usage

Displaying the tool capabilities

choco -?

Useful Commands

CommandDescription
installInstalls a particular package.
list --local-onlyLists installed packages using chocolatey on local machine.
searchSearches a package availability.
uninstallUninstalls a package.
upgradeUpgrades a package.

Alternative to CLI search command

https://chocolatey.org/packages

Reference

https://chocolatey.org/

Configure Tinker Board with Static IP

    1. Using terminal go to /etc/network/interfaces.d directory.
    2. Create a file (e.g. ipv4) with the following content for IPv4:
      auto eth0
       iface eth0 inet static
       address <desired IP address for eth0>
       netmask <valid netmask>
       gateway <gateway address>
       #The following dns-nameservers field is optional
       #dns-nameservers <dns addresses delimited by space>
      
      auto wlan0
       iface wlan0 inet static
       address <desired IP address for wlan0>
       netmask <valid netmask>
       gateway <gateway address>
       #The following dns-nameservers field is optional
       #dns-nameservers <dns addresses delimited by space>

      Example

      auto eth0
       iface eth0 inet static
       address 192.168.0.100
       netmask 255.255.255.0
       gateway 192.168.0.1
       dns-nameservers 8.8.8.8 8.8.4.4
      
      auto wlan0
       iface wlan0 inet static
       address 192.168.0.101
       netmask 255.255.255.0
       gateway 192.168.0.1
    3. Disable the DHCPCD service using the following command:
      sudo systemctl disable dhcpcd.service
    4. Attach a LAN cable to your tinker board and run the following command:
      sudo systemctl restart networking

      Check your IP using the following command:

      ip addr show

      You might also want to disable the network manager using the following command if you don't need a desktop UI:

      sudo systemctl disable NetworkManager

       

Transport Layer Security (TLS) Handshake

  1. The client sends "client hello" and a number of specifications in plain text, such as the version of the TLS protocol it is running, the list of supported ciphersuites, and other TLS options it may want to use.
  2. The server picks the TLS protocol version for further communication, responds with "server hello", decides on a ciphersuite from the list provided by the client, attaches its certificate and public key and sends the response back to the client. Optionally, the server can also send a request for the client’s certificate and parameters for other TLS extensions.
  3. Assuming both sides are able to negotiate a common version and cipher, and the client is happy with the certificate provided by the server.The client creates a random Pre-Master Secret and encrypts it with the public key from the server's certificate, sending the encrypted Pre-Master Secret to the server.
  4. The server receives the Pre-Master Secret. The server and client each generate the Master Secret and session keys based on the Pre-Master Secret.
  5. The client sends "Change cipher spec" notification to server to indicate that the client will start using the new session keys for hashing and encrypting messages. Client also sends "Client finished" message.
  6. Server receives "Change cipher spec" and switches its record layer security state to symmetric encryption using the session keys. Server sends "Server finished" message to the client.
  7. Client and server can now exchange application data over the secured channel they have established. All messages sent from client to server and from server to client are encrypted using session key.

Reference

Make the Target Folder always in-sync from the Source Folder using Beyond Compare

  1. Create a folder compare session with beyond compare like the following:
  2. Save the session with a file name without any special characters (e.g. < or >) or spaces (i.e. necessary not to confuse the scheduler).
  3. Save the following beyond compare script to right-updater.bc file.
    #Write to right-updater.log
    log normal "right-updater.log"
    
    #Load a folder compare session.
    load "%1"
    
    #Confirmation will be set to yes by default. 
    option confirm:yes-to-all
    
    #Make the right equals to the left of the loaded folder from the session.
    sync mirror:left->right
  4. Save the following powershell script to bcrunner.ps1 file along the side right-updater.bc file.
    #expects 2 command line parameters
    param([string]$script, [string]$session)
    
    #The beyond compare command to be used.
    $cmd="C:\Program Files\Beyond Compare 4\BComp.com"
    
    #Holds the session argument for the beyond compare command.
    $args="""$session"""
    
    #Write on screen the actual command to be executed.
    write-output "$cmd /silent @$script $args"
    
    #Perform the command.
    &$cmd /silent @$script $args
  5. Schedule using windows task scheduler and on the actions tab fill in the following fields like:
    Action Start a program
    Program/script powershell
    Add arguments(optional) ./bcrunner.ps1 right-updater.bc <SAVED_SESSION_FROM_STEP_2>
    Start in (optional) <DIRECTORY_OF_BCRUNNER.PS1_FILE_FROM_STEP_4>

    Note: Fill-in the other tabs of the scheduler accordingly.

Triggering builds remotely with Jenkins using HTTP Request plugin

Use Cases

Lower Jenkins (e.g. v1.x) but requires later Jenkins (e.g. v2.x) functionality (e.g. using git plugin which has some transitive dependencies which requires v2.x).

For some reason , we just wanted a Jenkins server to delegate a task to another Jenkins server.

You've created a script (i.e. python, ruby, groovy, etc) that will trigger a Jenkins task via HTTP protocol. This is not part of this document but the Backend Jenkins Server section is applicable.

Backend Jenkins Server

This is the server that will perform the task.

Setting-up a role that can do remote build

  1. Click Jenkins -> Manage Jenkins -> Manage and Assign Roles entry and select Manage Roles.
  2. In the Global roles, add a new role (e.g. remote_triggers) that is allowed to trigger a task remotely.
  3. Once added enable the following:
Overall Read
Job Build
Job Read
Job Workspace

Example 1: Sample role configuration

If we have remote_triggers as the new role, we have the following:

Assigning a role to a user

  1. Click Jenkins -> Manage Jenkins -> Manage and Assign Roles entry and select Assign Role.
  2. In the Global roles, add a known Jenkins user.
  3. Once added assign the role.

Example 2: Assign a role following Example 1.

If the known user is GwDevOpsDev, we have the following:

Setting-up the Build Triggers

  1. On the Build Triggers section, select the Trigger builds remotely (e.g., from scripts).
  2. Provide an Authentication Token.

Note: You can use https://www.guidgenerator.com/ just remove the hyphen for this.

This will enable you to trigger the job remotely via HTTP Post using one of the following:

Without Build Parameters

<JENKINS_URL>/job/<URL_ENCODED_ITEM_NAME>/build?token=<AUTHENTICATION_TOKEN>

With Build Parameters

<JENKINS_URL>/job/<URL_ENCODED_ITEM_NAME>/buildWithParameters?token=<AUTHENTICATION_TOKEN>

Example 3: Address to call a task remotely.

If we have the following:

JENKINS_URL = http://d-51306g2.fmg.net:9080
ITEM_NAME=Statement Date Update to Saturday
URL_ENCODED_ITEM_NAME=Statement%20Date%20Update%20to%20Saturday
AUTHENTICATION_TOKEN=49dcf6a2164147b1871de70b40afef6f

The URL to call this job remotely with build parameters must be:

http://d-51306g2.fmg.net:9080/job/Statement%20Date%20Update%20to%20Saturday/buildWithParameters?token=49dcf6a2164147b1871de70b40afef6f

User Facing Jenkins Server

This is the Jenkins server that is normally use by the user to invoke tasks but the task is actually being done by a another Jenkins server.

Pre-requisite

  • HTTP Request Plugin

Setting-up Credential to be used with the HTTP request.

  1. Click Jenkins -> Credentials entry.
  2. Click the global store seen as follows:
  3. Click Add Credentials.
  4. Set the Kind field to Username with Password.
  5. Set the Scope field to Global if not yet set.
  6. Set the Username field to the user that can to remote triggering configured on the backend Jenkins server (e.g. GwDevOpsDev from Example 2).
  7. Set the Password field to a valid password.
  8. Set the ID field to something unique.
    Note: You can use https://www.guidgenerator.com/  for this.
  9. Click the OK button.

Setting-up a New Item to Delegate Task to Backend Jenkins Server

  1. Click New Item.
  2. Provide an Item name.
  3. Select Freestyle project.
  4. Click the OK button.
  5. Click Add build step-> HTTP Request entry.
  6. Fill-in the URL with the one configured in the backend Jenkins server (e.g. http://d-51306g2.fmg.net:9080/job/Statement%20Date%20Update%20to%20Saturday/buildWithParameters?token=49dcf6a2164147b1871de70b40afef6f from Example 3)
  7. Select POST for the HTTP mode.
  8. Click the Advanced… button.
  9. In the Authenticate field select the set up from Setting-up Credential to be used with the HTTP request section earlier.
  10. Click the Save button.
  11. Now every time you click Build now it will complete immediately since the actual build will be done on the Backend Jenkins Server.

Unit Test with Expando Object

Introduction

Expando object can make dynamic object, properties and methods if you like. This can be used for stubbing an object to be used for testing.

For example if have the following:

Code 1 - processor function that requires the SSampleStructure structure

//Structure works with objects with similar features but no common inheritance and interface declarations
structure SSampleStructure {
  function someAction() : int
}

function processor(sample : SSampleStructure) : int {
  return sample.someAction() + 10
}

The SSampleStructure structure will be compliant to all the classes that has someAction function that returns an int. And the actual compliant implementation of the structure is the following:

Code 2 - Actual implementation that is compatible with SSampleStructure

class SampleCodeWithStaticMethods {

  private construct() {}

  function someAction() : int {
    return (Math.round(Math.random()*100) as int) + 100
  }
}

We can notice that in Code 2, we cannot have a consistent output for someAction method since it is return arbitrary values. And what the processor function does is just add 10 on it.

What we can do here is to create an Expando object that will also comply with SSampleStructure structure like the following:

Code 3 - Using Expando class

uses gw.lang.reflect.Expando
  
    // By declaring the Dynamic type for the variable or function argument, you can
// programmatically access any property or method without requiring compile-time confirmation of its existence.
var testObj : Dynamic =

// An expando object is an object whose properties, and potentially methods, are created dynamically on assignment.
    new Expando()

// We can attached function using the block syntax as below.  
testObj.someAction = \ -> 25

Since the testObj of Code 3, also has someAction function that returns an int. This is compliant with SSampleStructure structure. So we can safely run the following test of logic without any problem:

Code 4 - Actual test

function testProcessor() {
  print("Is35: " + (processor(testObj)==35))
}

testProcessor()

As we can see from Code 4, we are using testObj to test the processor method.

« Older posts Newer posts »