Best Python code snippet using localstack_python
tasks.py
Source:tasks.py  
1import os2import sys3import urllib4import hashlib5# import paramiko6import json7from . import apt, remote8from celery import shared_task9from core import confirm, get_git_root10# from django.conf import settings11from subprocess import Popen12import logging13log = logging.getLogger(__name__)14@shared_task15def install_pkg(pkg_name):16    import platform17    # import subprocess18    # check if we are root19    # if os.geteuid() != 0:20    #     raise ValueError("Need to be root to install packages")21    if 'Linux' == platform.system():22        # platform.linux_distribution() deprecated in 3.723        # Arch24        pacman = '/usr/bin/pacman'25        if os.path.isfile(pacman):26            p = Popen(['sudo', '-S', pacman, '-S', '--noconfirm', pkg_name])27            p.communicate()28            p.wait()29            # sudo_prompt = p.communicate(sudo_password + '\n')[1]30        else:31            raise NotImplementedError(32                "don't know how to install packages in this Linux")33    elif 'Windows' == platform.system():34        raise NotImplementedError(35            "don't know how to install packages in Windows")36    else:37        raise NotImplementedError(38            "don't know how to install packages in Unknown")39def service_is_running(service):40    # systemctl is-active sshd41    exit_code = Popen(['systemctl', '-q', 'is-active', service]).wait()42    return exit_code == 043def configure_docker():44    import docker45    cli = docker.from_env()46    try:47        cli.images.get('ubuntu:17.04')48    except Exception:49        print('Getting Ubuntu image...')50        cli.images.pull('ubuntu:17.04')51    try:52        cli.images.get('postgres:latest')53    except Exception:54        print('Getting Postgres image...')55        cli.images.pull('postgres:latest')56    print(os.getcwd())57    cli.images.build(path='../docker/', tag='pashinin.com')58    # container = cli.containers.run('pashinin.com',59    #                                detach=True)60    print(cli.containers.list())61    print(cli.images.list())62def setup_info(key, value=None):63    root = get_git_root()64    info_file = os.path.join(root, 'tmp', 'info.json')65    info = {}66    try:67        info = json.load(open(info_file, 'r'))68    except Exception:69        pass70    if value:71        info[key] = value72        with open(info_file, 'w') as outfile:73            json.dump(info, outfile)74    else:75        return info.get(key)76# This can be executed when no packages are installed yet (including77# Celery's shared_task)78#79# @shared_task80def requirements(*args):81    """$(vebin)/pip install -r docker/requirements.txt"""82    key = 'requirements-hash'83    root = get_git_root()84    reqs_file = os.path.join(root, 'docker', 'requirements.txt')85    hasher = hashlib.sha1()86    with open(reqs_file, "rb") as f:87        buf = f.read(32768)88        while len(buf) > 0:89            hasher.update(buf)90            buf = f.read(32768)91    hash = hasher.hexdigest()92    if setup_info(key) == hash:93        log.warning('requirements.txt: no changes. Skipping pip install.')94    else:95        pip = 'tmp/ve/bin/pip'96        p = Popen(97            [pip, 'install', '-r', reqs_file],98            cwd=root99        )100        p.communicate()101        p.wait()102        if p.returncode == 0:103            setup_info(key, hash)104def install(program):105    from shutil import which106    if which(program) is not None:107        if program == 'docker':108            if service_is_running('docker'):109                print('Docker is running')110            else:111                print('starting Docker...')112                p = Popen(['sudo', '-S', 'systemctl', 'start', 'docker'])113                p.communicate()114                p.wait()115            # check docker images116            try:117                import docker118                docker.from_env()119            except Exception:120                print('Installing docker via pip')121                p = Popen(['sudo', 'pip', 'install', 'docker'])122                p.communicate()123                p.wait()124            # if permission denied - add user to "docker" group125            try:126                import docker127                client = docker.from_env()128                client.containers.list()129            except Exception as e:130                print(e)131                # gpasswd -a user docker132                import getpass133                p = Popen(['sudo', 'gpasswd', '-a', getpass.getuser(),134                           'docker'])135                p.communicate()136                p.wait()137                msg = """You were added to group "docker".138Please restart! Logout and login may not help!"""139                print('*'*len(msg))140                print(msg)141                print('*'*len(msg))142                raise ValueError("Restart!")143            configure_docker()144        return145    import platform146    pkgs = {147        'flake8': {148            'ubuntu': 'python-flake8',149            'arch': 'flake8',150        },151        'docker': {152            # 'ubuntu': 'python-flake8',153            'arch': 'docker',154        },155        'docker-compose': {156            # 'ubuntu': 'python-flake8',157            'arch': 'docker-compose',158        },159    }160    if program in pkgs:161        if 'Linux' == platform.system():162            # platform.linux_distribution() deprecated in 3.7163            # Arch164            if os.path.isfile('/usr/bin/pacman'):165                install_pkg(pkgs[program]['arch'])166            else:167                raise NotImplementedError(168                    "don't know how to install {} in this Linux"169                    .format(program))170        elif 'Windows' == platform.system():171            raise NotImplementedError(172                "don't know how to install packages in Windows")173        else:174            raise NotImplementedError(175                "don't know how to install packages in Unknown")176    else:177        raise ValueError("Don't know how to install {}".format(program))178# class Program(object):179#     name = None180#     host = 'localhost'181#     def __init__(self, name, host='localhost'):182#         self.name = name183#         self.host = host184#     def install(self):185#         raise NotImplementedError(f'How to install {self.name}?')186# class ElasticSearch(object):187#     name = 'ElasticSearch'188#     def install(self):189#         pass190@shared_task191def install_ElasticSearch(host=None):192    # TODO:193    # change bind addresses in config:194    # network.host: ["localhost", "10.254.239.2"]195    tty = sys.stdout.isatty()196    url = 'https://artifacts.elastic.co/downloads/' \197          'elasticsearch/elasticsearch-6.0.1.deb'198    # sha = 'fa92bf63d32712dc166ab7c21e460cb6b424d4b8ae839a0fe0c8ee6167b981c' \199    #       'e53902f51088cbdbc9ae8fc0e31e621e3dfa878de0b88e11f7a23aea13e6d6fa3'  # noqa200    if apt.installed('elasticsearch', host=host):201        if host:202            print(f'ElasticSearch is already installed on {host}.')203        else:204            print(f'ElasticSearch is already installed on this machine.')205    elif not tty or confirm(f'Install ElasticSearch on {host}?'):206        if host:207            print(f'Installing ElasticSearch on {host}...')208            apt.install_from_url(url, host)209            # raise NotImplementedError('another host')210        else:211            print(f'Installing ElasticSearch locally...')212            f = '/tmp/es.deb'213            if not os.path.isfile(f):214                urllib.request.urlretrieve(url, f)215            else:216                file_size = os.path.getsize(f)217                print(f'{f} exists ({file_size})')218                # TODO: install deb file219                # cache = apt.Cache()220                # if cache['package-name'].is_installed:221                if apt.installed('elasticsearch', host=host):222                    print(f"installed on {host}")223                else:224                    print("NO it's NOT installed")225                    apt.install(f)226def get_java_version(host=None):227    return None228@shared_task229def install_java8(host=None):230    c = remote.create_connection(host)231    res = remote.get_output(232        'java -version',233        host=host,234        c=c235    )236    java_installed = 'command not found' not in res['err']237    version_string = res['err'].split('\n')[0]238    # version string start with either239    # "openjdk" or "java"240    oracle = version_string.startswith('java')241    openjdk = not oracle242    print(version_string)243    if oracle:244        print('ORACLE')245    # print('out:', out)246    # print('err:', err)247    # echo debconf shared/accepted-oracle-license-v1-1 select true | \248    #     sudo debconf-set-selections249    # $ echo debconf shared/accepted-oracle-license-v1-1 seen true | \250    #     sudo debconf-set-selections251    if not java_installed or openjdk:252        apt.ppa('webupd8team/java', host=host)253        remote.get_output(254            'echo debconf shared/accepted-oracle-license-v1-1 select true '255            '|  sudo debconf-set-selections',256            host=host,257            c=c258        )259        remote.get_output(260            'echo debconf shared/accepted-oracle-license-v1-1 seen true '261            '| sudo debconf-set-selections',262            host=host,263            c=c264        )265        apt.install('oracle-java8-installer', host=host, c=c)266    # apt.install('vim', host=host, c=c)267def install_xpack():268    """X-Pack is an Elastic Stack extension.269    It bundles security, alerting, monitoring, reporting, machine270    learning, and graph capabilities into one easy-to-install package.271    """272    pass273def install_Metricbeat(host=None):274    url = 'https://artifacts.elastic.co/downloads/beats/' \275          'metricbeat/metricbeat-6.1.0-amd64.deb'276    apt.install_from_url(url, host)277    # TODO: enable in systemd278    # TODO: start279@shared_task280def install_grafana(host=None):281    # TODO: enable in systemd (disabled by default)282    pass283@shared_task284def install_Logstash(host=None):285    # tty = sys.stdout.isatty()286    url = 'https://artifacts.elastic.co/downloads/logstash/logstash-6.1.0.deb'287    # sha = 'fa92bf63d32712dc166ab7c21e460cb6b424d4b8ae839a0fe0c8ee6167b981c' \288    #       'e53902f51088cbdbc9ae8fc0e31e621e3dfa878de0b88e11f7a23aea13e6d6fa3'  # noqa289    install_java8(host=host)290    apt.install_from_url(url, host)291    # TODO292    # Before starting - create configs files or get an error:293    # No config files found in path {:path=>"/etc/logstash/conf.d/*.conf"}294    # /etc/logstash/conf.d/logstash-simple.conf295    # sudo usermod -a -G adm logstash296    # Or error: failed to open /var/log/syslog: Permission denied297    # add_user_to_group('logstash', 'adm', host=host)298    # TODO:299    # enable service300    # run service301@shared_task302def install_vault():303    hosts = (304        '10.254.239.2',  # desktop305        '10.254.239.3',  # student306        '10.254.239.4',  # balancer307    )308    url = 'https://releases.hashicorp.com/vault/0.9.0/' \309          'vault_0.9.0_linux_amd64.zip'310    basename = os.path.basename(url)311    filename = os.path.join('/tmp', basename)312    # c = None313    # if c is None:314    #     c = remote.create_connection(host)315    for host in hosts:316        c = remote.create_connection(host)317        # install vault binary318        if remote.file_exists('/usr/local/bin/vault', host, c=c):319            print(f'Vault is already installed on {host}')320        else:321            if remote.file_exists(filename, host, c=c):322                print(f'{filename} exists on {host}')323            else:324                print(f'Going to download {basename} on {host}...')325                filename = remote.download(url, host, c=c)326            res = remote.extract_zip(327                filename,328                '/usr/local/bin/',329                host=host,330                c=c331            )332            print(res['out'], res['err'])333            # install(filename, host=host, c=c)334        # systemd: copy vault.service file335        REPO_PATH = get_git_root()336        f = os.path.join(REPO_PATH, 'configs', 'systemd-vault.service')337        r = '/etc/systemd/system/vault.service'338        # print(f, r)339        print('Copy vault.service file...', end='')340        ftp = c.open_sftp()341        ftp.put(f, r)342        ftp.close()343        print('OK')344        # create configs dir345        remote.mkdirs('/etc/vault.d', host=host, c=c)346        # Vault config347        f = os.path.join(REPO_PATH, 'configs', 'vault.config.hcl')348        r = '/etc/vault.d/config.hcl'349        # print(f, r)350        print('Copy Vault config file...', end='')351        ftp = c.open_sftp()352        ftp.put(f, r)353        ftp.close()354        print('OK')355@shared_task356def install_project_locally():357    # import pip358    # pip.main(['install', 'python-distutils-extra'])359    # pip.main(['install', 'python-apt'])360    hosts_all = (361        '10.254.239.1',362        '10.254.239.2',363        '10.254.239.3',364        '10.254.239.4',365    )366    # prepare:367    # ve368    # requirements369    #370    # tty = sys.stdout.isatty()371    # ES372    for host in [373            '10.254.239.2',374            '10.254.239.3',375    ]:376        install_java8(host=host)377        install_ElasticSearch(host=host)378    # ElasticSearch379    # install_ElasticSearch()380    # install_ElasticSearch(host="10.254.239.3")381    host = "10.254.239.3"382    # install_Logstash(host=host)383    for host in hosts_all:...fabfile.py
Source:fabfile.py  
...61            sudo('./install_appserver.sh')62@task63@fabric.api.roles(['application'])64@parallel65def install_elasticsearch():66    deploy_scripts_file = 'deploy.tgz'67    target_dir = '/tmp'68    upload_deploy_scripts(deploy_scripts_file, target_dir)69    with cd(target_dir):70        sudo('rm -rf deploy/')71        run('tar xvzf ' + deploy_scripts_file)72        with cd('deploy'):73            sudo('./install_elasticsearch.sh')74@task75@fabric.api.roles(['application'])76@parallel77def service_status():...component_install.py
Source:component_install.py  
1#!/usr/bin/python2# coding:utf-83import os, sys, json4# æ¥æ¶å°çjsonå符串åå
¥ä¸´æ¶æä»¶5with open('/tmp/inventory.json', 'w') as f:6    json.dump(sys.argv[1], f)7# 读å
¥json å符串读å
¥ï¼è¿ålist 对象8with open('/tmp/inventory.json', 'r') as f:9    data_json = json.load(f)10data_analytical = json.loads(data_json)11# æ¥çä¼ å
¥çjson 䏿¯å¦æéå¤ip12ip_list = []13for i in range(len(data_analytical)):14    ip_list.append(data_analytical[i]["ip"])15if __name__ == '__main__':16    path = "/tmp"17    if len(ip_list) == len(set(ip_list)):18        # 没æéå¤ipä½¿ç¨æ£å¸¸æ¹å¼( åæºæè
é群é½å¯ä»¥ï¼19        os.system("ansible-playbook -i ansible_inventory.py playbook/install_elasticsearch.yml")20    else:21        # 妿æéå¤ip ç»ä¸ä½¿ç¨åèç¹å®è£
å®è£
åè¿è¡ é群é
ç½®22        j =023        file_list = []24        for i in data_analytical:25            with open("/tmp/inventory." + str(j) + "json", "w") as f:26                json.dump(i, f, ensure_ascii=False)27            file_list.append("/tmp/inventory." + str(j) + "json")28            j = j + 129        # çæçæä»¶è¦ç/tmp/inventory.jsonæä»¶è¿è¡å¾ªç¯æ§è¡ï¼ç¡®ä¿æç
§ç®å½ä¸åè¿è¡åæºå¤èç¹å®è£
30        for tmpfile in file_list:31            os.rename(tmpfile, "/tmp/inventory.json")32            # æç
§åèç¹å®è£
ï¼å®è£
åä¿®æ¹å®è£
ç®å½...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
