Friday, 29 May 2020

How To Install Docker Compose On CentOS 7

Introduction
Docker Compose is a tool used to define and run multi-container Docker applications. Users utilize this software to launch, execute, communicate, and close containers with a single coordinated command.
This tutorial will show you how to install Docker Compose on CentOS 7.
guide for centos install docker-compose
Prerequisites
  • A system running CentOS 7
  • A user account with sudo privileges
  • An existing Docker installation on CentOS
  • A command-line/terminal window (Ctrl-Alt-F2)

Installing Docker Compose from GitHub Repository

You can download Docker Composer from the official CentOS repository, but it is generally not advisable to do so. The best option is to install the binary package from the GitHub repository as is it ensures you are downloading the most up-to-date software.
Following these simple steps to start using Docker Compose on CentOS.

Step 1: Update Repositories and Packages

Before starting any installation, make sure to update the software repositories and software packages.
In the terminal enter the following commands:
sudo yum update
sudo yum upgrade
In the next step, you will use the curl command to download the binaries for Docker Compose. Beforehand, check whether you have the required command-tool by typing:
curl
curl is installed on the system if the output displays the message curl: try 'curl --help' or 'curl --manual' for more information.
curl missing during installation with a prompt for more information
If you see curl: command not found, you will need to install it before setting up Docker Compose.
To install curl, use the command:
sudo yum install curl

Step 2: Download Docker Compose 

First, download the current stable release of Docker Compose (1.24.1.) by running the curl command:
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
The –L option tells the system to follow any redirects, in case the file has been moved. –o changes the filename to docker-compose so you can easily find it when needed. The option /usr/local/bin/ specifies the location in which to store the software package.

Note: At the time of writing, the latest stable release of Docker Container was 1.24.1. To check for new releases, refer to the Docker Compose page on GitHub. To install a specific version, substitute 1.24.1 from the URL with the preferred version number.

Next, change the file permissions to make the software executable:
sudo chmod +x /usr/local/bin/docker-compose
Docker Compose does not require running an installation script. As soon as you download the software, it will be ready to use.

Step 3: Verify Installation

To verify the installation, use the following command that checks the version installed:
docker–compose –-version
output and verification of docker compose installed showing the version

Install Docker Compose using Pip

If you are having trouble installing Docker Compose with the steps mentioned above, try downloading the software using the Pip package manager.
Simply type in the following command in the terminal window:
sudo pip install docker-compose
example of code for installing docker-compose with pip
The output should show you if docker-compose-1.24.1 (the latest version at the time of writing) has been successfully installed.
docker compose version 1.24.1 installed successfully using PIP

Note: If you do not have Pip, take a look at our guide on How to Install Pip on CentOS.

How to Uninstall Docker Compose

To uninstall Docker Compose, if you installed using curl run:
sudo rm /usr/local/bin/docker-compose
If you installed the software using the pip command, remove Docker Compose with the command:
pip uninstall docker-compose
Conclusion
Now you know how to install Docker Compose on CentOS 7. The article includes two options for installation, one of which should work on your system.
After installation, you may want to look into the best practices for Docker container Management.

from : https://phoenixnap.com/kb/install-docker-compose-centos-7

Monday, 11 May 2020

VSCode Dev environment setup

venv

python3 -m venv .venv
source .venv/bin/activate
pip install -r ./app-server/src/requirements-customer.txt
pip install -r ./app-server/src/tests/requirements.txt
deactivate
pip freeze | xargs pip uninstall -y

vscode

command + P: Python: Select Interpreter選預設的等建出settings.json後再改成venv裡面的
  • .vscode/settins.json
    {
      "python.pythonPath"".venv\\Scripts\\python.exe"
    }
command + P: Python: Select Linter選pylint
  • .vscode/settins.json
    {
      "python.pythonPath"".venv\\Scripts\\python.exe",
      "python.linting.enabled"true,
      "python.linting.pylintEnabled"true
    }
    # https://code.visualstudio.com/docs/python/linting#_enable-linters
    # vscode還是會跳出說要安裝pylint, 裝完後就可以ok (不過不知道vscode把pylint裝到哪邊, 因為requirement不需要列, pip freeze也沒有看到)
command + P: Python: Configure Tests pytest
  • {
        "python.pythonPath"".venv/bin/python3",
        "python.linting.pylintEnabled"true,
        "python.linting.enabled"true,
        "python.testing.pytestEnabled"true,
        "python.testing.nosetestsEnabled"false,
        "python.testing.pytestArgs": [
            "app-server"
        ]
    }

Sunday, 10 May 2020

Convert string to Python class object?

Warningeval() can be used to execute arbitrary Python code. You should never use eval() with untrusted strings. (See Security of Python's eval() on untrusted strings?)
This seems simplest.
>>> class Foo(object):
...     pass
... 
>>> eval("Foo")
<class '__main__.Foo'>

from : https://stackoverflow.com/questions/1176136/convert-string-to-python-class-object

ASSERT THAT STR MATCHES REGEX IN PYTEST

Once in a while every Pythonista faces a need to test that some string value matches a regex pattern. When it comes we have no option but to use re module directly.
import re

def test_something_very_useful():
    value = get_some_string()
    assert re.match('\d+', value)
While it works fine in case of lone string, it's not always convenient to do the same when a string is a part of more complex data structure (e.g. dict), because you need to extract the string, check it and only then get back to check rest attributes.
import re

def test_something_even_more_useful():
    mapping = get_some_structure()

    # inconvenient and boring! :(
    assert 'value' in mapping
    value = mapping.pop('value')
    assert re.match('\d+', value)

    # rest assertion
    assert mapping = {'foo': 1,
                      'bar': 2}
Fortunately, it's pretty easy to write convenient pytest helper that would check a pattern as a part of checking the whole data structure.
def test_something_even_more_useful():
    mapping = get_some_structure()
    assert mapping = {'foo': 1,
                      'bar': 2,
                      'value': pytest_regex('\d+')}
Looks better, huh? :) So the trick is done by the following few lines which we're successfully using in XSnippet API.
import re

class pytest_regex:
    """Assert that a given string meets some expectations."""

    def __init__(self, pattern, flags=0):
        self._regex = re.compile(pattern, flags)

    def __eq__(self, actual):
        return bool(self._regex.match(actual))

    def __repr__(self):
        return self._regex.pattern
Well, actually, it's not by any means tied to pytest and could be used even in non-test production code, though I found this rather queer.

from : https://kalnytskyi.com/howto/assert-str-matches-regex-in-pytest/

Friday, 1 May 2020

Verify if a local port is available in python

Simply create a temporary socket and then try to bind to the port to see if it's available. Close the socket after validating that the port is available.
def tryPort(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = False
    try:
        sock.bind(("0.0.0.0", port))
        result = True
    except:
        print("Port is in use")
    sock.close()
    return result

from : https://stackoverflow.com/questions/43270868/verify-if-a-local-port-is-available-in-python