How to use is_redhat method in localstack

Best Python code snippet using localstack_python

install_owrang.py

Source:install_owrang.py Github

copy

Full Screen

1# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.2# License: GNU General Public License v3. See license.txt3#!/usr/bin/env python4from __future__ import unicode_literals5import os6import sys7apache_user = None8is_redhat = is_debian = None9root_password = None10def install(install_path=None):11 install_pre_requisites()12 if not install_path:13 install_path = os.getcwd()14 install_owrang(install_path)15 post_install(install_path)16def install_pre_requisites():17 global is_redhat, is_debian18 is_redhat, is_debian = validate_install()19 if is_redhat:20 install_using_yum()21 elif is_debian:22 install_using_apt()23 install_python_modules()24 print "-" * 8025 print "Pre-requisites Installed"26 print "-" * 8027def validate_install():28 import platform29 # check os30 operating_system = platform.system()31 print "Operating System =", operating_system32 if operating_system != "Linux":33 raise Exception, "Sorry! This installer works only for Linux based Operating Systems"34 # check python version35 python_version = sys.version.split(" ")[0]36 print "Python Version =", python_version37 if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 6):38 raise Exception, "Hey! Owrang needs Python version to be 2.6+"39 # check distribution40 distribution = platform.linux_distribution()[0].lower().replace('"', '')41 print "Distribution = ", distribution42 is_redhat = distribution in ("redhat", "centos", "centos linux", "fedora")43 is_debian = distribution in ("debian", "ubuntu", "elementary os", "linuxmint")44 if not (is_redhat or is_debian):45 raise Exception, "Sorry! This installer works only with yum or apt-get package management"46 return is_redhat, is_debian47def install_using_yum():48 packages = "python python-setuptools gcc python-devel MySQL-python httpd git memcached ntp vim-enhanced screen"49 print "-"*8050 print "Installing Packages: (This may take some time)"51 print packages52 print "-"*8053 exec_in_shell("yum install -y %s" % packages)54 if not exec_in_shell("which mysql"):55 packages = "mysql mysql-server mysql-devel"56 print "Installing Packages:", packages57 exec_in_shell("yum install -y %s" % packages)58 exec_in_shell("service mysqld restart")59 # set a root password post install60 global root_password61 print "Please create a password for root user of MySQL"62 root_password = (get_root_password() or "owrang").strip()63 exec_in_shell('mysqladmin -u root password "%s"' % (root_password,))64 print "Root password set as", root_password65 # install htop66 if not exec_in_shell("which htop"):67 try:68 exec_in_shell("cd /tmp && rpm -i --force http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm && yum install -y htop")69 except:70 pass71 update_config_for_redhat()72def update_config_for_redhat():73 import re74 global apache_user75 apache_user = "apache"76 # update memcache user77 with open("/etc/sysconfig/memcached", "r") as original:78 memcached_conf = original.read()79 with open("/etc/sysconfig/memcached", "w") as modified:80 modified.write(re.sub('USER.*', 'USER="%s"' % apache_user, memcached_conf))81 # set to autostart on startup82 for service in ("mysqld", "httpd", "memcached", "ntpd"):83 exec_in_shell("chkconfig --level 2345 %s on" % service)84 exec_in_shell("service %s restart" % service)85def install_using_apt():86 exec_in_shell("apt-get update")87 packages = "python python-setuptools python-dev build-essential python-pip python-mysqldb apache2 git memcached ntp vim screen htop"88 print "-" * 8089 print "Installing Packages: (This may take some time)"90 print packages91 print "-" * 8092 exec_in_shell("apt-get install -y %s" % packages)93 if not exec_in_shell("which mysql"):94 packages = "mysql-server libmysqlclient-dev"95 print "Installing Packages:", packages96 exec_in_shell("apt-get install -y %s" % packages)97 update_config_for_debian()98def update_config_for_debian():99 global apache_user100 apache_user = "www-data"101 # update memcache user102 with open("/etc/memcached.conf", "r") as original:103 memcached_conf = original.read()104 with open("/etc/memcached.conf", "w") as modified:105 modified.write(memcached_conf.replace("-u memcache", "-u %s" % apache_user))106 exec_in_shell("a2enmod rewrite")107 for service in ("mysql", "apache2", "memcached", "ntpd"):108 exec_in_shell("service %s restart" % service)109def install_python_modules():110 python_modules = "pytz python-dateutil jinja2 markdown2 termcolor python-memcached requests chardet dropbox google-api-python-client pygeoip"111 print "-" * 80112 print "Installing Python Modules: (This may take some time)"113 print python_modules114 print "-" * 80115 if not exec_in_shell("which pip"):116 exec_in_shell("easy_install pip")117 exec_in_shell("pip install --upgrade pip")118 exec_in_shell("pip install --upgrade setuptools")119 exec_in_shell("pip install --upgrade virtualenv")120 exec_in_shell("pip install -q %s" % python_modules)121def install_owrang(install_path):122 print123 print "-" * 80124 print "Installing Owrang"125 print "-" * 80126 # ask for details127 global root_password128 if not root_password:129 root_password = get_root_password()130 test_root_connection(root_password)131 db_name = raw_input("Owrang Database Name: ")132 if not db_name:133 raise Exception, "Sorry! You must specify Owrang Database Name"134 # install folders and conf135 setup_folders(install_path)136 setup_conf(install_path, db_name)137 # setup paths138 sys.path.extend([".", "lib", "app"])139 # install database, run patches, update schema140 setup_db(install_path, root_password, db_name)141 setup_cron(install_path)142 setup_apache_conf(install_path)143def get_root_password():144 # ask for root mysql password145 import getpass146 root_pwd = None147 root_pwd = getpass.getpass("MySQL Root user's Password: ")148 return root_pwd149def test_root_connection(root_pwd):150 out = exec_in_shell("mysql -u root %s -e 'exit'" % \151 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))152 if "access denied" in out.lower():153 raise Exception("Incorrect MySQL Root user's password")154def setup_folders(install_path):155 app = os.path.join(install_path, "app")156 if not os.path.exists(app):157 print "Cloning Owrang"158 exec_in_shell("cd %s && git clone https://github.com/Yellowen/Owrang.git app" % install_path)159 exec_in_shell("cd app && git config core.filemode false")160 if not os.path.exists(app):161 raise Exception, "Couldn't clone owrang repository"162 lib = os.path.join(install_path, "lib")163 if not os.path.exists(lib):164 print "Cloning wnframework"165 exec_in_shell("cd %s && git clone https://github.com/Yellowen/wnframework.git lib" % install_path)166 exec_in_shell("cd lib && git config core.filemode false")167 if not os.path.exists(lib):168 raise Exception, "Couldn't clone wnframework repository"169 public = os.path.join(install_path, "public")170 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),171 os.path.join(install_path, "logs")]:172 if not os.path.exists(p):173 os.mkdir(p)174def setup_conf(install_path, db_name):175 import os, string, random, re176 # generate db password177 char_range = string.ascii_letters + string.digits178 db_password = "".join((random.choice(char_range) for n in xrange(16)))179 # make conf file180 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:181 conf = template.read()182 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)183 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)184 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:185 conf_file.write(conf)186 return db_password187def setup_db(install_path, root_password, db_name):188 from webnotes.install_lib.install import Installer189 inst = Installer("root", root_password)190 inst.import_from_db(db_name, verbose=1)191 # run patches and sync192 exec_in_shell("./lib/wnf.py --patch_sync_build")193def setup_cron(install_path):194 owrang_cron_entries = [195 "*/3 * * * * cd %s && python lib/wnf.py --run_scheduler >> /var/log/owrang-sch.log 2>&1" % install_path,196 "0 */6 * * * cd %s && python lib/wnf.py --backup >> /var/log/owrang-backup.log 2>&1" % install_path197 ]198 for row in owrang_cron_entries:199 try:200 existing_cron = exec_in_shell("crontab -l")201 if row not in existing_cron:202 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)203 except:204 exec_in_shell('echo "%s" | crontab' % row)205def setup_apache_conf(install_path):206 apache_conf_content = """Listen 8080207NameVirtualHost *:8080208<VirtualHost *:8080>209 ServerName localhost210 DocumentRoot %s/public/211 AddHandler cgi-script .cgi .xml .py212 AddType application/vnd.ms-fontobject .eot213 AddType font/ttf .ttf214 AddType font/otf .otf215 AddType application/x-font-woff .woff216 <Directory %s/public/>217 # directory specific options218 Options -Indexes +FollowSymLinks +ExecCGI219 # directory's index file220 DirectoryIndex web.py221 AllowOverride all222 Order Allow,Deny223 Allow from all224 # rewrite rule225 RewriteEngine on226 RewriteCond %%{REQUEST_FILENAME} !-f227 RewriteCond %%{REQUEST_FILENAME} !-d228 RewriteCond %%{REQUEST_FILENAME} !-l229 RewriteRule ^([^/]+)$ /web.py?page=$1 [QSA,L]230 </Directory>231</VirtualHost>""" % (install_path, install_path)232 new_apache_conf_path = os.path.join(install_path, os.path.basename(install_path)+".conf")233 with open(new_apache_conf_path, "w") as apache_conf_file:234 apache_conf_file.write(apache_conf_content)235def post_install(install_path):236 global apache_user237 exec_in_shell("chown -R %s %s" % (apache_user, install_path))238 apache_conf_filename = os.path.basename(install_path)+".conf"239 if is_redhat:240 os.symlink(os.path.join(install_path, apache_conf_filename),241 os.path.join("/etc/httpd/conf.d", apache_conf_filename))242 exec_in_shell("service httpd restart")243 elif is_debian:244 os.symlink(os.path.join(install_path, apache_conf_filename),245 os.path.join("/etc/apache2/sites-enabled", apache_conf_filename))246 exec_in_shell("service apache2 restart")247 print248 print "-"*80249 print "To change url domain, run: lib/wnf.py --domain example.com"250 print "-"*80251 print "Installation complete"252 print "Open your browser and go to http://localhost:8080"253 print "Login using username = Administrator and password = admin"254def exec_in_shell(cmd):255 # using Popen instead of os.system - as recommended by python docs256 from subprocess import Popen257 import tempfile258 with tempfile.TemporaryFile() as stdout:259 with tempfile.TemporaryFile() as stderr:260 p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)261 p.wait()262 stdout.seek(0)263 out = stdout.read()264 stderr.seek(0)265 err = stderr.read()266 if err and any((kw in err.lower() for kw in ["traceback", "error", "exception"])):267 print out268 raise Exception, err269 else:270 print "."271 return out272if __name__ == "__main__":...

Full Screen

Full Screen

install_erpnext.py

Source:install_erpnext.py Github

copy

Full Screen

1# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors2# License: GNU General Public License v3. See license.txt3#!/usr/bin/env python4from __future__ import unicode_literals5import os, sys6import argparse7import subprocess8is_redhat = is_debian = None9root_password = None10requirements = [11 "chardet", 12 "cssmin", 13 "dropbox", 14 "google-api-python-client", 15 "gunicorn", 16 "httplib2", 17 "jinja2", 18 "markdown2", 19 "markupsafe", 20 "mysql-python",21 "pygeoip", 22 "python-dateutil", 23 "python-memcached", 24 "pytz==2013d", 25 "requests", 26 "six", 27 "slugify", 28 "termcolor", 29 "werkzeug",30 "semantic_version",31 "gitpython==0.3.2.RC1"32]33def install(install_path):34 setup_folders(install_path)35 install_erpnext(install_path)36 37 post_install(install_path)38 39def install_pre_requisites():40 global is_redhat, is_debian41 is_redhat, is_debian = validate_install()42 if is_redhat:43 install_using_yum()44 elif is_debian:45 install_using_apt()46 47 install_python_modules()48 49 print "-"*8050 print "Pre-requisites Installed"51 print "-"*8052def validate_install():53 import platform54 # check os55 operating_system = platform.system()56 print "Operating System =", operating_system57 if operating_system != "Linux":58 raise Exception, "Sorry! This installer works only for Linux based Operating Systems"59 60 # check python version61 python_version = sys.version.split(" ")[0]62 print "Python Version =", python_version63 if not (python_version and int(python_version.split(".")[0])==2 and int(python_version.split(".")[1]) >= 7):64 raise Exception, "Hey! ERPNext needs Python version to be 2.7+"65 66 # check distribution67 distribution = platform.linux_distribution()[0].lower().replace('"', '')68 print "Distribution = ", distribution69 is_redhat = distribution in ("redhat", "red hat enterprise linux server", "centos", "centos linux", "fedora")70 is_debian = distribution in ("debian", "ubuntu", "elementary os", "linuxmint")71 72 if not (is_redhat or is_debian):73 raise Exception, "Sorry! This installer works only with yum or apt-get package management"74 75 return is_redhat, is_debian76 77def install_using_yum():78 packages = "gcc MySQL-python git memcached ntp vim-enhanced screen"79 80 print "-"*8081 print "Installing Packages: (This may take some time)"82 print packages83 print "-"*8084 exec_in_shell("yum install -y %s" % packages)85 86 try:87 exec_in_shell("which mysql")88 except subprocess.CalledProcessError:89 packages = "mysql mysql-server mysql-devel"90 print "Installing Packages:", packages91 exec_in_shell("yum install -y %s" % packages)92 exec_in_shell("service mysqld restart")93 94 # set a root password post install95 global root_password96 print "Please create a password for root user of MySQL"97 root_password = (get_root_password() or "erpnext").strip()98 exec_in_shell('mysqladmin -u root password "%s"' % (root_password,))99 print "Root password set as", root_password100 101 update_config_for_redhat()102 103def update_config_for_redhat():104 import re105 106 # set to autostart on startup107 for service in ("mysqld", "memcached"):108 exec_in_shell("chkconfig --level 2345 %s on" % service)109 exec_in_shell("service %s restart" % service)110 111def install_using_apt():112 exec_in_shell("apt-get update")113 packages = "python python-setuptools python-dev build-essential python-mysqldb git memcached ntp vim screen htop"114 print "-"*80115 print "Installing Packages: (This may take some time)"116 print packages117 print "-"*80118 exec_in_shell("apt-get install -y %s" % packages)119 global root_password120 if not root_password:121 root_password = get_root_password()122 exec_in_shell("echo mysql-server mysql-server/root_password password %s | sudo debconf-set-selections" % root_password)123 exec_in_shell("echo mysql-server mysql-server/root_password_again password %s | sudo debconf-set-selections" % root_password)124 125 try:126 exec_in_shell("which mysql")127 except subprocess.CalledProcessError:128 packages = "mysql-server libmysqlclient-dev"129 print "Installing Packages:", packages130 exec_in_shell("apt-get install -y %s" % packages)131 132 update_config_for_debian()133 134def update_config_for_debian():135 for service in ("mysql",):136 exec_in_shell("service %s restart" % service)137 138def install_python_modules():139 print "-"*80140 print "Installing Python Modules: (This may take some time)"141 print "-"*80142 143 try:144 exec_in_shell("which pip2.7") 145 except subprocess.CalledProcessError:146 exec_in_shell("easy_install-2.7 pip")147 148 exec_in_shell("pip2.7 install --upgrade setuptools --no-use-wheel")149 exec_in_shell("pip2.7 install --upgrade setuptools")150 exec_in_shell("pip2.7 install {}".format(' '.join(requirements)))151 152def install_erpnext(install_path):153 print154 print "-"*80155 print "Installing ERPNext"156 print "-"*80157 158 # ask for details159 global root_password160 if not root_password:161 root_password = get_root_password()162 test_root_connection(root_password)163 164 db_name = raw_input("ERPNext Database Name: ")165 if not db_name:166 raise Exception, "Sorry! You must specify ERPNext Database Name"167 168 # setup paths169 sys.path = [".", "lib", "app"] + sys.path170 import wnf171 172 # install database, run patches, update schema173 # setup_db(install_path, root_password, db_name)174 wnf.install(db_name, root_password=root_password)175 setup_cron(install_path)176 177def get_root_password():178 # ask for root mysql password179 import getpass180 root_pwd = None181 root_pwd = getpass.getpass("MySQL Root user's Password: ")182 return root_pwd183 184def test_root_connection(root_pwd):185 out = exec_in_shell("mysql -u root %s -e 'exit'" % \186 (("-p"+root_pwd) if root_pwd else "").replace('$', '\$').replace(' ', '\ '))187 if "access denied" in out.lower():188 raise Exception("Incorrect MySQL Root user's password")189 190def setup_folders(install_path):191 os.chdir(install_path)192 app = os.path.join(install_path, "app")193 if not os.path.exists(app):194 print "Cloning erpnext"195 exec_in_shell("cd %s && git clone --branch master https://github.com/webnotes/erpnext.git app" % install_path)196 exec_in_shell("cd app && git config core.filemode false")197 if not os.path.exists(app):198 raise Exception, "Couldn't clone erpnext repository"199 200 lib = os.path.join(install_path, "lib")201 if not os.path.exists(lib):202 print "Cloning wnframework"203 exec_in_shell("cd %s && git clone --branch master https://github.com/webnotes/wnframework.git lib" % install_path)204 exec_in_shell("cd lib && git config core.filemode false")205 if not os.path.exists(lib):206 raise Exception, "Couldn't clone wnframework repository"207 208 public = os.path.join(install_path, "public")209 for p in [public, os.path.join(public, "files"), os.path.join(public, "backups"),210 os.path.join(install_path, "logs")]:211 if not os.path.exists(p):212 os.mkdir(p)213 214def setup_conf(install_path, db_name):215 import os, string, random, re216 # generate db password217 char_range = string.ascii_letters + string.digits218 db_password = "".join((random.choice(char_range) for n in xrange(16)))219 220 # make conf file221 with open(os.path.join(install_path, "lib", "conf", "conf.py"), "r") as template:222 conf = template.read()223 224 conf = re.sub("db_name.*", 'db_name = "%s"' % (db_name,), conf)225 conf = re.sub("db_password.*", 'db_password = "%s"' % (db_password,), conf)226 227 with open(os.path.join(install_path, "conf.py"), "w") as conf_file:228 conf_file.write(conf)229 230 return db_password231 232def post_install(install_path):233 pass234def exec_in_shell(cmd):235 # using Popen instead of os.system - as recommended by python docs236 import subprocess237 out = subprocess.check_output(cmd, shell=True)238 return out239def parse_args():240 parser = argparse.ArgumentParser()241 parser.add_argument('--create_user', default=False, action='store_true')242 parser.add_argument('--username', default='erpnext')243 parser.add_argument('--password', default='erpnext')244 parser.add_argument('--no_install_prerequisites', default=False, action='store_true')245 return parser.parse_args()246def create_user(username, password):247 import subprocess, pwd248 p = subprocess.Popen("useradd -m -d /home/{username} -s {shell} {username}".format(username=username, shell=os.environ.get('SHELL')).split())249 p.wait()250 p = subprocess.Popen("passwd {username}".format(username=username).split(), stdin=subprocess.PIPE)251 p.communicate('{password}\n{password}\n'.format(password=password))252 p.wait()253 return pwd.getpwnam(username).pw_uid254def setup_cron(install_path):255 erpnext_cron_entries = [256 "*/3 * * * * cd %s && python2.7 lib/wnf.py --run_scheduler >> erpnext-sch.log 2>&1" % install_path,257 "0 */6 * * * cd %s && python2.7 lib/wnf.py --backup >> erpnext-backup.log 2>&1" % install_path258 ]259 for row in erpnext_cron_entries:260 try:261 existing_cron = exec_in_shell("crontab -l")262 if row not in existing_cron:263 exec_in_shell('{ crontab -l; echo "%s"; } | crontab' % row)264 except:265 exec_in_shell('echo "%s" | crontab' % row)266if __name__ == "__main__":267 args = parse_args()268 install_path = os.getcwd()269 if os.getuid() != 0 and args.create_user and not args.no_install_prequisites:270 raise Exception, "Please run this script as root"271 if args.create_user:272 uid = create_user(args.username, args.password)273 install_path = '/home/{username}/erpnext'.format(username=args.username)274 if not args.no_install_prerequisites:275 install_pre_requisites()276 if os.environ.get('SUDO_UID') and not args.create_user:277 os.setuid(int(os.environ.get('SUDO_UID')))278 279 if os.getuid() == 0 and args.create_user:280 os.setuid(uid)281 if install_path:282 os.mkdir(install_path)283 284 install(install_path=install_path)285 print286 print "-"*80287 print "Installation complete"288 print "To start the development server,"289 print "Login as {username} with password {password}".format(username=args.username, password=args.password)290 print "cd {}".format(install_path)291 print "./lib/wnf.py --serve"292 print "-"*80293 print "Open your browser and go to http://localhost:8000"...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful